栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Python

python生成带背景的字符图片(crnn数据集制作)

Python 更新时间:发布时间: 百科书网 趣学号
代码背景:

需求:识别汽车行驶证证芯号
图片示例:

解决方法:检测+识别(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:UserssoliDesktop1/%s.jpg" % i, photo)

输出结果:

提示:这里填写问题的分析:


转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/874870.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号