Displaying cv2.line on the stream

I’m trying to visualize triggering line using cv2.line but there is no line displayed on the stream. Here is the code:

def app_callback(pad, info, user_data):
    # Get the GstBuffer from the probe info
    buffer = info.get_buffer()
    # Check if the buffer is valid
    if buffer is None:
        return Gst.PadProbeReturn.OK

    # Get the format, width, and height from the pad
    format, width, height = get_caps_from_pad(pad)

    if format is None or width is None or height is None:
        print("Failed to get format, width, or height from pad")
        return Gst.PadProbeReturn.OK

    # Get the detections from the buffer
    roi = hailo.get_roi_from_buffer(buffer)
    hailo_detections = roi.get_objects_typed(hailo.HAILO_DETECTION)
    n = len(hailo_detections)

    boxes = np.zeros((n, 4))
    confidence = np.zeros(n)
    class_id = np.zeros(n)
    tracker_id = np.empty(n)

    for i, detection in enumerate(hailo_detections):
        frame = get_numpy_from_buffer(buffer, format, width, height)  # Pass format, width, height here
        start_point = (50, 50)
        end_point = (200, 50)
        color = (0, 255, 0)
        thickness = 2
        cv2.line(frame, start_point, end_point, color, thickness)
        cv2.imshow("User Frame", frame)
        cv2.waitKey(1)
        class_id[i] = detection.get_class_id()
        confidence[i] = detection.get_confidence()
        tracker_id[i] = detection.get_objects_typed(hailo.HAILO_UNIQUE_ID)[0].get_id()
        bbox = detection.get_bbox()
        boxes[i] = [bbox.xmin() * width, bbox.ymin() * height, bbox.xmax() * width, bbox.ymax() * height]

    detections = sv.Detections(
            xyxy=boxes, 
            confidence=confidence, 
            class_id=class_id,
            tracker_id=tracker_id)

    line_zone.trigger(detections)

    textoverlay_top = app.pipeline.get_by_name("hailo_text_top")
    textoverlay_bottom = app.pipeline.get_by_name("hailo_text_bottom")

    # Set the top and bottom text overlays
    textoverlay_top.set_property('text', f'\u2190 : {line_zone.in_count} \u2190')  # Arrows pointing left and right
    textoverlay_bottom.set_property('text', f'\u2192 : {line_zone.out_count} \u2192')  # Arrows pointing left and right 

    textoverlay_top.set_property('font-desc', 'Monospace 25')
    textoverlay_bottom.set_property('font-desc', 'Monospace 25')

    return Gst.PadProbeReturn.OK

Hey @ruslan,

Welcome to the Hailo Community!

It looks like you haven’t initialized the frame in the callback function. Also, I believe you need just one frame and then iterate over the detections, similar to how it’s done here: Basic Pipelines - Detection.

Although the code below hasn’t been tested, it should look something like this:


def app_callback(pad, info, user_data):
    buffer = info.get_buffer()
    if buffer is None:
        return Gst.PadProbeReturn.OK

    format, width, height = get_caps_from_pad(pad)
    if format is None or width is None or height is None:
        print("Failed to get format, width, or height from pad")
        return Gst.PadProbeReturn.OK

    # Get the frame only once
    frame = get_numpy_from_buffer(buffer, format, width, height)
    
    # Draw the line
    start_point = (50, 50)
    end_point = (200, 50)
    color = (0, 255, 0)
    thickness = 2
    cv2.line(frame, start_point, end_point, color, thickness)

    # Display the frame
    cv2.imshow("User Frame", frame)
    cv2.waitKey(1)

    roi = hailo.get_roi_from_buffer(buffer)
    hailo_detections = roi.get_objects_typed(hailo.HAILO_DETECTION)
    n = len(hailo_detections)
    boxes = np.zeros((n, 4))
    confidence = np.zeros(n)
    class_id = np.zeros(n)
    tracker_id = np.empty(n)

    for i, detection in enumerate(hailo_detections):
        class_id[i] = detection.get_class_id()
        confidence[i] = detection.get_confidence()
        tracker_id[i] = detection.get_objects_typed(hailo.HAILO_UNIQUE_ID)[0].get_id()
        bbox = detection.get_bbox()
        boxes[i] = [bbox.xmin() * width, bbox.ymin() * height, bbox.xmax() * width, bbox.ymax() * height]

    detections = sv.Detections(
            xyxy=boxes, 
            confidence=confidence, 
            class_id=class_id,
            tracker_id=tracker_id)
    line_zone.trigger(detections)

    textoverlay_top = app.pipeline.get_by_name("hailo_text_top")
    textoverlay_bottom = app.pipeline.get_by_name("hailo_text_bottom")
    textoverlay_top.set_property('text', f'\u2190 : {line_zone.in_count} \u2190')
    textoverlay_bottom.set_property('text', f'\u2192 : {line_zone.out_count} \u2192')
    textoverlay_top.set_property('font-desc', 'Monospace 25')
    textoverlay_bottom.set_property('font-desc', 'Monospace 25')

    return Gst.PadProbeReturn.OK

Thank you for reply, theres a highlight on the original detection.py for cv2.imshow

Note: using imshow will not work here, as the callback function is not running in the main thread.

when im running your suggested code i got this error

cv2.imshow(“User Frame”, frame)
cv2.error: OpenCV(4.10.0) /io/opencv/modules/highgui/src/window.cpp:1301: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvShowImage’


1 Like

Hello russe,
Have you found a solution tu use cv2.imshow ?