我有要进行屏幕记录的代码,每帧我都有一组要在每帧上显示的边界框。我可以使用matplotlib或其他方式执行此操作,但mss的工作速度约为30fps,因此我需要能够快速显示边界框。

我在this example文档中注意到了,但是我尝试运行它,但无法显示任何内容。而且我甚至不确定这是否适用于我的示例。

import cv2
import time
import numpy as np
from mss import mss

with mss() as sct:
        # Part of the screen to capture
        monitor = {"top": 79, "left": 265, "width": 905, "height": 586}

        while "Screen capturing":
            last_time = time.time()

            # Get raw pixels from the screen, save it to a Numpy array
            screen = np.array(sct.grab(monitor))

            # print("fps: {}".format(1 / (time.time() - last_time)))

            print('loop took {} seconds'.format(time.time()-last_time))
            last_time = time.time()
            screen = cv2.cvtColor(screen, cv2.COLOR_BGR2RGB)
            screen = cv2.resize(screen, (224,224)).astype(np.float32)/255

            # Display the picture
            cv2.imshow("OpenCV/Numpy normal", screen)

            # Press "q" to quit
            if cv2.waitKey(25) & 0xFF == ord("q"):
                cv2.destroyAllWindows()
                break

现在说我有一组边框要显示在每个帧上,例如,
bboxes = [np.array([12, 16, 29, 25]), np.array([5,  5, 38, 35])]

我可以以某种方式更改像素以显示此像素吗?我想我也可以通过opencv来做到这一点,因为这最终将显示在屏幕上。

编辑:关于边界框的注释,它们是x1, y1, width, height,并且在调整后的(224,224)图像中

最佳答案

有一些遗漏的细节:

  • 边框的格式是什么:[x1, y1, x2, y2][x1, y1, width, height]或其他?
  • 边界框值是在调整大小(224, 224)还是原始范围内?

  • 无论如何,您可以使用下面的函数绘制矩形(您需要根据格式进行选择):

    def draw_bboxes(img, bboxes, color=(0, 0, 255), thickness=1):
        for bbox in bboxes:
            # if [x1, y1, x2, y2]
            cv2.rectangle(img, tuple(bbox[:2]), tuple(bbox[-2:]), color, thickness)
            # if [x1, y1, width, height]
            cv2.rectangle(img, tuple(bbox[:2]), tuple(bbox[:2]+bbox[-2:]), color, thickness)
    

    假设您定义了bboxes,则可以调用以下函数:
  • 如果要在原始框架上绘制:

  • # [...]
    screen = np.array(sct.grab(monitor))
    draw_bboxes(screen, bboxes)
    # [...]
    
  • 如果要在调整大小的框架上绘制:

  • # [...]
    screen = cv2.resize(screen, (224,224)).astype(np.float32)/255
    draw_bboxes(screen, bboxes)
    # [...]
    

    进行一些更改后,完整代码将如下所示:

    import cv2
    import time
    import numpy as np
    from mss import mss
    
    def draw_bboxes(img, bboxes, color=(0, 0, 255), thickness=1):
        for bbox in bboxes:
            cv2.rectangle(img, tuple(bbox[:2]), tuple(bbox[:2]+bbox[-2:]), color, thickness)
    
    # bounding boxes
    bboxes = [np.array([12, 16, 29, 25]), np.array([5,  5, 38, 35])]
    
    with mss() as sct:
        # part of the screen to capture
        monitor = {"top": 79, "left": 265, "width": 905, "height": 586}
        while "Screen capturing":
            # get screen
            last_time = time.time()
            screen = np.asarray(sct.grab(monitor))
            print('loop took {} seconds'.format(time.time()-last_time))
    
            # convert from BGRA --> BGR
            screen = cv2.cvtColor(screen, cv2.COLOR_BGRA2BGR)
            # resize and draw bboxes
            screen = cv2.resize(screen, (224,224))
            draw_bboxes(screen, bboxes)
    
            # display
            cv2.imshow("OpenCV/Numpy normal", screen)
    
            # Press "q" to quit
            if cv2.waitKey(25) & 0xFF == ord("q"):
                cv2.destroyAllWindows()
                break
    

    输出将是这样的:

    python - 使用python mss在屏幕记录顶部绘制边框-LMLPHP

    关于python - 使用python mss在屏幕记录顶部绘制边框,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54296285/

    10-14 15:33
    查看更多