10-line Python Code for Object Detection with DetectNet

Image not Found

For those of you who already tried out the Jetson-Inference provided by Nvidia. You might also want to try out my 10-line Python Code for real-time Object Detection with detectNet Engine. I hope you would enjoy it xD!

If you have not tested out the jetson-inference, you may find the repo here .

Please make sure you have compiled jetson-inference properly

Otherwise, the code below will not work. Before you run the demo, I recommend you to max out the performance on your Jetson Kit. To do so, you may type the following commands on your console:

1$ sudo nvpmodel -m 0
2$ sudo jetson_clocks

Demo (on Jetson AGX Xavier)

The Python interface is very simple to get up & running. Here’s an object detection example in 10 lines of Python code using SSD-Mobilenet-v2 (90-class MS-COCO) with TensorRT, which runs at 25FPS on Jetson Nano and at 190FPS on Jetson Xavier on a live camera stream with OpenGL visualization:

Setup environment

1$ export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
2$ export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Create a demo script

1$ cd ${HOME}
2$ touch detect.sh
3$ gedit detect.sh

Insert the codes below to the script

 3import jetson.inference
 4import jetson.utils
 6net = jetson.inference.detectNet("ssd-mobilenet-v2")
 7camera = jetson.utils.gstCamera(640, 480, "/dev/video0")
 8display = jetson.utils.glDisplay()
10while display.IsOpen():
11    img, width, height = camera.CaptureRGBA()
12    detections = net.Detect(img, width, height)
13    display.RenderOnce(img, width, height)
14    display.SetTitle("Object Detection | Network {:.0f} FPS".format(1000.0 / net.GetNetworkTime()))

Test it out and enjoy!

1$ sudo chmod +x detect.sh
2$ ./detec.sh

You can even re-train models onboard Nano using PyTorch and transfer learning ! Example datasets for training a Cat/Dog model and Plant classifier are provided, in addition to a camera-based tool for collecting and labeling your own datasets:

Have fun training!

You May Also Like