简体   繁体   English

使用Raspberry Pi +网络摄像头以python录制视频总是会得到空视频或仅一帧视频

[英]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). 我想用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! 第一个问题,我尝试了从OpenCV编写框架到文件python的所有解决方案,但似乎这些解决方案不适合我...所以我想知道是否有人对此问题有其他解决方案,我将不胜感激! 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? 另外,我尝试使用cv2.VideoWriter_fourcc('XVID'),但收到错误消息:'module'对象没有属性'VideoWriter_fourcc'...我该如何解决? Thanks! 谢谢!

You used 'out' to create your video writer object 您使用“ out”创建了视频编写器对象

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

So perhaps you should replace video_writer.write(frame) with out.write(frame) 因此,也许您应该将video_writer.write(frame)替换为out.write(frame)

Also replace video_writer.release() with out.release() 还要用video_writer.release()替换out.release()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Raspberry Pi +网络摄像头以python录制视频,但有时相机无法像我预期的那样打开/关闭 - Using Raspberry Pi + Webcam to record video in python, but sometimes camera does not turn on/off as I expected 使用python录制网络摄像头视频 - Record a Webcam video with python 如何使用 USB 网络摄像头在树莓派上拍摄视频 - How to take video on a raspberry pi with a usb webcam 通过在OpenCV中使用python,无法在Raspberry PI上保存视频 - Can't save video on Raspberry PI by using python with OpenCV 使用 Python 的带 GPIO 按钮的 Raspberry Pi 随机视频 - Raspberry Pi Random Video w/ GPIO Button using Python 在我的Raspberry PI上使用Python和OpenCV未保存任何视频文件 - NO video file was saved by using Python and OpenCV on my Raspberry PI 如何在树莓派中使用 phonon + python + qt 显示任何视频 - How to display any video using phonon + python + qt in raspberry pi 在Raspberry Pi上编辑视频帧 - Editing video frames on Raspberry Pi 如何使用 opencv-python 代码在树莓派 4b 上使用 MJPG 而不是 YUYV 录制视频 - How can I record a video with MJPG instead of YUYV on a raspberry PI 4b with opencv-python code 来自 Raspberry PI 的视频流 - Python 与 raspivid + netcat - Video streaming from Raspberry PI - Python vs. raspivid + netcat
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM