ucm*_*mou 4 python video video-capture
我想每 10 秒后从视频文件中捕获一帧,所以如果有人可以帮助我,我将非常感激。我的python代码是这样的:
import cv2
print(cv2.__version__)
vidcap = cv2.VideoCapture('Standoff.avi')
vidcap.set(cv2.CAP_PROP_POS_MSEC,96000)
success,image = vidcap.read()
count = 0
success = True
while success:
success,image = vidcap.read()
print 'Read a new frame: ', success
cv2.imwrite("frame%d.jpg" % count, image) # save frame as JPEG file
cv2.waitKey(200)
count += 1
Run Code Online (Sandbox Code Playgroud)
在这里,我在 10 帧转换后捕获帧。您可以使用时间函数并在 if 条件语句中类似地捕获帧
import cv2
vidcap = cv2.VideoCapture('testing.mp4')
count = 0
success = True
fps = int(vidcap.get(cv2.CAP_PROP_FPS))
while success:
success,image = vidcap.read()
print('read a new frame:',success)
if count%(10*fps) == 0 :
cv2.imwrite('frame%d.jpg'%count,image)
print('successfully written 10th frame')
count+=1
Run Code Online (Sandbox Code Playgroud)
如果您可以从文件中获取视频的帧率,则以下内容应该可以工作(您可能需要检查语法,因为我尚未对其进行测试):
import cv2
cap = cv2.VideoCapture('Standoff.avi')
framerate = cap.get(cv2.cv.CV_CAP_PROP_FPS)
framecount = 0
while(True):
# Capture frame-by-frame
success, image = cap.read()
framecount += 1
# Check if this is the frame closest to 10 seconds
if framecount == (framerate * 10)
framecount = 0
cv2.imshow('image',image)
# Check end of video
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12716 次 |
| 最近记录: |