![](/img/trans.png)
[英]Using Raspberry Pi + Webcam to record video in python, but sometimes camera does not turn on/off as I expected
[英]Using Raspberry Pi + Webcam to record video in python always gets an empty video or only one frame video
我想用Raspberry Pi + wedcam(logitech)錄制視頻。 盡管我發現了許多示例,這些示例實際上與以下代碼幾乎相同:
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()
第一個問題,我嘗試了從OpenCV編寫框架到文件python的所有解決方案,但似乎這些解決方案不適合我...所以我想知道是否有人對此問題有其他解決方案,我將不勝感激! 第二個問題,我發現有人使用
cv2.VideoWriter_fourcc('XVID')
代替
cv2.cv.CV_FOURCC(*'XVID')
那會是問題嗎? 另外,我嘗試使用cv2.VideoWriter_fourcc('XVID'),但收到錯誤消息:'module'對象沒有屬性'VideoWriter_fourcc'...我該如何解決? 謝謝!
您使用“ out”創建了視頻編寫器對象
out = cv2.VideoWriter(path,fourcc, 20.0, (640,480))
因此,也許您應該將video_writer.write(frame)
替換為out.write(frame)
還要用video_writer.release()
替換out.release()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.