首页 > 编程语言 > 详细

python opencv cv2 imshow threading 多线程

时间:2020-08-28 14:57:00      阅读:166      评论:0      收藏:0      [点我收藏+]

除了线程同步,还需要注意的是「窗口处理」要放在主线程

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import threading
from time import sleep
from queue import Queue
import cv2
import numpy as np
from functools import reduce

print(sys.version)

q = Queue()

def run(n):
    thread = threading.current_thread()
    thread.setName(‘thread-fuck‘)
    print(‘tid is: {0}‘.format(thread.ident))
    print(‘thread name is: {0}‘.format(thread.getName()))

    for i in range(100):
        img = np.random.randint(0,255,(200,300)).astype(np.uint8)
        q.put(img)
        sleep(0.1)

    q.put(0)


if __name__ == ‘__main__‘:
    thread = threading.Thread(target=run, args=(6,))
    thread.start()

    while True:
        item = q.get()

        if item is not None:
            if type(item) == type(0):
                break
            cv2.imshow(‘fuck‘, item)

        cv2.waitKey(50)

    thread.join()

    cv2.waitKey(0)
    cv2.destroyAllWindows()
    print(‘done!‘)

python opencv cv2 imshow threading 多线程

原文:https://www.cnblogs.com/hangj/p/13577143.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!