看板 PCReDive 關於我們 聯絡資訊
覺得小遊戲查表好麻煩,做了一個查表器 附上人權 http://i.imgur.com/vzuo7ru.jpg # 實驗成果 v1 https://youtu.be/IcirpEYB2lY
v2,感謝小精靈改scrcpy https://youtu.be/Nb9cCh4DmgY
# source code aHR0cHM6Ly9wYXN0ZWJpbi5jb20vajhNY3F4RGE= # 實驗環境 - Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz - 12G RAM - MX500 SSD - Ubuntu 19.10 # 辨識九宮格圖示 OpenCV提供大量函式,為此我們使用了 `matchTemplate` 辨識圖示,整體流程如下 1. 切割原始圖片成單張圖示(icon),大小為106x106px 2. 調整大小至78x78px 3. 用 `matchTemplate` 比對148張既有圖示,排序 `min_val`,找出最大值所對應的圖 示和ID ![](https://i.imgur.com/tATMR9t.png) 得益於Profiling工具的日新月異,我用cProfile分析單執行緒的 python script,發現 最耗時的地方位於`templateMatching`,足足佔了整體執行時間97%‧ ```bash ~$ python3 -m cProfile -s tottime ./tmp.py 0.7606322765350342 325680 function calls (318751 primitive calls) in 1.289 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 1287 0.741 0.001 0.741 0.001 {matchTemplate} 28/17 0.162 0.006 0.240 0.014 {built-in method _imp.create_dynamic} 280 0.045 0.000 0.045 0.000 {built-in method marshal.loads} 2 0.041 0.020 0.041 0.020 {imread} ``` ### 全彩轉灰階 試著將全彩圖示轉換成灰階再進行比對 ```bash ~$ python3 -m cProfile -s tottime ./tmp.py 0.2725179195404053 326034 function calls (319105 primitive calls) in 0.811 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 1287 0.256 0.000 0.256 0.000 {matchTemplate} ``` 看的出 maximum overhead 還是在`templateMatch`,但總執行時間足足少了65%‧ ### 縮小圖示再比對 將兩邊要比對的78x78px縮小至26x26px,於是`matchTemplate`讓出寶座,整體執行時間 更是來到 0.05s(-80%),圖示辨識率也還是維持100%。 ``` 0.05796694755554199 326071 function calls (319142 primitive calls) in 0.613 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 28/17 0.153 0.005 0.234 0.014 {built-in method _imp.create_dynamic} 280 0.046 0.000 0.046 0.000 {built-in method marshal.loads} 2 0.043 0.022 0.043 0.022 {imread} 1287 0.042 0.000 0.042 0.000 {matchTemplate} 1175/1120 0.021 0.000 0.073 0.000 {built-in method builtins.__build_class__} ``` 為了減少執行時間,我們也測試圖片大小和辨識準確率關係,程式有做一些改動(印出正 確率、存測試資料),下方數據只是作為 Image size 的選取依據並非實際執行時間。測 試腳本如下 ```bash for i in `seq 78 -2 1`;do icon_size=$i python3 tmp.py; done; ``` 只用一張遊戲畫面圖片做測試 icon size | elapsed time (sec)| accurate rate %) ---|---|--- 78x78|0.2452|100.0 26x26|0.0758|100.0 24x24|0.0447|100.0 22x22|0.0434|100.0 20x20|0.0390|100.0 18x18|0.0389|88.9 16x16|0.0339|77.8 14x14|0.0473|77.8 12x12|0.0277|66.7 10x10|0.0295|77.8 8x8|0.0248|55.6 6x6|0.0234|55.6 4x4|0.0221|11.1 2x2|0.0210|11.1 看來極限能到20x20px,取20x20的耗時較26x26減少30% 26x26 ``` 0.0584 326155 function calls (319226 primitive calls) in 0.573 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 28/17 0.144 0.005 0.221 0.013 {built-in method _imp.create_dynamic} 280 0.045 0.000 0.045 0.000 {built-in method marshal.loads} 1287 0.043 0.000 0.043 0.000 {matchTemplate} ``` 20x20 ``` 0.0406 326057 function calls (319128 primitive calls) in 0.628 seconds Ordered by: internal time ncalls tottime percall cumtime percall filename:lineno(function) 28/17 0.158 0.006 0.242 0.014 {built-in method _imp.create_dynamic} 280 0.050 0.000 0.050 0.000 {built-in method marshal.loads} 2 0.043 0.021 0.043 0.021 {imread} 1287 0.025 0.000 0.025 0.000 {matchTemplate} ``` 不知道為什麼行的通...但是算了 ![](https://i.imgur.com/KCjfbQS.png) ### 使用python的sorted Python的`sorted`使用名為Timsort的混合式排序演算法,使用`sorted`減少了0.0006秒 的執行時間 ``` 0.0385->0.0379 ``` ```python= # Before def findIconId(img): id = None result = -1 for icon in icons.values(): min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res) if (result < min_val): result = min_val id = icon.id return id # After def findIconId(img): arr=[] for icon in icons.values(): res = cv.matchTemplate(resized,icon.img,cv.TM_CCOEFF) min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res) arr.append((min_val, icon.id)) arr = sorted(arr, key= lambda x: x[0],reverse=True) return arr[0][1] ``` ### Multithreading 最後手段,但用了反而增加時間,不予採用。 `0.0373->0.0494 secs` 你知道 python 的 multithreading 跟你想像的不一樣嗎? :::success "_Each thread that wants to run must wait for the GIL to be released by the other thread, which means your multi-threaded Python application is actually single threaded._" https://medium.com/practo-engineering/threading-vs-multiprocessing-in-python-7b57f224eadb ::: ![](https://i.imgur.com/ePnGiZC.png) ### 保險機制 遊戲中的過場畫面可不能拿來辨識,因此次要設計用一套方法來判斷何時該開始辨識 1. 用像素點顏色判斷白邊存在 2. 偵測白色反光,取圖示中心的點做判斷 兩項測試都通過後才會進行template matching **valid** > ![](https://i.imgur.com/xiSrrvv.jpg) > >**invalid** > ![](https://i.imgur.com/73aqDUW.jpg) > ![](https://i.imgur.com/u4Te7oH.jpg) > ![](https://i.imgur.com/5e6IJ6V.jpg) > ![](https://i.imgur.com/7PNnNHy.jpg) > ![](https://i.imgur.com/Iwmn5de.jpg) > ![](https://i.imgur.com/GgCjvoC.jpg) ### 辨識韻母 韻母只有一張圖要和38張圖比對,尺寸小,耗時短,不在此多著墨。 # 選取演算法 這部份沒什麼特別,目標就是最高分的圖示。以滿圖鑑為目標?不會多點幾次? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.248.3.243 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/PCReDive/M.1583628323.A.04D.html ※ 編輯: gR7P4zXH (118.161.105.58 臺灣), 03/08/2020 08:52:43
KParmy: 看不懂你在幹嘛 但好像很厲害 推一下 03/08 08:52
ikaros35: 我做研究都沒那麼認真 跪了 03/08 08:53
adamacon: 你的指導教授知道你在作這種東西嗎? 03/08 08:56
跑實驗閒暇時做的
qni1104: 這技術用在遊戲上,我只能跪了謝謝大大造福群眾 03/08 09:07
Jongkook: 三小= = 03/08 09:09
r22742557: 不好意思請教一下,你在辨識九宮格圖示部分,是使用螢 03/08 09:10
講完R
shaodw791017: ? 03/08 09:11
CYL009: 我今天流量夠了 該睡覺了 03/08 09:11
askye546388: 看起來很厲害的樣子 03/08 09:11
KMSNY: 很好 明天來上班 03/08 09:15
amsterdamDK: 三小。。 03/08 09:21
aha8877: 先推,免得其他人以為我看不懂 03/08 09:41
honey4617912: 嗯嗯我也是這麼想的 03/08 09:48
x8s8x8s8x: 偶先推 03/08 09:57
dajon: 所以是螢幕辨識+按鍵精靈嗎 03/08 10:02
沒有按鍵精靈
sakuravivio: 先推,裝懂 03/08 10:03
Incredible99: 看不懂你在幹嘛 不過你之後要去so-net? 03/08 10:03
比較想去.net折衣服
crazycy: 這不就外掛... 03/08 10:09
我自己手動點,不算外掛喔
Tiamat6716: 科技的力量 03/08 10:09
felixden: 看不懂 03/08 10:12
Tamama56: 先推 03/08 10:13
otakuxavier: 請問可以把py檔放github嗎 想clone下來好好研究一下 03/08 10:17
糞扣公開傷眼
mifire: sonet:你要來我這裡報到了嗎 03/08 10:17
zodhk01054: 不懂,先推再說 03/08 10:23
Terry1984: 太神啦 03/08 10:24
pionlang5566: 有沒有打算放在 github 開源 03/08 10:29
a890324: 哇靠 文組看不懂啊 03/08 10:31
pionlang5566: 用 numba 會比較快嗎 <-出張嘴 03/08 10:33
loltrg42972: 科技始終來自人性 03/08 10:33
rhythm7321: 推 如果能找想要的押韻就更猛了 03/08 10:44
請開spec
sweetmiki: 我覺得你在做壞事 但我看不懂 03/08 10:45
smart0eddie: 姆咪 03/08 10:53
shinobunodok: 現在打電動都要這麼硬派寫實喔 03/08 11:01
wsttuub: .......看不懂但先跪再說 03/08 11:13
dend930037: 你的指導教授知道這件事嗎 03/08 11:17
這是在家搞的
gsfate: 這算外掛嗎XD 03/08 11:17
nutta: 先推免得 03/08 11:20
qwert65732: 上次是連點這次是小遊戲腳本 某人要高潮了嗎 03/08 11:26
泳犬小遊戲?
roc074: 這跟去so-net沒一點關係,原po應該會去做影像辨識的公司吧 03/08 11:28
影像公司泊車小弟
bomda: 這就是外掛啊 03/08 11:38
paul31788: 推 03/08 11:42
CVTaihouKai: 第1行不就是寫查表器了 03/08 11:44
ptgeorge2: 我只是想打手槍,你給我看這個= = 03/08 12:11
frosagen2018: 我也是這麼想的 03/08 12:18
DanielHAO: 跟我想得差不多 03/08 12:18
x80307: 窩不知道 你再說蛇某 03/08 12:23
bightp36936: 現在玩公連這麼硬的嗎 03/08 12:44
enders346: 太強啦 03/08 12:47
kingoflag: 我需要git網址 為了研究 03/08 13:04
怎麼這麼多人知道git
js850604: 玩公連先學影像辨識 03/08 13:10
不用學,這沒用到理論
abjeffop: 嗯嗯 跟我當初想的一樣 謝謝付諸行動 03/08 13:17
he01023815: 嗯嗯跟我想的差不多 03/08 13:20
sismiku: IOTA凸了木有 03/08 13:29
sismiku: 發錢辣 03/08 13:29
凸了墓
joe1211: 這山小... 03/08 14:02
littlestar66: 嗯嗯 跟我當初想的一樣 03/08 14:16
powerkshs: 你的got hub勒 03/08 14:25
mikeneko: 講中文可以嗎 03/08 14:31
都蕊咪發搜
ruler76818: python。ai辨識 03/08 15:09
Raynor: source code 呢? (敲碗 03/08 15:25
站內信
NoLimination: 說人話 03/08 15:48
duncan1315: 請問有哪些教授有在做upscale的嗎?滿有興趣的 03/08 16:13
大佬跑錯板了
zax5566x2: 請問paper哪裡可以找到 03/08 16:15
dkchronos: 我需要網址,為了研究用途。03/08 17:03
asteea: ...我竟然看的懂 推有心去寫 又有心分享成果03/08 17:14
lionel20002: scale前可以把邊界剪掉 外面那一圈白框對辨識沒幫助03/08 17:43
感謝建議,但已達到可用標準,懶得再改
mose56789: 瑟瑟發抖03/08 17:47
taikobo: 有看有推03/08 18:17
b940315: 感覺會被當外掛 還好我打玩了03/08 19:17
to9527: 求github XD 03/09 00:03
KaiKaiGod: 私! 03/09 00:35
※ 編輯: gR7P4zXH (111.248.3.243 臺灣), 03/09/2020 03:20:04 ※ 編輯: gR7P4zXH (111.248.3.243 臺灣), 03/09/2020 03:30:37 ※ 編輯: gR7P4zXH (111.248.3.243 臺灣), 03/09/2020 03:57:56 ※ 編輯: gR7P4zXH (1.171.87.231 臺灣), 03/09/2020 04:49:11
y124421473: 你贏了... 03/09 07:58
MiRRorX: 果然等著就有神人,太猛了,求站內codeXD 03/09 08:41
kevin801117: 太神拉 要是我會還是懶得做 03/10 08:54
stocking0: 電腦白痴的我 連怎樣弄和用都不知道 03/10 11:07
eric910005: 傻眼 03/10 21:23