# Week 29: 驗證碼大亂鬥前篇
###### tags: `技術研討`
## 收集資料紀錄
檔名即答案_{亂碼}.png
紅字資料夾名稱暫定移除
| 資料夾名稱 | 來源 | 說明 | 資料量 | 大小(MB) | 上傳者| 圖片範例|
| ---- | ----- | ----- |------ | ------ | ------ | ------ |
| random_digit | 根元 | | 10525 | 385.9 | 信賢||
| 進出口廠商 | [進出口廠商基本資料查詢]( https://fbfh.trade.gov.tw/fb/web/queryBasicf.do) | | 3318| 21 | 信賢||
| 國民身分證 | [國民身分證領補換資料查詢作業](https://www.ris.gov.tw/app/portal/3014) | | 10528 | 1184 | 信賢||
| <font color=red>醫事<font> | [衛生部 醫事查詢系統](https://ma.mohw.gov.tw/masearch/) | pytesseract 辨識成功 | 2237 | 13.4 | 信賢||
| <font color=red>信保<font> | [信保基金](https://guar.smeg.org.tw/Login.aspx) | pytesseract 辨識成功 | 10410 | 7.26 | 信賢||
| 信保2 | [信保基金](https://guar.smeg.org.tw/Login.aspx) | 根元登入成功 (已驗證為 ground truth) | 14017 | 12.11 | 信賢 | |
| <font color=red>大安<font> | [大安運動中心](https://scr.cyc.org.tw/tp03.aspx?Module=login_page&files=login) | pytesseract 辨識成功 | 7810 | 12.5 | 信賢||
| <font color=red>emake5000<font> | [振興五倍券](https://emake.5000.gov.tw/hpg/webLogin?token=query&language=ch) | pytesseract 辨識成功、[疫苗預約平台](https://vas.1922.gov.tw/vas/?language=ch)也是一樣的驗證碼 | 5,014 | 59.6 | 沛筠 ||
| tnpl_captcha | [臺南市立圖書館](https://www.tnpl.tn.edu.tw/n14811877788362283118/Spread) | 點擊圖片後開啟的分頁有解答 | 8,151 | 92.5 | 沛筠 | |
| e-Learning | [人資系統](https://ehrd.esunbank.com.tw/ehrd/eHRDOrg) | 原始碼可以直接看到 ground truth src="CaptchaCtrl.ashx?Code=19<font color=red>0</font>7<font color=red>0</font>8<font color=red>9</font>1<font color=red>0</font>8"|10,001 |14.5| 昊中 ||
| 假資料 1 (color captcha) | 生成假資料 |[J-Rios/multicolorcaptcha](https://github.com/J-Rios/multicolorcaptcha)|10,800|42.3 MB| 昱睿 ||
| 假資料 2 (模擬台鐵) | 生成假資料 |[JasonLiTW/simple-railway-captcha-solver](https://github.com/JasonLiTW/simple-railway-captcha-solver)|11,180|43.6 MB| 昱睿 ||
### 其他清單
| 資料夾名稱 | 來源 | 說明 | 資料量 | 大小(MB) | 上傳者| 圖片範例|
| ---- | ----- | ----- |------ | ------ | ------ | ------ |
| wracb_captcha | [經濟部水利署中區水資源局](https://www.wracb.gov.tw/8831/8850/9330/) | 點擊圖片後開啟的分頁有解答 | 8,018 | 53.5 | 沛筠 | |
| frfa_captcha | [舊版農會轉寄好友](http://www.frfa.com.tw/WebMember/MailFriend.aspx?EpfJdId9UuC90KK%2bkfE76GU8PKmXYyQOtY3hCoeRdv9GMMuQLesqcDPFCjetk%2bWk) | 圖片網址後五碼 | 7,028 | 7.43 | 沛筠 | |
| chiefmail | [舊版新竹縣政府縣民信箱](https://chiefmail.hsinchu.gov.tw/swsfront35/SWSF/SWSF01003.aspx?FunId=SWSF08) | 點擊圖片後開啟的分頁有解答 | 10,075 | 15.3 | 沛筠 | |
| ntm_captcha | [國立臺灣博物館](https://www.ntm.gov.tw/forward) | 原始碼可以直接看到 ground truth (alt="請輸入驗證碼xxxx") | 10,000 | 11.2 | 沛筠 | |
### 待爬清單 / 歡迎認領 <font color=red>→ 目前暫停爬蟲</font>
* 隨時更新
* 蒐集完畢後手動將資訊加到上方總表
* 請注意命名方式和檔案類型
* 家珍說要**一萬張**
| 網站 | 是否有正解 / 在哪 | 認領者 | 進度 |
| -------- | -------- | -------- | -------- |
| [郵局中文地址英譯](https://www.post.gov.tw/post/internet/Postal/index.jsp?ID=207) | 新視窗內文字 | 家珍 | v |
| [稅籍登記資料公示查詢](https://www.etax.nat.gov.tw/etwmain/etw113w1/ban/query) | 統編輸入 86517510 持續查詢 | 信賢| v(用stt暫不加入)|
| [科技部意見信箱](https://www.most.gov.tw/petition/forgetPwd.jsp) | 新視窗內文字 |信賢 |沒答案 |
| [海管志工專區](http://marine.cpami.gov.tw/volunteer/index.php) | 新視窗內文字 | 昊中|v|
| [彰化縣政府身心障礙福利服務中心](https://dwsc.chcg.gov.tw/contact.php) | 新視窗內文字 | 昊中 |v|
| [高雄三民地政局](https://smland.kcg.gov.tw/suggestion.php?nid=639) | 點擊「顯示驗證碼」 |昊中 |v|
| [桃園市立圖書館](https://www.typl.gov.tw/form.asp?fpage=cp&spec=form.forward&xItem=230134&ctNode=1439&CtUnit=230&BaseDSD=7&mp=1&Captcha.ImageValidation=xkiPF) | 新視窗內文字 | 沛筠| v |
| [新北市停車費用查詢](https://parkweb.traffic.ntpc.gov.tw/) | 新視窗內文字 | 沛筠| v |
| [高雄市立美術館](https://www.kmfa.gov.tw/ArtActivityInfo/Login.aspx) | 點擊「聽取驗證碼」 | 沛筠| v |
| [舊花蓮縣長信箱](http://www1.hl.gov.tw/mailbox/Mail.asp) | 新視窗內文字 | 家珍 | v|
### Clova Pretrained Model Predict 資料分配
* 2021/11/22 會後通過的訓練集
* ~~之後將分為 case sensitive 和 case insensitive 兩類~~
* 2021/11/29 先排除 **稅籍登記資料公示查詢**
* 於研發雲上的資料夾為 <font color=red>training_images</font>
* 2021/12/06 已確認所有檔名符合命名規則 + 補傳郵局驗證碼
| 網站/資料夾名稱 | 負責人 | 張數 |正確率|case sensitive
| -------- | -------- | -------- | -------- | -------- |
| random_digits_images | 信賢 | | 0.99427 | |
| 進出口廠商 | 信賢 | | 1 | |
| 身份證 | | | 0.99382 | |
| 信保2 | | | 0.99894| |
| 臺南市立圖書館 / tnpl_captcha | 沛筠 | | 0.98722 | |
| e-Learning/elearning_captcha | 昊中 | | 0.99852 |X |
| 假資料 1/colorful_captcha_images | | | 0.90549 | |
| 假資料 2/railway_captcha_images | | | 0.79100 | |
| 郵局中文地址英譯/postmail_captcha | 家珍 | | 0.99897 | |
| 海管志工專區/海管志工專區_captcha | 昊中 | |1 |X |
| 彰化縣政府身心障礙福利服務中心/彰化縣政府身心障礙福利服務中心_captcha | 昊中 | | 1 |X |
| 高雄三民地政局/高雄三民地政局_captcha | 昊中 | | 0.93904 |X |
| 桃園市立圖書館 / typl_captcha | 沛筠 | | 0.99845 | |
| 新北市停車費用查詢 / parkweb | 沛筠 | |0.98972 | |
| 高雄市立美術館 / kmfa_captcha | 沛筠 | | 0.99287 | |
| 舊花蓮縣長信箱 / hualien | 家珍 | | 0.99638 | |
| 經濟部水利署中區水資源局 / wracb_captcha | 沛筠 | | 0.98947 | |
| 舊版農會轉寄好友 / frfa_captcha | 沛筠 | | 0.99858 | |
| 舊版新竹縣政府縣民信箱 / chiefmail | 沛筠 | | 0.97851 | |
| 國立臺灣博物館 / ntm_captcha | 沛筠 | | 0.99096 | |
* 找新網站驗證模型(湊到38000張):
1. 稅籍
2. 高鐵 -> 難爬
3. 大安(運動中心)
4. 臺大醫院...
5. ???
於研發雲上的資料夾為 <font color=red>testing</font>,進度 5/7
| 網站 / 資料夾名稱 | 是否有正解 / 在哪 | 進度 |成效
| -------- | -------- | -------- |-----
| [內政部少年之家](https://adh.mohw.gov.tw/epaper_no.asp) / adh_mohw | 新視窗內文字 | v |
| [TTQS 人才發展品質管理系統](https://ttqs.wda.gov.tw/ttqs/menu_0011.php) / ttqs | 音檔網址 (需另開視窗) | v |
| [南強工商](http://www.ncvs.ntpc.edu.tw/contact.php) / ncvs | 新視窗內文字 | v |
| [國軍高雄總醫院左營分院](https://806.mnd.gov.tw/index.php?page=inquire_search) / zuoying | 新視窗內文字 | v |
| [地方產業整合發展計畫](https://www.srido.org.tw/ContactApplys?ModuleID)| 新視窗內文字 | |
| [新店國民運動中心](http://www.xdsports.com.tw/zh_TW/contact)| 音檔 script 標籤裡||
| [農航所影像快照碼查詢](https://image.afasi.gov.tw/ashx/permacode/permacode.aspx) / image_afasi| 圖片網址後 4 碼 / 和大安運動中心一致的產出邏輯| v |
* **打包成套件可以import**
* **github readme**
1. 有訓練過的網站提供範例圖片
2. 資料表現如何,給使用者建議
* **12/27**
1. 改Clova Inference Code (信賢)
2. 階段性先上,開新repo,信賢撰寫使用規則。
3. 下一步將新的驗證碼加上去重train。
4. 學員使用回饋,看需不需要分模型。
<!-- #### 不同的案例
需要計算的場景:[金融研訓院](https://www.tabf.org.tw/Organization/Login.aspx) -->
## 關於爬蟲
如果沒有帳密就可能沒有 ground truth,要先 train 一版(懇求大家提供有 ground truth 的資料)
- selenium 套件介紹:
Selenium 是為瀏覽器自動化(Browser Automation)需求所設計的一套工具集合,讓程式可以直接驅動瀏覽器進行各種網站操作。
- selenium 怎麼啟動瀏覽器
- [chromedriver 下載](https://chromedriver.chromium.org/)
- 不囉嗦程式碼給你看
``` python=
from selenium import webdriver
chrome = webdriver.Chrome('./chromedriver')
chrome.get("https://www.facebook.com")
```
- 奇技淫巧
[配置 chrome 參數](https://zhuanlan.zhihu.com/p/60852696)
``` python=
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
# 背景執行
chrome_options.add_argument('--headless
# 谷歌文檔提到需要加上這個屬性來規避bug
chrome_options.add_argument('--disable-gpu')
# 為了在登入Facebook後,避免跳出訊息視窗,阻礙之後的自動化執行,可以引用chrome模組底下的Options類別,設定不啟用通知
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-default-apps")
# 避免 Page Crash 問題
chrome_options.add_argument("--disable-dev-shm-usage")
```
- selenium 怎麼找你要的東西
- find_element_by_id
- find_element_by_name
- find_element_by_class_name
- find_element_by_link_text
- find_element_by_xpath

- 信保舉例
``` python=
# 定義驅動程式
driver = webdriver.Chrome(executable_path, options=chrome_options)
# 到網頁
driver.get('https://guar.smeg.org.tw/Login.aspx')
# 按掉跳出問題
driver.switch_to_alert().accept()
# 抓驗證碼
img_ele = driver.find_element_by_xpath('//*[@id="imgCode"]')
s = io.BytesIO(img_ele.screenshot_as_png)
image = Image.open(s)
# 用 pytesseract 辨識
result = pytesseract.image_to_string(image, config=r'--psm 7 -c tessedit_char_whitelist=' + string.ascii_lowercase + string.digits + string.ascii_lowercase)
```
- 加碼區
看到這邊不知道人臉組會不會覺得很無聊,沒關係!
這邊額外分享一個 IG 爬蟲,可以爬很多帥勾水水
[github 程式碼](https://github.com/erik1110/face-transformation/blob/main/CollectData/instagram_downloader_public.py)
有滾動卷軸無窮盡的解法
``` python=
last_height = driver.execute_script("return document.body.scrollHeight")
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
new_height = driver.execute_script("return document.body.scrollHeight")
# Calculate new scroll height and compare with last scroll height
if new_height == last_height:
driver.execute_script("window.scrollTo(document.body.scrollHeight,0);")
break
```
最後成品

## 關於模型
v1: 訓練集共 40,579 張、測試集共 1,080 張
v2: 訓練集共 76,148 張、測試集共 1,080 張
#### 模型成效 (v1)
| Model | Training Acc | Testing Acc |inferencetime|
| -------- | -------- | -------- |-|
| Clova |0.9195|0.9046|0.06 秒|
| Focus | - | 0.9630 |0.4 秒|
| CRNN | 0.8 左右 | - ||
#### 模型成效 (v2)
| Model | Training Acc | Testing Acc |inferencetime|
| -------- | -------- | -------- |-|
| Focus | - | 0.8139 |0.4 秒|
### Focus 錯誤的圖片



## 未來展望
os:一個月後的展望
- 原本網路上抓的 ground truth 有問題,因此我們先 training 一版模型將有問題的資料作剔除。預計下一次重新 train 一版,並加上 data argumentation。
- 請大家可以提供想破解的網站驗證碼(僅限用OCR,不包含需要運算)。
https://www.tabf.org.tw/Organization/Login.aspx
- 打包成套件,給 RPA track 使用
## 許願區
* [高鐵搶票](https://irs.thsrc.com.tw/IMINT/?locale=tw&_ga=2.7226992.396262615.1634188888-355616240.1634188888)
<!-- #### 各模型指標
| Model | Accuracy | Inference(s) | Model Size (MB)| GPU memory usage(MiB) | Training Time | epoch / iteration |
| -------- | -------- | -------- | -------- | -------- |-------- |-------- |
| ViTSTR | 0.9002 | 0.0192 | 326 | 1451 |||
| SATRN(training) | 0.9402 |N/A| 1052 | 8601 |5 mins/200 iters|60000 iterations|
|MASTER(160, 48)|0.7960|0.4154|619|15779|1~2 step / s|120 epochs|
|MASTER(240, 48)|0.9465|0.4025|619|15779|1~2 step / s|99 epochs|
| PaddleOCR (3, 32, 100)| 0.8711 | 0.0173 | 299 | 7763 | 5 s /iter|epoch:139 <br> iteration: 130000|
| PaddleOCR (3, 32, 256)| 0.9573 | 0.02~0.03(GPU)<br>0.25 (CPU) | 299 | 1000左右 | 5 s /iter||
| attention_ocr | 0.9267 |0.835 <br/>(奇怪怎麼這麼久,可是我改 batch inference 也是差不多 0.88)|2|2469<br/>(我 session fconfig 設定 10%)|1 ~ 2 sec / step|118304<br/>(最後到 115076,但 118304 表現最好)| -->