Real-time object detection using Jetson Nano to detect apples with LED feedback

import time
import Jetson.GPIO as GPIO
import jetson.inference
import jetson.utils

LED_PIN = 10
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_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()

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:  
                    GPIO.output(LED_PIN, GPIO.HIGH)  
                    print("Apple detected")
                    Apple_detected = True
                    break

            if not Apple_detected:
                GPIO.output(LED_PIN, GPIO.LOW)

            jetson.utils.cudaDeviceSynchronize()

        except:
            print("Error capturing image from camera")

if __name__ == "__main__":
    main()
2 Likes
import time
import Jetson.GPIO as GPIO
import jetson.inference
import jetson.utils

LED_PIN = 10
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_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()

apple_counter = 0  # Counter for detected apples

# Function to calculate the estimated yield based on the apple count
def calculate_estimated_yield():
    apple_count = apple_counter
    estimated_yield = apple_count * 0.15  # Assuming an average weight of 150 grams per apple
    return estimated_yield

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:
                    GPIO.output(LED_PIN, GPIO.HIGH)
                    print("Apple detected")
                    apple_detected = True
                    break

            if apple_detected:
                apple_counter += 1  # Increment the apple counter
                print("Total Apples:", apple_counter)

            if not apple_detected:
                GPIO.output(LED_PIN, GPIO.LOW)

            estimated_yield = calculate_estimated_yield()
            print("Estimated Yield:", estimated_yield, "kg")

            jetson.utils.cudaDeviceSynchronize()

        except:
            print("Error capturing image from camera")

if __name__ == "__main__":
    main()

Added a function to calculate the estimated yield based on the apple count