197 lines
5.4 KiB
Python
Executable File
197 lines
5.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
from typing import Union, overload
|
|
from picamera2 import Picamera2, MappedArray
|
|
from pprint import pprint
|
|
import json
|
|
import cv2
|
|
import time
|
|
|
|
#import numpy as np
|
|
#import argparse
|
|
#import sys
|
|
#import csv
|
|
#import matplotlib.pyplot as plt
|
|
|
|
items = [
|
|
"ColourCorrectionMatrix",
|
|
"Saturation",
|
|
"Contrast",
|
|
"Sharpness",
|
|
"Brightness",
|
|
"NoiseReductionMode",
|
|
"AeEnable",
|
|
"AeMeteringMode",
|
|
"AeConstraintMode",
|
|
"AeExposureMode",
|
|
"AwbEnable",
|
|
"AwbMode",
|
|
"ExposureValue",
|
|
"ExposureTime",
|
|
"AnalogueGain",
|
|
"ColourGains",
|
|
"ScalerCrop",
|
|
"FrameDurationLimits",
|
|
"FrameRate"
|
|
]
|
|
|
|
picam2 = Picamera2()
|
|
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)}
|
|
fps = 0
|
|
|
|
|
|
def load_settings():
|
|
_result = None
|
|
try:
|
|
with open("settings.json", "r") as fp:
|
|
_result = json.load(fp)
|
|
except Exception:
|
|
pass
|
|
|
|
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]
|
|
value = _settings[item]
|
|
if value < c[0] or value > c[1]:
|
|
raise Exception(f"Invalid Value for {item}")
|
|
|
|
picam2.set_controls({item: value})
|
|
|
|
def set_value(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]
|
|
|
|
def process(request):
|
|
global fps
|
|
|
|
timestamp = time.strftime("%Y-%m-%d %X")
|
|
|
|
with MappedArray(request, "main") as m:
|
|
# Display timestamp on frame
|
|
cv2.putText(m.array, timestamp, (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
|
|
|
# Display FPS on frame
|
|
cv2.putText(m.array, "FPS : " + str(int(fps)), (20, m.array.shape[0] - 20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 240, 0), 2)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
for item in items:
|
|
try:
|
|
caps[item] = picam2.camera_controls[item]
|
|
except KeyError:
|
|
print(f"{item}: Not implemented")
|
|
|
|
# Config
|
|
camera_config = picam2.create_preview_configuration({'format': 'XRGB8888', 'size': picam2.sensor_modes[0]['size']})
|
|
camera_config = picam2.create_preview_configuration({'format': 'XRGB8888', 'size': (800, 600)})
|
|
picam2.configure(camera_config)
|
|
|
|
|
|
settings |= load_settings()
|
|
shutter = settings['ExposureTime']
|
|
gain = settings['AnalogueGain']
|
|
fps = settings['FrameRate']
|
|
apply_settings(settings)
|
|
|
|
pprint(f"Capabilities: {caps}")
|
|
pprint(f"Settings: {settings}")
|
|
pprint(f"Camera config: {picam2.camera_configuration()}")
|
|
|
|
# Start
|
|
picam2.pre_callback = process
|
|
picam2.start()
|
|
|
|
while True:
|
|
# Start timer
|
|
timer = cv2.getTickCount()
|
|
|
|
frame = picam2.capture_array("main")
|
|
|
|
# Process the frame using OpenCV operations
|
|
cv2.imshow('Frame', frame)
|
|
|
|
# Exit the loop on pressing 'q'
|
|
key = cv2.pollKey() & 0xFF
|
|
if key == ord('q'):
|
|
break
|
|
elif key == ord('w'):
|
|
shutter -= 1000
|
|
shutter = set_value('ExposureTime', shutter)
|
|
print(f"Shutter: {shutter}")
|
|
elif key == ord('s'):
|
|
shutter -= 100
|
|
shutter = set_value('ExposureTime', shutter)
|
|
print(f"Shutter: {shutter}")
|
|
elif key == ord('x'):
|
|
shutter -= 1
|
|
shutter = set_value('ExposureTime', shutter)
|
|
print(f"Shutter: {shutter}")
|
|
elif key == ord('e'):
|
|
shutter += 1000
|
|
shutter = set_value('ExposureTime', shutter)
|
|
print(f"Shutter: {shutter}")
|
|
elif key == ord('d'):
|
|
shutter += 100
|
|
shutter = set_value('ExposureTime', shutter)
|
|
print(f"Shutter: {shutter}")
|
|
elif key == ord('c'):
|
|
shutter += 1
|
|
shutter = set_value('ExposureTime', shutter)
|
|
print(f"Shutter: {shutter}")
|
|
elif key == ord('h'):
|
|
gain -= 1
|
|
gain = set_value('AnalogueGain', gain)
|
|
print(f"Gain: {gain}")
|
|
elif key == ord('j'):
|
|
gain += 1
|
|
gain = set_value('AnalogueGain', gain)
|
|
print(f"Gain: {gain}")
|
|
elif key == ord('r'):
|
|
fps -= 1
|
|
fps = set_value('FrameRate', fps)
|
|
print(f"FPS: {fps}")
|
|
elif key == ord('f'):
|
|
fps -= 0.1
|
|
fps = set_value('FrameRate', fps)
|
|
print(f"FPS: {fps}")
|
|
elif key == ord('v'):
|
|
fps -= 0.01
|
|
fps = set_value('FrameRate', fps)
|
|
print(f"FPS: {fps}")
|
|
elif key == ord('t'):
|
|
fps += 1
|
|
fps = set_value('FrameRate', fps)
|
|
print(f"FPS: {fps}")
|
|
elif key == ord('g'):
|
|
fps += 0.1
|
|
fps = set_value('FrameRate', fps)
|
|
print(f"FPS: {fps}")
|
|
elif key == ord('b'):
|
|
fps += 0.01
|
|
fps = set_value('FrameRate', fps)
|
|
print(f"FPS: {fps}")
|
|
|
|
# Calculate Frames per second (FPS)
|
|
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
|
|
|
|
|
|
# Release the capture and close windows
|
|
save_settings(settings)
|
|
cv2.destroyAllWindows()
|