Home > other >  How can I display data on opencv window?
How can I display data on opencv window?

Time:09-28

I'm working on a project with april tags and a computer vision system to detect them from a webcam. I have a good system as of now that prints the data to the terminal but I would like to display this numerical/text data on top of the video window or in another window. I've already tried using cv2.putText() but that only puts static text on the page and it can't be updated in real time like I want. This is my code that tries to update a window in real time with the number of tags detected in the webcam video. But it ends up just writing a 1 for example and I can't figure out a way to erase that text and update it.

Is this even possible in opencv? Or is there another way?

Thanks in advance.

while True:


        success, frame = cap.read()
        if not success:
            break

        gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        detections, dimg = detector.detect(gray, return_image=True)
        print(detections)

        num_detections = len(detections)
        # print('Detected {} tags.\n'.format(num_detections))
        num_detections_string = str(num_detections)


        overlay = frame // 2   dimg[:, :, None] // 2

        clear_text = ''
        text = checkNumDetections(num_detections, num_detections_string)
        
        cv2.putText(whiteBackground, clear_text, (100, 100), cv2.FONT_HERSHEY_PLAIN, 10, (0, 255, 0), 2)
        cv2.putText(whiteBackground, text, (100, 100), cv2.FONT_HERSHEY_PLAIN, 10, (0, 255, 0), 2)
        cv2.imshow(window, overlay)
        k = cv2.waitKey(1)
        cv2.imshow(dataWindow, whiteBackground)

        if k == 27:
            break```

CodePudding user response:

You need to re-initialise the 'whiteBackground' image in each loop, before you draw anything on it.

I know this will work, but it will give you a black background:

whiteBackground = np.zeros((columns, rows, channels), dtype = "uint8")

This should work to give you a white background, but experiment and see:

whiteBackground = np.full((columns, rows, channels), 255, dtype = "uint8")

I usually work with opencv in c , so I'm not 100% sure of the exact syntax, but that should work.

  • Related