代码背景:
需求:识别汽车行驶证证芯号
图片示例:
解决方法:检测+识别(dbnet+crnn)
数据来源:真实图片+生成模拟图片
真实证芯号:
代码生成证芯号:
前期准备:
生成字符图片的背景图片
代码
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import re
from th import *
# 背景图片路径
background = 'background'
imagepath = os.listdir(background)
# 生成字符字体
font_list = ['msyh.ttc', 'Dengb.ttf', 'msyh.ttc', 'arial.ttf']
for i in range(0, 100):
font = ImageFont.truetype(random.choice(font_list), 21)
photo = Image.open(background + os.sep + (random.choice(imagepath)))
w, h = photo.size
add_number = ImageDraw.Draw(photo) # 创建一个操作的对象
# 设置需生成的字符
text = random.sample('0123456789X0123456789', 13)
text = ''.join(text)
text = re.sub(r"(?<=\w)(?=(?:\w)+$)", " ", text)
add_number.text((int(w / 15), int(h / 2) - 10), u'*', font=ImageFont.truetype("C:\\Windows\\Fonts\\simsun.ttc", 21),
fill='black')
add_number.text((int(w / 15) + 23, int(h / 2) - 12), u'%s' % ''.join(text), font=font, fill='black')
add_number.text((int(w / 15) + 16 * 21 - 2, int(h / 2) - 10), u'*',
font=ImageFont.truetype("C:\\Windows\\Fonts\\simsun.ttc", 21), fill='black')
# 图片变换处理
photo = np.asarray(photo, order='F')
photo = cv2.cvtColor(photo, cv2.COLOR_RGB2BGR)
# photo=random_op(photo, op=random.randint(1, 3))
cv2.imwrite(r"C:\Users\soli\Desktop\1/%s.jpg" % i, photo)
输出结果:
提示:这里填写问题的分析: