opencv python的detectmultiscale((函数在我在覆盖物pi中使用多线程方案时停止响应。
我希望制作两个线程;一个是用于显示实时图像,另一个是检测对象并找到其中心。即使显示函数的实时图像也顺利运行其检测函数((函数,但未显示任何响应。我进行了一些在线研究,发现OpenCV无法很好地处理并行处理。
import threading
import cv2
import numpy as np
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
global image, center
center=[]
def capture():
global image, center
cv2.namedWindow('image',cv2.WINDOW_NORMAL)
cv2.moveWindow('image',0,0)
cv2.resizeWindow('image', 800,608)
camera=PiCamera()
camera.resolution=(800,608)
camera.framerate=50
rawCapture=PiRGBArray(camera,size=(800,608))
for frame in camera.capture_continuous(rawCapture,format='bgr',use_video_port=True):
image=frame.array
cv2.imshow('image',image)
key=cv2.waitKey(1)& 0xFF
rawCapture.truncate(0)
if key==ord('q'):
break
cv2.destroyAllWindows()
def detect():
global image, center
time.sleep(2)
drop_cascade = cv2.CascadeClassifier('cascade.xml')
while True:
faces = drop_cascade.detectMultiScale(image, 1.25, 7)
for (x, y, w, h) in faces:
center.append([x,y,w,h])
if __name__ == '__main__':
thread1=threading.Thread(target=capture)
thread2=threading.Thread(target=detect)
thread1.start()
thread2.start()
问题是Threads
固有的,它是由全局解释器锁定引起的。线程共享程序内存。为了防止由单独的线程更改相同变量引起的冲突,Python将执行到特定线程。这意味着任何时候都只能运行一个线程。当CPU空闲时,程序在线程之间切换,使IO-BOND应用程序运行速度更快。
要同时执行CPU重型任务,您必须使用multiprocessing
。过程同时在单独的CPU内核上运行,并且不共享内存。
这是关于全球解释器锁的出色且更冗长的解释。
编辑:
这是有关多处理的信息。
将线程与异步多处理的示例代码:
import time
import threading
from multiprocessing import Pool
# cpu heavy functions
def doTask(val):
for i in range(10000000):
x = i*i
print(str.format('Task {} done!',val))
def doOtherTask(val):
for i in range(10000000):
x = i*i
print(str.format('Other Task {} done!',val))
if __name__ == '__main__':
print('Threads:')
# note starttime
starttime = time.time()
# create and run threads
thread1 = threading.Thread(doTask(1))
thread2 = threading.Thread(doTask(2))
thread3 = threading.Thread(doOtherTask(1))
thread4 = threading.Thread(doOtherTask(2))
thread1.start()
thread2.start()
thread3.start()
thread4.start()
print(str.format('Threads took {} seconds', time.time()-starttime))
print('Multiprocessing:')
#reset starttime
starttime = time.time()
# create and run processes
arguments = [1,2]
p = Pool()
p.map_async(doTask,arguments)
p.map_async(doOtherTask,arguments)
# close and join pool, so program execution waits
# for all tasks to finish
p.close()
p.join()
print(str.format('Multiprocessing took {} seconds', time.time()-starttime))