欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

教你用Python寫安卓游戲外掛

 更新時間:2018年01月11日 10:51:36   投稿:laozhang  
本篇文章主要給教給大家如何用python這個語言寫出安卓游戲的輔助外掛以及思路講解,有興趣的朋友跟著學(xué)習(xí)下吧。

本次我們選擇的安卓游戲?qū)ο蠼小皢卧~英雄”,大家可以先下載這個游戲。

游戲的界面是這樣的:

通過選擇單詞的意思進(jìn)行攻擊,選對了就正常攻擊,選錯了就象征性的攻擊一下。玩了一段時間之后琢磨可以做成自動的,通過PIL識別圖片里的單詞和選項,然后翻譯英文成中文意思,根據(jù)中文模糊匹配選擇對應(yīng)的選項。

查找了N多資料以后開始動手,程序用到以下這些東西:

PIL:Python Imaging Library 大名鼎鼎的圖片處理模塊

pytesser:Python下用來驅(qū)動tesseract-ocr來進(jìn)行識別的模塊

Tesseract-OCR:圖像識別引擎,用來把圖像識別成文字,可以識別英文和中文,以及其它語言

autopy:Python下用來模擬操作鼠標(biāo)和鍵盤的模塊。

安裝步驟(win7環(huán)境):

(1)安裝PIL,下載地址:http://www.pythonware.com/products/pil/,安裝Python Imaging Library 1.1.7 for Python 2.7。

(2)安裝pytesser,下載地址:http://code.google.com/p/pytesser/,下載解壓后直接放在
C:\Python27\Lib\site-packages下,在文件夾下建立pytesser.pth文件,內(nèi)容為C:\Python27\Lib\site-packages\pytesser_v0.0.1

(3)安裝Tesseract OCR engine,下載:https://github.com/tesseract-ocr/tesseract/wiki/Downloads,下載Windows installer of tesseract-ocr 3.02.02 (including English language data)的安裝文件,進(jìn)行安裝。

(4)安裝語言包,在https://github.com/tesseract-ocr/tessdata下載chi_sim.traineddata簡體中文語言包,放到安裝的Tesseract OCR目標(biāo)下的tessdata文件夾內(nèi),用來識別簡體中文。

(5)修改C:\Python27\Lib\site-packages\pytesser_v0.0.1下的pytesser.py的函數(shù),將原來的image_to_string函數(shù)增加語音選擇參數(shù)language,language='chi_sim'就可以用來識別中文,默認(rèn)為eng英文。

改好后的pytesser.py:

"""OCR in Python using the Tesseract engine from Google
http://code.google.com/p/pytesser/
by Michael J.T. O'Kelly
V 0.0.1, 3/10/07"""
import Image
import subprocess
import util
import errors
tesseract_exe_name = 'tesseract' # Name of executable to be called at command line
scratch_image_name = "temp.bmp" # This file must be .bmp or other Tesseract-compatible format
scratch_text_name_root = "temp" # Leave out the .txt extension
cleanup_scratch_flag = True # Temporary files cleaned up after OCR operation
def call_tesseract(input_filename, output_filename, language):
 """Calls external tesseract.exe on input file (restrictions on types),
 outputting output_filename+'txt'"""
 args = [tesseract_exe_name, input_filename, output_filename, "-l", language]
 proc = subprocess.Popen(args)
 retcode = proc.wait()
 if retcode!=0:
  errors.check_for_errors()
def image_to_string(im, cleanup = cleanup_scratch_flag, language = "eng"):
 """Converts im to file, applies tesseract, and fetches resulting text.
 If cleanup=True, delete scratch files after operation."""
 try:
  util.image_to_scratch(im, scratch_image_name)
  call_tesseract(scratch_image_name, scratch_text_name_root,language)
  text = util.retrieve_text(scratch_text_name_root)
 finally:
  if cleanup:
   util.perform_cleanup(scratch_image_name, scratch_text_name_root)
 return text
def image_file_to_string(filename, cleanup = cleanup_scratch_flag, graceful_errors=True, language = "eng"):
 """Applies tesseract to filename; or, if image is incompatible and graceful_errors=True,
 converts to compatible format and then applies tesseract. Fetches resulting text.
 If cleanup=True, delete scratch files after operation."""
 try:
  try:
   call_tesseract(filename, scratch_text_name_root, language)
   text = util.retrieve_text(scratch_text_name_root)
  except errors.Tesser_General_Exception:
   if graceful_errors:
    im = Image.open(filename)
    text = image_to_string(im, cleanup)
   else:
    raise
 finally:
  if cleanup:
   util.perform_cleanup(scratch_image_name, scratch_text_name_root)
 return text
if __name__=='__main__':
 im = Image.open('phototest.tif')
 text = image_to_string(im)
 print text
 try:
  text = image_file_to_string('fnord.tif', graceful_errors=False)
 except errors.Tesser_General_Exception, value:
  print "fnord.tif is incompatible filetype. Try graceful_errors=True"
  print value
 text = image_file_to_string('fnord.tif', graceful_errors=True)
 print "fnord.tif contents:", text
 text = image_file_to_string('fonts_test.png', graceful_errors=True)
 print text

(6)安裝autopy,下載地址:https://pypi.python.org/pypi/autopy,下載autopy-0.51.win32-py2.7.exe進(jìn)行安裝,用來模擬鼠標(biāo)操作。

說下程序的思路:

1. 首先是通過模擬器在WINDOWS下執(zhí)行安卓的程序,然后用PicPick進(jìn)行截圖,將戰(zhàn)斗畫面中需要用到的區(qū)域進(jìn)行測量,記錄下具體在屏幕上的位置區(qū)域,用圖中1來判斷戰(zhàn)斗是否開始(保存下來用作比對),用2,3,4,5,6的區(qū)域抓取識別成文字。

計算圖片指紋的程序:

def get_hash(self, img):
    #計算圖片的hash值
    image = img.convert("L")
    pixels = list(image.getdata())
    avg = sum(pixels) / len(pixels)
    return "".join(map(lambda p : "1" if p > avg else "0", pixels))

圖片識別成字符:

#識別出對應(yīng)位置圖像成字符,把字符交給chose處理
  def getWordMeaning(self):
    pic_up = ImageGrab.grab((480,350, 480+300, 350+66))
    pic_aws1 = ImageGrab.grab((463,456, 463+362, 456+45))
    pic_aws2 = ImageGrab.grab((463,530, 463+362, 530+45))
    pic_aws3 = ImageGrab.grab((463,601, 463+362, 601+45))
    pic_aws4 = ImageGrab.grab((463,673, 463+362, 673+45))
    str_up = image_to_string(pic_up).strip().lower()
    #判斷當(dāng)前單詞和上次識別單詞相同,就不繼續(xù)識別
    if str_up <> self.lastWord:
      #如果題目單詞是英文,選項按中文進(jìn)行識別
      if str_up.isalpha():
        eng_up = self.dt[str_up].decode('gbk') if self.dt.has_key(str_up) else ''
        chs1 = image_to_string(pic_aws1, language='chi_sim').decode('utf-8').strip()
        chs2 = image_to_string(pic_aws2, language='chi_sim').decode('utf-8').strip()
        chs3 = image_to_string(pic_aws3, language='chi_sim').decode('utf-8').strip()
        chs4 = image_to_string(pic_aws4, language='chi_sim').decode('utf-8').strip()
        print str_up, ':', eng_up
        self.chose(eng_up, (chs1, chs2, chs3, chs4))
      #如果題目單詞是中文,選項按英文進(jìn)行識別
      else:
        chs_up = image_to_string(pic_up, language='chi_sim').decode('utf-8').strip()
        eng1 = image_to_string(pic_aws1).strip()
        eng2 = image_to_string(pic_aws2).strip()
        eng3 = image_to_string(pic_aws3).strip()
        eng4 = image_to_string(pic_aws4).strip()
        
        e2c1 = self.dt[eng1].decode('gbk') if self.dt.has_key(eng1) else ''
        e2c2 = self.dt[eng2].decode('gbk') if self.dt.has_key(eng2) else ''
        e2c3 = self.dt[eng3].decode('gbk') if self.dt.has_key(eng3) else ''
        e2c4 = self.dt[eng4].decode('gbk') if self.dt.has_key(eng4) else ''
        print chs_up
        self.chose(chs_up, (e2c1, e2c2, e2c3, e2c4))
      self.lastWord = str_up
    return str_up

2. 對于1位置的圖片提前截一個保存下來,然后通過計算當(dāng)前畫面和保存下來的圖片的距離,判斷如果小于40的就表示已經(jīng)到了選擇界面,然后識別2,3,4,5,6成字符,判斷如果2位置識別成英文字符的,就用2解析出來的英文在字典中獲取中文意思,然后再通過2的中文意思和3,4,5,6文字進(jìn)行匹配,匹配上漢字最多的就做選擇,如果匹配不上默認(rèn)返回最后一個。之前本來考慮是用Fuzzywuzzy來進(jìn)行模糊匹配算相似度的,不過后來測試了下對于中文匹配的效果不好,就改成按漢字單個進(jìn)行匹配計算相似度。

匹配文字進(jìn)行選擇:

#根據(jù)傳入的題目和選項進(jìn)行匹配選擇
  def chose(self, g, chs_list):
    j, max_score = -1, 0
    same_list = None
    #替換掉題目里的特殊字符
    re_list = [u'~', u',', u'.', u';', u' ', u'a', u'V', u'v', u'i', u'n', u'【', u')', u'_', u'W', u'd', u'j', u'-', u't']
    for i in re_list:
      g = g.replace(i, '')
    print type(g)
    #判斷2個字符串中相同字符,相同字符最多的為最佳答案
    for i, chsWord in enumerate(chs_list):
      print type(chsWord)
      l = [x for x in g if x in chsWord and len(x)>0]
      score = len(l) if l else 0
      
      if score > max_score:
        max_score = score
        j = i
        same_list = l
    #如果沒有匹配上默認(rèn)選最后一個
    if j ==-1:
      print '1. %s; 2. %s; 3. %s; 4. %s; Not found choice.' % (chs_list[0], chs_list[1], chs_list[2], chs_list[3])
    else:
      print '1. %s; 2. %s; 3. %s; 4. %s; choice: %s' % (chs_list[0], chs_list[1], chs_list[2], chs_list[3], chs_list[j])
      for k, v in enumerate(same_list):
        print str(k) + '.' + v,
    order = j + 1
    self.mouseMove(order)
    return order

