0
import cv2
import numpy as np
from PIL import ImageGrab
from multiprocessing import Pool


def getGrayBase():
    base = ImageGrab.grab(bbox=(0, 0, 1920, 1080))
    base.save(f'Screen\\base.png')
    base = f'Screen\\base.png'
    rgbBase = cv2.imread(base)
    grayBase = cv2.cvtColor(rgbBase, cv2.COLOR_BGR2GRAY)
    return grayBase


def findPict(sabjektSearch):
    grayBase=getGrayBase()
    template = cv2.imread(sabjektSearch, 0)
    rezult = {'flag': False, 'x': 0, 'y': 0, 'func': 0,'scroll':0}
    # w, h = template.shape[::-1]  # Высота ширина
    # We find the center of the desired image
    M = cv2.moments(template)
    centr_x = int(M['m10'] / M['m00'])
    centr_y = int(M['m01'] / M['m00'])
    # print(f'centr {centr_x, centr_y}')
    rez_search = cv2.matchTemplate(grayBase, template, cv2.TM_CCOEFF_NORMED)
    threshold = 0.8
    # Check if the image you are looking for in the base
    flag = False
    for i in rez_search:
        if np.amax(rez_search) > threshold:
            flag = True
            rezult['flag'] = flag
    if flag == True:
        loc = np.where(rez_search >= threshold)
        for pt in zip(*loc[::-1]):
            x = int(pt[0]) + centr_x
            y = int(pt[1]) + centr_y
            rezult['x'] = x
            rezult['y'] = y
    nameFunc = sabjektSearch.split('\\')[-1]
    rez = f'{nameFunc} - YES ' if flag else f' {nameFunc} - no '
    print(rez,end=" ")
    return rezult

arr_pick_all=[f'Screen\p1.png',f'Screen\\p2',f'Screen\p3.png',f'Screen\p4.PNG',f'Screen\p5.png',f'Screen\\p6.png']
if __name__ == '__main__':
    p = Pool(len(arr_pick_all))
    rezult=p.map(findPict,arr_pick_all)
    print()
    print(rezult.get(timeout=1))
    p.close()
    p.join()

1 ~ 3 個の画像を検索しますが、数が多いとエラーが発生します cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: ( -215:Assertion failed) 関数 'cv::cvtColor' の !_src.empty()

4

0 に答える 0