当前位置:网站首页>OCR/STR生僻字数据集生成 | PaddleOCR的垂类Fine-tune(1)
OCR/STR生僻字数据集生成 | PaddleOCR的垂类Fine-tune(1)
2022-07-20 11:58:00 【訢詡】
根据字库生成一个图片,图片里面约10多个字,白底黑字,字号大约6号:
大概长这个样子
首先找个字库再说:https://github.com/elephantnose/characters
导入依赖包
import cv2
from PIL import ImageFont,ImageDraw,Image
import numpy as np
import uuid
import os
import random
import math
配置函数,画图函数,生成函数
def config():
with open("word_dict2.txt") as f:
word = f.readlines()
word_dict_length = len(word)#字库字数
fontPath = "PingFang Heavy.ttf"#黑体
save_dir = "gen/images"
min_words_length = 9
max_words_length = 13
img_count = 10
return word,fontPath,save_dir,min_words_length,max_words_length,img_count
def draw_to_image(size,fontPath,font_size,text,save_dir,file_name):
background = np.ones(size)*255
font = ImageFont.truetype(fontPath,font_size)
img = Image.fromarray(background)
draw = ImageDraw.Draw(img)
draw.text((1,1),text,font=font,fill='black')
img = np.array(img)
out_path = os.path.join(save_dir,file_name)
cv2.imwrite(out_path,img)
def generate_words(word,count,length_range_min,length_range_max):
line_words_list = []
line_words = ""
globe_word_dict_index = 0
for k in range(count+1):
bit_length = np.random.randint(length_range_min,length_range_max)
for i in range(bit_length+1):
try:
line_words = line_words + word[globe_word_dict_index].split("\n")[0]
except:
globe_word_dict_index = 0
line_words = line_words + word[globe_word_dict_index].split("\n")[0]
globe_word_dict_index+=1
line_words_list.append(line_words)
line_words = ""
return line_words_list
调用
word,fontPath,save_dir,min_words_length,max_words_length,img_count = config()
line_words_list = generate_words(word,img_count,min_words_length,max_words_length)
for line in line_words_list:
length = len(line)
font_size = 33 if np.random.randint(0,2)==0 else 20
file_name = str(uuid.uuid4())+".jpg"
if(font_size==33):
size = (42,math.ceil(length*34.4))
draw_to_image(size,fontPath,font_size,line,save_dir,file_name)
else:
size = (29,math.ceil(length*20.8))
draw_to_image(size,fontPath,font_size,line,save_dir,file_name)
with open("label","a") as f:
f.write("images/"+file_name+"\t"+line+"\n")
最后这段是写出图像文件的同时把文件位置,文件名以及文字标签追加到本地文档
格式是PaddleOCR自定义数据集格式:
" 图像文件名 图像标注信息 "
train_data/rec/train/word_001.jpg 简单可依赖
train_data/rec/train/word_002.jpg 用科技让复杂的世界更简单
...
边栏推荐
- Open source remote desktop software rustdesk
- C and pointer Chapter 1 lexical "trap" 1.3 "greedy method" in lexical analysis
- IO advanced advanced IO
- vim加密文件「建议收藏」
- Technical scheme of video access AI platform
- HCIP --- BGP --- 边界网关协议
- Tiflash source code reading (V) deltatree storage engine design and implementation analysis - Part 2
- 300 题: 第五讲 特征值与特征向量
- 年度重磅!华为云2021应用构建技术实践精选集,七大领域400页+云上开发宝典 | 云享·书库 No.02 期推荐(附免费下载)
- Kusionstack open source | exploration and practice of kusion model library and tool chain
猜你喜欢
随机推荐
精品方案|海泰云密码应用服务解决方案 打造安全合规的云上应用
R语言epiDisplay包的kap函数计算配对列联表的计算一致性的比例以及Kappa统计量的值(总一致性、期望一致性)、使用xtabs函数生成二维列联表、使用wttable参数设置权重表参数为w2
G024-DB-GS-INS-01 OpenEuler 部署 OpenGauss(单实例)
[IOT] Product Manager: the underlying logic of human insight
你必须知道的4种 Redis 集群方案及优缺点对比
利用wget下载文件和网页
Leetcode 437 path sum 3
Tiflash source code reading (V) deltatree storage engine design and implementation analysis - Part 2
Peripherals: SD card and other external memory devices
How is agile tested in the new PMP Exam?
C traps and defects Chapter 2 syntax "traps" 2.5 function calls
How to realize distributed ID under quarkus
推荐一个 WordPress 付费主题站
OSPF comprehensive experiment
web页面的点对点复制粘贴
ZCMU--5150: gsy 的仓库盲跑(C语言)
云原生平台,让边缘应用玩出花!
Director of Shanghai Bureau of culture and Tourism: safety is the lifeline of culture and tourism, and we are seizing the new track of yuancosmos
LeetCode#572 另一颗树的子树
绿色低碳天翼云,数字经济新引擎!