![](/img/trans.png)
[英]"with" keyword does not work with "cv2.VideoCapture(0)" in python
[英]cv2.VideoCapture on python
我正在運行以下代碼:
import numpy as np
import cv2
import os
count = 0
cap = cv2.VideoCapture("/home/simon/PROJECT/real_data/00000020.mp4")
while not cap.isOpened():
cap = cv2.VideoCapture("./00000020.mp4")
cv2.waitKey(1000)
print "Wait for the header"
pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
while True:
flag, frame = cap.read()
if flag:
# The frame is ready and already captured
cv2.imshow('video', frame)
pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)
print str(pos_frame)+" frames"
else:
# The next frame is not ready, so we try to read it again
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1)
print "frame is not ready"
# It is better to wait for a while for the next frame to be ready
cv2.waitKey(1000)
if cv2.waitKey(10) & 0xFF == 27:
break
if cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT):
# If the number of captured frames is equal to the total number of frames,
# we stop
break
if ret == True:
frame = cv2.VideoCapture.grab()
frame = 'frame%d' % count
cv2.imwrite('frame%d.png', frame)
count += 1
else:
print 'stopped at' + count
break
每當我運行它時,它就會在while not循環中循環,並顯示“ waiting header”。 永遠不會有錯誤代碼或類似的東西。
我試圖將其作為更簡單的代碼運行,其中沒有所有這些檢查,並且再次沒有引發任何錯誤。
我正在嘗試運行此代碼以打開視頻,然后在整個視頻中將幀另存為png文件。
是否有人發現代碼有任何特殊問題? 或者,是否有人知道一段代碼可以更有效地完成我想要的事情,因為我最近通過Google搜索進行了搜索,並且堆棧溢出很多,卻一無所獲
在此先感謝熊貓
您需要在python目錄中包含幾個DLL才能播放視頻。 請查看以下詳細信息:
https://li8bot.wordpress.com/2014/07/09/opencvpython-part-1-working-with-videos/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.