- refactored spaghetti code
- added key_pressed
This commit is contained in:
+44
-60
@@ -13,6 +13,9 @@ import time
|
|||||||
#import csv
|
#import csv
|
||||||
#import matplotlib.pyplot as plt
|
#import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
picam2 = Picamera2()
|
||||||
|
pprint(picam2.sensor_modes)
|
||||||
|
|
||||||
items = [
|
items = [
|
||||||
"ColourCorrectionMatrix",
|
"ColourCorrectionMatrix",
|
||||||
"Saturation",
|
"Saturation",
|
||||||
@@ -35,10 +38,10 @@ items = [
|
|||||||
"FrameRate"
|
"FrameRate"
|
||||||
]
|
]
|
||||||
|
|
||||||
picam2 = Picamera2()
|
settings = {'AeEnable': 0, 'AwbEnable': 0, 'NoiseReductionMode': 0, 'ExposureTime': 10000, 'AnalogueGain': 1, 'FrameRate': picam2.sensor_modes[0]['fps']}
|
||||||
pprint(picam2.sensor_modes)
|
|
||||||
settings = {'NoiseReductionMode': 0, 'AeExposureMode': 0, 'ExposureTime': 10000, 'AnalogueGain': 1, 'FrameRate': picam2.sensor_modes[0]['fps']}
|
|
||||||
caps = {"FrameRate": (1, picam2.sensor_modes[0]['fps'], None)}
|
caps = {"FrameRate": (1, picam2.sensor_modes[0]['fps'], None)}
|
||||||
|
fps_ist = 0
|
||||||
|
fps_soll = 0
|
||||||
|
|
||||||
|
|
||||||
def load_settings():
|
def load_settings():
|
||||||
@@ -51,10 +54,12 @@ def load_settings():
|
|||||||
|
|
||||||
return _result
|
return _result
|
||||||
|
|
||||||
|
|
||||||
def save_settings(_settings):
|
def save_settings(_settings):
|
||||||
with open("settings.json", "w") as fp:
|
with open("settings.json", "w") as fp:
|
||||||
json.dump(_settings, fp, indent=0)
|
json.dump(_settings, fp, indent=0)
|
||||||
|
|
||||||
|
|
||||||
def apply_settings(_settings):
|
def apply_settings(_settings):
|
||||||
for item in _settings:
|
for item in _settings:
|
||||||
c = caps[item]
|
c = caps[item]
|
||||||
@@ -64,22 +69,16 @@ def apply_settings(_settings):
|
|||||||
|
|
||||||
picam2.set_controls({item: value})
|
picam2.set_controls({item: value})
|
||||||
|
|
||||||
def set_value(item: str, value):
|
|
||||||
|
def clamp_and_store(item: str, value):
|
||||||
clamped_value = value
|
clamped_value = value
|
||||||
c = caps[item]
|
c = caps[item]
|
||||||
clamped_value = max(c[0], min(c[1], value))
|
clamped_value = max(c[0], min(c[1], value))
|
||||||
picam2.set_controls({item: clamped_value})
|
|
||||||
settings[item] = clamped_value
|
settings[item] = clamped_value
|
||||||
|
|
||||||
return clamped_value
|
return clamped_value
|
||||||
|
|
||||||
|
|
||||||
def get_value(item: str):
|
|
||||||
return picam2.camera_controls[item]
|
|
||||||
|
|
||||||
fps_ist = 0
|
|
||||||
fps_soll = 0
|
|
||||||
|
|
||||||
def process(request):
|
def process(request):
|
||||||
|
|
||||||
timestamp = time.strftime("%Y-%m-%d %X")
|
timestamp = time.strftime("%Y-%m-%d %X")
|
||||||
@@ -104,6 +103,18 @@ def process(request):
|
|||||||
# Display FPS on frame
|
# 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)
|
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__':
|
if __name__ == '__main__':
|
||||||
for item in items:
|
for item in items:
|
||||||
@@ -152,59 +163,32 @@ if __name__ == '__main__':
|
|||||||
key = cv2.pollKey() & 0xFF
|
key = cv2.pollKey() & 0xFF
|
||||||
if key == ord('q'):
|
if key == ord('q'):
|
||||||
break
|
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}")
|
print(f"Shutter: {shutter}")
|
||||||
elif key == ord('s'):
|
|
||||||
shutter -= 100
|
v = keypress_changed(gain_keys, key)
|
||||||
print(f"Shutter: {shutter}")
|
if v is not None:
|
||||||
elif key == ord('x'):
|
control_changed = True
|
||||||
shutter -= 1
|
gain = clamp_and_store('AnalogueGain', gain + v)
|
||||||
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
|
|
||||||
print(f"Gain: {gain}")
|
print(f"Gain: {gain}")
|
||||||
elif key == ord('j'):
|
|
||||||
gain += 1
|
v = keypress_changed(fps_keys, key)
|
||||||
print(f"Gain: {gain}")
|
if v is not None:
|
||||||
elif key == ord('r'):
|
control_changed = True
|
||||||
fps_soll -= 1
|
fps_soll = clamp_and_store('FrameRate', fps_soll + v)
|
||||||
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
|
|
||||||
print(f"FPS: {fps_soll}")
|
print(f"FPS: {fps_soll}")
|
||||||
|
|
||||||
# Update control settings and keep vars within ranges
|
if control_changed:
|
||||||
shutter = set_value('ExposureTime', shutter)
|
# Write contrrols to camera
|
||||||
gain = set_value('AnalogueGain', gain)
|
with picam2.controls as controls:
|
||||||
fps_soll = set_value('FrameRate', fps_soll)
|
controls.ExposureTime = shutter
|
||||||
|
controls.AnalogueGain = gain
|
||||||
# Write contrrols to camera
|
controls.FrameRate = fps_soll
|
||||||
with picam2.controls as controls:
|
|
||||||
controls.ExposureTime = shutter
|
|
||||||
controls.AnalogueGain = gain
|
|
||||||
controls.FrameRate = fps_soll
|
|
||||||
|
|
||||||
# Calculate Frames per second (FPS)
|
# Calculate Frames per second (FPS)
|
||||||
fr = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
|
fr = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
|
||||||
|
|||||||
+6
-4
@@ -1,7 +1,9 @@
|
|||||||
{
|
{
|
||||||
|
"AeEnable": 0,
|
||||||
|
"AwbEnable": 0,
|
||||||
"NoiseReductionMode": 0,
|
"NoiseReductionMode": 0,
|
||||||
"AeExposureMode": 0,
|
"ExposureTime": 20007,
|
||||||
"ExposureTime": 10000,
|
"AnalogueGain": 9.0,
|
||||||
"AnalogueGain": 1.0,
|
"FrameRate": 16,
|
||||||
"FrameRate": 60.38
|
"AeExposureMode": 0
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user