Switch to cam_type config

This commit is contained in:
ABelliqueux 2025-01-31 16:47:12 +01:00
parent 0d8434136a
commit d8cc388279
2 changed files with 38 additions and 24 deletions

View File

@ -1,6 +1,6 @@
[DEFAULT] [DEFAULT]
cam_is_picam = true # Camera type - can be : showmewebcam, picam, webcam, dslr
cam_is_showmewebcam = false cam_type = 'picam'
use_date_for_folder = false use_date_for_folder = false
file_extension = 'jpg' file_extension = 'jpg'
jpg_quality = 88 jpg_quality = 88

View File

@ -15,6 +15,9 @@ import tomllib
import numpy as np import numpy as np
import serialutils import serialutils
# DSLR
import gphoto2 as gp
# Run from SSH # Run from SSH
if not os.getenv('DISPLAY'): if not os.getenv('DISPLAY'):
os.putenv('DISPLAY', ':0') os.putenv('DISPLAY', ':0')
@ -22,6 +25,12 @@ if not os.getenv('DISPLAY'):
running_from_folder = os.path.realpath(__file__) 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'] 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 # l10n
LOCALE = os.getenv('LANG', 'en_EN') LOCALE = os.getenv('LANG', 'en_EN')
@ -30,8 +39,7 @@ _ = gettext.translation('template', localedir='locales', languages=[LOCALE]).get
# Config # Config
# defaults # defaults
project_settings_defaults = { project_settings_defaults = {
'cam_is_picam': True, 'cam_type': "webcam",
'cam_is_showmewebcam': False,
'use_date_for_folder': False, 'use_date_for_folder': False,
'file_extension':'png', 'file_extension':'png',
'jpg_quality':90, 'jpg_quality':90,
@ -72,7 +80,7 @@ for location in config_locations:
config_found_msg = _("Found configuration file in {}").format(os.path.expanduser(location)) config_found_msg = _("Found configuration file in {}").format(os.path.expanduser(location))
print(config_found_msg) print(config_found_msg)
if project_settings['cam_is_showmewebcam']: if project_settings['cam_type'] == "showmewebcam":
camera_current_settings = { camera_current_settings = {
'auto_exposure': dict(min=0, max=1, default=camera_settings['auto_exposure'], value=camera_settings['auto_exposure']), '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']), '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']), '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']), '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 = { camera_current_settings = {
'auto_exposure': dict(min=0, max=4, default=camera_settings['auto_exposure'], value=camera_settings['auto_exposure']), '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']), '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']), '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), '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): def apply_cam_setting(cam_settings:dict, to_set:list=None):
cmd = 'v4l2-ctl -d /dev/video0' cmd = 'v4l2-ctl -d /dev/video0'
@ -114,10 +125,10 @@ def apply_cam_setting(cam_settings:dict, to_set:list=None):
else: else:
print(_("Unknown setting!")) print(_("Unknown setting!"))
break break
if project_settings['cam_is_showmewebcam']: if project_settings['cam_type'] == "showmewebcam":
serialutils.send_serial_cmd(serialutils.find_cam_port(), cmd.format(*args)) serialutils.send_serial_cmd(serialutils.find_cam_port(), cmd.format(*args))
else: else:
# TODO: v4l2 support # TODO: execute v4l2 directly
print(_("Camera function not supported.")) print(_("Camera function not supported."))
return cam_settings 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 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): if not testDevice(0):
print(_("No camera device found. Exiting...")) print(_("No camera device found. Exiting..."))
return 1 return 1
@ -493,7 +504,7 @@ def main(args):
frame = get_onionskin_frame(savepath, index) frame = get_onionskin_frame(savepath, index)
if project_settings['cam_is_showmewebcam']: if project_settings['cam_type'] == "showmewebcam":
# Apply config camera settings # Apply config camera settings
camera_current_settings = apply_cam_setting(camera_current_settings) camera_current_settings = apply_cam_setting(camera_current_settings)
time.sleep(.5) time.sleep(.5)
@ -529,7 +540,7 @@ def main(args):
if liveview_only: if liveview_only:
# ~ onionskin = False # ~ onionskin = False
if not project_settings['cam_is_picam']: if project_settings['cam_type'] == "showmewebcam":
ret, overlay = cam.read() ret, overlay = cam.read()
if not ret: if not ret:
print(_("Failed to grab frame.")) print(_("Failed to grab frame."))
@ -543,7 +554,7 @@ def main(args):
# ~ onionskin = True # ~ onionskin = True
if onionskin: if onionskin:
if not project_settings['cam_is_picam']: if project_settings['cam_type'] == "showmewebcam":
ret, overlay = cam.read() ret, overlay = cam.read()
if not ret: if not ret:
print(_("Failed to grab frame.")) print(_("Failed to grab frame."))
@ -579,13 +590,14 @@ def main(args):
elif (k%256 == 119) or (k%256 == 55) or (k%256 == 183): elif (k%256 == 119) or (k%256 == 55) or (k%256 == 183):
print(_("White balance mode")) print(_("White balance mode"))
camera_current_settings = apply_cam_setting(camera_current_settings, ['white_balance_auto_preset']) 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']}) cam.set_controls({'AwbMode': camera_current_settings['white_balance_auto_preset']['value']})
# Key x / 1 - cycle exposure # Key x / 1 - cycle exposure
elif (k%256 == 120) or (k%256 == 49) or (k%256 == 177): elif (k%256 == 120) or (k%256 == 49) or (k%256 == 177):
print(_("Exp. mode")) print(_("Exp. mode"))
camera_current_settings = apply_cam_setting(camera_current_settings, ['auto_exposure']) 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']) print(camera_current_settings['auto_exposure']['value'])
if camera_current_settings['auto_exposure']['value'] == 4: if camera_current_settings['auto_exposure']['value'] == 4:
cam.set_controls({'AeEnable': 1}) cam.set_controls({'AeEnable': 1})
@ -596,7 +608,8 @@ def main(args):
elif (k%256 == 102) or (k%256 == 51) or (k%256 == 179): elif (k%256 == 102) or (k%256 == 51) or (k%256 == 179):
print(_("Flip image")) print(_("Flip image"))
camera_current_settings = apply_cam_setting(camera_current_settings, ['vertical_flip','horizontal_flip']) 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() cam.stop()
picam_config["transform"] = Transform(vflip=camera_current_settings['vertical_flip']['value'],hflip=camera_current_settings['horizontal_flip']['value']) picam_config["transform"] = Transform(vflip=camera_current_settings['vertical_flip']['value'],hflip=camera_current_settings['horizontal_flip']['value'])
cam.configure(picam_config) cam.configure(picam_config)
@ -635,7 +648,8 @@ def main(args):
elif (k%256 == 114) or (k%256 == 57) or (k%256 == 185) : elif (k%256 == 114) or (k%256 == 57) or (k%256 == 185) :
print(_("Reset camera settings")) print(_("Reset camera settings"))
camera_current_settings = apply_cam_setting(camera_current_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: if camera_current_settings['auto_exposure']['default'] == 4:
cam.set_controls({'AeEnable': 0}) cam.set_controls({'AeEnable': 0})
else: else:
@ -662,22 +676,22 @@ def main(args):
# Remove frame # Remove frame
print(_("Remove frame")) print(_("Remove frame"))
img_list, index, frame = remove_frame(img_list, index) img_list, index, frame = remove_frame(img_list, index)
# TODO: replace keys with rotary encoder
# Focus +/- with a,z # 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 cam_lenspos += 0.2
# Set AfMode to Manual # Set AfMode to Manual
cam.set_controls({'AfMode': 0, 'LensPosition': cam_lenspos}) cam.set_controls({'AfMode': 0, 'LensPosition': cam_lenspos})
print(_("+Lens pos: {}".format(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 cam_lenspos -= 0.2
# Set AfMode to Manual # Set AfMode to Manual
cam.set_controls({'AfMode': 0, 'LensPosition': cam_lenspos}) cam.set_controls({'AfMode': 0, 'LensPosition': cam_lenspos})
print(_("-Lens pos: {}".format(cam_lenspos))) print(_("-Lens pos: {}".format(cam_lenspos)))
# Set anti-flicker mode with q # Set anti-flicker mode with q
elif (k%256 == 113) and project_settings['cam_is_picam']: elif (k%256 == 113) and project_settings['cam_type'] == "picam":
# Set AfMode to Manual print(_("Anti-flicker mode"))
camera_current_settings = apply_cam_setting(camera_current_settings, ['anti_flicker']) 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: if camera_current_settings['anti_flicker']['value'] == 0:
cam.set_controls({'AeFlickerMode': 0}) cam.set_controls({'AeFlickerMode': 0})
elif camera_current_settings['anti_flicker']['value'] == 1: elif camera_current_settings['anti_flicker']['value'] == 1:
@ -685,7 +699,7 @@ def main(args):
else: else:
cam.set_controls({'AeFlickerMode': 1, 'AeFlickerPeriod':10000}) cam.set_controls({'AeFlickerMode': 1, 'AeFlickerPeriod':10000})
print(camera_current_settings['anti_flicker']['value']) 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 # ~ # Set AfMode to Manual
# ~ cam.set_controls({'AeFlickerMode': 0, 'AeFlickerPeriod': 8333}) # ~ cam.set_controls({'AeFlickerMode': 0, 'AeFlickerPeriod': 8333})
# Take pic # Take pic
@ -738,7 +752,7 @@ def main(args):
except: except:
print(_("Terminating running process...")) print(_("Terminating running process..."))
ffmpeg_process.terminate() ffmpeg_process.terminate()
if not project_settings["cam_is_picam"]: if project_settings['cam_type'] == "showmewebcam":
cam.release() cam.release()
else: else:
cam.close() cam.close()