0

我正在尝试使用 mss 和 opencv 在屏幕上查找图像。但是,每当我尝试使用带有 mss 的区域时,我都会收到此错误: 在此处输入图像描述

这个问题似乎与一些没有被释放的资源有关:python mss mss.exception.ScreenShotError:

我发现另一种解决方案是不使用区域并查看整个屏幕,但是对于小图像来说找到正确的图像变得更加困难。此外,在修改区域坐标时,错误可能会无缘无故消失(例如,不适用于 600、600、1200、800,而是使用 200、200、1200、800)。

测试.py

from python_imagesearch.imagesearch import *
imagesearcharea("images/fermer.png", 600, 600, 1200, 800)

图像搜索.py

def region_grabber(region):
    if is_retina: region = [n * 2 for n in region]
    x1 = region[0]
    y1 = region[1]
    width = region[2]
    height = region[3]

    region = x1, y1, width, height
    with mss.mss() as sct:
        return sct.grab(region)
        
def imagesearcharea(image, x1, y1, x2, y2, precision=0.8, im=None):
    if im is None:
        im = region_grabber(region=(x1, y1, x2, y2))
        if is_retina:
            im.thumbnail((round(im.size[0] * 0.5), round(im.size[1] * 0.5)))

    img_rgb = np.array(im)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = cv2.imread(image, 0)
    if template is None:
        raise FileNotFoundError('Image file not found: {}'.format(image))

    res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
    if max_val < precision:
        return [-1, -1]
    return max_loc

我的 mss 版本是 6.1,python 是 3.7,使用 windows 10 你有任何解决方案的想法,或者你知道替代 mss 的替代库吗?

4

1 回答 1

0

经过数小时的调查后,我没有找到任何解决方案使其工作。如果您使用 mss.grab,API 是 grab(region),其中 region 是 (x, y, width, length)。因此,x1 + 宽度不应大于您的屏幕分辨率,就像我的示例中的情况一样。

然而,考虑到这些信息,它仍然没有,所以我决定搬到另一个名为 PIL 的图书馆。我换了这个

with mss.mss() as sct:
    sct.grab(region)

这样

ImageGrab.grab(bbox)

至少它有效

于 2021-07-29T16:26:18.630 回答