设计思路
前言
国家企业信用信息公示系统中的验证码是按语序点击汉字,如下图所示:
即,如果依次点击:‘无’,‘意’,‘中’,‘发’,‘现’,就会通过验证。
本项目的破解思路主要分为以下步骤:
- 使用目标探测网络YOLOV2进行汉字定位
- 设计算法进行汉字切割
- 使用darknet的分类器进行汉字识别
- 设计算法进行汉字纠错与语序识别
汉字定位与汉字识别
本项目的汉字定位和汉字识别部分都是基于darknet
框架进行训练的。本项目对它们使用的训练网络并没有太高要求,只需懂得如何使用darknet就可以了,关于如何使用darknet框架训练汉字定位模型和汉字识别模型可查阅模型训练文档以及官方文档的YOLO和Train a Classifier部分。那么,下面主要对汉字切割和语序识别进行讲解,最后再对整个破解程序进行讲解。
汉字切割算法
def seg_one_img(img_path, rets):
img = cv2.imread(img_path)
hanzi_list = [] # 用于记录每个汉字对应的坐标:key为切割后汉字图片路径,value为中心点坐标
# 对定位框进行遍历
for ret in rets:
per_dict = {
}
if ret[1] > 0.5: # 只取置信度大于0.5的定位框
coordinate = ret[2] # ret[2]为定位器返回的归一化坐标(x,y,w,h)
center = (int(coordinate[0]*344), int(coordinate[1]*384)) #汉字定位框中心点坐标
origin = (coordinate[0] - coordinate[2]/2,
coordinate[1] - coordinate[3]/2) # 汉字定位框左上角坐标(归一化)
# 将定位框向四周均匀扩大2个像素,尽量将整个汉字切割下来。
x = int(origin[0]*344 - 2)
x_plus_w =int((origin[0] + coordinate[2])*344 + 4)
y = int(origin[1]*384 - 2)
y_plus_h = int((origin[1] + coordinate[3])*384 + 4)
# 扩大后的定位框可能会出现越界的可能,如一个紧挨着图片边缘的汉字,fix函数调整越界的定位框
x, y, x_plus_w, y_plus_h = fix(x,y,x_plus_w,y_plus_h)
# 下面对图片进行切割,并保存
try:
hanzi_img = img[y:y_plus_h, x:x_plus_w] # 切割
normal_img = cv2.resize(hanzi_img, (65,65),
interpolation=cv2.INTER_CUBIC) # 将截取的图片规范化为65*65*3
path = 'hanzi_img/{
}_label.jpg.format(timestamp())
cv2.imwrite(path, normal_img)
per_dict[path] = center
hanzi_list.append(per_dict)
except:
print('#'*20)
print('存在不规则的图片')
return hanzi_list
# 修正定位框的坐标,如果扩大后的定位框越界则将其设置为边界坐标
def fix(x, y, x_plus_w, y_plus_h ):
x = 0 if x < 0 else x
y = 0 if y < 0 else y
x_plus_w = 384 if x_plus_w > 384 else x_plus_w
y_plus_h = 344 if y_plus_h > 344 else y_plus_h
return x, y, x_plus_w, y_plus_h
seg_one_img
函数是对一张验证码图片进行汉字切割,切割后的汉字图片保存在当前路径下的hanzi_img
文件夹中,并且返回由字典(key为汉字图片路径,value为坐标)组成的列表。需要注意的是,定位接口返回的定位框信息均是归一化信息,需要转换成实际的坐标信息,验证码图片大小信息为:344 × 384 × 3。如(0.25,,75)>> (0.25×344,0.75×384)
算法大体思路:
切割一张图片(图片路径,定位接口返回的定位框信息):
遍历定位框信息,对置信度大于0.5的定位框进行如下操作:
计算汉字定位框中心坐标和左上角坐标;
将汉字定位框向四周均匀扩大两个像素;
对越界的坐标进行修正;
对汉字进行切割;
定位框向四周扩大两个像素的目的:尽量将整个汉字切割下来。因为经过测试,有些定位框定位正确但是IOU不是很高,即汉字的某一小部分可能在定位框外部。扩大定位框可以更好的用于后面的汉字识别。
语序识别算法
语序识别算法结合了使用结巴分词识别语序和使用搜索引擎识别语序两个函数,下面分别对两个函数进行讲解。
使用结巴分词识别语序
本部分使用的是 Python 中文分词词库jieba
,关于结巴分词的基础知识请先阅读结巴分词Github文档,下面对使用结巴分词识别语序进行讲解。
# 结巴分词 识别语序
def recog_order_jieba(str):
l = len(str) # l表示输入字符串个数
word_list = _permutation(str) # 获得该字符串的所有排列方式
possible_words = [] # 用来存放语序可能正确的词
for word in word_list: # 编列所有排列方式
seg_list = jieba.lcut(word, cut_all=True ) # 对某一种排列方式使用结巴分词
index = find_longest(seg_list) # 寻找结巴分词返回的列表中字符串最长的索引,并返回
if len(seg_list[index]) == l: # 若最长的字符串与输入的字符串长度相同,则加入可能正确列表
possible_words.append(seg_list[index])
if len(possible_words) ==1: # 遍历完后,若可能正确的列表只有一个元素,那么他就是正确的,返回
return possible_words[0]
elif len(possible_words) >1: # 若有可能正确列表中若有多个元素,则选取词频高的返回
return highest_frequency(possible_words)
else: # 如果可能正确的列表元素为0,则返回0
return 0
# 获得汉字的所有排列方式
def _permutation(str, r = None):
word_list = list(permutations(str, r))
for i in range(len(word_list)):
word_list[i] = ''.join(word_list[i])
return word_list
# 寻找列表中最长的词
def find_longest(list):
l = 0
index = 0
for i,word in enumerate(list):
if len(word) > l:
l = len(word)
index = i
return index
# 输入词列表,返回结巴分词内词频最高的词
def highest_frequency(possible_words):
word_dict = file2dict('dict.txt')
possible_dict = {
}
for possible_word in possible_words:
possible_dict[word_dict[possible_word]] = possible_word
sorted = sortedDictValues(possible_dict)
print(sortedList)
return sortedList[-1][1]
# 对输入的字典根据key大小排序
def sortedDictValues(di):
return [(k,di[k]) for k in sorted(di.keys())]
# 将文件数据转换为字典
def file2dict(filename):
with open(filename) as f:
array_lines = f.readlines()
returnDict = {
}
# 以下三行解析文件数据到列表
for line in array_lines:
line = line.strip()
listFromLine = line.split()
returnDict[listFromLine[0]] = int(listFromLine[1])
return returnDict
下面我通过一个具体的实例来讲解算法思路:
输入:‘到马功成’
- 获得字符串长度:
l=4
- 获得字符串的全排列
['到马功成', '到马成功', '到功马成', '到功成马', '到成马功', '到成功马', '马到功成', '马到成功', '马功到成', '马功成到', '马成到功', '马成功到', '功到马成', '功到成马', '功马到成', '功马成到', '功成到马', '功成马到', '成到马功', '成到功马', '成马到功', '成马功到', '成功到马', '成功马到']
- 对每一个排列进行结巴分词,并打印其中字符串最长元素的索引
['到', '马', '功', '成']
0
['到', '马', '成功']
2
['到', '功', '马', '成']
0
['到', '功', '成', '马']
0
['到', '成', '马', '功']
0
['到', '成功', '马']
1
['马到功成']
0
['马到成功', '成功']
0
['马', '功', '到', '成']
0
['马', '功', '成', '到']
0
['马', '成', '到', '功']
0
['马', '成功', '到']
1
['功', '到', '马', '成']
0
['功', '到', '成', '马']
0
['功', '马', '到', '成']
0
['功', '马', '成', '到']
0
['功', '成', '到', '马']
0
['功', '成', '马', '到']
0
['成', '到', '马', '功']
0
['成', '到', '功', '马']
0
['成', '马', '到',