10 lines of code: Showing number of detections in output video and drawing a line

Hello,

my question is about your my-detections.py example (jetson-inference/detectnet-example-2.md at master · dusty-nv/jetson-inference · GitHub).

I wonder if it is possible to show the number of counted objects in the output video. Within the command line it is already possible but it would be nice to see this number in the output video.

In addition, is it also possible to draw a line into the output video?
For example to add a counting functionality?

Thanks and best regards,
chris

Hi @chrisTopp, if you check out the imagenet.py example, that shows using the cudaFont object to overlay text on top of the image. So you could add that to the other script to show the number of detections.

Ok thanks.
I will try to put it into my script.

I also tried the following changes to display the confidence value instead of the class name above the bounding boxes but it did not work (I wanted to show only the confidence to test if it works at all):

Edit:
CONF=detect.Confidence

Comment line one and use line two

#item=net.GetClassDesc(ID)	# RUNS
item_CONF=net.GetClassDesc(CONF) #does NOT RUN

(Edit: CONF=detect.Confidence)

AND

Comment line one and use line two

    #cv2.putText(img,item, (left,top+20),font,.8,(0,0,255),1) #RUNS
    cv2.putText(img,item_CONF, (left,top+20),font,.8,(0,0,255),1) #does NOT RUN

You can see the entire code and the error below:

ENTIRE CODE

import jetson.inference
import jetson.utils
import time
import cv2
import numpy as np 

timeStamp=time.time()
fpsFilt=0
net=jetson.inference.detectNet('ssd-mobilenet-v2',threshold=.4)

dispW=640
dispH=480
flip=2
font=cv2.FONT_HERSHEY_SIMPLEX

cam=cv2.VideoCapture('/usr/share/visionworks/sources/data/pedestrians.mp4')
cam.set(cv2.CAP_PROP_FRAME_WIDTH, dispW)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, dispH)

while True:
    _,img = cam.read()
    height=img.shape[0]
    width=img.shape[1]

    frame=cv2.cvtColor(img,cv2.COLOR_BGR2RGBA).astype(np.float32)
    frame=jetson.utils.cudaFromNumpy(frame)
    
    detections=net.Detect(frame, width, height)
    for detect in detections:
        ID=detect.ClassID
        CONF=detect.Confidence

        top=int(detect.Top)	
        left=int(detect.Left)
        bottom=int(detect.Bottom)
        right=int(detect.Right)	

        CONF_2="%.2f" % CONF

        #item=net.GetClassDesc(ID)	# RUNS
        item_CONF=net.GetClassDesc(CONF) #does NOT RUN

	# create Bounding Box
        cv2.rectangle(img,(left,top),(right,bottom),(0,255,0),1)
        #cv2.putText(img,item, (left,top+20),font,.8,(0,0,255),1) #RUNS
        cv2.putText(img,item_CONF, (left,top+20),font,.8,(0,0,255),1) #does NOT RUN

        print(item,top,left,bottom,right, CONF_2)

    dt=time.time()-timeStamp
    timeStamp=time.time()
    fps=1/dt
    fpsFilt=.9*fpsFilt + .1*fps

    cv2.putText(img,str(round(fpsFilt,1))+' fps',(0,30),font,.8,(0,255,0),2)
    cv2.imshow('detCam',img)
    cv2.moveWindow('detCam',0,0)
    if cv2.waitKey(1)==ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

ERROR

  File "failure.py", line 41, in <module>
    item_CONF=net.GetClassDesc(CONF) #does NOT RUN
Exception: jetson.inference -- detectNet.GetClassDesc() failed to parse arguments

Any idea what is going wrong?

Best regards
chris

Edit:
I had a look into Python: package jetson.inference

And GetClassDesc() is not intended to display the confidence. Is that right?

Hi,

net.GetClassDesc(ID) is to retrive the corresponding class name from the label file based on a given ID.
So you will meet error if the input doesn’t be a valid class ID.

For confidence value, you can use CONF directly.

ID=detect.ClassID
CONF=detect.Confidence

print(CONF)

Thanks.

What you can do is use the optional overlay argument to net.Detect() like so:

net.Detect(img, overlay='conf')     # draw the confidence values only

If you refer to the detectnet.py sample, you can see how these are set. The overlay option string can be a combination of 'box,labels,conf' (which is the default). For example:

net.Detect(img, overlay='box,labels')       # draw bounding boxes and class labels only
net.Detect(img, overlay='box,conf')         # draw bounding boxes and confidence values
net.Detect(img, overlay='box,labels,conf')  # draw boxes, labels, and confidences (the default)

Hi,

thanks a lot for your reply!