2017-04-03 393 views
0

我试图使用下面的代码来生成直播流。但有一个Bug说:通过opencv和python生成直播流

2017-04-03 22:48:46.836 Python [77177:10332216] !!! BUG:目前的 事件队列与主事件队列不一样。事件不会 正确处理。这可能是因为_TSGetMainThread是 第一次被主线程调用。

如果我想通过写入和读取图片进行流式处理,该怎么办?

import cv2 
import time 
import threading 

cap = cv2.VideoCapture(0) 

count = 0 

def produce(): 
    global count 
    while True: 
     ret,frame = cap.read() 
     cv2.imwrite('./img/'+str(count)+'.png',frame) 
     count+=1 

def consume(): 
    global count 
    while True: 
     if count>0: 
      img = cv2.imread('./img/'+str(count_read)+'.png') 
      cv2.imshow('a',img) 
     else: 
      time.sleep(1) 

if __name__=='__main__': 
    producer = threading.Thread(target=produce) 
    customer = threading.Thread(target=consume) 
    producer.start() 
    customer.start() 

回答

0

除了定义count_readcv2.imshow('a',img)后使用cv2.waitKey(30),你的代码对我的作品

您是否尝试过使用视频文件的代码?通常需要一段时间才能打开摄像头,也许这就是你错误的原因?