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

标签 python python-3.x opencv asynchronous opencv3.1

在通过blog尝试通过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)

我想针对多个IP摄像机运行它
为多个IP摄像机运行此代码的任何建议将非常有帮助

最佳答案

您需要在VideoCaptureAsync函数中多次启动test。此外,您不需要多次设置高度和宽度,因为您定义的类已经将其作为参数。我对测试功能做了一些修改,以显示一个示例,用于同时运行两个网络摄像头:

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)

关于python - 在python中使用opencv流式传输的视频的异步列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60501795/

相关文章:

python - 'read_csv'数据的内存与原始数据不同

c++ - C/C++ 中固定长度实数输入数据的高效二维 FFT

python - 使用 TF 再训练模型

python-3.x - 使用 Python 请求库将 unicode 字符串发布到 Web 服务

Mako 模板中的 Python 函数(不在模块级 block 中)

python - setUpClass() 缺少 1 个必需的位置参数 : 'cls'

使用 XOR 的图像加密

python - 将二进制图像划分为 4x4 Python 并计算像素

python - 为什么 WSGI 必须使用 start_response 并返回一个迭代器?

python - OpenERP fields.function() 解释