10-line Python Code for Object Detection with detectNet

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:

$ sudo nvpmodel -m 0
$ 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
$ export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
$ export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
Create a demo script
$ cd ${HOME}
$ touch detect.sh
$ gedit detect.sh
Insert the codes below to the script
#!/usr/bin/python

import jetson.inference
import jetson.utils

net = jetson.inference.detectNet("ssd-mobilenet-v2")
camera = jetson.utils.gstCamera(640, 480, "/dev/video0")
display = jetson.utils.glDisplay()

while display.IsOpen():
	img, width, height = camera.CaptureRGBA()
	detections = net.Detect(img, width, height)
	display.RenderOnce(img, width, height)
	display.SetTitle("Object Detection | Network {:.0f} FPS".format(1000.0 / net.GetNetworkTime()))
Test it out and enjoy !
$ sudo chmod +x detect.sh
$ ./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 !


More Posts

Leave a Reply