Created
March 4, 2016 18:59
-
-
Save royshil/0f674c96281b686a9a62 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 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.cv.CV_LOAD_IMAGE_COLOR) | |
cv2.imshow('frame', frame) | |
key = cv2.waitKey(1) | |
if key & 0xFF == ord('q'): | |
break | |
#cap.release() | |
video.close() | |
cv2.destroyAllWindows() |
@ronbarak
Try to install libudev-dev
, or other "udev" packages. (use apt search udev
to check)
I have same error and I install this package to fix it.
I know this is quite old stuff.... and still works like a charm... thanks...
hi
i met libv4l2: error dequeuing buf: Broken pipe
, when run the code.
anyone have encoutered it before?
In order to make it working with the last version of OpenCV I changed the row
frame = cv2.imdecode(np.frombuffer(image_data, dtype=np.uint8), cv2.cv.CV_LOAD_IMAGE_COLOR)
to
frame = cv2.imdecode(np.frombuffer(image_data, dtype=np.uint8), cv2.IMREAD_COLOR)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ronbarak