diff --git a/ocv_rpicam.py b/ocv_rpicam.py index 14d8146..21930d7 100755 --- a/ocv_rpicam.py +++ b/ocv_rpicam.py @@ -13,6 +13,9 @@ import time #import csv #import matplotlib.pyplot as plt +picam2 = Picamera2() +pprint(picam2.sensor_modes) + items = [ "ColourCorrectionMatrix", "Saturation", @@ -35,10 +38,10 @@ items = [ "FrameRate" ] -picam2 = Picamera2() -pprint(picam2.sensor_modes) -settings = {'NoiseReductionMode': 0, 'AeExposureMode': 0, 'ExposureTime': 10000, 'AnalogueGain': 1, 'FrameRate': picam2.sensor_modes[0]['fps']} +settings = {'AeEnable': 0, 'AwbEnable': 0, 'NoiseReductionMode': 0, 'ExposureTime': 10000, 'AnalogueGain': 1, 'FrameRate': picam2.sensor_modes[0]['fps']} caps = {"FrameRate": (1, picam2.sensor_modes[0]['fps'], None)} +fps_ist = 0 +fps_soll = 0 def load_settings(): @@ -51,10 +54,12 @@ def load_settings(): return _result + def save_settings(_settings): with open("settings.json", "w") as fp: json.dump(_settings, fp, indent=0) + def apply_settings(_settings): for item in _settings: c = caps[item] @@ -64,22 +69,16 @@ def apply_settings(_settings): picam2.set_controls({item: value}) -def set_value(item: str, value): + +def clamp_and_store(item: str, value): clamped_value = value c = caps[item] clamped_value = max(c[0], min(c[1], value)) - picam2.set_controls({item: clamped_value}) settings[item] = clamped_value return clamped_value -def get_value(item: str): - return picam2.camera_controls[item] - -fps_ist = 0 -fps_soll = 0 - def process(request): timestamp = time.strftime("%Y-%m-%d %X") @@ -104,6 +103,18 @@ def process(request): # Display FPS on frame cv2.putText(m.array, f"Frame rate : {fps_ist:.1f}/{fps_soll:.1f}", (20, m.array.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 240, 0), 2) +shutter_keys = {'w': -1000, 's': -100, 'x': -1, 'e': 1000, 'd': 100, 'c': 1} +gain_keys = {'h': -1, 'j': 1} +fps_keys = {'r': -1, 'f': -0.1, 'v': -0.01, 't': 1, 'g': 0.1, 'b': 0.01} + +def keypress_changed(key_defs: dict, key: int): + _res = None + try: + _res = key_defs[str(chr(key))] + except KeyError: + pass + + return _res if __name__ == '__main__': for item in items: @@ -152,59 +163,32 @@ if __name__ == '__main__': key = cv2.pollKey() & 0xFF if key == ord('q'): break - elif key == ord('w'): - shutter -= 1000 + + control_changed = False + v = keypress_changed(shutter_keys, key) + if v is not None: + control_changed = True + shutter = clamp_and_store('ExposureTime', shutter + v) print(f"Shutter: {shutter}") - elif key == ord('s'): - shutter -= 100 - print(f"Shutter: {shutter}") - elif key == ord('x'): - shutter -= 1 - print(f"Shutter: {shutter}") - elif key == ord('e'): - shutter += 1000 - print(f"Shutter: {shutter}") - elif key == ord('d'): - shutter += 100 - print(f"Shutter: {shutter}") - elif key == ord('c'): - shutter += 1 - print(f"Shutter: {shutter}") - elif key == ord('h'): - gain -= 1 + + v = keypress_changed(gain_keys, key) + if v is not None: + control_changed = True + gain = clamp_and_store('AnalogueGain', gain + v) print(f"Gain: {gain}") - elif key == ord('j'): - gain += 1 - print(f"Gain: {gain}") - elif key == ord('r'): - fps_soll -= 1 - print(f"FPS: {fps_soll}") - elif key == ord('f'): - fps_soll -= 0.1 - print(f"FPS: {fps_soll}") - elif key == ord('v'): - fps_soll -= 0.01 - print(f"FPS: {fps_soll}") - elif key == ord('t'): - fps_soll += 1 - print(f"FPS: {fps_soll}") - elif key == ord('g'): - fps_soll += 0.1 - print(f"FPS: {fps_soll}") - elif key == ord('b'): - fps_soll += 0.01 + + v = keypress_changed(fps_keys, key) + if v is not None: + control_changed = True + fps_soll = clamp_and_store('FrameRate', fps_soll + v) print(f"FPS: {fps_soll}") - # Update control settings and keep vars within ranges - shutter = set_value('ExposureTime', shutter) - gain = set_value('AnalogueGain', gain) - fps_soll = set_value('FrameRate', fps_soll) - - # Write contrrols to camera - with picam2.controls as controls: - controls.ExposureTime = shutter - controls.AnalogueGain = gain - controls.FrameRate = fps_soll + if control_changed: + # Write contrrols to camera + with picam2.controls as controls: + controls.ExposureTime = shutter + controls.AnalogueGain = gain + controls.FrameRate = fps_soll # Calculate Frames per second (FPS) fr = cv2.getTickFrequency() / (cv2.getTickCount() - timer) diff --git a/settings.json b/settings.json index 6de1cb7..fd4f37c 100755 --- a/settings.json +++ b/settings.json @@ -1,7 +1,9 @@ { +"AeEnable": 0, +"AwbEnable": 0, "NoiseReductionMode": 0, -"AeExposureMode": 0, -"ExposureTime": 10000, -"AnalogueGain": 1.0, -"FrameRate": 60.38 +"ExposureTime": 20007, +"AnalogueGain": 9.0, +"FrameRate": 16, +"AeExposureMode": 0 } \ No newline at end of file