• 感谢大家访问本站,希望本站的内容可以帮助到大家!

  • 本站由于前段时间遭受到大量临时和国外邮箱注册,所以对可注册的邮箱类型进行了限制!

  • 如果觉得本站的内容有帮助,可以考虑打赏博主哦!

  • 工资「喂饱肚子」,副业「养活灵魂」!

  • 问题反馈可发送邮件到stubbornhuang@qq.com

  • 本站会放置Google广告用于维持域名以及网站服务器费用。

  • 计算机图形学与计算几何经典必备书单整理,下载链接可参考:https://www.stubbornhuang.com/1256/

  • 欢迎大家交换友链,可在https://www.stubbornhuang.com/申请友情链接进行友链交换申请!

  • 在本站开通年度VIP,无限制下载本站资源和阅读本站文章

Python – 在子线程中使用OpenCV异步读取摄像头视频帧传递到主线程中进行处理

Python 发布于2022-08-19 阅读 8,445次 0次评论 1次点赞 本文共2327个字,阅读需要6分钟。

一般情况下,我们会在主线程中直接使用opencv打开摄像头然后读取的视频帧进行操作,这种方式是阻塞的,如果我们想要在主线程读取摄像头视频帧的时候还进行其他的操作会非常不方便。这个时候有必要将读取摄像头视频帧的这一部分代码移动到子线程中,然后通过队列使得主线程可以获取摄像头的视频帧,同时也不会阻塞主线程。

1 子线程使用OpenCV读取摄像头视频帧

import cv2
import threading
from queue import Queue

class ReadCameraThread(threading.Thread):
    def __init__(self, video_queue):
        super(ReadCameraThread).__init__()
        threading.Thread.__init__(self)

        self.read_capture = cv2.VideoCapture(0)
        self.video_queue = video_queue
        self.running = True


    def run(self):
        if not self.read_capture.isOpened():
            print('未检测到摄像头')
            exit(0)

        while self.read_capture.isOpened() and self.running:
            ret, frame = self.read_capture.read()

            if not ret:
                break

            self.video_queue.put(frame)
            print('put the frame')

        self.read_capture.release()
        cv2.destroyAllWindows()

    def stop_read_camera(self):
        self.running = False

2 主线程中从缓存队列读取视频帧

class StreamingOnlineRecognize:
    def __init__(self):
        self.video_queue = Queue()

        self.video_stream_in = ReadCameraThread(self.video_queue)
        self.video_stream_in.start()

    def start(self):
        self._running = True

        while self._running:
            if not self.video_queue.empty():
                frame = self.video_queue.get()
                print('get the frame')

                if frame is not None:
                    cv2.imshow('test', frame)
                    cv2.waitKey(1)
            else:
                print('the size of self.video_queue is <= 0')


        cv2.destroyAllWindows()
        self.video_stream_in.join()

3 完整示例

# -*- coding: utf-8 -*-

import cv2
import threading
from queue import Queue

class ReadCameraThread(threading.Thread):
    def __init__(self, video_queue):
        super(ReadCameraThread).__init__()
        threading.Thread.__init__(self)

        self.read_capture = cv2.VideoCapture(0)
        self.video_queue = video_queue
        self.running = True


    def run(self):
        if not self.read_capture.isOpened():
            print('未检测到摄像头')
            exit(0)

        while self.read_capture.isOpened() and self.running:
            ret, frame = self.read_capture.read()

            if not ret:
                break

            self.video_queue.put(frame)
            print('put the frame')

        self.read_capture.release()
        cv2.destroyAllWindows()

    def stop_read_camera(self):
        self.running = False

class StreamingOnlineRecognize:
    def __init__(self):
        self.video_queue = Queue()

        self.video_stream_in = ReadCameraThread(self.video_queue)
        self.video_stream_in.start()

    def start(self):
        self._running = True

        while self._running:
            if not self.video_queue.empty():
                frame = self.video_queue.get()
                print('get the frame')

                if frame is not None:
                    cv2.imshow('test', frame)
                    cv2.waitKey(1)
            else:
                print('the size of self.video_queue is <= 0')


        cv2.destroyAllWindows()
        self.video_stream_in.join()

if __name__ == '__main__':
    streaming_online_recognize = StreamingOnlineRecognize()
    streaming_online_recognize.start()

欢迎扫码关注我的微信公众号,及时获取文章更新

微信公众号二维码

本文作者:StubbornHuang

版权声明:本文为站长原创文章,如果转载请注明原文链接!

原文标题:Python – 在子线程中使用OpenCV异步读取摄像头视频帧传递到主线程中进行处理

原文链接:https://www.stubbornhuang.com/2318/

发布于:2022年08月19日 8:36:20

修改于:2023年06月21日 18:16:18

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

文章末尾
上一篇
资源分享 - Geometric and Discrete Path Planning for Interactive Virtual Worlds 英文PDF下载
计算几何与计算机图形学资源
下一篇
深度学习 - 图像标准化与归一化方法
深度学习
当前分类随机文章推荐

发表评论

您必须 [ 登录 ] 才能发表留言!

关注我们的公众号

微信公众号