简体   繁体   中英

Using Raspberry Pi + Webcam to record video in python always gets an empty video or only one frame video

I want to record video with Raspberry Pi + wedcam (logitech). Although I found many examples which are actually almost the same code as following:

import numpy as np
import cv2

path = ('/.../output.avi')
fourcc = cv2.cv.CV_FOURCC(*'XVID')
out = cv2.VideoWriter(path,fourcc, 20.0, (640,480))

while(cap.isOpened()):
    #read the frame
    ret, frame = cap.read()
    if ret==True:
        #Write the frame
        video_writer.write(frame)
        #show the frame
        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
video_writer.release()
cv2.destroyAllWindows()

First question, I have tried all solutions from OpenCV write frame to file python but it seems those solutions didn't suitable for me... so I want to know if anyone has other solutions for this problem, I will appreciate! Second question, I found that someone use

cv2.VideoWriter_fourcc('XVID')

instead of

cv2.cv.CV_FOURCC(*'XVID')

Would that be the problem? Also, I have tried to use cv2.VideoWriter_fourcc('XVID'), but get an error: 'module' object has no attribute 'VideoWriter_fourcc'... How can I solve this? Thanks!

You used 'out' to create your video writer object

out = cv2.VideoWriter(path,fourcc, 20.0, (640,480))

So perhaps you should replace video_writer.write(frame) with out.write(frame)

Also replace video_writer.release() with out.release()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM