CV2 in "Coding Your Own Object Detection Program"

Hi @dusty_nv !
Is it possible to use cv2 to creation my own Python script for realtime object detection on a live camera?

I tryied this:
import jetson_inference
import jetson_utils
import cv2

net = jetson_inference.detectNet(argv=[‘–model=/home/jv/jetson-inference/python/training/detection/ssd/models/04obj/ssd-mobilenet.onnx’, ‘–labels=/home/jv/jetson-inference/python/training/detection/ssd/models/04obj/labels.txt’, ‘–input-blob=input_0’, ‘–output-cvg=scores’, ‘–output-bbox=boxes’], threshold=0.5)

camera = cv2.VideoCapture(0)
display = jetson_utils.glDisplay()

while display.IsOpen():
_, img = camera.read()

detections = net.Detect(img)

for detection in detections:
    class_id = detection.ClassID
    class_name = net.GetClassDesc(class_id)
    confidence = detection.Confidence
    left = int(detection.Left)
    top = int(detection.Top)
    right = int(detection.Right)
    bottom = int(detection.Bottom)

    if confidence > 0.5:
        cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
        cv2.putText(img, class_name, (left + 5, top + 20), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)

display.RenderOnce(img, img.shape[1], img.shape[0])
display.SetStatus("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))

But I received this:
[TRT] didn’t load expected number of class colors (0 of 6)
[TRT] filling in remaining 6 class colors with default colors
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
[OpenGL] glDisplay – X screen 0 resolution: 1920x1080
[OpenGL] glDisplay – X window resolution: 1920x1080
libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast
X Error of failed request: GLXBadContext
Major opcode of failed request: 154 (GLX)
Minor opcode of failed request: 6 (X_GLXIsDirect)
Serial number of failed request: 43
Current serial number in output stream: 42

@jardel.vieira is detectnet.py or video-viewer.py able to successfully show you the window, or does it produce the same error?

If detectnet.py works, then what I would recommend is creating the jetson_utils videoOutput or glDisplay object before importing cv2 module. OpenCV seems to have it’s own OpenGL extension manager which messes with the OpenGL code. Or you could just use OpenCV for the display too.

BTW in your example, you can simply change the detection threshold (with --threshold=0.5 or detectNet.SetConfidenceThreshold(0.5)) and it will ignore those detections already.

You can also use these CUDA functions from Python for drawing basic shapes: https://github.com/dusty-nv/jetson-inference/blob/master/docs/aux-image.md#drawing-shapes

Also, imagenet.py shows an example of using CUDA to render text: https://github.com/dusty-nv/jetson-inference/blob/cbd9c0610143beb289ae77b42c0b40d69bc4c801/python/examples/imagenet.py#L81

1 Like

I am currently facing an issue while trying to use the detectnet.py script. Unfortunately, I am unable to display any window, and instead, I receive an error message.

Do you have any exemple using CV2 in this case?

@jardel.vieira are you getting the same error message as above? If it’s a different error, can you post it here?

If you see libGL error: failed to load driver: swrast or the glxinfo utility doesn’t show the NVIDIA driver being used, then I think your NVIDIA OpenGL drivers got overwritten by another package at some point, and need re-installed in order for OpenGL to work again (in which case, it may just be easier to re-flash your SD card)

The cv2 examples that I have can be found under jetson-inference/utils/python/examples

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.