Last active
January 12, 2023 16:18
-
-
Save ricardodeazambuja/12755fe39a613d66c808a8dfd2c63adb to your computer and use it in GitHub Desktop.
How to capture a raw frame using OpenCV (python, should be "almost" the same with C++ besides creating a Mat first) compiled with V4L2 support
This file contains 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
# $ v4l2-ctl --list-formats-ext | |
# It shows my webcam supports MJPG and YUYV | |
# More details https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-013.html | |
# And here we have some explanations about the flags available | |
# https://docs.opencv.org/4.x/d4/d15/group__videoio__flags__base.html | |
import cv2 | |
cap = cv2.VideoCapture(0, cv2.CAP_V4L2) # I don't really need the CAP_V4L2 since it's the default | |
print(cap.getBackendName()) # just confirming | |
print(cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))) | |
# if the command returns False, it means it was not accepted | |
# I could also use cv2.VideoWriter_fourcc('Y', 'U', 'Y', 'V') | |
print(cap.set(cv2.CAP_PROP_CONVERT_RGB, 0)) | |
# if the command returns False, it means it was not accepted | |
print(cap.get(cv2.CAP_PROP_BUFFERSIZE)) | |
# You may want to change the buffer size too: | |
# cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) | |
# Curiously, the format changes only after the first frame was retrieved | |
print(cap.get(cv2.CAP_PROP_FORMAT)) | |
ret, frame = cap.read() | |
# and the frame should be raw! | |
# Curiously, the format changes only after the first frame was retrieved | |
print(cap.get(cv2.CAP_PROP_FORMAT)) | |
# print all formats, with codes: | |
# for i in dir(cv2): | |
# if "CV_" in i: | |
# print(i, getattr(cv2,i)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment