From 0d8434136a81a0cd1293bc5dd3a2ef5ee7f0bd8e Mon Sep 17 00:00:00 2001 From: ABelliqueux Date: Sun, 12 Jan 2025 10:29:04 +0100 Subject: [PATCH] Add anti-flicker control --- frame_opencv.py | 54 ++++++++++++++++++++++++++++++++++++------------- readme.md | 2 +- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/frame_opencv.py b/frame_opencv.py index 7f9ba60..39ccb5f 100644 --- a/frame_opencv.py +++ b/frame_opencv.py @@ -32,7 +32,6 @@ _ = gettext.translation('template', localedir='locales', languages=[LOCALE]).get project_settings_defaults = { 'cam_is_picam': True, 'cam_is_showmewebcam': False, - 'cam_is_dslr': False, 'use_date_for_folder': False, 'file_extension':'png', 'jpg_quality':90, @@ -73,8 +72,6 @@ for location in config_locations: config_found_msg = _("Found configuration file in {}").format(os.path.expanduser(location)) print(config_found_msg) - -# Populate default values according to config and camera type if project_settings['cam_is_showmewebcam']: camera_current_settings = { 'auto_exposure': dict(min=0, max=1, default=camera_settings['auto_exposure'], value=camera_settings['auto_exposure']), @@ -83,13 +80,13 @@ 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']), } -elif project_settings['cam_is_picam']: - # Todo : add framerate ? see "FrameDurationLimits" and "FrameDuration" in camera_controls FrameDurationLimits': (33333, 120000, None) +else: # cam is 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']), 'horizontal_flip': dict(min=0, max=1, default=camera_settings['hflip'], value=camera_settings['hflip']), '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), } @@ -117,14 +114,10 @@ def apply_cam_setting(cam_settings:dict, to_set:list=None): else: print(_("Unknown setting!")) break - # TODO: Refactor to call method according to cam type if project_settings['cam_is_showmewebcam']: serialutils.send_serial_cmd(serialutils.find_cam_port(), cmd.format(*args)) - elif project_settings['cam_is_picam']: - pass - elif project_settings['cam_is_dslr']: - pass else: + # TODO: v4l2 support print(_("Camera function not supported.")) return cam_settings @@ -476,7 +469,7 @@ def main(args): cam.configure(picam_config) # Autofocus, get lens position and switch to manual mode - # Set Af mode to Manual (0). Default is Continuous (2), Auto is 1 + # Set Af mode to Auto then Manual (0). Default is Continuous (2), Auto is 1 cam.set_controls({'AfMode':1}) cam.start() cam.autofocus_cycle() @@ -487,7 +480,14 @@ def main(args): 'AwbMode': camera_current_settings['white_balance_auto_preset']['default'], 'AeEnable': 1, 'AeExposureMode': camera_current_settings['auto_exposure']['default'], - # ~ 'FrameDurationLimits':(40000,120000,None) + # Enable flicker avoidance due to mains + 'AeFlickerMode': 1, + # Mains 50hz = 10000, 60hz = 8333 + # ~ 'AeFlickerPeriod': 8333, + 'AeFlickerPeriod': 10000, + # Format is (min, max, default) in ms + # here: (60fps, 12fps, None) + # ~ 'FrameDurationLimits':(16666,83333,None) }) # ~ cam.stop() @@ -588,9 +588,9 @@ def main(args): if project_settings['cam_is_picam']: print(camera_current_settings['auto_exposure']['value']) if camera_current_settings['auto_exposure']['value'] == 4: - cam.set_controls({'AeEnable': 0}) - else: cam.set_controls({'AeEnable': 1}) + else: + cam.set_controls({'AeEnable': 0}) cam.set_controls({"AeExposureMode": camera_current_settings['auto_exposure']['value']}) # Key f / 3 - flip image elif (k%256 == 102) or (k%256 == 51) or (k%256 == 179): @@ -662,6 +662,32 @@ 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']: + 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']: + 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 + camera_current_settings = apply_cam_setting(camera_current_settings, ['anti_flicker']) + if camera_current_settings['anti_flicker']['value'] == 0: + cam.set_controls({'AeFlickerMode': 0}) + elif camera_current_settings['anti_flicker']['value'] == 1: + cam.set_controls({'AeFlickerMode': 1, 'AeFlickerPeriod':8333}) + 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']: + # ~ # Set AfMode to Manual + # ~ cam.set_controls({'AeFlickerMode': 0, 'AeFlickerPeriod': 8333}) # Take pic # SPACE or numpad 0 pressed elif (k%256 == 32) or (k%256 == 48) or (k%256 == 176): diff --git a/readme.md b/readme.md index dd57517..b7e546c 100644 --- a/readme.md +++ b/readme.md @@ -3,7 +3,7 @@ ## Branche libcamera **Ceci est la branche qui restaure la possibilité d'utiliser des périphériques compatibles rpi-libcamera (Modules Raspicam v1,v2 et v3).** -**En utiulisant la [branche correspondante pour la télécommande picote](/arthus/picote/src/branch/picamera), vous pouvez régler la mise au point du module caméra avec un [codeur rotatif](https://fr.wikipedia.org/wiki/Codeur_rotatif).** +**En utilisant la [branche correspondante pour la télécommande picote](/arthus/picote/src/branch/picamera), vous pouvez régler la mise au point du module caméra avec un [codeur rotatif](https://fr.wikipedia.org/wiki/Codeur_rotatif).**