aircv 大图找小图 并识别小图中信息

import aircv as ac
import pytesseract,pyautogui
import win32con,win32api,time,re
from ctypes import *
from PIL import Image, ImageGrab
def matchImg(imgsrc, imgobj,confidencevalue=0.8): # imgsrc=原始图像,imgobj=待查找的图片
ImageGrab.grab().save(imgsrc)
imsrc = ac.imread(imgsrc)
imobj = ac.imread(imgobj)
match_result = ac.find_template(imsrc, imobj)
#‘confidence’ 匹配相似度,rectangle:匹配图片在原始图像上四边形的坐标
#result:匹配图片在原始图片上的中心坐标点,也就是我们要找的点击点
# {‘confidence’: 0.983245, ‘rectangle’: ((394, 384), (394, 416), (450, 384), (450, 416)), ‘result’: (422.0, 400.0)}
if match_result is not None:
match_result[‘shape’] = (imsrc.shape[1], imsrc.shape[0]) # 0为高,1为宽
if int((match_result[‘confidence’])) < int(0.8):
print(“未找到目标图片”)
return match_result
#print(matchImg(imgsrc, imgobj,confidencevalue=0.5))
def main1():
imgsrc = ‘./imgsrc.png’
imgobj = ‘./firefox.png’ #以这张图片为中心
s = (matchImg(imgsrc, imgobj, confidencevalue=0.8))
print(s)
#print(s[‘rectangle’][0][0],s[‘rectangle’][0][1],s[‘rectangle’][1][0],s[‘rectangle’][1][1])
x,y,z,w =(s[‘rectangle’][0][0], s[‘rectangle’][0][1], s[‘rectangle’][2][0], s[‘rectangle’][3][1])
bbox = (x,y,z,w) #左上,左下,右上,右下
print(bbox)
bbox2 = (x+(z-x),y,z+300,w)
ImageGrab.grab(bbox).save(‘recap.png’) #按照指定坐标截图
ImageGrab.grab(bbox2).save(‘recap2.png’)
#pyautogui.moveTo(z+180,y)
main1()
def getstring(strpath):
text = pytesseract.image_to_string(Image.open(strpath))
print(text)
text2 = re.search(’(.*/)’,text).group()
text3 = re.search(’\d+’,text2).group()
print(“运行的圈数为%d”%int(text3))
with open(“s3log.txt”,“w+”) as f: #把圈数写入log中
f.write(text3)
f.close()
getstring(’./recap2.png’)

ps:运行效果图aircv 大图找小图 并识别小图中信息_第1张图片

你可能感兴趣的:(Python3)