1

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

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

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格式保存视频。

4

1 回答 1

1

对此的解决方案是键入:

out = cv2.VideoWriter(filename, fourcc, float(frame_rate),
                      (display_width, display_height), True)
于 2019-08-19T14:22:01.903 回答