栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python设置额外线程处理图像

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python设置额外线程处理图像

用处:可自由关闭和打开摄像头,同时可进行处理其他事物
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 26 16:30:08 2022

@author: victor
"""

import threading
import cv2, time

# 视频流线程
class VideoStreamThread(threading.Thread):
    def __init__(self, src=0,sleep_time=0.01):
        self._stop_event = threading.Event()
        self._sleep_time = sleep_time
        self.capture = cv2.VideoCapture(src)
        """call base class constructor"""
        super().__init__()

    def update(self):
        # Read the next frame from the stream in a different thread
        if self.capture.isOpened():
            (self.status, self.frame) = self.capture.read()
        self._stop_event.wait(self._sleep_time)
    
    def run(self):
        while not self._stop_event.isSet():
            #do work
            self.update()
            self.show_frame()
        
    def show_frame(self):
        # Display frames in main program
        cv2.imshow('frame', self.frame)
        cv2.waitKey(1)

    
    def join(self, timeout=None):
        """set stop event and join within a given time period"""
        self._stop_event.set()
        self.capture.release()
        cv2.destroyAllWindows()
        super().join(timeout)
        

if __name__ == '__main__':
    video_stream_thread = VideoStreamThread()
    video_stream_thread.start()
    time.sleep(5)
    video_stream_thread.join()
    if(video_stream_thread.is_alive()==False):
        video_stream_thread = VideoStreamThread()
        video_stream_thread.start()
    time.sleep(5)
    video_stream_thread.join()
        
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/834964.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号