1 Star 1 Fork 2

刘小虎/generateCarPlate

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
genCarPlate.py 3.97 KB
一键复制 编辑 原始数据 按行查看 历史
刘小虎 提交于 2020-02-05 22:29 . 修改为python2.7可用。
#coding=utf-8
import argparse
from PIL import ImageFont
from PlateCommon import *
import PlateCommon
TEMPLATE_IMAGE = "./template/template.bmp"
class GenPlateScene:
def __init__(self, fontCh, fontEng, NoPlates):
self.fontC = ImageFont.truetype(fontCh, 43, 0) # 省简称使用字体
self.fontE = ImageFont.truetype(fontEng, 60, 0) # 字母数字字体
# self.img = np.array(Image.new("RGB", (226, 70), (255, 255, 255)))
# self.bg = cv2.resize(cv2.imread(TEMPLATE_IMAGE), (226, 70))
self.img = np.array(Image.new("RGB", (226, 70), (255, 255, 255)))
self.bg = cv2.resize(cv2.imread(TEMPLATE_IMAGE), (226, 70))
self.noplates_path = []
for parent, _, filenames in os.walk(NoPlates):
for filename in filenames:
self.noplates_path.append(parent + "/" + filename)
def gen_plate_string(self, iter, perSize):
plate_str = ""
i = iter // perSize
# iterChar = (iter % perSize) // 9
for cpos in range(7):
if cpos == 0:
plate_str += chars[i] #+r(31)]
elif cpos == 1:
plate_str += chars[41 + r(24)]
else:
plate_str += chars[31 + r(34)]
return plate_str
def draw(self, val):
offset= 2
self.img[0:70, offset+8:offset+8+23] = GenCh(self.fontC, val[0])
self.img[0:70, offset+8+23+6:offset+8+23+6+23] = GenCh1(self.fontE, val[1])
for i in range(5):
base = offset+8+23+6+23+17+i*23+i*6
self.img[0:70, base:base+23]= GenCh1(self.fontE, val[i+2])
return self.img
def generate(self,text):
print(text + " " + str(len(text)))
fg = self.draw(text) # 得到白底黑字
# cv2.imwrite('01.jpg', fg)
fg = cv2.bitwise_not(fg) # 得到黑底白字
# cv2.imwrite('02.jpg', fg)
com = cv2.bitwise_or(fg, self.bg) # 字放到(蓝色)车牌背景中
# cv2.imwrite('03.jpg', com)
com = rot(com, r(20)-10, com.shape, 10) # 矩形-->平行四边形
# cv2.imwrite('04.jpg', com)
com = rotRandrom(com, 5, (com.shape[1], com.shape[0])) # 旋转
# cv2.imwrite('05.jpg', com)
com = tfactor(com) # 调灰度
# cv2.imwrite('06.jpg', com)
com, loc = random_scene(com, self.noplates_path) # 放入背景中
if com is None or loc is None:
return None, None
# cv2.imwrite('07.jpg', com)
com = AddGauss(com, 0) # 加高斯平滑
# cv2.imwrite('08.jpg', com)
com = addNoise(com) # 加噪声
# cv2.imwrite('09.jpg', com)
return com, loc
def gen_batch(self, perSize, outDir):
for i in range(perSize*31-1):
outputPath = outDir# + str(i // perSize) + "/"
if (not os.path.exists(outputPath)):
os.mkdir(outputPath)
plate_str = self.gen_plate_string(i, perSize)
img, loc = self.generate(plate_str)
if img is None:
continue
img_name = ''
for k in range(0, len(plate_str)):
img_name += str(PlateCommon.index[plate_str[k]]) + ' '
cv2.imwrite(outputPath + "/" + img_name + ".jpg", img)
#with open(outputPath + "/" + plate_str + ".txt", 'w') as obj:
# line = ','.join([str(v) for v in loc]) + ',"' + plate_str + '"\n'
# obj.write(line)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('gen_count_per_province', type=int, help='gen car plt num for per_province')
parser.add_argument('--bg_dir', default='./Background', help='bg_img dir')
parser.add_argument('--out_dir', default='./gen_res/', help='output dir')
return parser.parse_args()
def main(args):
#print args
G = GenPlateScene("./font/platech.ttf", './font/platechar.ttf', args.bg_dir)
G.gen_batch(args.gen_count_per_province, args.out_dir)
if __name__ == '__main__':
main(parse_args())
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/popkx/generateCarPlate.git
git@gitee.com:popkx/generateCarPlate.git
popkx
generateCarPlate
generateCarPlate
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385