添加了ocr用来识别获取经验.检测圆形来判断游戏状态,还有修复bug和优化逻辑

识别经验

思路就三个(大佬还有其他思路求告知),CV2模板匹配,本地OCR和OCR api.

CV2模板匹配

经典套路对图片去色二值化处理然后获取模板识别轮廓挨个对比,没错,我也是这样写的,写完之后确实成功识别,但是碰到很恼人的问题,动物派对经验值有时候两个数字挨得太近,查找轮廓会把两个字符分在一个框里,在调了半天不成功后果断放弃.代码没有咧,已经删了不过就像第一句那样先

  1. cv2.imread()获取灰度图,然后用算法二值化,还可以做其他降噪等处理
  2. 查找轮廓,分块画图
  3. 根据分块挨个按模板匹配

思路就这样,具体不多说,分享一篇文章给大家,写的很好

Python开发游戏自动化脚本(六)数字识别 - 知乎 (zhihu.com)

一位朋友/老师提供了一个思路,回头尝试一下,二值化后提取轮廓.

本地OCR

使用了大佬写的PaddleOCR-json v1.3.0,好用强大,不多说

OCR API

使用ocr space提供的免费api,每个月有25000次提交额度,速度也很快,好用,不多说.

圆形检测

为了区分当前是否在游戏中,但是动物派对游戏中界面没有特别的图标存在,所以模板匹配判断没有什么好的模板,但是左上角有小动物头像,并且有一圈体力值,可以检测图片是否存在圆形(头像)来判断是否在游戏中(红色是画上的)

image-20231004223857768
def isthereCircle(filepath):
   img = cv2.imread(filepath, 0)
   size = 200
   x1 = 0
   y1 = 0
   x2 = x1+size
   y2 = y1+size
   cut = img[y1:y2, x1:x2]
   numpy_img = cv2.adaptiveThreshold(cut, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 9, 13)   # 自动阈值二值化
   circles = cv2.HoughCircles(numpy_img, cv2.HOUGH_GRADIENT , 1 , 90 , param1=100 , param2= 30 , minRadius=30 , maxRadius=60)
   if circles is not None:
       print(f'圆形坐标{circles}')
       return True
   else:
       return False

Bug

网络请求用try except来摒除请求失败    

def check_version(self):
       try:
           latest_version= requests.get('https://api.github.com/repos/q1263868407/animal-auto/releases/latest').json()['tag_name'][1:]
           if current_version < float(latest_version):
               InfoBar.new(
                   icon=FluentIcon.GITHUB,
                   title='',
                   content=f"作者又叕叒更新啦,新版本{latest_version}已经发布啦,快去下载吧",
                   orient=Qt.Horizontal,
                   isClosable=True,  # disable close button
                   position=InfoBarPosition.TOP_LEFT,
                   duration=4000,
                   parent=self
              ).setCustomBackgroundColor("#F5C563", "#000000")
           else:
               InfoBar.success(
                   title='',
                   content=f"最新版本呢!",
                   orient=Qt.Horizontal,
                   isClosable=True,  # disable close button
                   position=InfoBarPosition.TOP_LEFT,
                   duration=2000,
                   parent=self
              )
       except Exception as e:
           InfoBar.new(
               icon=FluentIcon.GITHUB,
               title='',
               content=f"网络请求失败,版本检测不到了o(╥﹏╥)o",
               orient=Qt.Horizontal,
               isClosable=True,  # disable close button
               position=InfoBarPosition.TOP_LEFT,
               duration=4000,
               parent=self
          ).setCustomBackgroundColor("#FF6A6A","#000000")

如今的样子

动物派对GUIv1.81

Angel,请你不要放开我的手