我正在尝试使用 Python 3 OpenCV 中的 gstreamer 管道显示和保存我的 Flir Hadron 相机视频。我有一个完美工作的 Gstreamer 管道,能够以高分辨率显示和保存视频,但是当我执行 Python3 代码来显示和保存时,出现以下错误:
无法打开相机。现有
在我看来,这意味着我的 camset 代码行有问题,因为它在代码中打印消息
if cap.isOpened() is not True
我还是不知道如何解决这个问题。
还有一点:我的 opencv 中有 gstreamer。任何帮助将不胜感激。
Gstreamer 管道(显示并保存):
gst-launch-1.0 v4l2src io-mode=4 device=/dev/video0 do-timestamp=true ! tee name=t ! 'video/x-raw, format=UYVY, width=1920, height=1080, framerate=30/1' ! queue leaky=1 ! xvimagesink sync=false t. ! queue ! nvvidconv ! nvv4l2h265enc bitrate=8000000 ! h265parse ! qtmux ! filesink location=/home/nvidia/Desktop/RGB_$(date '+%Y-%m-%d_%H-%M-%S').mp4 -e
python3代码:
import cv2
fps=30
frame_width=1920
frame_height=1080
camset='v4l2src io-mode=4 device=/dev/video0 do-timestamp=true ! video/x-raw,format=UYVY, width=1920, height=1080, framerate=30/1 ! queue ! nvvidconv ! appsink'
cam=cv2.VideoCapture(camset,cv2.CAP_GSTREAMER)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, frame_width)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT,frame_height)
cam.set(cv2.CAP_PROP_FPS, fps)
if cam.isOpened() is not True:
print("Cannot open camera. Existing.")
quit()
gst_str = ' appsrc ! video/x-raw,format=UYVY, width=1920, height=1080, framerate=30/1 ! queue ! videoconvert ! nvvidconv ! nvv4l2h265enc maxperf-enable=1 preset-level=4 control-rate=1 bitrate=16000000 ! h265parse ! qtmux ! filesink location=streaming.mp4 '
fourcc = cv2.VideoWriter_fourcc(*'H265')
out=cv2.VideoWriter(gst_str, fourcc , 30, (frame_width, frame_height), True)
while (True):
ret,frame = cam.read()
cv2.imshow('camera',frame)
out.write(frame)
if cv2.waitKey(1) & 0XFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
我尝试更改 camset 代码行,但没有更好的结果
您可能会纠正的一些观点:
所以你可以尝试这个修正后的版本:
import cv2
#fps=30
#frame_width=1920
#frame_height=1080
# For your camera case (not tested)
#camset='v4l2src io-mode=4 device=/dev/video0 do-timestamp=true ! video/x-raw,format=UYVY,width=1920,height=1080,framerate=30/1 ! queue ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1'
# Simulated Video source
camset='videotestsrc is-live=1 ! video/x-raw,format=UYVY,width=1920,height=1080,framerate=30/1 ! queue ! videoconvert ! video/x-raw,format=BGR ! appsink drop=1'
cam=cv2.VideoCapture(camset,cv2.CAP_GSTREAMER)
if cam.isOpened() is not True:
print("Cannot open camera. Exiting.")
quit()
actual_width = int(cam.get(cv2.CAP_PROP_FRAME_WIDTH))
actual_height = int(cam.get(cv2.CAP_PROP_FRAME_HEIGHT))
actual_fps = float(cam.get(cv2.CAP_PROP_FPS));
print('Capture opened framing %d x %d @ %f' % (actual_width,actual_height,actual_fps))
gst_writer_str = 'appsrc ! video/x-raw,format=BGR,width=1920,height=1080,framerate=30/1 ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! nvv4l2h265enc maxperf-enable=1 preset-level=4 control-rate=1 bitrate=16000000 ! h265parse ! qtmux ! filesink location=streaming.mp4'
fourcc = 0 #RAW
out = cv2.VideoWriter(gst_writer_str, cv2.CAP_GSTREAMER, fourcc, actual_fps, (actual_width, actual_height), True)
if not out.isOpened():
print("Cannot open writer. Exiting.")
quit()
while (True):
ret,frame = cam.read()
cv2.imshow('camera',frame)
out.write(frame)
if cv2.waitKey(1) & 0XFF == ord('q'):
break
out.release()
cam.release()
cv2.destroyAllWindows()