python多处理在不终止整个程序的情况下第二次运行速度较慢



我有一个带有opencvgui的python程序,它在类实例中使用线程中的多处理。

运行以下脚本后,当我键入q时,它将退出子循环,并终止或加入模块中所有现有的线程、进程和关闭队列。

然后,如果我键入r,它将再次启动子循环,但这一次,多处理中的工作进程运行速度慢了一倍。我不知道为什么会发生这种事。

代码如下。

module_started = False
height = 480
width = 800
img_base = np.zeros((height,width,3), np.uint8)
cv2.namedWindow("VIN", cv2.WINDOW_NORMAL)

while True:

if not module_started:
cam1 = PersonDetector(0)
cam1.start()
outputs = []
module_started = True

while True:

if cam1.new_detection:
outputs = cam1.get_output()

if outputs:

for xys in outputs:
(startX, startY, endX, endY) = xys
cv2.rectangle(cam1.frame, (startX, startY), (endX, endY), (255,5,150),2)


# show the output image
cv2.imshow("VIN", cam1.frame)
key = cv2.waitKey(40) & 0xFF
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break

cam1.stop()
del cam1

# show the output frame

cv2.imshow("VIN", img_base)
key = cv2.waitKey(40) & 0xFF

if key == ord("e"):          
break
if key == ord("r"):             # go back to start sub loop
module_started = False

cv2.destroyAllWindows()

它是在树莓派4。它有cpu 4核。htop首次显示CPU%超过300%但第二次,htop显示CPU%只有99%或100%。。。

为什么会发生这种情况?

我自己测试了一整天。这既不是因为多处理,也不是因为线程。

这是因为cv2.imshow((,我删除了imshow,然后一切都保持相同的速度,但当像上面的代码一样重复imshow时,它会显示程序关闭,我仍然不知道为什么。。。

相关内容

最新更新