Hi dusty, it is an honor been helped by you.
this is my code
import jetson.inference
import jetson.utils
import time
import cv2
import numpy as np
import math
import os
import pyttsx3
a = 200
engine=pyttsx3.init()
engine.setProperty(‘rate’,150)
engine.setProperty(‘voice’,‘english+f1’)
text=‘initialising’
text1=’ Powering down’
engine.say(text)
engine.runAndWait()
timeStamp=time.time()
fpsFilt=0
net=jetson.inference.detectNet(‘ssd-mobilenet-v1’,[‘–model=/media/kc/1.0T/jetson-inference/python/training/detection/ssd/models/tg3/ssd-mobilenet.onnx’,‘–input-blob=input_0’,‘–labels=/media/kc/1.0T/jetson-inference/python/training/detection/ssd/models/tg3/labels.txt’,‘–output-cvg=scores’,‘–output-bbox=boxes’,‘–camera=/dev/video0’],threshold=.6)
dispW=1280
dispH=720
snapy=100
snapy2=.5
th1=5
th2=100
reset=0
font=cv2.FONT_HERSHEY_SIMPLEX
Servo Code
from adafruit_servokit import ServoKit
myKit=ServoKit(channels=16)
pan=90
tilt=100
myKit.servo[0].angle=pan
myKit.servo[2].angle=tilt
cam=cv2.VideoCapture(‘/dev/video0’)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, dispW)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, dispH)
while True:
_,img = cam.read()
img=cv2.resize(img,(854,480))
height=img.shape[0]
width=img.shape[1]
frame=cv2.cvtColor(img,cv2.COLOR_BGR2RGBA).astype(np.float32)
frame=jetson.utils.cudaFromNumpy(frame)
img=cv2.circle(img,(427,240),5,(0,0,200),2)
detections=net.Detect(frame, width, height)
for detect in detections:
#print(detect)
ID=detect.ClassID
top=int(detect.Top)
left=int(detect.Left)
bottom=int(detect.Bottom)
right=int(detect.Right)
item=net.GetClassDesc(ID)
cv2.rectangle(img,(left,top),(right,bottom),(0,225,0),1)
cv2.putText(img,item,(left,top+20),font,.5,(0,255,0),2)
if item=='Car':
reset=0
Xcenter=(right-left)/2+left
Ycenter=(bottom-top)/2+top
errorPan=Xcenter-854/2
errorTilt=Ycenter-480/2
realdistance = math.sqrt(pow(errorPan, 2)+pow(errorTilt, 2))
#print(str(errorPan))
#print(errorPan)
print(realdistance)
if errorPan>th1:
pan=pan-(errorPan/snapy)**snapy2
if errorPan<-th1:
pan=pan+(abs(errorPan)/snapy)**snapy2
if errorTilt>th1:
tilt=tilt-(errorTilt/snapy)**snapy2
if errorTilt<-th1:
tilt=tilt+(abs(errorTilt)/snapy)**snapy2
if pan>180:
pan=180
if pan<0:
pan=0
if tilt>120:
tilt=120
if tilt<30:
tilt=30
myKit.servo[0].angle=pan
myKit.servo[2].angle=tilt
#display.RenderOnce(img,width,height)
dt=time.time()-timeStamp
timeStamp=time.time()
fps=1/dt
fpsFilt=.9*fpsFilt + .1*fps
#print(str(round(fps,1))+' fps')
cv2.putText(img,str(round(fpsFilt,1))+' fps',(0,30),font,1,(0,0,255),2)
cv2.imshow('detCam',img)
reset=reset+1
print("New cicle")
#cv2.moveWindow('detCam',0,0)
if cv2.waitKey(1)==ord('q'):
myKit.servo[0].angle=90
myKit.servo[2].angle=100
engine.say(text1)
engine.runAndWait()
#time.sleep(1)
break
cam.release()
cv2.destroyAllWindows()
the result of print(realdistance) is
New cicle
118.15773355984787
277.15203408959496
New cicle
119.57006314291216
277.55224733372273
New cicle
119.67664768032233
276.1892467131912
New cicle
121.06403264388643
276.1892467131912
New cicle
As you see when exposed to 2 cars it returns 2 values but I only need the smallest one to know which care is closest to the center of the frame.So I used min(realdistance) and it gave me this
New cicle
New cicle
Traceback (most recent call last):
File “/media/kc/1.0T/jetson-inference/python/training/detection/ssd/robot.py”, line 75, in
m = min(realdistance)
TypeError: ‘float’ object is not iterable
Exiting…
Cleaning up pins
I very new to this and I put this together through tutorials and blogs so please help me out.