如何根据用户操作有选择地切换OpenCV的VideoWriter

2024-06-16 11:00:29 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有许多视频片段,它们应该显示特定的步骤序列,但是当这些片段被创建时,它们可能在事件发生之前或之后包含了不需要的动作。在

是否可以使用OpenCV逐帧自动播放视频,以便操作员可以在看到所需动作的开始时按一个键,在序列结束时按另一个键,并将该部分视频保存为新的更小、更精确的序列视频。在

The code below will read in the webcam frame by frame, flip the frame before writing to a video until the user hits the qkey on their keyboard.

我如何确保用户可以在画面进入时观看画面流,当他们看到他们感兴趣的事件时,他们可以切换录像机开始将这些帧写入磁盘,但操作员不需要不断地按一个键来将每个帧发送给录像机,当操作员看到事件结束时,他们可以再次关闭录像机

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

Tags: theread视频if事件序列outcv2
1条回答
网友
1楼 · 发布于 2024-06-16 11:00:29

用一个框架来思考,用一个框架来写可能不是很好。在

我将展开这段代码,将事件数据写入一个CSV文件,并将其与MoviePy结合使用,根据鼠标左键按下时记录的时间戳提取子clip

如果其他人能改进解决方案,我欢迎他们的意见

import cv2
import numpy as np
cap = cv2.VideoCapture('YourVideoFile.mp4')

#Define the Mouse Callback Function
def record_action(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print "Left Button Down @ " + str(cap.get(cv2.CAP_PROP_POS_MSEC)) + " milliseconds into video \n"
    elif event == cv2.EVENT_LBUTTONUP:
        print "Left Button Up @ " + str(cap.get(cv2.CAP_PROP_POS_MSEC)) + " milliseconds into video \n"

#Need to use a Named Window so it can be referenced in the mouse definition 
#and used when outputting the frames from the video in the imshow call later on
cv2.namedWindow("RecordMe")
#Bind the function above to the window
cv2.setMouseCallback("RecordMe",record_action)

while True:
    ret, frame = cap.read()
    #Use NamedWindow we created earlier to show the frames
    cv2.imshow('RecordMe',frame)
    if cv2.waitKey(30) & 0xff == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

相关问题 更多 >