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:运行效果图在这里插入图片描述

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值