如何解决如何并行检索树莓派图像信息和arduino传感器信息
我目前正在做一个项目来检测最近的预定义对象并转到那个位置。在前往最近的物体之前,它使用安装在伺服电机上的超声波传感器搜索该区域,并将信息(距离和角度)发送到树莓派。我写了一个 python 代码来检测对象并创建边界框。两个代码单独工作得很好。当我要合并这些代码时,它不起作用。 这是从arduino获取数据的代码。
import serial
import time
import random
distance=[]
angle=[]
occupancy=[]
def rotatecamera():
while(len(angle)<180):
ser.write(b"ROCA000000\n")
line = ser.readline().decode('utf-8').rstrip()
print(line)
if(line!=''):
try:
splitlist=line.split("+")
occupancy_value=random.randint(0,1)
distance.append(int(splitlist[0]))
angle.append(int(splitlist[1]))
except:
pass
if __name__ == '__main__':
ser = serial.Serial('COM3',9600,timeout=1)
ser.flush()
rotatecamera()
print(len(angle))
print(angle)
print(distance)
#time.sleep(1)
这是使用视频进行物体检测的代码(我计划使用树莓派摄像头。出于测试目的,我使用了视频)
import cv2
#img= cv2.imread('pottest.jpg')
cap=cv2.VideoCapture('testvideo.mp4')
classNames=[]
classFile='coco.names'
with open(classFile,'rt') as f:
classNames=f.read().rstrip('\n').split('\n')
configPath='ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt'
weightsPath='frozen_inference_graph.pb'
net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5,127.5,127.5))
net.setInputSwapRB(True)
while True:
success,img = cap.read()
classIds,confs,bBox=net.detect(img,confThreshold=0.58)
#print(classIds,bBox)
if len(classIds) != 0:
for classId,confidence,Box in zip(classIds.flatten(),confs.flatten(),bBox):
#print(classId)
#print(Box)
if (classId==64 or classId==86):
cv2.rectangle(img,Box,color=(0,255,0))
cv2.putText(img,classNames[classId - 1].upper(),(Box[0] + 10,Box[1] + 30),cv2.FONT_HERShey_COMPLEX,0.5,(0,0),2)
cv2.putText(img,str(round(confidence * 100,2)),(Box[0] + 50,Box[1] + 55),1)
cv2.line(img,(220,320),(255,(260,1)
if((Box[0]>220 and Box[0]<260)):#to identify in particular threshold whether there is a plant or not
value=1
print(value)
else:
value=0
print(value)
cv2.imshow("Output",img)
cv2.waitKey(1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。