用python-mss在屏幕记录上方绘制边界框

2024-04-28 23:33:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有代码屏幕记录和每一帧我有一套边界框,我想显示在每一帧。我可以使用matplotlib或其他方法来完成这项工作,但是我mss的工作速度大约为每秒30帧,我需要能够快速显示边界框。在

我在docsthis 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

现在假设我有一组边界框要显示在每一帧上,例如

^{pr2}$

我可以改变像素以显示这个吗。我想我也可以通过opencv来完成,因为这是最终显示屏幕的地方。在

{cd5>中的{cd4>框的大小都在{cd5>中


Tags: thetofromimport屏幕timeasnp
1条回答
网友
1楼 · 发布于 2024-04-28 23:33:25

有一些遗漏的细节:

  • 边框的格式是什么:[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,那么可以调用函数:

  • 如果要在原始帧上绘制:
^{pr2}$
  • 如果要在调整大小的框架上绘制:
# [...]
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

输出如下:

enter image description here

相关问题 更多 >