While I was working, found a problem that I’m not able to run this code in python:
import sys
import cv2
def read_cam():
cap = cv2.VideoCapture(“v4l2src device=/dev/video0 ! jpegdec ! video/x-raw, width=640, height=480 ! xvimagesink”, cv2.CAP_GSTREAMER)
if True:
cv2.namedWindow(“demo”, cv2.WINDOW_AUTOSIZE)
while True:
ret_val, img = cap.read();
cv2.imshow(‘demo’,img)
cv2.waitKey(10)
else:
print(“camera open failed”)
cv2.destroyAllWindows()
if name == ‘main’:
read_cam()
However if I’m trying to run « v4l2src device=/dev/video0 ! jpegdec ! video/x-raw, width=640, height=480 ! xvimagesink» in terminal, it works, so what is the problem?
Thanks for reply, but it didn’t help me, now it throws me a mistake:
cv2.error: OpenCV(4.6.0post deleted by author) /io/opencv/modules/highgui/src/window.cpp:967: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘imshow’
Hey!
It still doesn’t want to work
I crashed my monitor, that’s why while I’m waiting for new one, I’m practicing on Ubuntu on my laptop. My laptop’s web cam supports mjpeg and yuyv formats I still can’t understand why.
When I’m putting these commands in terminal, everything is okay and it’s working, also usual cv2.VideoCapture(‘/dev/video0’) works, sudo gst-launch-1.0 v4l2src device=/dev/video0 ! jpegdec ! ‘video/x-raw, width=640, height=480’ ! xvimagesink works too, but when I try it in python, it seems that it just doesn’t receives a frame (according to a mistake)
Hi,
It seems like gstreamer is not enabled in OpenCV. If you are not using Jetson Nano now, may need to manually enable gstreamer and rebuild OpenCV on the host PC.
Gstreamer support as backend for VideoCapture and VideoWriter in opencv videoio is an option that can be activated or not at opencv build time (well just before in CMake config).
You may check if the opencv lib used by your current python setup does support gstreamer with:
import cv2
print(cv2.getBuildInformation())
in displayed log, look for GSTREAMER support, if it shows NO you would have to install another opencv build supporting gstreamer for your current python install.
If GSTREAMER support is ok, you may try:
cap = cv2.VideoCapture("videotestsrc ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1", cv2.CAP_GSTREAMER)
# If ok try from camera:
cap = cv2.VideoCapture("v4l2src device=/dev/video0 ! video/x-raw,format=YUY2,width=640,height=480,framerate=30/1 ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1", cv2.CAP_GSTREAMER)
# or trying to read from camera in JPEG format:
cap = cv2.VideoCapture("v4l2src device=/dev/video0 ! image/jpeg,format=MJPG,width=640,height=480,framerate=30/1 ! nvv4l2decoder mjpeg=1 ! nvvidconv ! video/x-raw,format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1", cv2.CAP_GSTREAMER)
Last, you may also follow kernel messages with:
sudo dmesg --follow
and check if running your python code triggers some messages (from USB stack ?)