时间:2021-05-22
python实现连连看辅助–图像识别延伸(百度AI),供大家参考,具体内容如下
百度AI平台提供图片相似检索API接口,并有详细的API文档说明,可以更好的实现图片识别。
from aip import AipImageSearch""" 你的 APPID AK SK """APP_ID = '***'API_KEY = '***'SECRET_KEY = '***'client = AipImageSearch(APP_ID, API_KEY, SECRET_KEY)with open("{}-{}.jpg".format(1, 1), "rb") as f: im = f.read()# im = self.image_list[row][col]# 将图片与百度云自建相似图库中的图片对比相似度res = client.similarSearch(im)for r in res["result"]: if r["score"] > 0.9: print(r["brief"])百度AI平台提供非常多的API接口,值得研究。
代码
import win32guiimport timefrom PIL import ImageGrab , Imageimport numpy as npfrom pymouse import PyMousefrom aip import AipImageSearchclass GameAuxiliaries(object): def __init__(self): self.wdname = r'宠物连连看经典版2,宠物连连看经典版2小游戏,4399小游戏 pare_img(self,im1,im2): img1 = im1.resize((20, 20), Image.ANTIALIAS).convert('L') img2 = im2.resize((20, 20), Image.ANTIALIAS).convert('L') pi1 = list(img1.getdata()) pi2 = list(img2.getdata()) avg1 = sum(pi1) / len(pi1) avg2 = sum(pi2) / len(pi2) hash1 = "".join(map(lambda p: "1" if p > avg1 else "0", pi1)) hash2 = "".join(map(lambda p: "1" if p > avg2 else "0", pi2)) match = 0 for i in range(len(hash1)): if hash1[i] != hash2[i]: match += 1 # match = sum(map(operator.ne, hash1, hash2)) # match 值越小,相似度越高 return match # 将图片矩阵转换成数字矩阵 def create_array(self): array = np.zeros((10,14),dtype=np.int32) img_type_list = [] for row in range(1,len(self.image_list)+1): for col in range(1,len(self.image_list[1])+1): # im = Image.open('{}-{}.jpg'.format(row,col)) with open("{}-{}.jpg".format(row,col), "rb") as f: im = f.read() # im = self.image_list[row][col] # 将图片与百度云自建相似图库中的图片对比相似度 res = self.client.similarSearch(im) while len(res) == 2: res = self.client.similarSearch(im) print(res) print(row, col) time.sleep(0.2) print(row,col) for r in res["result"]: if r["score"] > 0.9: array[row][col]=r["brief"] return array def row_zero(self,x1,y1,x2,y2,array): '''相同的图片中间图标全为空''' if x1 == x2: min_y = min(y1,y2) max_y = max(y1,y2) if max_y - min_y == 1: return True for y in range(min_y+1,max_y): if array[x1][y] != 0 : return False return True else: return False def col_zero(self,x1,y1,x2,y2,array): '''相同的图片同列''' if y1 == y2: min_x = min(x1,x2) max_x = max(x1,x2) if max_x - min_x == 1: return True for x in range(min_x+1,max_x): if array[x][y1] != 0 : return False return True else: return False def two_line(self,x1,y1,x2,y2,array): '''两条线相连,转弯一次''' for row in range(1,9): for col in range(1,13): if row == x1 and col == y2 and array[row][col]==0 and self.row_zero(x1,y1,row,col,array) and self.col_zero(x2,y2,row,col,array): return True if row == x2 and col == y1 and array[row][col]==0 and self.row_zero(x2,y2,row,col,array) and self.col_zero(x1,y1,row,col,array): return True return False def three_line(self,x1,y1,x2,y2,array): '''三条线相连,转弯两次''' for row1 in range(10): for col1 in range(14): for row2 in range(10): for col2 in range(14): if array[row1][col1] == array[row2][col2] == 0 and self.row_zero(x1,y1,row1,col1,array) and self.row_zero(x2,y2,row2,col2,array) and self.col_zero(row1,col1,row2,col2,array): return True if array[row1][col1] == array[row2][col2] == 0 and self.col_zero(x1,y1,row1,col1,array) and self.col_zero(x2,y2,row2,col2,array) and self.row_zero(row1,col1,row2,col2,array): return True if array[row1][col1] == array[row2][col2] == 0 and self.row_zero(x2,y2,row1,col1,array) and self.row_zero(x1,y1,row2,col2,array) and self.col_zero(row1,col1,row2,col2,array): return True if array[row1][col1] == array[row2][col2] == 0 and self.col_zero(x2,y2,row1,col1,array) and self.col_zero(x1,y1,row2,col2,array) and self.row_zero(row1,col1,row2,col2,array): return True return False def mouse_click(self,x,y): top = (x - 1) * 38 + (x - 2) left = (y - 1) * 38 + (y - 2) right = y * 38 + (y - 1) bottom = x * 38 + (x - 1) if top < 0: top = 0 if left < 0: left = 0 self.m.press(int(417+(left+right)/2) ,int(289+(top+bottom)/2) ) def find_same_img(self,array): for x1 in range(1,9): for y1 in range(1,13): if array[x1][y1] == 0: continue for x2 in range(1,9): for y2 in range(1,13): if x1==x2 and y1 == y2: continue if array[x2][y2] == 0 : continue if array[x1][y1] != array[x2][y2] : continue if array[x1][y1] ==array[x2][y2] and (self.row_zero(x1,y1,x2,y2,array) or self.col_zero(x1,y1,x2,y2,array) or self.two_line(x1,y1,x2,y2,array) or self.three_line(x1,y1,x2,y2,array)): print("可消除!x{}y{} 和 x{}y{}".format(x1,y1,x2,y2)) self.mouse_click(x1,y1) time.sleep(0.1) self.mouse_click(x2,y2) time.sleep(0.1) array[x1][y1]=array[x2][y2]=0 def run(self): #找到游戏运行窗口 self.find_game_wd(self.wdname) # 截图,切割成小图标 self.get_img() print("切割完成") # 将图片矩阵转换成数字矩阵 array = self.create_array() print(array) # 遍历矩阵,找到可消除项,点击消除 for i in range(10): self.find_same_img(array) print(array)if __name__ == '__main__': ga = GameAuxiliaries() ga.run()以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
个人兴趣,用python实现连连看的辅助程序,总结实现过程及知识点。总体思路1、获取连连看程序的窗口并前置2、游戏界面截图,将每个一小图标切图,并形成由小图标组
本文实例为大家分享了js实现连连看游戏的具体代码,供大家参考,具体内容如下连连看body{text-align:center;}.text{text-align
向大家分享一款如何实现js版连连看游戏,如下图所示:首先看一下html的布局方式在index.html文件中:复制代码代码如下:连连看分数0时间0css文件夹下
C++连连看消除代码的实现,过程比较复杂。#include#includeusingnamespacestd;boolCheckRemove(intx1,int
本文实例为大家分享了python实现图像识别的具体代码,供大家参考,具体内容如下#!/usr/bin/envpythonfromPILimportImageim