python - 如何在 rasberry pi 中修复 python 中的“多线程问题”
问题描述
我无法找出我在树莓派 3 中用 python 编写的用于对象识别的多线程程序可能有什么问题。
该代码在树莓派 3 模型上运行。我希望做两个线程;一种用于显示实时图像,另一种用于检测对象并找到其中心。我尝试了我在下面编写的代码。当我在笔记本电脑上使用它时它工作正常,但它不起作用(第二个函数;detect() 在树莓派上的 line faces = drop_cascade.detectMultiScale(image, 1.25, 7)) 之后没有响应,但如果我删除该行并遵循 for 循环并替换为简单的代码,例如 print("hello") 它工作正常。
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()
我想同时运行这些线程,以便捕获功能显示的图像顺利运行,并由检测功能完成检测。
解决方案
推荐阅读
- python - 我在下载 python 包 fast_slic 时遇到问题
- html - 为什么代码中的类会在代码中生成 404 硬编码 URL?
- javascript - 尽管在传单 JS 中使用 when().then() 函数,但在文档之前加载 ajax
- android - 重用谷歌地图片段时获取“ E/libprocessgroup: set_timerslack_ns write failed: Operation not allowed”
- python - 根据列值将特定行放在数据框的末尾
- angular - 角度调整 fromEvent 到 HostListener 之间有区别吗?
- java - 无法解析值“$ {datasource.driver}”中的占位符“datasource.driver”
- reactjs - 在没有 AWS-SDK 的情况下向 AWS 服务操作(Cloudwatch 日志)createLogStream 和 putLogEvents 发出 http 请求所需的标头和 api 版本?
- typescript - Electron-Typescript:如何在命令行中运行部分代码?
- java - 如何在 WildFly 中配置协议处理程序