From d8cc3882795d6c7b7de46e4972ad29e743058fe2 Mon Sep 17 00:00:00 2001 From: ABelliqueux Date: Fri, 31 Jan 2025 16:47:12 +0100 Subject: [PATCH] Switch to cam_type config --- config.toml | 4 ++-- frame_opencv.py | 58 ++++++++++++++++++++++++++++++------------------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/config.toml b/config.toml index ff43ee0..b81b0f1 100644 --- a/config.toml +++ b/config.toml @@ -1,6 +1,6 @@ [DEFAULT] -cam_is_picam = true -cam_is_showmewebcam = false +# Camera type - can be : showmewebcam, picam, webcam, dslr +cam_type = 'picam' use_date_for_folder = false file_extension = 'jpg' jpg_quality = 88 diff --git a/frame_opencv.py b/frame_opencv.py index 39ccb5f..dc5dbc1 100644 --- a/frame_opencv.py +++ b/frame_opencv.py @@ -15,6 +15,9 @@ import tomllib import numpy as np import serialutils +# DSLR +import gphoto2 as gp + # Run from SSH if not os.getenv('DISPLAY'): os.putenv('DISPLAY', ':0') @@ -22,6 +25,12 @@ if not os.getenv('DISPLAY'): running_from_folder = os.path.realpath(__file__) alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] +cam_lenspos = 0 +index = 0 +playhead = 0 +playback = 0 +liveview_only = 0 +camera_settings = 0 # l10n LOCALE = os.getenv('LANG', 'en_EN') @@ -30,8 +39,7 @@ _ = gettext.translation('template', localedir='locales', languages=[LOCALE]).get # Config # defaults project_settings_defaults = { - 'cam_is_picam': True, - 'cam_is_showmewebcam': False, + 'cam_type': "webcam", 'use_date_for_folder': False, 'file_extension':'png', 'jpg_quality':90, @@ -72,7 +80,7 @@ for location in config_locations: config_found_msg = _("Found configuration file in {}").format(os.path.expanduser(location)) print(config_found_msg) -if project_settings['cam_is_showmewebcam']: +if project_settings['cam_type'] == "showmewebcam": camera_current_settings = { 'auto_exposure': dict(min=0, max=1, default=camera_settings['auto_exposure'], value=camera_settings['auto_exposure']), 'white_balance_auto_preset': dict(min=0, max=9, default=camera_settings['white_balance_auto_preset'], value=camera_settings['white_balance_auto_preset']), @@ -80,7 +88,7 @@ if project_settings['cam_is_showmewebcam']: 'vertical_flip': dict(min=0, max=1, default=camera_settings['vflip'], value=camera_settings['vflip']), 'video_bitrate': dict(min=25000000, max=25000000, default=camera_settings['video_bitrate'], value=camera_settings['video_bitrate']), } -else: # cam is picam +elif project_settings['cam_type'] == "picam": camera_current_settings = { 'auto_exposure': dict(min=0, max=4, default=camera_settings['auto_exposure'], value=camera_settings['auto_exposure']), 'white_balance_auto_preset': dict(min=0, max=7, default=camera_settings['white_balance_auto_preset'], value=camera_settings['white_balance_auto_preset']), @@ -88,7 +96,10 @@ else: # cam is picam 'vertical_flip': dict(min=0, max=1, default=camera_settings['vflip'], value=camera_settings['vflip']), 'anti_flicker': dict(min=0, max=2, default=1, value=1), } - +elif project_settings['cam_type'] == "dslr": + camera_current_settings = {} +else: + camera_current_settings = {} def apply_cam_setting(cam_settings:dict, to_set:list=None): cmd = 'v4l2-ctl -d /dev/video0' @@ -114,10 +125,10 @@ def apply_cam_setting(cam_settings:dict, to_set:list=None): else: print(_("Unknown setting!")) break - if project_settings['cam_is_showmewebcam']: + if project_settings['cam_type'] == "showmewebcam": serialutils.send_serial_cmd(serialutils.find_cam_port(), cmd.format(*args)) else: - # TODO: v4l2 support + # TODO: execute v4l2 directly print(_("Camera function not supported.")) return cam_settings @@ -451,7 +462,7 @@ def main(args): global onionskin, liveview_only, playback, loop_playback, playhead, index, img_list, first_playback, camera_current_settings - if not project_settings['cam_is_picam']: + if not project_settings['cam_type'] == "showmewebcam": if not testDevice(0): print(_("No camera device found. Exiting...")) return 1 @@ -493,7 +504,7 @@ def main(args): frame = get_onionskin_frame(savepath, index) - if project_settings['cam_is_showmewebcam']: + if project_settings['cam_type'] == "showmewebcam": # Apply config camera settings camera_current_settings = apply_cam_setting(camera_current_settings) time.sleep(.5) @@ -529,7 +540,7 @@ def main(args): if liveview_only: # ~ onionskin = False - if not project_settings['cam_is_picam']: + if project_settings['cam_type'] == "showmewebcam": ret, overlay = cam.read() if not ret: print(_("Failed to grab frame.")) @@ -543,7 +554,7 @@ def main(args): # ~ onionskin = True if onionskin: - if not project_settings['cam_is_picam']: + if project_settings['cam_type'] == "showmewebcam": ret, overlay = cam.read() if not ret: print(_("Failed to grab frame.")) @@ -579,13 +590,14 @@ def main(args): elif (k%256 == 119) or (k%256 == 55) or (k%256 == 183): print(_("White balance mode")) camera_current_settings = apply_cam_setting(camera_current_settings, ['white_balance_auto_preset']) - if project_settings['cam_is_picam']: + if project_settings['cam_type'] == "picam": cam.set_controls({'AwbMode': camera_current_settings['white_balance_auto_preset']['value']}) # Key x / 1 - cycle exposure elif (k%256 == 120) or (k%256 == 49) or (k%256 == 177): print(_("Exp. mode")) camera_current_settings = apply_cam_setting(camera_current_settings, ['auto_exposure']) - if project_settings['cam_is_picam']: + # TODO : Move in function + if project_settings['cam_type'] == "picam": print(camera_current_settings['auto_exposure']['value']) if camera_current_settings['auto_exposure']['value'] == 4: cam.set_controls({'AeEnable': 1}) @@ -596,7 +608,8 @@ def main(args): elif (k%256 == 102) or (k%256 == 51) or (k%256 == 179): print(_("Flip image")) camera_current_settings = apply_cam_setting(camera_current_settings, ['vertical_flip','horizontal_flip']) - if project_settings['cam_is_picam']: + # TODO : Move in function + if project_settings['cam_type'] == "picam": cam.stop() picam_config["transform"] = Transform(vflip=camera_current_settings['vertical_flip']['value'],hflip=camera_current_settings['horizontal_flip']['value']) cam.configure(picam_config) @@ -635,7 +648,8 @@ def main(args): elif (k%256 == 114) or (k%256 == 57) or (k%256 == 185) : print(_("Reset camera settings")) camera_current_settings = apply_cam_setting(camera_current_settings) - if project_settings['cam_is_picam']: + # TODO : This should be in a function ? + if project_settings['cam_type'] == "picam": if camera_current_settings['auto_exposure']['default'] == 4: cam.set_controls({'AeEnable': 0}) else: @@ -662,22 +676,22 @@ def main(args): # Remove frame print(_("Remove frame")) img_list, index, frame = remove_frame(img_list, index) - # TODO: replace keys with rotary encoder # Focus +/- with a,z - elif (k%256 == 97) and project_settings['cam_is_picam']: + elif (k%256 == 97) and project_settings['cam_type'] == "picam": cam_lenspos += 0.2 # Set AfMode to Manual cam.set_controls({'AfMode': 0, 'LensPosition': cam_lenspos}) print(_("+Lens pos: {}".format(cam_lenspos))) - elif (k%256 == 122) and project_settings['cam_is_picam']: + elif (k%256 == 122) and project_settings['cam_type'] == "picam": cam_lenspos -= 0.2 # Set AfMode to Manual cam.set_controls({'AfMode': 0, 'LensPosition': cam_lenspos}) print(_("-Lens pos: {}".format(cam_lenspos))) # Set anti-flicker mode with q - elif (k%256 == 113) and project_settings['cam_is_picam']: - # Set AfMode to Manual + elif (k%256 == 113) and project_settings['cam_type'] == "picam": + print(_("Anti-flicker mode")) camera_current_settings = apply_cam_setting(camera_current_settings, ['anti_flicker']) + # TODO : Move this to a function ? if camera_current_settings['anti_flicker']['value'] == 0: cam.set_controls({'AeFlickerMode': 0}) elif camera_current_settings['anti_flicker']['value'] == 1: @@ -685,7 +699,7 @@ def main(args): else: cam.set_controls({'AeFlickerMode': 1, 'AeFlickerPeriod':10000}) print(camera_current_settings['anti_flicker']['value']) - # ~ elif (k%256 == 115) and project_settings['cam_is_picam']: + # ~ elif (k%256 == 115) and project_settings['cam_type'] == "picam": # ~ # Set AfMode to Manual # ~ cam.set_controls({'AeFlickerMode': 0, 'AeFlickerPeriod': 8333}) # Take pic @@ -738,7 +752,7 @@ def main(args): except: print(_("Terminating running process...")) ffmpeg_process.terminate() - if not project_settings["cam_is_picam"]: + if project_settings['cam_type'] == "showmewebcam": cam.release() else: cam.close()