Bounding box ssd jetson nano

I am running MY-DETECTION.PY , I would need to extract the bounding box as an image and store it on disk, is it possible to do it? thank you

Hi @cbenglenok, you can use the jetson.utils.cudaCrop() function to extract the pixels in the bounding box from the image. Here is an example of using it that function:

https://github.com/dusty-nv/jetson-inference/blob/master/docs/aux-image.md#cropping

The example above crops the image around the center, but you can replace the ROI with the (detection.Left, detection.Top, detection.Right, detection.Bottom). You would also want to disable the overlay in the initial processing so that what you are extracting doesn’t have the bounding boxes drawn over them.

Here is an example of how it could look - I haven’t tried this, but is hopefully somewhat close for you:

import jetson.inference
import jetson.utils

net = jetson.inference.detectNet("ssd-mobilenet-v2", threshold=0.5)
camera = jetson.utils.videoSource("csi://0")      # '/dev/video0' for V4L2
display = jetson.utils.videoOutput("display://0") # 'my_video.mp4' for file

object_count = 0    # keep track of the number of detected objects for the filenames

while display.IsStreaming():
	img = camera.Capture()
	detections = net.Detect(img, overlay='none')    # disable drawing of overlay

    for detection in detections:
        # allocate the output image, with the cropped size
        imgCropped = jetson.utils.cudaAllocMapped(width=detection.Width,
                                                  height=detection.Height,
                                                  format=img.format)

        # get the cropping ROI from the bounding box
        crop_roi = (detection.Left, detection.Top, detection.Right, detection.Bottom)

        # crop the image
        jetson.utils.cudaCrop(img, imgCropped, crop_roi)

        # save the image
        jetson.utils.saveImage('object_{:d}.jpg'.format(object_count), imgCropped)
        object_count += 1
        del imgCropped

    display.Render(img)
    display.SetStatus("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))

Note that saving the images of the detected objects to disk may slow down the application.

1 Like

Thank you very much for the help