pythonを使用して認証コード識別を自動的に行います.しかし、正確には修正を手伝ってくれる大物が必要です.ありがとうございます.
4864 ワード
この文章は転載するhttps://blog.csdn.net/weixin_38641983/article/details/80899354#commentseditここでとても感谢して、原文の作者は考えとコードを分かち合って、私は転载して、もし不当なところがあれば、私に连络して削除します.
上には原作者の考え方と過程がつながっている.自分で少し修正しました.しかし、まだ問題があります.
テストは50回のみで、正しく検証されたのは1回のみです.修正をお愿いします、ありがとうございます!!!
上には原作者の考え方と過程がつながっている.自分で少し修正しました.しかし、まだ問題があります.
import requests
import time
from io import BytesIO
from PIL import Image
import os
import numpy as np
#
CAPT_URL = "http://my.cnki.net/elibregister/CheckCode.aspx"
#
CAPT_PATH = "capt/"
if not os.path.exists(CAPT_PATH):
os.mkdir(CAPT_PATH)
# "lookup table"
THRESHOLD = 165
LUT = [0]*THRESHOLD + [1]*(256 - THRESHOLD)
#
def capt_fetch():
"""
, Image
:require requests: import requests
:require time: import time
:require BytesIO: from io import BytesIO
:require Image: from PIL import Image
:param:
:return capt: Image
"""
#
capt_raw = requests.get(CAPT_URL)
# IO
f = BytesIO(capt_raw.content)
# Image
capt = Image.open(f)
return capt
#
def capt_download():
"""
Image
:require Image: from PIL import Image
:require os: import os
:require capt_fetch(): nbsc
:require CAPT_PATH:
:param:
:return:
"""
capt = capt_fetch()
capt.show()
text = input(" :")
suffix = str(int(time.time() * 1e3))
capt.save(CAPT_PATH + text + "_" + suffix + ".jpg")
#
def capt_process(capt):
"""
: ,
:require Image: from PIL import Image
:require LUT: A lookup table, 256
:param capt: Image
:return capt_per_char_list: ,
"""
capt_gray = capt.convert("L")
capt_bw = capt_gray.point(LUT, "1")
capt_per_char_list = []
for i in range(4):
x = 5 + i * 15
y = 2
capt_per_char = capt_bw.crop((x, y, x + 15, y + 18))
capt_per_char_list.append(capt_per_char)
return capt_per_char_list
#
def capt_inference(capt_per_char):
"""
:require numpy: import numpy as np
:param capt_per_char:
:return char_features: , capt_per_char
"""
char_array = np.array(capt_per_char)
total_pixels = np.sum(char_array)
cols_pixels = np.sum(char_array, 0)
rows_pixels = np.sum(char_array, 1)
char_features = np.append(cols_pixels, rows_pixels)
char_features = np.append(total_pixels, char_features)
return char_features.tolist()
#
def train():
"""
:require Image: from PIL import Image
:require os: import os
:require capt_process():
:require capt_inference():
:param:
:return train_table:
:return train_labels:
"""
files = os.listdir(CAPT_PATH)
train_table = []
train_labels = []
for f in files:
train_labels += list(f.split("_")[0])
capt = Image.open(CAPT_PATH + f)
capt_per_char_list = capt_process(capt)
for capt_per_char in capt_per_char_list:
char_features = capt_inference(capt_per_char)
train_table.append(char_features)
return train_table, train_labels
#
def nnc(train_table, test_vec, train_labels):
"""
Nearest Neighbour Classification( ),
,
:require numpy: import numpy as np
:param train_table:
:param test_vec: ,
:param labels:
:return :
"""
dist_mat = np.square(np.subtract(train_table, test_vec))
dist_vec = np.sum(dist_mat, axis = 1)
pos = np.argmin(dist_vec)
return train_labels[pos]
#
def test():
"""
:require Image: from PIL import Image
:require capt_fetch(): nbsc
:require capt_process():
:require capt_inference():
:train_table, train_labels: train_table, train_labels = train()
:param:
:return capt:
:return test_labels:
"""
test_labels = []
capt = capt_fetch()
capt_per_char_list = capt_process(capt)
for capt_per_char in capt_per_char_list:
char_features = capt_inference(capt_per_char)
label = nnc(train_table, char_features, train_labels)
test_labels.append(label)
test_labels = "".join(test_labels)
return capt, test_labels
# ,
# 120
for i in range(1):
capt_download()
#
train_table, train_labels = train()
test_capt, test_labels = test()
print(test_labels)
テストは50回のみで、正しく検証されたのは1回のみです.修正をお愿いします、ありがとうございます!!!