简体   繁体   English

在 python 中使用 opencv 流式传输的视频的异步列表

[英]Asynchronous list of Videos to be stream using opencv in python

Trying to Run the list of videos through OpenCV using python while going through the blog I was able to run for Webcam but I am trying run it for list of cameras for which I am not able to make it work,尝试在浏览博客时使用 python 通过 OpenCV 运行视频列表我能够为网络摄像头运行,但我正在尝试为我无法使其工作的相机列表运行它,

import threading
import cv2
import time

class VideoCaptureAsync:
    def __init__(self, src=0, width=640, height=480):
        self.src = src
        self.cap = cv2.VideoCapture(self.src)
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
        self.grabbed, self.frame = self.cap.read()
        self.started = False
        self.read_lock = threading.Lock()

    def set(self, var1, var2):
        self.cap.set(var1, var2)

    def start(self):
        if self.started:
            print('[!] Asynchroneous video capturing has already been started.')
            return None
        self.started = True
        self.thread = threading.Thread(target=self.update, args=())
        self.thread.start()
        return self

    def update(self):
        while self.started:
            grabbed, frame = self.cap.read()
            with self.read_lock:
                self.grabbed = grabbed
                self.frame = frame

    def read(self):
        with self.read_lock:
            frame = self.frame.copy()
            grabbed = self.grabbed
        return grabbed, frame

    def stop(self):
        self.started = False
        self.thread.join()

    def __exit__(self, exec_type, exc_value, traceback):
        self.cap.release()

def test(n_frames=500, width=1280, height=720, async=False):
    if async:
        cap = VideoCaptureAsync(0)
    else:
        cap = cv2.VideoCapture(0)
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
    if async:
        cap.start()
    t0 = time.time()
    i = 0
    while i < n_frames:
        _, frame = cap.read()
        cv2.imshow('Frame', frame)
        cv2.waitKey(1) & 0xFF
        i += 1
    print('[i] Frames per second: {:.2f}, async={}'.format(n_frames / (time.time() - t0), async))
    if async:
        cap.stop()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    test(n_frames=500, width=1280, height=720, async=False)
    test(n_frames=500, width=1280, height=720, async=True)

I wanted to run this for multiple IP cameras Any suggestions for running this code for multiple IP cameras will be very helpful我想为多个 IP 摄像机运行此代码 任何为多个 IP 摄像机运行此代码的建议都将非常有帮助

You need to initiate the VideoCaptureAsync multiple times in your test function.您需要在test函数中多次启动VideoCaptureAsync Furthermore you don't need to set height and width multiple times as the class you have defined already have that as parameters.此外,您不需要多次设置高度和宽度,因为您定义的类已经将其作为参数。 I have modified your test function a bit to show an example for running two webcams at the same time:我稍微修改了您的测试功能,以显示同时运行两个网络摄像头的示例:

def test(n_frames=500, width=1280, height=720, async_flag=False):
    if async_flag:
        cap = VideoCaptureAsync(src=0, width=width, height=height) #<----Change src to ip camera
        cap1 = VideoCaptureAsync(src=1, width=width, height=height) #<----Change src to ip camera 2
    else:
        cap = cv2.VideoCapture(0)
    if async_flag:
        cap.start()
        cap1.start()
    t0 = time.time()
    i = 0
    while i < n_frames:
        _, frame = cap.read()
        _, frame1 = cap1.read()
        cv2.imshow('Frame', frame)
        cv2.imshow('Frame 1', frame1)
        cv2.waitKey(1) & 0xFF
        i += 1
    print('[i] Frames per second: {:.2f}, async={}'.format(n_frames / (time.time() - t0), async_flag))
    if async_flag:
        cap.stop()
        cap1.stop()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    # test(n_frames=500, width=1280, height=720, async_flag=False)
    test(n_frames=500, width=1280, height=720, async_flag=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM