[英]Save Video as Frames OpenCV{PY}
我想創建一個程序來保存從網絡攝像頭(幀)中獲取的.jpg圖像。 我的程序現在做的是,打開網絡攝像頭,只取一個幀,然后一切都停止。
我想要的是多個幀我的錯誤代碼就是這個:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
count = 0
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imwrite("frame%d.jpg" % ret, frame) # save frame as JPEG file
count +=1
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(10):
break
實際上聽起來你總是使用相同的名稱保存你的圖像,因為你是連接ret而不是imwrite方法中的count
嘗試這個 :
name = "frame%d.jpg"%count
cv2.imwrite(name, frame) # save frame as JPEG file
當沒有按下任何鍵並且時間延遲到期時, cv2.waitKey
返回-1
。 您可以在doc中查看它。
基本上,您所要做的就是略微改變程序的結尾:
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(10) != -1:
break
用這個 -
count = 0
cv2.imwrite("frame%d.jpg" % count, frame)
count = count+1
雖然這已經很晚了但我不得不說prtkp的答案就是你需要的......你用來枚舉圖像的ret值是錯誤的。 Ret只是一個布爾值...所以當它檢測到圖像時,它會在那里放置一個圖像名稱...
我剛剛使用了這個......標題上的ac = 0
cv2.imwrite("img/frame %d.jpg" % c,img)
c=c+1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.