python实现多线程的一个例子

下面是一个使用python Thread 实现多线程的例子,图像的获取和特征点提取是独立的线程。

import cv2
from threading import Thread
import copy
import numpy as np 


class VideoGet:
    """
    Class that continuously gets frames from a VideoCapture object
    with a dedicated thread.
    """

    def __init__(self, src=0):
        self.stream = cv2.VideoCapture(src)
        (self.grabbed, self.frame) = self.stream.read()
        self.stopped = False

    def start(self):    
        Thread(target=self.get, args=()).start()
        return self

    def get(self):
        while not self.stopped:
            if not self.grabbed:
                self.stop()
            else:
                (self.grabbed, self.frame) = self.stream.read()

    def stop(self):
        self.stopped = True


class VideoShow:
    """
    Class that continuously shows a frame using a dedicated thread.
    """

    def __init__(self, frame=None):
        self.frame = frame
        self.stopped = False

    def start(self):
        Thread(target=self.show, args=()).start()
        return self

    def show(self):
        out = self.frame
        cv2.namedWindow("Fast Feature",cv2.WINDOW_NORMAL)
        while not self.stopped:
            gray = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
            fast = cv2.FastFeatureDetector_create()
            kps3 = fast.detect(gray, None)
            cv2.drawKeypoints(gray, kps3, out, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
            cv2.imshow("Fast Feature", out)
            if cv2.waitKey(1) == ord("q"):
                self.stopped = True

    def stop(self):
        self.stopped = True


def threadBoth(source=0):

    video_getter = VideoGet(source).start()
    video_shower = VideoShow(video_getter.frame).start()

    while True:
        if video_getter.stopped or video_shower.stopped:
            video_shower.stop()
            video_getter.stop()
            break

        frame = video_getter.frame
        video_shower.frame = frame

if __name__ == "__main__":
    threadBoth(0)

参考:https://github.com/nrsyed/computer-vision/tree/master/multithread

利用subprocess实现多线程的例子:

import subprocess
import numpy as np 
import time

proc_list = []
idx_list = []

process_num = 100
max_subprocess_num = 16

def launch_subprocess(i):
    proc = subprocess.Popen(["sleep", "0.1"], shell=False)
    proc.wait()
    return proc

def clearup_subprocess(proc_list, idx_list,idx):
    proc_list[idx].wait()
    proc_list.remove(proc_list[idx])
    print("subprocess: ",idx_list[idx])
    idx_list.remove(idx_list[idx])

for i in range(0, process_num):
    proc_new = launch_subprocess(i)
    proc_list.append(proc_new)
    idx_list.append(i)
    while len(proc_list)>= max_subprocess_num:
        for proc_temp in reversed(proc_list):
            if proc_temp.poll() is not None:
                idx = proc_list.index(proc_temp)
                clearup_subprocess(proc_list,idx_list, idx)
        time.sleep(0.1)

for proc_temp in proc_list:
    proc_temp.wait()

print("all subprocess have completed!!!")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值