paddle:手把手教你训练自己的验证码识别模型

更新时间:2023-06-24 11:34:03 阅读:11 评论:0

paddle:⼿把⼿教你训练⾃⼰的验证码识别模型1、使⽤环境:
window10,6GGPU,NADIA GTX 1660SUPER,CUDA10.0.130,cudnn-10.0
2、使⽤代码:
官⽅提供的ocr模型代码
3、⽣成⾃⼰的数据集
import random
import os
from PIL import Image, ImageDraw, ImageFont
char_t ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
image_size =(128,32)
"""
基本:
1 图⽚size
2 字符个数
3 字符区域(重叠、等分)
4 字符位置(固定、随机)
5 字符size(所占区域⼤⼩的百分⽐)
6 字符fonts
7 字符 type (数字、字母、汉字、数学符号)
8 字符颜⾊
9 背景颜⾊
⾼级:
10 字符旋转
11 字符扭曲
12 噪⾳(点、线段、圈)
"""
def randRGB():
return random.randint(0,255), random.randint(0,255), random.randint(0,255)
def cha_draw(cha, text_color, font, rotate, size_cha):
im = w(mode='RGBA', size=(size_cha *2, size_cha *2))
drawer = ImageDraw.Draw(im)
鼻孔外露
<(xy=(0,0), text=cha, fill=text_color, font=font)# text 内容,fill 颜⾊, font 字体(包括⼤⼩)
if rotate:
max_angle =40# to be tuned
angle = random.randint(-max_angle, max_angle)
im = im.rotate(angle, Image.BILINEAR, expand=1)
im = im.bbox())
return im
def choice_cha(chas):
x = random.randint(0,len(chas))
return chas[x -1]
def captcha_draw(size_im, nb_cha, t_cha, fonts=None, overlap=0.0,
rd_bg_color=Fal, rd_text_color=Fal, rd_text_pos=Fal, rd_text_size=Fal,
rotate=Fal, noi=None, dir_path='', img_num=0, img_now=0):
"""
overlap: 字符之间区域可重叠百分⽐, 重叠效果和图⽚宽度字符宽度有关
字体⼤⼩⽬前长宽认为⼀致!!!离职图片
所有字⼤⼩⼀致
扭曲暂未实现
noi 可选:point, line , circle
noi 可选:point, line , circle
fonts 中分中⽂和英⽂字体
label全保存在 中,⽂件第i⾏对应"i.jpg"的图⽚标签,i从1开始
"""
rate_cha =0.8# rate to be tuned
width_im, height_im = size_im
width_cha =int(width_im /max(nb_cha - overlap,3))# 字符区域宽度
#    height_cha = height_im * 1.2  # 字符区域⾼度
height_cha = height_im *0.8# 字符区域⾼度
bg_color ='white'
text_color ='black'
derx =0
dery =0
if rd_text_size:
rate_cha = random.uniform(rate_cha -0.1, rate_cha +0.1)# to be tuned
size_cha =int(rate_cha *min(width_cha, height_cha)*2.0)# 字符⼤⼩
if rd_bg_color:
bg_color = randRGB()
im = w(mode='RGB', size=size_im, color=bg_color)# color 背景颜⾊,size 图⽚⼤⼩
drawer = ImageDraw.Draw(im)
contents =[]
for i in range(nb_cha):
if rd_text_color:
text_color = randRGB()
if rd_text_pos:
derx = random.randint(0,max(width_cha - size_cha -5,0))
dery = random.randint(0,max(height_cha - size_cha -5,0))
cha = random.choice(t_cha)
font = uetype(fonts['eng'], size_cha)
contents.append(cha)
im_cha = cha_draw(cha, text_color, font, rotate, size_cha)
im.paste(im_cha,(int(max(i - overlap,0)* width_cha)+ derx +2, dery +3), im_cha)# 字符左上⾓位置
if'point'in noi:
nb_point =20
color_point = randRGB()
for i in range(nb_point):
x = random.randint(0, width_im)
y = random.randint(0, height_im)
drawer.point(xy=(x, y), fill=color_point)
if'line'in noi:
nb_line =3
for i in range(nb_line):
color_line = randRGB()
肉末粥sx = random.randint(0, width_im)
sy = random.randint(0, height_im)
ex = random.randint(0, width_im)
ey = random.randint(0, height_im)
drawer.line(xy=(sx, sy, ex, ey), fill=color_line)
if'circle'in noi:
nb_circle =20
color_circle = randRGB()
for i in range(nb_circle):
sx = random.randint(0, width_im -10)
sy = random.randint(0, height_im -10)
temp = random.randint(1,5)
ex = sx + temp
ey = sy + temp
drawer.arc((sx, sy, ex, ey),0,360, fill=color_circle)
if ists(dir_path)==Fal:# 如果⽂件夹不存在,则创建对应的⽂件夹
os.mkdir(dir_path)
img_name =str(img_now)+'_'+''.join(contents)+'.jpg'
img_path = os.path.join(dir_path, img_name)
print(img_path,str(img_now)+'/'+str(img_num))
im.save(img_path)
def captcha_generator(ctc=Fal):
size_im = image_size  # (176, 25)
t_chas =[char_t]
if ctc:
nb_chas =[4,5,6,7,8]
el:
nb_chas =[4,5,6]
# ⽣成的图⽚数量
nb_image =10000
font_dir ='./fonts/'
北岛生活
rd_bg_color =Fal
overlaps =[0.0,0.1,0.2]
# overlaps = [0.2]
nois =[[],['point'],['line'],['line','point'],['circle']]
# nois = [[], ]
rd_text_poss =[Fal]# [True, Fal]
rd_text_sizes =[Fal]# [True, Fal]
rd_text_colors =[True,Fal]# fal 代表字体颜⾊全⼀致,但都是⿊⾊
# rd_text_colors = [Fal]  # fal 代表字体颜⾊全⼀致,但都是⿊⾊
rotates =[Fal]# [True, Fal]
font_paths =[]
for dirpath, dirnames, filenames in os.walk(font_dir):
for filename in filenames:
filepath = dirpath + os.p + filename
font_paths.append({'eng': filepath})
for i in range(nb_image):
overlap = random.choice(overlaps)
rd_text_pos = random.choice(rd_text_poss)
rd_text_size = random.choice(rd_text_sizes)
清炒小白菜的做法
rd_text_color = random.choice(rd_text_colors)
t_cha = random.choice(t_chas)
noi = random.choice(nois)
rotate = random.choice(rotates)
nb_cha = random.choice(nb_chas)
#            font_path = random.choice(font_paths)
font_path = font_paths[0]
if ctc:
dir_name ='ctc'
el:
dir_name ='test'
dir_path ='./img_data/'+ dir_name +'/'
captcha_draw(size_im=size_im, nb_cha=nb_cha, t_cha=t_cha,
overlap=overlap, rd_text_pos=rd_text_pos, rd_text_size=rd_text_size,
rd_text_color=rd_text_color, rd_bg_color=rd_bg_color, noi=noi,
rotate=rotate, dir_path=dir_path, fonts=font_path, img_num=nb_image, img_now=i) # def test():
挡不住的青春
#    print("test begining ------------------")
#    #    size_im = (100, 30)
#    size_im = (128, 32)
#    t_chas = [
#        "0123456789",
#        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
#    ]
#    overlaps = [0.8, 0.4, 0.6, 0.8, 0.4, 0.6, 0.5, 0.0, 0.2]
#    rd_text_poss = [Fal, True]
#    rd_text_sizes = [Fal, True]
#    rd_text_colors = [Fal, True]  # fal 代表字体颜⾊全⼀致,但都是⿊⾊
#    rd_bg_color = Fal
#
#    nois = [['point'], ['line'], ['line', 'point']]
#    # nois = []
#    rotates = [Fal]
#    nb_chas = [4]
#    nb_image = 100  # 1000 * 100
#    #    font_dir = '/usr/share/fonts/truetype/ubuntu-font-family'
#    #    font_dir = 'C:/Windows/Fonts/'
#    font_dir = './fonts/'
#    font_paths = []
#    num_pic = 0
#    dir_folder = 0
#
#    try:
#        for dirpath, dirnames, filenames in os.walk(font_dir):
#            print("test begining ---------0---------")
#            for filename in filenames:
#                filepath = dirpath + os.p + filename
#                font_paths.append({'eng': filepath})
#                print("font-------", filepath)
#
#            for i in range(nb_image):
#                print("test begining -----1-------------")
#                num_pic += 1
#                overlap = random.choice(overlaps)
#                rd_text_pos = random.choice(rd_text_poss)
#                rd_text_size = random.choice(rd_text_sizes)
#                rd_text_color = random.choice(rd_text_colors)
#                t_cha = random.choice(t_chas)
#                noi = random.choice(nois)
#                rotate = random.choice(rotates)
中国拳击
#                nb_cha = random.choice(nb_chas)
#                #                font_path = random.choice(font_paths)
#                font_path = font_paths[0]
#                if num_pic % 1001 == 0:
#                    dir_folder += 1
人中旁边有痣#                dir_name = 'train_data'
#                dir_path = './img_data/' + dir_name + '/'
#                captcha_draw(size_im=size_im, nb_cha=nb_cha, t_cha=t_cha,
#                              overlap=overlap, rd_text_pos=rd_text_pos, rd_text_size=rd_text_size,
#                              rd_text_color=rd_text_color, rd_bg_color=rd_bg_color, noi=noi,
#                              rotate=rotate, dir_path=dir_path, fonts=font_path, img_num=nb_image, img_now=i) #    except Exception:
#        print("io Exception--- ")
if __name__ =="__main__":
# test()
captcha_generator(True)
4、对⽣成的图⽚进⾏标注
import os
from PIL import Image
dict01 ={'0':'90','1':'91','2':'92','3':'93','4':'94','5':'95','6':'58','7':'59','8':'60','9':'61', 'a':'64',
'b':'65','c':'66','d':'67','e':'68','f':'69','g':'70','h':'71','i':'72','j':'73',
'k':'74',
'l':'75','m':'76','n':'77','o':'78','p':'79','q':'80','r':'81','s':'82','t':'83',

本文发布于:2023-06-24 11:34:03,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1028395.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字符   宽度   模型   区域   字体
相关文章
留言与评论(共有 0 条评论)
昵称:
匿名发表 登录账号
         
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图