暂停并重新启动Python中的视频 [英] Pausing and restarting a video in Python

查看:623
本文介绍了暂停并重新启动Python中的视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个视频,我想逐帧浏览它们,然后通过按键盘键(取决于帧)对其中一些进行注释.对于许多帧,我都不会按任何键.这是我到目前为止的内容:

I have several video and I want to go through them frame by frame, and annotate some of them by pressing a keyboard key (which depends on the frame). For many of the frames I won't press any key. Here is what I have so far:

import numpy as np
import cv2

cap = cv2.VideoCapture('video.mp4')

frame_number = []
annotation_list = []

i = 0
while(True):        
    # Read one frame.
    ret, frame = cap.read()

    # Show one frame.
    cv2.imshow('frame', frame)

    # Set the time between frames in miliseconds
    c = cv2.waitKey(500)
    i = i + 1

    try:
        annotation_list = annotation_list + [chr(c)]
        frame_number = frame_number + [i]
    except:
        continue

因此,这将显示每个帧0.5秒,并将其与我按下按钮(即给定字母)的每个帧相关联.我现在需要的是这样一个选项,即对于给定的帧,我可以在需要的时间停止视频,例如按空格"键,以考虑如何对其进行注释,然后按空格"键. "(我决定如何添加注释)后,再次继续播放视频.如何添加此暂停/继续选项?谢谢!

So this is showing each frame for 0.5 seconds, and associates to each frame where I press a button, the given letter. What I need now is an option such that for a given frame I can stop the video at that frame for as long as I need, by pressing "Space" for example, in order to think about how to annotate it, then press "Space" again to continue the video, once I decide how to annotate. How can I add this pause/continue option? Thank you!

推荐答案

您可以通过从返回值cv2.waitKey()确定按下了哪个键来实现暂停/恢复功能.要暂停视频,您可以不向cv2.waitKey()传递任何参数(或0),该参数将无限期等待直到按下某个键,然后它将恢复视频.从 docs :

You can implement a pause/resume feature by determining what key was pressed from the return value of cv2.waitKey(). To pause the video, you can pass no parameter (or 0) to cv2.waitKey() which will wait indefinitely until there is a key press then it will resume the video. From the docs:

cv2.waitKey()是键盘绑定功能.它的参数是以毫秒为单位的时间.该函数将为任何键盘事件等待指定的毫秒数.如果在此期间按任意键,程序将继续.如果传递了0,它将无限期地等待击键.还可以将其设置为检测特定的击键,例如是否按下键 a 等,我们将在下面讨论.

cv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds. The function waits for specified milliseconds for any keyboard event. If you press any key in that time, the program continues. If 0 is passed, it waits indefinitely for a key stroke. It can also be set to detect specific key strokes like, if key a is pressed etc which we will discuss below.

要确定是否按下空格键,我们可以检查返回的值是否为32.如果按下此键,则我们将无限期暂停帧,直到按下任意键,然后恢复视频.这是一个示例:

To determine if the spacebar was pressed, we can check if the returned value is 32. If this key was pressed then we indefinitely pause the frame until any key is pressed then we resume the video. Here's an example:

import cv2

cap = cv2.VideoCapture('video.mp4')
if not cap.isOpened():
    print("Error opening video")

while(cap.isOpened()):
    status, frame = cap.read()
    if status:
        cv2.imshow('frame', frame)
    key = cv2.waitKey(500)

    if key == 32:
        cv2.waitKey()
    elif key == ord('q'):
        break


将来,如果要在按某个键后执行某些操作,则可以使用以下脚本确定键码":


In the future if you want to perform some action after pressing a key, you can determine the "key code" with this script:

import cv2

# Load a test image
image = cv2.imread('1.jpg')

while(True):
    cv2.imshow('image', image)
    key = cv2.waitKey(1)
    # 'q' to stop
    if key == ord('q'):
        break
    # Print key 
    elif key != -1:
        print(key)

这篇关于暂停并重新启动Python中的视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