OpenCV обнаруживает шаблон изображения на экране, как сделать код проще - python - PullRequest
2 голосов
/ 11 октября 2019

В настоящее время я работаю над обнаружением чисел на экране с помощью OpenCV. Но из любопытства я хотел бы спросить вас, как упростить код объявления и обнаружения изображений. Этот выглядит некрасиво. Я довольно новый программист на Python и не могу найти никакого другого решения для этой работы, кроме следующего:

import numpy as np
from PIL import ImageGrab
import cv2
import time


def screen_record(): 
    last_time = time.time()
    while(True):
        printscreen =  np.array(ImageGrab.grab(bbox=(384,1257,1176,1362)))
        print('loop took {} seconds'.format(time.time()-last_time))
        last_time = time.time()
        img_rgb = printscreen
        img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

        template0 = cv2.imread('0.png',0)
        template1 = cv2.imread('1.png',0)
        template2 = cv2.imread('2.png',0)
        template3 = cv2.imread('3.png',0)
        template4 = cv2.imread('4.png',0)
        template5 = cv2.imread('5.png',0)
        template6 = cv2.imread('6.png',0)
        template7 = cv2.imread('7.png',0)
        template8 = cv2.imread('8.png',0)
        template9 = cv2.imread('9.png',0)
        w, h = template0.shape[::-1]
        w, h = template1.shape[::-1]
        w, h = template2.shape[::-1]
        w, h = template3.shape[::-1]
        w, h = template4.shape[::-1]
        w, h = template5.shape[::-1]
        w, h = template6.shape[::-1]
        w, h = template7.shape[::-1]
        w, h = template8.shape[::-1]
        w, h = template9.shape[::-1]
        res0 = cv2.matchTemplate(img_gray,template0,cv2.TM_CCOEFF_NORMED)
        res1 = cv2.matchTemplate(img_gray,template1,cv2.TM_CCOEFF_NORMED)
        res2 = cv2.matchTemplate(img_gray,template2,cv2.TM_CCOEFF_NORMED)
        res3 = cv2.matchTemplate(img_gray,template3,cv2.TM_CCOEFF_NORMED)
        res4 = cv2.matchTemplate(img_gray,template4,cv2.TM_CCOEFF_NORMED)
        res5 = cv2.matchTemplate(img_gray,template5,cv2.TM_CCOEFF_NORMED)
        res6 = cv2.matchTemplate(img_gray,template6,cv2.TM_CCOEFF_NORMED)
        res7 = cv2.matchTemplate(img_gray,template7,cv2.TM_CCOEFF_NORMED)
        res8 = cv2.matchTemplate(img_gray,template8,cv2.TM_CCOEFF_NORMED)
        res9 = cv2.matchTemplate(img_gray,template9,cv2.TM_CCOEFF_NORMED)
        threshold = 0.8
        loc0 = np.where( res0 >= threshold)
        loc1 = np.where( res1 >= threshold)
        loc2 = np.where( res2 >= threshold)
        loc3 = np.where( res3 >= threshold)
        loc4 = np.where( res4 >= threshold)
        loc5 = np.where( res5 >= threshold)
        loc6 = np.where( res6 >= threshold)
        loc7 = np.where( res7 >= threshold)
        loc8 = np.where( res8 >= threshold)
        loc9 = np.where( res9 >= threshold)
        for pt in zip(*loc0[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
        for pt in zip(*loc1[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
        for pt in zip(*loc2[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
        for pt in zip(*loc3[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
        for pt in zip(*loc4[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
        for pt in zip(*loc5[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
        for pt in zip(*loc6[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
        for pt in zip(*loc7[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
        for pt in zip(*loc8[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)
        for pt in zip(*loc9[::-1]):
            cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)



        cv2.imshow('Detected',img_rgb)
        if cv2.waitKey(25) & 0xFF == ord('q'):
                cv2.destroyAllWindows()
                break

screen_record()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...