Last active
June 15, 2023 20:14
-
-
Save hugs/f375b61f0b3b3fcd18046b02cafffa64 to your computer and use it in GitHub Desktop.
Tapster 3 - Camera Setting Test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import cv2 | |
import time | |
# Set image resolution | |
# Option 1 | |
#frame_width = 4656 | |
#frame_height = 3496 | |
# Option 2 | |
frame_width = 3840 # "4K" resolution | |
frame_height = 2160 | |
# Option 3 | |
#frame_width = 2592 | |
#frame_height = 1944 | |
# Option 4 | |
#frame_width = 2320 # Slower resolution, but might be faster... | |
#frame_height = 1744 | |
# Set goal Frame Per Second rate | |
fps = 5 | |
def camera_config(): | |
print("Camera config: Starting") | |
# Focus | |
# focus_automatic_continuous | |
# default=1 | |
os.system("v4l2-ctl --device /dev/video0 --set-ctrl=focus_automatic_continuous=1") | |
time.sleep(3) | |
os.system("v4l2-ctl --device /dev/video0 --set-ctrl=focus_automatic_continuous=0") | |
# focus_absolute | |
# min=1 max=1023 default=1 | |
os.system("v4l2-ctl --device /dev/video0 --set-ctrl=focus_absolute=432") | |
# Exposure | |
# auto_exposure | |
# default=3 | |
# 1: Manual Mode | |
# 3: Aperture Priority Mode | |
os.system("v4l2-ctl --device /dev/video0 --set-ctrl=auto_exposure=3") | |
time.sleep(.5) | |
os.system("v4l2-ctl --device /dev/video0 --set-ctrl=auto_exposure=1") | |
# exposure_time_absolute | |
# min=1 max=5000 default 157 | |
os.system("v4l2-ctl --device /dev/video0 --set-ctrl=exposure_time_absolute=1900") | |
# White Balance | |
# white_balance_automatic | |
# default=1 | |
os.system("v4l2-ctl --device /dev/video0 --set-ctrl=white_balance_automatic=1") | |
time.sleep(.5) | |
os.system("v4l2-ctl --device /dev/video0 --set-ctrl=white_balance_automatic=0") | |
# white_balance_temperature | |
# min=2800 max=6500 default=4600 | |
os.system("v4l2-ctl --device /dev/video0 --set-ctrl=white_balance_temperature=1000") | |
time.sleep(1) | |
print("Camera config: Complete") | |
if __name__ == "__main__": | |
# Open connection to camera | |
cap = cv2.VideoCapture("/dev/video0", cv2.CAP_V4L) | |
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG')) | |
cap.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width) | |
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, frame_height) | |
# Get the settings (to make sure they were set correctly) | |
prop_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
prop_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
print("Camera resolution: " + str(prop_width) + "x" + str(prop_height)) | |
# Read one frame. | |
# Apparently other camera settings get reset by the first cap.read(), | |
# ... so we need to read a frame before we change any other camera settings | |
grabbed, frame = cap.read() | |
# Set camera configuration | |
camera_config() | |
# Read frames and write the last one to file. | |
# This flushes out the initial frames from the camera's buffer that | |
# might be out of focus. | |
for i in range(20): | |
grabbed, frame = cap.read() | |
# Get a frame and write to disk... | |
grabbed, frame = cap.read() | |
cv2.imwrite("frame.jpg", frame) | |
# We're done! | |
cap.release() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment