【问题标题】:How to print 1 in every thousand frames using opencv and python?如何使用opencv和python在每千帧中打印1个?
【发布时间】:2017-10-20 21:14:02
【问题描述】:

我正在尝试在视频的每千帧中保存一帧。以下是我目前使用的代码:

import cv2
import numpy as np
import os
# Playing video from file:
cap = cv2.VideoCapture('D:/01 Projects/AMAZON CATALYST PROJECT/Surgery1.mpg')
try:
    if not os.path.exists('D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1'):
    os.makedirs('D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1')
except OSError:
    print ('Error: Creating directory of data_surg1')
currentFrame = 0
while(True):
    # Capture frame-by-frame
    if currentFrame > 0:
        cap.set(cv2.CAP_PROP_POS_MSEC,currentFrame) 
    ret, frame = cap.read()

    # Saves image of the current frame in jpg file
    name = 'D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1/frame' + str(currentFrame/1000) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)

    # To stop duplicate images
    currentFrame += 1000

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

但是,我不确定这是否正确保存。当我在文件资源管理器中查看帧时,数字最初非常高,然后与之前的图像相比减少以形成连续的帧数。我正在使用 Python 2.7 和 OpenCV3.3。

【问题讨论】:

  • 你能告诉我们你的文件名是什么吗?
  • 当你想按帧号过滤时,为什么要以毫秒为单位设置位置?虽然这在视频文件的帧速率为 1000 FPS 的情况下可能有效,但即使那样,这也是表达您意图的一种相当模糊的方式。
  • 尝试 cv2.CAP_PROP_POS_FRAMES 而不是毫秒
  • 谢谢@Micka 当我使用 cv2.CAP_PROP_PROP_POS_FRAMES 而不是 MSEC 时得到的输出正是我想要的。谢谢!

标签: python opencv video


【解决方案1】:

对于从某些帧存储而不是基于时间的保存,以下脚本有效:

import cv2
import numpy as np
import os
# Playing video from file:
cap = cv2.VideoCapture('D:/01 Projects/AMAZON CATALYST PROJECT/Surgery1.mpg')
try:
    if not os.path.exists('D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1'):
    os.makedirs('D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1')
except OSError:
    print ('Error: Creating directory of data_surg1')
currentFrame = 0
while(True):
    # Capture frame-by-frame
    if currentFrame > 0:
        cap.set(cv2.CAP_PROP_POS_FRAMES,currentFrame) 
    ret, frame = cap.read()

    # Saves image of the current frame in jpg file
    name = 'D:/01 Projects/AMAZON CATALYST PROJECT/data_surg1/frame' + str(currentFrame/1000) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)

    # To stop duplicate images
    currentFrame += 1

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 2018-07-09
    • 2019-09-03
    • 1970-01-01
    相关资源
    最近更新 更多