Search code examples
pythonopencvnvidia-jetsonnvidia-jetson-nano

cv2.VideoWriter doesn't work well on Jetson Nano


I'm using OpenCV v.4.1.0 on Jetson Nano to capture video with RPi camera and the module cv2.VideoWriter doesn't work well saving video. The file is empty.

I've proved cv2.VideoWriter on RPi 3B+ and it worked fine.

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()

This code should show an image from RPi camera, and save a video with .avi format.


Solution

  • A solution to this is typing:

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