簡體   English   中英

如何根據 OpenCV VideoWriter 的觸發器開始和停止保存視頻幀

[英]How to start and stop saving video frames according to a trigger with OpenCV VideoWriter

我正在構建一個通過 RTSP 記錄來自 IP 攝像機的幀的應用程序。

我的引擎負責將視頻保存在 mp4 中,Opencv VideoWriter 運行良好。 我正在尋找的是創建一個 startRecord 和一個 stopRecord 類方法,它們將根據觸發器分別開始和停止記錄(它可能是我傳遞給線程的參數)。 有誰知道做這種事情的最好方法是什么?

這是我的課:

from threading import Thread
import cv2
import time
import multiprocessing
import threading
class RTSPVideoWriterObject(object):
    def __init__(self, src=0):
        # Create a VideoCapture object
        self.capture = cv2.VideoCapture(src)

        # Start the thread to read frames from the video stream
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    def update(self):
        # Read the next frame from the stream in a different thread
        while True:
            if self.capture.isOpened():
                (self.status, self.frame) = self.capture.read()

    def endRecord(self):
        self.capture.release()
        self.output_video.release()
        exit(1)

    def startRecord(self,endRec):

        self.frame_width = int(self.capture.get(3))
        self.frame_height = int(self.capture.get(4))
        self.codec = cv2.VideoWriter_fourcc(*'mp4v')
        self.output_video = cv2.VideoWriter('fileOutput.mp4', self.codec, 30, (self.frame_width, self.frame_height))
        while True:          
            try:
                self.output_video.write(self.frame)
                if endRec:
                    self.endRecord()
            except AttributeError:
                pass




if __name__ == '__main__':

    rtsp_stream_link = 'rtsp://foo:192.5545....'
    video_stream_widget = RTSPVideoWriterObject(rtsp_stream_link)

    stop_threads = False
    t1 = threading.Thread(target = video_stream_widget.startRecord, args =[stop_threads]) 
    t1.start() 
    time.sleep(15)
    stop_threads = True

正如您在主 I 閱讀框架中看到的那樣,並將它們存儲在一個單獨的線程中。 然后我開始記錄(記錄方法是無限循環,因此阻塞),然后在 15 秒后,我試圖傳遞一個“stop_record”參數以正確停止記錄。

部分代碼來自Storing RTSP stream as video file with OpenCV VideoWriter

有人有想法嗎? 我讀了很多 OpenCV 對於多線程來說可能非常棘手

N。

不是將參數傳遞給線程,而是使用類中的內部標志來確定何時開始/停止記錄。 觸發器可以像按空格鍵開始/停止錄制一樣簡單。 當按下空格鍵時,它會切換一個內部變量,將self.record設為True以開始錄制,設為False以停止錄制。 具體來說,要檢查何時按下空格鍵,您可以檢查cv2.waitKey()返回的鍵值是否為32 如果您想要基於任何其他鍵的觸發器,請查看此帖子以確定鍵代碼。 這是使用空格鍵開始/停止錄制視頻的快速示例:

from threading import Thread
import cv2

class RTSPVideoWriterObject(object):
    def __init__(self, src=0):
        # Create a VideoCapture object
        self.capture = cv2.VideoCapture(src)
        self.record = True

        # Default resolutions of the frame are obtained (system dependent)
        self.frame_width = int(self.capture.get(3))
        self.frame_height = int(self.capture.get(4))

        # Set up codec and output video settings
        self.codec = cv2.VideoWriter_fourcc(*'mp4v')
        self.output_video = cv2.VideoWriter('output.mp4', self.codec, 30, (self.frame_width, self.frame_height))

        # Start the thread to read frames from the video stream
        self.thread = Thread(target=self.update, args=())
        self.thread.daemon = True
        self.thread.start()

    def update(self):
        # Read the next frame from the stream in a different thread
        while True:
            if self.capture.isOpened():
                (self.status, self.frame) = self.capture.read()

    def show_frame(self):
        # Display frames in main program
        if self.status:
            cv2.imshow('frame', self.frame)
            if self.record:
                self.save_frame()

        # Press Q on keyboard to stop recording
        key = cv2.waitKey(1)
        if key == ord('q'):
            self.capture.release()
            self.output_video.release()
            cv2.destroyAllWindows()
            exit(1)
        # Press spacebar to start/stop recording
        elif key == 32:
            if self.record:
                self.record = False
                print('Stop recording')
            else:
                self.record = True
                print('Start recording')

    def save_frame(self):
        # Save obtained frame into video output file
        self.output_video.write(self.frame)

if __name__ == '__main__':
    rtsp_stream_link = 'Your stream link!'
    video_stream_widget = RTSPVideoWriterObject(rtsp_stream_link)
    while True:
        try:
            video_stream_widget.show_frame()
        except AttributeError:
            pass

我通過在類中創建一個全局變量來解決這個問題,對於你的案例 endRec。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM