-
-
Save lrob/a1d2d2d1f6ea702885eab069d4ac7ce4 to your computer and use it in GitHub Desktop.
A way to set V4L2 camera params for OpenCV, when cv2.VideoCapture doesn't work. This requires the python-v42lcapture module (https://github.com/gebart/python-v4l2capture)
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
#!/usr/bin/env python | |
import numpy as np | |
import cv2 | |
import os | |
import v4l2capture | |
import select | |
if __name__ == '__main__': | |
#cap = cv2.VideoCapture(0) | |
#cap.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 1920) # <-- this doesn't work. OpenCV tries to set VIDIO_S_CROP instead of the frame format | |
#cap.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 1080) | |
# The following is from: https://github.com/gebart/python-v4l2capture | |
# Open the video device. | |
video = v4l2capture.Video_device("/dev/video0") | |
# Suggest an image size to the device. The device may choose and | |
# return another size if it doesn't support the suggested one. | |
size_x, size_y = video.set_format(1920, 1080, fourcc='MJPG') | |
print "device chose {0}x{1} res".format(size_x, size_y) | |
# Create a buffer to store image data in. This must be done before | |
# calling 'start' if v4l2capture is compiled with libv4l2. Otherwise | |
# raises IOError. | |
video.create_buffers(30) | |
# Send the buffer to the device. Some devices require this to be done | |
# before calling 'start'. | |
video.queue_all_buffers() | |
# Start the device. This lights the LED if it's a camera that has one. | |
print "start capture" | |
video.start() | |
while(True): | |
#We used to do the following, but it doesn't work :( | |
#ret, frame = cap.read() | |
#Instead... | |
# Wait for the device to fill the buffer. | |
select.select((video,), (), ()) | |
# The rest is easy :-) | |
image_data = video.read_and_queue() | |
print "decode" | |
frame = cv2.imdecode(np.frombuffer(image_data, dtype=np.uint8), cv2.IMREAD_COLOR) | |
cv2.imshow('frame', frame) | |
key = cv2.waitKey(1) | |
if key & 0xFF == ord('q'): | |
break | |
#cap.release() | |
video.close() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment