pythonopencvtemplate-matching

Counting how many times an image appears on screen


This code takes a screenshot of the screen and then looks for a given object on the screen, by comparing it to the template given, and then counts how many times the object is found. This can be seen below with the mario coins picture in which the program will identify each mario coin and then count how many there are total. My problem is that I would like for the program to continue counting the coins while running, so that if a coin is added or subtracted on the screen the program updates the count number.

Ex: Counts 19 coins, Counts 19 coins, Counts 19 coins, (Two coins added), Counts 21 coins, Counts 21 coins etc.

import cv2 as cv2
import numpy
import pyautogui

# Takes a screen shot and saves the file in the specified location
loc1 = (r'Capture.png')
pyautogui.screenshot(loc1)

# Reads the screen shot and loads the image it will be compared too
img_rgb = cv2.imread(loc1)
count = 0
n = 0

while n < 5:
    # Reads the file
    template_file_ore = r"mario.png"
    template_ore = cv2.imread(template_file_ore)
    w, h = template_ore.shape[:-1]

    # Compares screen shot to given image, gives error thresh hold
    res = cv2.matchTemplate(img_rgb, template_ore, cv2.TM_CCOEFF_NORMED)
    threshold = 0.80
    loc = numpy.where(res >= threshold)

    # Puts red box around matched images and counts coins
    for pt in zip(*loc[::-1]):
        cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
        count = count + 1

        print(count)
    n = n + 1

Mario Picture


Solution

  • I ended up figuring it out just have to rerun the whole code in the "for" loop, as seen below.

        import cv2 as cv2
        import numpy
        import pyautogui
    
        # Takes a screen shot and saves the file in the specified location
        loc1 = (r'Capture.png')
        pyautogui.screenshot(loc1)
    
        # Reads the screen shot and loads the image it will be compared too
        img_rgb = cv2.imread(loc1)
        count = 0
        n = 0
    
        while n < 20:
            # Reads the file
            template_file_ore = r"mario.png"
            template_ore = cv2.imread(template_file_ore)
            w, h = template_ore.shape[:-1]
    
            # Compares screen shot to given image, gives error thresh hold
            res = cv2.matchTemplate(img_rgb, template_ore, cv2.TM_CCOEFF_NORMED)
            threshold = 0.80
            loc = numpy.where(res >= threshold)
    
            # Puts red box around matched images and counts coins
            for pt in zip(*loc[::-1]):
                loc1 = (r'Capture.png')
                pyautogui.screenshot(loc1)
    
             # Reads the file
                template_file_ore = r"mario.png"
                template_ore = cv2.imread(template_file_ore)
                w, h = template_ore.shape[:-1]
    
             # Compares screen shot to given image, gives error thresh hold
                res = cv2.matchTemplate(img_rgb, template_ore, cv2.TM_CCOEFF_NORMED)
                threshold = 0.80
                loc = numpy.where(res >= threshold)
    
              # Reads the screen shot and loads the image it will be compared too
                img_rgb = cv2.imread(loc1)
                cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 0, 255), 2)
                count = count + 1
    
                print(count)
            n = n + 1