覺得小遊戲查表好麻煩,做了一個查表器
附上人權
![]()
# 實驗成果
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

得益於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 %)