中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python實現連連看輔助之圖像識別延伸

發布時間:2020-09-12 22:01:43 來源:腳本之家 閱讀:172 作者:許武 欄目:開發技術

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 win32gui
import time
from PIL import ImageGrab , Image
import numpy as np
from pymouse import PyMouse
from aip import AipImageSearch


class GameAuxiliaries(object):
 def __init__(self):
  self.wdname = r'寵物連連看經典版2,寵物連連看經典版2小游戲,4399小游戲 www.4399.com - Google Chrome'
  # self.wdname = r'main.swf - PotPlayer'
  self.image_list = {}
  self.m = PyMouse()
  self.APP_ID = '15633871'
  self.API_KEY = 'LNMuXHmULcZM0PRKX8ZT4OnB'
  self.SECRET_KEY = 'IwvyYxeDLIR5XvEmnX3ENWoVzMITkdBL'

  self.client = AipImageSearch(self.APP_ID, self.API_KEY, self.SECRET_KEY)


 def find_game_wd(self,wdname):
  # 取得窗口句柄
  hdwd = win32gui.FindWindow(0,wdname)
  # 設置為最前顯示
  win32gui.SetForegroundWindow(hdwd)
  time.sleep(1)

 def get_img(self):
  image = ImageGrab.grab((417, 289, 884, 600))
  # image = ImageGrab.grab((417, 257, 885, 569))
  image.save('1.jpg','JPEG')
  for x in range(1,9):
   self.image_list[x] = {}
   for y in range(1,13):
    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
    im_temp = image.crop((left,top,right,bottom))
    im = im_temp.crop((1,1,37,37))
    im.save('{}-{}.jpg'.format(x,y))
    self.image_list[x][y]=im

 def compare_img_baiduapi(self,im):
  '''將圖片與百度云自建相似圖庫中的圖片對比相似度'''
  pass

 # 判斷兩個圖片是否相同。漢明距離,平均哈希
 def compare_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)
  hash2 = "".join(map(lambda p: "1" if p > avg1 else "0", pi1))
  hash3 = "".join(map(lambda p: "1" if p > avg2 else "0", pi2))
  match = 0
  for i in range(len(hash2)):
   if hash2[i] != hash3[i]:
    match += 1
  # match = sum(map(operator.ne, hash2, hash3))
  # 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()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

敦煌市| 万安县| 新宁县| 临江市| 丹棱县| 全州县| 曲靖市| 菏泽市| 九江市| 青冈县| 开平市| 昌乐县| 闽侯县| 高雄县| 定南县| 乳源| 靖宇县| 锦屏县| 阿拉尔市| 湖口县| 靖江市| 白玉县| 宾川县| 秦皇岛市| 会昌县| 盐山县| 泉州市| 大安市| 开封县| 沙坪坝区| 荔浦县| 衡山县| 多伦县| 玉屏| 兰考县| 客服| 甘孜县| 监利县| 津南区| 南雄市| 若羌县|