import time
import Jetson.GPIO as GPIO
import jetson.inference
import jetson.utils
SERVO_PIN = 10
GPIO.setmode(GPIO.BOARD)
GPIO.setup(SERVO_PIN, GPIO.OUT)
net = jetson.inference.detectNet(model=‘models/fruit/ssd-mobilenet.onnx’,
labels=‘models/fruit/labels.txt’,
input_blob=‘input_0’,
output_cvg=‘scores’,
output_bbox=‘boxes’,
threshold=0.5)
camera = jetson.utils.videoSource(“/dev/video0”)
display = jetson.utils.videoOutput()
servo = GPIO.PWM(SERVO_PIN, 50) # frequency of 50 Hz
def set_servo_angle(angle):
duty_cycle = 2 + (angle / 18) # Map angle to duty cycle
servo.ChangeDutyCycle(duty_cycle)
def main():
while True:
try:
img = camera.Capture()
detections = net.Detect(img)
display.Render(img)
display.SetStatus(“Object Detection | Network {:.0f} FPS”.format(net.GetNetworkFPS()))
APPLE_detected = False
for detection in detections:
if detection.ClassID == 1:
set_servo_angle(90) # Rotate the servo to 90 degrees
print("Apple detected")
Apple_detected = True
break
if not Apple_detected:
set_servo_angle(0) # Rotate the servo to 0 degrees
jetson.utils.cudaDeviceSynchronize()
except:
print("Error capturing image from camera")
if name == “main”:
main()