cv2.VideoWriter 在 Jetson Nano 上运行不佳

cv2.VideoWriter doesn't work well on Jetson Nano

我在 Jetson Nano 上使用 OpenCV v.4.1.0 来使用 RPi 相机捕捉视频,模块 cv2.VideoWriter 不能很好地保存视频。该文件为空。

我已经在 RPi 3B+ 上证明 cv2.VideoWriter 并且工作正常。

import numpy as np
import cv2

capture_height = 720
capture_width = 1280
frame_rate = 21
display_width = 860
display_height = 640
flip_method = 0

gstr = ('nvarguscamerasrc ! video/x-raw(memory:NVMM),'
        'width=%s, height=%s,'
        'framerate= %s'
        'format=NV12 ! nvvidconv flip-method= %s ! video/x-raw,'   
        'width=%s, height=%s,'
        'format=BGRx ! videoconvert ! appsink'
        % (capture_width, capture_height, frame_rate, flip_method, 
        display_width, display_height))

filename = 'video.avi'
fourcc = cv2.VideoWriter_fourcc(*'XVID')

cap = cv2.VideoCapture(gstr, cv2.CAP_GSTREAMER)
out = cv2.VideoWriter(filename, fourcc, float(frame_rate), 
                     (capture_width,capture_height),True)

while True:
    ret, img = cap.read()
    out.write(img)
    cv2.imshow('img',img)
    if cv2.waitKey(1) & 0xff == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()

此代码应显示来自 RPi 相机的图像,并以 .avi 格式保存视频。

解决这个问题的方法是输入:

out = cv2.VideoWriter(filename, fourcc, float(frame_rate),
                      (display_width, display_height), True)