Running Detectnet with opencv using Jetson Nano

I was trying to add opencv to my pretrained model detectnet then this error. Help me pleaseee.

gstDecoder video options:

– URI: file:///home/pedicab/Live_feed.mp4
- protocol: file
- location: Live_feed.mp4
- extension: mp4
– deviceType: file
– ioType: input
– codec: H264
– codecType: omx
– width: 1280
– height: 720
– frameRate: 30
– numBuffers: 4
– zeroCopy: true
– flipMethod: none
– loop: 0

[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

This is the code I wrote using python:

import jetson_inference
import jetson_utils
import time
import cv2

def draw_label(image, text, position, font=cv2.FONT_HERSHEY_SIMPLEX, font_scale=1, thickness=2, color=(255, 255, 255)):
    cv2.putText(image, text, position, font, font_scale, color, thickness, cv2.LINE_AA)

def main():
    net = jetson_inference.detectNet(argv=['--model=jetson-inference/python/training/detection/ssd/models/Pedicab_Final100/ssd-mobilenet.onnx', 
                                            '--labels=jetson-inference/python/training/detection/ssd/models/Pedicab_Final100/labels.txt', 
                                            '--input-blob=input_0', 
                                            '--output-cvg=scores', 
                                            '--output-bbox=boxes',
                                            '--input-codec=decode'], 
                                     threshold=0.75)

    input_uri = 'Live_feed.mp4'
    input = jetson_utils.videoSource(input_uri)
    display = jetson_utils.videoOutput("display://0")

    frame_skip = 0 
    skip_interval = 2 
    start_time = time.time()

    pedicab_points = set()
    baobao_points = set()

    while display.IsStreaming():
        img = input.Capture()
        frame_skip += 1
        if frame_skip % skip_interval != 0:
            continue

        detections = net.Detect(img)
        
        pedicab_detections = set()
        baobao_detections = set()
        
        for detection in detections:
            if detection.Confidence >= 0.95:
                if detection.ClassID == 1:
                    pedicab_detections.add((detection.Left, detection.Top))
                    cv2.rectangle(img, (int(detection.Left), int(detection.Top)), (int(detection.Right), int(detection.Bottom)), (255, 0, 0), 2)
                    draw_label(img, "Pedicab", (int(detection.Left), int(detection.Top) - 10), color=(255, 0, 0))
                elif detection.ClassID == 2:
                    baobao_detections.add((detection.Left, detection.Top))
                    cv2.rectangle(img, (int(detection.Left), int(detection.Top)), (int(detection.Right), int(detection.Bottom)), (0, 255, 255), 2)
                    draw_label(img, "Bao-Bao", (int(detection.Left), int(detection.Top) - 10), color=(0, 255, 255))

        pedicab_points.update(pedicab_detections)
        baobao_points.update(baobao_detections)
        
        pedicab_count = len(pedicab_points)
        baobao_count = len(baobao_points)
        
        display.Render(img)
        display.SetStatus("Pedicabs detected: {}, Bao-Bao detected: {}".format(pedicab_count, baobao_count))

    end_time = time.time()
    fps = 1 / (end_time - start_time)
    print("Average FPS:", fps)

    input.Close()
    display.Close()

if __name__ == '__main__':
    main()

Hi @Ken9112, I think that error is unrelated to your cv2.rectangle() code, instead I think that importing cv2 changes the GL/GLX bindings. Can you check that glxinfo/glxgears (from mesa-utils) works properly and uses the NVIDIA driver?

Hi! sir dusty, I checked the glxinfo and glxgears it seems everything is working fine.

OK - in your detection script, can you try importing cv2 after you have created the videoOutput display?

Hi again sir dusty I tried importing the cv2 first but there is an error of the cv2 (overload resolution failed).

Hi @Ken9112, sorry for the delay, see this documentation and examples for converting jetson_utils.cudaImage to ndarray in cv2 colorspace format: