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

通過選擇單詞的意思進(jìn)行攻擊,選對(duì)了就正常攻擊,選錯(cuò)了就象征性的攻擊一下。玩了一段時(shí)間之后琢磨可以做成自動(dòng)的,通過PIL識(shí)別圖片里的單詞和選項(xiàng),然后翻譯英文成中文意思,根據(jù)中文模糊匹配選擇對(duì)應(yīng)的選項(xiàng)。
查找了N多資料以后開始動(dòng)手,程序用到以下這些東西:
PIL:Python Imaging Library 大名鼎鼎的圖片處理模塊
pytesser:Python下用來驅(qū)動(dòng)tesseract-ocr來進(jìn)行識(shí)別的模塊
Tesseract-OCR:圖像識(shí)別引擎,用來把圖像識(shí)別成文字,可以識(shí)別英文和中文,以及其它語言
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),用來識(shí)別簡體中文。
(5)修改C:/Python27/Lib/site-packages/pytesser_v0.0.1下的pytesser.py的函數(shù),將原來的image_to_string函數(shù)增加語音選擇參數(shù)language,language='chi_sim'就可以用來識(shí)別中文,默認(rèn)為eng英文。
改好后的pytesser.py:
"""OCR in Python using the Tesseract engine from Googlehttp://code.google.com/p/pytesser/by Michael J.T. O'KellyV 0.0.1, 3/10/07"""import Imageimport subprocessimport utilimport errorstesseract_exe_name = 'tesseract' # Name of executable to be called at command linescratch_image_name = "temp.bmp" # This file must be .bmp or other Tesseract-compatible formatscratch_text_name_root = "temp" # Leave out the .txt extensioncleanup_scratch_flag = True # Temporary files cleaned up after OCR operationdef 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 textdef 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 textif __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            
新聞熱點(diǎn)
疑難解答
圖片精選