From ae7e582d583bf19159184b3f2fbdbb34c5d19db9 Mon Sep 17 00:00:00 2001 From: ABelliqueux Date: Fri, 7 Mar 2025 11:08:24 +0100 Subject: [PATCH] Use inc arg in apply_setting(), fix saved picture file case --- frame_opencv.py | 62 ++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/frame_opencv.py b/frame_opencv.py index 94e802e..3e20749 100644 --- a/frame_opencv.py +++ b/frame_opencv.py @@ -157,21 +157,22 @@ class webcam(): return capture_ok def increment_setting(self, setting:str, inc:int=-1): - # If value has default -1 value, increment setting - if inc == -1: - if setting in self.camera_current_settings: + if setting in self.camera_current_settings: + if inc == -1: + if self.camera_current_settings[setting]['value'] - self.camera_current_settings[setting]['step'] in range(self.camera_current_settings[setting]['min'],self.camera_current_settings[setting]['max']+1): + self.camera_current_settings[setting]['value'] -= self.camera_current_settings[setting]['step'] + else: + self.camera_current_settings[setting]['value'] = self.camera_current_settings[setting]['max'] + elif inc == 1: if self.camera_current_settings[setting]['value'] + self.camera_current_settings[setting]['step'] in range(self.camera_current_settings[setting]['min'],self.camera_current_settings[setting]['max']+1): self.camera_current_settings[setting]['value'] += self.camera_current_settings[setting]['step'] else: self.camera_current_settings[setting]['value'] = self.camera_current_settings[setting]['min'] - # Apply special cases - else: - pass - def build_v4l2_cmd(self, to_set:list=None): + def build_v4l2_cmd(self, to_set:list=None, inc:int=-1): cmd = '{} -d /dev/video0'.format(project_settings['v4l2-ctl_path']) args = [] - value = -1 + value = inc for setting in self.camera_current_settings: if to_set is None: # Apply defaults @@ -205,8 +206,8 @@ class webcam(): v4l2_ctl_process = subprocess.Popen(cmd.split(' ')) return v4l2_ctl_process - def apply_setting(self, to_set:list=None, inc:int=0): - cmd = self.build_v4l2_cmd(to_set) + def apply_setting(self, to_set:list=None, inc:int=-1): + cmd = self.build_v4l2_cmd(to_set, inc) self.run_v4l2_ctl(cmd) return self.camera_current_settings @@ -244,8 +245,8 @@ class showmewebcam(webcam): 'exposure': 'exposure_time_absolute' } - def apply_setting(self, to_set:list=None, inc:int=0): - cmd = self.build_v4l2_cmd(to_set) + def apply_setting(self, to_set:list=None, inc:int=-1): + cmd = self.build_v4l2_cmd(to_set, inc) self.serialutils.send_serial_cmd(self.serialutils.find_cam_port(), cmd) return self.camera_current_settings @@ -422,7 +423,7 @@ class dslr(): 'whitebalance' : dict(min=0, max=7, step=1, default=2, value=1), # 0 Automatic 1 Daylight 2 Fluorescent 3 Tungsten 4 Flash 5 Cloudy 6 Shade 7 Preset 'capturetarget' : dict(min=0, max=1, step=1, default=0, value=0), # Internal memory 'iso' : dict(min=0, max=5, default=0, step=1, value=0), # 0:100, 5:3200 - 'shutterspeed' : dict(min=0, max=51, step=1, default=0, value=30), # 0 : 1/4000, 51: 30s + 'shutterspeed' : dict(min=0, max=51, step=1, default=30, value=30), # 0 : 1/4000, 51: 30s # ~ 'manualfocusdrive' : dict(min=0, max=1, step=1, default=0, value=0), # Trigger autofocus # manualfocusdrive } # Map generic config name to specific picamera setting name @@ -480,7 +481,7 @@ class dslr(): # if the path doesn't contain file name, return camera's FS filename if not full_path.endswith(('.jpg', '.JPG', '.raw')): return gp_name - suffix = gp_name.split('.')[-1] + suffix = gp_name.split('.')[-1].lower() prefix = new_name.split('.')[:-1] prefix.insert(len(prefix), suffix) return os.path.join(dirname, '.'.join(prefix)) @@ -518,7 +519,7 @@ class dslr(): ) capture_ok = camera_file.save(img_path) except self.gp.GPhoto2Error as ex: - print(ex.code) + print(ex) # This is the way to find which error code is returned by gphoto # See http://gphoto.org/doc/api/gphoto2-result_8h.html for error codes # Cam was turned off/on @@ -565,10 +566,16 @@ class dslr(): def increment_setting(self, setting:str, inc:int=-1): if setting in self.camera_current_settings: - if self.camera_current_settings[setting]['value'] + self.camera_current_settings[setting]['step'] in range(self.camera_current_settings[setting]['min'],self.camera_current_settings[setting]['max']+1): - self.camera_current_settings[setting]['value'] += self.camera_current_settings[setting]['step'] - else: - self.camera_current_settings[setting]['value'] = self.camera_current_settings[setting]['min'] + if inc == -1: + if self.camera_current_settings[setting]['value'] - self.camera_current_settings[setting]['step'] in range(self.camera_current_settings[setting]['min'],self.camera_current_settings[setting]['max']+1): + self.camera_current_settings[setting]['value'] -= self.camera_current_settings[setting]['step'] + else: + self.camera_current_settings[setting]['value'] = self.camera_current_settings[setting]['max'] + elif inc == 1: + if self.camera_current_settings[setting]['value'] + self.camera_current_settings[setting]['step'] in range(self.camera_current_settings[setting]['min'],self.camera_current_settings[setting]['max']+1): + self.camera_current_settings[setting]['value'] += self.camera_current_settings[setting]['step'] + else: + self.camera_current_settings[setting]['value'] = self.camera_current_settings[setting]['min'] def apply_setting(self, to_set:list=None, inc:int=0): if self.cam is None: @@ -578,7 +585,7 @@ class dslr(): if to_set is None: for setting in self.camera_current_settings: if inc: - self.increment_setting(setting) + self.increment_setting(setting, inc) self.apply_gphoto_setting(setting) else: # Get corresponding setting name if possible @@ -586,7 +593,7 @@ class dslr(): if setting in self.cam_settings_map: setting = self.cam_settings_map[setting] if inc: - self.increment_setting(setting) + self.increment_setting(setting, inc) self.apply_gphoto_setting(setting) # validate config status = self.gp.check_result(self.gp.gp_camera_set_config(self.cam, self.camera_current_config)) @@ -598,7 +605,10 @@ class dslr(): self.flip_img = True def focus(self, direction:str='-'): - self.apply_setting(['shutterspeed'], 1) + if direction == '-': + self.apply_setting(['shutterspeed'], -1) + elif direction == '+': + self.apply_setting(['shutterspeed'], 1) def reset_picture_settings(self): if self.cam is None: @@ -611,7 +621,8 @@ class dslr(): status = self.gp.check_result(self.gp.gp_camera_set_config(self.cam, self.camera_current_config)) def close(self): - self.cam.exit() + if self.cam is not None: + self.cam.exit() def get_cam_class(camera_type): @@ -818,7 +829,7 @@ def get_before_last_frame(folder:str): def get_onionskin_frame(folder:str): prev_image = get_last_frame(folder) prev_image = '.'.join(prev_image) - if os.path.exists( os.path.expanduser(os.path.join(savepath, prev_image))): + if os.path.exists(os.path.expanduser(os.path.join(savepath, prev_image))): frm = cv2.imread(os.path.join(savepath, prev_image)) frm = cv2.resize(frm, (project_settings['screen_w'], project_settings['screen_h'])) # Img does not exist, load blank image @@ -1131,6 +1142,7 @@ def main(args): elif (k%256 == 115) or (k%256 == 65): print(_("Inc. exposure")) cam.apply_setting(['exposure'], 1) + # Exposure: key Maj Q elif (k%256 == 90): print(_("Dec. exposure")) cam.apply_setting(['exposure'], -1) @@ -1149,6 +1161,8 @@ def main(args): index += 1 cam.frame = get_onionskin_frame(savepath) cam.o_frame = cam.frame.copy() + else: + print(_("Error during capture. Try again.")) # Display a message if capture was not successfull # This happens when you try to take too much pictures in a short span of time with the DSLR. # With a long exposure, gphoto will sometimes throw a GP_ERROR_CAMERA_BUSY (-110) error.