DetectNet Methods

Hello, I am trying to set a variable in the my-detection.py script to a specific number when the detect net recognizes a certain object. For example, if it sees a person, it will set the variable x = 1, but if it sees a dog, it sets x = 2 etc… I want to use the GetClassDesc() method so that when it outputs the string description I can relate it to a number. I am a little confused on the parameter that this method takes in. What is the “index of the class” parameter exactly in this case. The ‘object’ hyperlink right above it does not work so I cannot get any further insight. A more in depth description of this method would be very helpful, as it seems simple enough but I am fairly new to python. Thanks!

Hi @jpurpura37, GetClassDesc() takes the ClassID from the detectNet.Detection object:

detections = net.Detect(img)

for detection in detections:
    class_name = net.GetClassDesc(detection.ClassID)
    print(f"Detected '{class_name}'")

So class_name here is the string description of the object? And detection.ClassID is something predefined in the detectNet object? If I retrained the SSD model and exported it through ONNX, then put it into this script instead of ssd-mobilenet-v2, how would the detectNet object change?

The class names are the names from labels.txt

The class ID’s are the number of the class in labels.txt (starting with 0)

So for example, with this labels.txt

BACKGROUND
car
truck
bus
bicycle
person

So BACKGROUND has class ID=0, car has class ID 1, truck has class ID 2, ect

detectNet loads your labels.txt which is where it gets the info from. Your model was trained so that it’s DNN outputs match the class ID’s. If you manually reorder the class labels in labels.txt, it will seem like you are getting incorrect results.

Thanks, you’re the man Dusty!

One more question, is there a method in detectNet that outputs the x and y coordinates of the image so that I can print them to the terminal?

Yep, the detectNet.Detection objects in the list returned from detectNet.Detect have the following members:

Detection = <type 'jetson.inference.detectNet.Detection'>
Object Detection Result
 
----------------------------------------------------------------------
Data descriptors defined here:
 
Area
    Area of bounding box
 
Bottom
    Bottom bounding box coordinate
 
Center
    Center (x,y) coordinate of bounding box
 
ClassID
    Class index of the detected object
 
Confidence
    Confidence value of the detected object
 
Height
    Height of bounding box
 
Instance
    Instance index of the detected object
 
Left
    Left bounding box coordinate
 
Right
    Right bounding box coordinate
 
Top
    Top bounding box coordinate
 
Width
     Width of bounding box

Okay so how would I pick the “center” piece of data out from that list?

Would it be like :

detections = net.Detect(img)
print(f"Center: " detections[2])

Since the center is the third piece of data in the detections data array?

It would be like this:

detections = net.Detect(img)

for detection in detections:
    class_name = net.GetClassDesc(detection.ClassID)
    center = detection.Center
    print(f"Detected '{class_name}' at x={center[0]} y={center[1]}")

I am getting an indentation error when I add center = detection.Center under the class_name line?

Make sure that your script is using all spaces for indentation or all tabs - Python doesn’t like it when you mix and match

Okay, I fixed the tabbing errors and it ran with class_names, but for some reason it will recognize class_names in the if statements but it wont recognize center using the exact same format?

Never mind I got it to work!