此代码截取屏幕截图,然后在屏幕上查找给定对象,将其与给定的模板进行比较,然后计算找到对象的次数。这可以在下面看到马里奥硬币图片,其中程序将识别每个马里奥硬币,然后计算总数。我的问题是我希望程序在运行时继续计算硬币,这样如果在屏幕上添加或减去硬币,程序就会更新计数。
例如:计数19个硬币,计数19个硬币,计数19个硬币,(增加两个硬币),计数21个硬币,计数21个硬币等。
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
怎么样,你可以使用存储当前计数硬币的while循环外的变量,然后再次重新运行(读取不同的mario_image)计数,并比较变量是否存在变量之间的差异。
currently_counted_coins =0 #init
...
#after for loop
difference = count-currently_counted_coins # if difference <0 coins removed else added
#update
currently_counted_coins += difference # to keep a total number of coins
我最终搞清楚只需要在“for”循环中重新运行整个代码,如下所示。
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
有很多方法可以做到这一点,例如创建一个包含图像的列表[screenshots],你要检查匹配,并将所有代码放在for循环迭代列表项中。
list_images = ['image1.png','image2.png',..]
for img in list_images:
# here put your code
img_to_be_checked = cv2.imread(img)
# continue with your code in the while loop
要创建图像列表,您可以拍摄一些快照并使用名称存储它们,或使用您的代码多次拍摄快照,但在拍摄新快照之前必须更改桌面图像以查看任何差异。您可以使用时间戳定期拍摄快照,以便您有时间更改输入图像。最简单的方法是预先保存屏幕截图,然后在上面的代码中看到你的时候阅读它们。