This commit is contained in:
NikolajDanger
2022-09-19 13:51:56 +02:00
parent 280de12b3d
commit cea460354e
8 changed files with 368 additions and 0 deletions

View File

@ -0,0 +1,49 @@
# This script shows how to open a camera in OpenCV and grab frames and show these.
# Kim S. Pedersen, 2022
import cv2 # Import the OpenCV library
def gstreamer_pipeline(capture_width=1024, capture_height=720, framerate=30):
"""Utility function for setting parameters for the gstreamer camera pipeline"""
return (
"libcamerasrc !"
"video/x-raw, width=(int)%d, height=(int)%d, framerate=(fraction)%d/1 ! "
"videoconvert ! "
"appsink"
% (
capture_width,
capture_height,
framerate,
)
)
print("OpenCV version = " + cv2.__version__)
# Open a camera device for capturing
cam = cv2.VideoCapture(gstreamer_pipeline(), apiPreference=cv2.CAP_GSTREAMER)
if not cam.isOpened(): # Error
print("Could not open camera")
exit(-1)
# Open a window
WIN_RF = "Example 1"
cv2.namedWindow(WIN_RF)
cv2.moveWindow(WIN_RF, 100, 100)
while cv2.waitKey(4) == -1: # Wait for a key pressed event
retval, frameReference = cam.read() # Read frame
if not retval: # Error
print(" < < < Game over! > > > ")
exit(-1)
# Show frames
cv2.imshow(WIN_RF, frameReference)
# Finished successfully