3.最后通過mouseMove調(diào)用autopy操作鼠標(biāo)點擊對應(yīng)位置進(jìn)行選擇。

程序運行的錄像:http://v.youku.com/v_show/id_XMTYxNTAzMDUwNA==.html

程序完成后使用正常,因為圖片識別準(zhǔn)確率和字典的問題,正確率約為70%左右,效果還是比較滿意。程序總體來說比較簡單,做出來也就是純粹娛樂一下,串聯(lián)使用了圖片識別、中文模糊匹配、鼠標(biāo)模擬操作,算是個簡單的小外掛吧,源程序和用到的文件如下:

http://git.oschina.net/highroom/My-Project/tree/master/Word%20Hero

相關(guān)文章

  • Python枚舉類定義和使用方法

    Python枚舉類定義和使用方法

    這篇文章主要介紹了Python枚舉類定義和使用方法,文章圍繞主題的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-05-05
  • 如何在conda虛擬環(huán)境中配置cuda+cudnn+pytorch深度學(xué)習(xí)環(huán)境

    如何在conda虛擬環(huán)境中配置cuda+cudnn+pytorch深度學(xué)習(xí)環(huán)境

    這篇文章主要介紹了如何在conda虛擬環(huán)境中配置cuda+cudnn+pytorch深度學(xué)習(xí)環(huán)境,想在服務(wù)器上配置深度學(xué)習(xí)的環(huán)境,看了很多資料后總結(jié)出來了對于新手比較友好的配置流程,需要的朋友可以參考下
    2023-03-03
  • python 實現(xiàn)德洛內(nèi)三角剖分的操作

    python 實現(xiàn)德洛內(nèi)三角剖分的操作

    這篇文章主要介紹了python 實現(xiàn)德洛內(nèi)三角剖分的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Python實現(xiàn)CART決策樹算法及詳細(xì)注釋

    Python實現(xiàn)CART決策樹算法及詳細(xì)注釋

    CART算法是一種樹構(gòu)建算法,既可以用于分類任務(wù),又可以用于回歸,本文僅討論基本的CART分類決策樹構(gòu)建,不討論回歸樹和剪枝等問題,感興趣的朋友跟隨小編一起看看吧
    2021-10-10
  • python出現(xiàn)更新庫失敗A?new?release?of?pip?is?available:?23.0.1?->?23.3解決辦法

    python出現(xiàn)更新庫失敗A?new?release?of?pip?is?available:?23.0.

    學(xué)習(xí)了Python我們知道它自帶了很多的庫,同時我們還需要對某個庫進(jìn)行升級,這篇文章主要給大家介紹了關(guān)于python出現(xiàn)更新庫失敗A?new?release?of?pip?is?available:?23.0.1?->?23.3的解決辦法,需要的朋友可以參考下
    2024-03-03
  • Python內(nèi)置函數(shù)locals和globals對比

    Python內(nèi)置函數(shù)locals和globals對比

    這篇文章主要介紹了Python內(nèi)置函數(shù)locals和globals對比,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • python字符串操作詳析

    python字符串操作詳析

    這篇文章主要介紹了python字符串操作,字符串是不可變類型可以重新賦值,但不可以索引改變其中一個值,只能拼接字符串建立新變量,下面來了解具體內(nèi)容吧,需要的小伙伴可以參考一下
    2022-02-02
  • Python爬蟲requests模塊之URL地址中的參數(shù)解讀

    Python爬蟲requests模塊之URL地址中的參數(shù)解讀

    這篇文章主要介紹了Python爬蟲requests模塊之URL地址中的參數(shù)解讀,在你拿到數(shù)據(jù)所在的url地址之后,發(fā)送網(wǎng)絡(luò)請求時,請求的url中包含兩種地址參數(shù):查詢參數(shù)和請求參數(shù),需要的朋友可以參考下
    2023-08-08
  • Python基于jieba分詞實現(xiàn)snownlp情感分析

    Python基于jieba分詞實現(xiàn)snownlp情感分析

    情感分析(sentiment analysis)是2018年公布的計算機科學(xué)技術(shù)名詞,它可以根據(jù)文本內(nèi)容判斷出所代表的含義是積極的還是負(fù)面的等。本文將通過jieba分詞實現(xiàn)snownlp情感分析,感興趣的可以了解一下
    2023-01-01
  • Python-jenkins模塊之folder相關(guān)操作介紹

    Python-jenkins模塊之folder相關(guān)操作介紹

    這篇文章主要介紹了Python-jenkins模塊之folder相關(guān)操作介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05

最新評論