Taiwan Train Verification Code 2 text ( 台鐵驗證碼轉文字 )
introduce
提供各位開發程式訂票,其中驗證碼破解部分的方法,可直接使用,不須再回傳 image 用人工方式辨識。
此 package 使用 keras & Tensorflow 建模&預測,需要安裝相依 package。
model test data 準確率約為 88%,使用 10 萬張圖進行 training。
pip3 install TaiwanTrainVerificationCode2text
cv2 比較難裝,以下提供安裝方法
conda install -c menpo opencv
# 你還需要這個
pip3 install h5py
demo
input
output
WNBA8S
exmaple
import os
import platform
from TaiwanTrainVerificationCode2text import verification_code2text
from TaiwanTrainVerificationCode2text import work_vcode
from TaiwanTrainVerificationCode2text import download
import TaiwanTrainVerificationCode2text
PATH = TaiwanTrainVerificationCode2text.__path__[0]
import cv2
import matplotlib.pyplot as plt
import random
from datetime import datetime
# 下載我 train 好的 weight,ttf 是驗證碼字形,用於以下生成模擬驗證碼
download.weight()
download.ttf()
# 生成模擬驗證碼
work_vcode.work_vcode_fun(10,'test_data',5)
work_vcode.work_vcode_fun(10,'test_data',6)
if 'linux' in platform.platform():
file_path = '{}/{}/'.format(PATH,'test_data')
train_image_path = [file_path + i for i in os.listdir(file_path+'/')]
else:
file_path = '{}\\{}\\'.format(PATH,'test_data')
train_image_path = [file_path + i for i in os.listdir(file_path+'\\')]
# 隨機取一個當作 demo
image_name = train_image_path[random.sample( range(len(train_image_path)) ,1)[0]]
# 讀取圖片
image = cv2.imread(image_name)
# 畫圖
plt.imshow(image)
# 辨識,驗證碼轉文字
text = verification_code2text.main(image)
# 印出最後結果
print(text)
最後結果就會類似 demo ,
如果想自己 train,可以使用
build_verification_code_cnn_model.py
稍微介紹主要 code
def main():
import work_vcode
#import time
# 因為台鐵驗整碼是 5~6 隨機,因此必須生成 5 碼驗證碼& 6 碼驗證碼
# 500 是 data 數量,建議數字為30000,500 只是 demo
work_vcode.work_vcode_fun(500,'train_data',5)
work_vcode.work_vcode_fun(500,'train_data',6)
# 生成 test data,可根據自己喜好調整 data 數量
work_vcode.work_vcode_fun(100,'test_data',5)
work_vcode.work_vcode_fun(100,'test_data',6)
self = build_verification_code_cnn_model()
# 建模,最後 weight 會存放在 package_path/cnn_weight/verificatioin_code.h5
self.build_model_process()
train 好後,可再使用以上 example code,會讀取你 train 好的 weight。
方法
由於驗證碼是26個英文字搭配10個數字,再加上隨機 5~6 碼,我將 class 分成 26 + 10 + null,37類,下去做分類。
null 代表沒有此文字, 我將 NN 結構中的 output 設計為 6 dimension,將5碼也看成6碼,只是最後一個是 null,藉此同時處理 5 or 6 碼問題。
future
面臨到最大的問題是,null 佔比例太大,以上面生成 data 的 code 為例
work_vcode.work_vcode_fun(500,'train_data',5)
work_vcode.work_vcode_fun(500,'train_data',6)
先不管英文&數字數量,有一半的 class 是 null,因此在分類問題上,會造成嚴重的 imbalance 問題,即使 data 增加也不會改善問題,未來將搭配 object detection,改善準確率。
如有問題,可寄信給我 or 留言在 issues。
email : linsam.tw.github@gmail.com
Metadata
Release files for TaiwanTrainVerificationCode2text 1.0.17
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| TaiwanTrainVerificationCode2text-1.0.17.tar.gz | 10.5 kB | Without provenance | Details for TaiwanTrainVerificationCode2text-1.0.17.tar.gz |
Release files / TaiwanTrainVerificationCode2text-1.0.17.tar.gz
| Download URL | TaiwanTrainVerificationCode2text-1.0.17.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
67b02a0a3c59c6ac7a992808a0817c6c72448af78722b22a3c17a4d0ecbd6c0e
|
|
BLAKE2b-256 checksum How to use checksums |
bf72e3489eac1291c71b0a3d68cc7c210a8285f725b4c97449ff2788d0c259ac
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0.post20200210 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6
|
Security for TaiwanTrainVerificationCode2text 1.0.17
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.