mirror of
https://github.com/we0091234/crnn_plate_recognition.git
synced 2025-09-26 23:45:51 +08:00
modify
This commit is contained in:
99
LPRNet.py
Normal file
99
LPRNet.py
Normal file
@@ -0,0 +1,99 @@
|
||||
import torch.nn as nn
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
|
||||
class small_basic_block(nn.Module):
|
||||
def __init__(self, ch_in, ch_out):
|
||||
super(small_basic_block, self).__init__()
|
||||
self.block = nn.Sequential(
|
||||
nn.Conv2d(ch_in, ch_out // 4, kernel_size=1),
|
||||
nn.ReLU(),
|
||||
nn.Conv2d(ch_out // 4, ch_out // 4, kernel_size=(3, 1), padding=(1, 0)),
|
||||
nn.ReLU(),
|
||||
nn.Conv2d(ch_out // 4, ch_out // 4, kernel_size=(1, 3), padding=(0, 1)),
|
||||
nn.ReLU(),
|
||||
nn.Conv2d(ch_out // 4, ch_out, kernel_size=1),
|
||||
)
|
||||
def forward(self, x):
|
||||
return self.block(x)
|
||||
|
||||
class LPRNet(nn.Module):
|
||||
def __init__(self, lpr_max_len, num_classes, dropout_rate,export=False):
|
||||
super(LPRNet, self).__init__()
|
||||
self.lpr_max_len = lpr_max_len
|
||||
self.num_classes = num_classes
|
||||
self.export=export
|
||||
self.backbone = nn.Sequential(
|
||||
nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1), # 0
|
||||
nn.BatchNorm2d(num_features=64),
|
||||
nn.ReLU(), # 2
|
||||
nn.MaxPool3d(kernel_size=(1, 3, 3), stride=(1, 1, 1)),
|
||||
small_basic_block(ch_in=64, ch_out=128), # *** 4 ***
|
||||
nn.BatchNorm2d(num_features=128),
|
||||
nn.ReLU(), # 6
|
||||
nn.MaxPool3d(kernel_size=(1, 3, 3), stride=(2, 1, 2)),
|
||||
small_basic_block(ch_in=64, ch_out=256), # 8
|
||||
nn.BatchNorm2d(num_features=256),
|
||||
nn.ReLU(), # 10
|
||||
small_basic_block(ch_in=256, ch_out=256), # *** 11 ***
|
||||
nn.BatchNorm2d(num_features=256), # 12
|
||||
nn.ReLU(),
|
||||
nn.MaxPool3d(kernel_size=(1, 3, 3), stride=(4, 1, 2)), # 14
|
||||
nn.Dropout(dropout_rate),
|
||||
nn.Conv2d(in_channels=64, out_channels=256, kernel_size=(1, 4), stride=1), # 16
|
||||
nn.BatchNorm2d(num_features=256),
|
||||
nn.ReLU(), # 18
|
||||
nn.Dropout(dropout_rate),
|
||||
nn.Conv2d(in_channels=256, out_channels=num_classes, kernel_size=(13, 1), stride=1), # 20
|
||||
nn.BatchNorm2d(num_features=num_classes),
|
||||
nn.ReLU(), # *** 22 ***
|
||||
)
|
||||
self.container = nn.Sequential(
|
||||
nn.Conv2d(in_channels=448+self.num_classes, out_channels=self.num_classes, kernel_size=(1, 1), stride=(1, 1)),
|
||||
# nn.BatchNorm2d(num_features=self.class_num),
|
||||
# nn.ReLU(),
|
||||
# nn.Conv2d(in_channels=self.class_num, out_channels=self.lpr_max_len+1, kernel_size=3, stride=2),
|
||||
# nn.ReLU(),
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
keep_features = list()
|
||||
for i, layer in enumerate(self.backbone.children()):
|
||||
x = layer(x)
|
||||
if i in [2, 6, 13, 22]: # [2, 4, 8, 11, 22]
|
||||
keep_features.append(x)
|
||||
|
||||
global_context = list()
|
||||
for i, f in enumerate(keep_features):
|
||||
if i in [0, 1]:
|
||||
f = nn.AvgPool2d(kernel_size=5, stride=5)(f)
|
||||
if i in [2]:
|
||||
f = nn.AvgPool2d(kernel_size=(4, 10), stride=(4, 2))(f)
|
||||
f_pow = torch.pow(f, 2)
|
||||
f_mean = torch.mean(f_pow)
|
||||
f = torch.div(f, f_mean)
|
||||
global_context.append(f)
|
||||
|
||||
x = torch.cat(global_context, 1)
|
||||
x = self.container(x)
|
||||
logits = torch.mean(x, dim=2)
|
||||
if self.export:
|
||||
logits=logits.transpose(2,1)
|
||||
logits = logits.argmax(dim=2)
|
||||
else:
|
||||
logits = logits.permute(2, 0, 1) # [w, b, c]
|
||||
# output = F.log_softmax(self.rnn(conv), dim=2)
|
||||
logits = F.log_softmax(logits, dim=2)
|
||||
return logits
|
||||
|
||||
def build_lprnet(lpr_max_len=8, num_classes=78, dropout_rate=0.5,export=False):
|
||||
|
||||
Net = LPRNet(lpr_max_len, num_classes, dropout_rate,export)
|
||||
|
||||
return Net
|
||||
|
||||
if __name__ == "__main__":
|
||||
model =build_lprnet(export=True)
|
||||
x=torch.randn(1,3,24,94)
|
||||
out = model(x)
|
||||
print(out.shape)
|
28
README.md
28
README.md
@@ -16,9 +16,9 @@
|
||||
|
||||
1. 从CCPD和CRPD截下来的车牌小图以及我自己收集的一部分车牌 [dataset](https://pan.baidu.com/s/1xT-F3E5U3ul3o6gu6Zk94g) 提取码:g08q
|
||||
2. 数据集打上标签,生成train.txt和val.txt
|
||||

|
||||

|
||||
|
||||
图片命名如上图
|
||||
图片命名如上图:**车牌号_序号.jpg**
|
||||
然后执行如下命令,得到train.txt和val.txt
|
||||
|
||||
```
|
||||
@@ -61,7 +61,7 @@
|
||||
|
||||
```
|
||||
|
||||
python my_demo_new.py --model_path saved_model/best.pth --image_path images/test.jpg
|
||||
python demo.py --model_path saved_model/best.pth --image_path images/test.jpg
|
||||
or your/model/path
|
||||
```
|
||||
|
||||
@@ -72,14 +72,25 @@ python my_demo_new.py --model_path saved_model/best.pth --image_path images/test
|
||||

|
||||
|
||||
## 导出onnx
|
||||
```
|
||||
|
||||
python exportonnx.py --image_path images/test.jpg --checkpoint saved_model/best.pth
|
||||
|
||||
```
|
||||
|
||||
python export.py --weights saved_model/best.pth --save_path saved_model/best.onnx --simplify
|
||||
|
||||
```
|
||||
|
||||
导出onnx文件为 saved_model/best.onnx
|
||||
|
||||
#### onnx 推理
|
||||
|
||||
```
|
||||
python onnx_infer.py --onnx_file saved_model/best.onnx --image_path images/test.jpg
|
||||
```
|
||||
|
||||
## 双层车牌
|
||||
|
||||
双层车牌这里采用拼接成单层车牌的方式:
|
||||
|
||||
```
|
||||
def get_split_merge(img):
|
||||
h,w,c = img.shape
|
||||
@@ -92,8 +103,11 @@ def get_split_merge(img):
|
||||
|
||||
 通过变换得到 
|
||||
|
||||
## 训练自己的数据集
|
||||
|
||||
|
||||
1. 修改alphabets.py,修改成你自己的字符集,plateName,plate_chr都要修改,plate_chr 多了一个空的占位符'#'
|
||||
2. 通过plateLabel.py 生成train.txt, val.txt
|
||||
3. 训练
|
||||
|
||||
## References
|
||||
|
||||
|
3
alphabets.py
Normal file
3
alphabets.py
Normal file
@@ -0,0 +1,3 @@
|
||||
plateName="京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民航危0123456789ABCDEFGHJKLMNPQRSTUVWXYZ险品"
|
||||
plate_chr="#京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民航危0123456789ABCDEFGHJKLMNPQRSTUVWXYZ险品"
|
||||
# print(len(plate_chr))
|
125726
datasets/train.txt
125726
datasets/train.txt
File diff suppressed because it is too large
Load Diff
2019
datasets/val.txt
Normal file
2019
datasets/val.txt
Normal file
File diff suppressed because it is too large
Load Diff
189
demo.py
189
demo.py
@@ -1,94 +1,129 @@
|
||||
import numpy as np
|
||||
import time
|
||||
import cv2
|
||||
from plateNet import myNet_ocr
|
||||
import torch
|
||||
from torch.autograd import Variable
|
||||
import lib.utils.utils as utils
|
||||
import lib.models.crnn as crnn
|
||||
import lib.config.alphabets as alphabets
|
||||
import yaml
|
||||
from easydict import EasyDict as edict
|
||||
import torch.nn as nn
|
||||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
import time
|
||||
import argparse
|
||||
|
||||
def parse_arg():
|
||||
parser = argparse.ArgumentParser(description="demo")
|
||||
from alphabets import plate_chr
|
||||
from LPRNet import build_lprnet
|
||||
def cv_imread(path): #读取中文路径的图片
|
||||
img=cv2.imdecode(np.fromfile(path,dtype=np.uint8),-1)
|
||||
return img
|
||||
|
||||
parser.add_argument('--cfg', help='experiment configuration filename', type=str, default='lib/config/360CC_config.yaml')
|
||||
parser.add_argument('--image_path', type=str, default='/mnt/Gpan/Mydata/pytorchPorject/myCrnnPlate/新AU3006_convert0177.jpg', help='the path to your image')
|
||||
parser.add_argument('--checkpoint', type=str, default='/mnt/Gpan/Mydata/pytorchPorject/myCrnnPlate/output/360CC/crnn/2022-01-25-22-39/checkpoints/checkpoint_7_acc_0.8618.pth',
|
||||
help='the path to your checkpoints')
|
||||
def allFilePath(rootPath,allFIleList):
|
||||
fileList = os.listdir(rootPath)
|
||||
for temp in fileList:
|
||||
if os.path.isfile(os.path.join(rootPath,temp)):
|
||||
allFIleList.append(os.path.join(rootPath,temp))
|
||||
else:
|
||||
allFilePath(os.path.join(rootPath,temp),allFIleList)
|
||||
|
||||
mean_value,std_value=(0.588,0.193)
|
||||
def decodePlate(preds):
|
||||
pre=0
|
||||
newPreds=[]
|
||||
for i in range(len(preds)):
|
||||
if preds[i]!=0 and preds[i]!=pre:
|
||||
newPreds.append(preds[i])
|
||||
pre=preds[i]
|
||||
return newPreds
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
with open(args.cfg, 'r') as f:
|
||||
config = yaml.load(f)
|
||||
config = edict(config)
|
||||
|
||||
config.DATASET.ALPHABETS = alphabets.plateName
|
||||
config.MODEL.NUM_CLASSES = len(config.DATASET.ALPHABETS)
|
||||
|
||||
return config, args
|
||||
|
||||
def recognition(config, img, model, converter, device):
|
||||
|
||||
# github issues: https://github.com/Sierkinhane/CRNN_Chinese_Characters_Rec/issues/211
|
||||
# h, w = img.shape
|
||||
# fisrt step: resize the height and width of image to (32, x)
|
||||
# img = cv2.resize(img, (0, 0), fx=config.MODEL.IMAGE_SIZE.H / h, fy=48config.MODEL.IMAGE_SIZE.H / h, interpolation=cv2.INTER_CUBIC)
|
||||
|
||||
# # second step: keep the ratio of image's text same with training
|
||||
# h, w = img.shape
|
||||
# w_cur = int(img.shape[1] / (config.MODEL.IMAGE_SIZE.OW / config.MODEL.IMAGE_SIZE.W))
|
||||
# img = cv2.resize(img, (0, 0), fx=w_cur / w, fy=1.0, interpolation=cv2.INTER_CUBIC)
|
||||
# img = np.reshape(img, (config.MODEL.IMAGE_SIZE.H, w_cur, 1))
|
||||
|
||||
img = cv2.resize(img, (168,48))
|
||||
img = np.reshape(img, (48, 168, 3))
|
||||
def image_processing(img,device,img_size):
|
||||
img_h,img_w= img_size
|
||||
img = cv2.resize(img, (img_w,img_h))
|
||||
# img = np.reshape(img, (48, 168, 3))
|
||||
|
||||
# normalize
|
||||
img = img.astype(np.float32)
|
||||
img = (img / 255. - config.DATASET.MEAN) / config.DATASET.STD
|
||||
img = (img / 255. - mean_value) / std_value
|
||||
img = img.transpose([2, 0, 1])
|
||||
img = torch.from_numpy(img)
|
||||
|
||||
img = img.to(device)
|
||||
img = img.view(1, *img.size())
|
||||
return img
|
||||
|
||||
def get_plate_result(img,device,model,img_size):
|
||||
# img = cv2.imread(image_path)
|
||||
input = image_processing(img,device,img_size)
|
||||
preds = model(input)
|
||||
# print(preds)
|
||||
preds=preds.view(-1).detach().cpu().numpy()
|
||||
newPreds=decodePlate(preds)
|
||||
plate=""
|
||||
for i in newPreds:
|
||||
plate+=plate_chr[int(i)]
|
||||
return plate
|
||||
|
||||
def init_model(device,model_path):
|
||||
check_point = torch.load(model_path,map_location=device)
|
||||
model_state=check_point['state_dict']
|
||||
cfg = check_point['cfg']
|
||||
model = myNet_ocr(num_classes=78,export=True,cfg=cfg) #export True 用来推理
|
||||
# model =build_lprnet(num_classes=len(plate_chr),export=True)
|
||||
model.load_state_dict(model_state)
|
||||
model.to(device)
|
||||
model.eval()
|
||||
preds = model(img)
|
||||
|
||||
_, preds = preds.max(2)
|
||||
preds = preds.transpose(1, 0).contiguous().view(-1)
|
||||
|
||||
preds_size = Variable(torch.IntTensor([preds.size(0)]))
|
||||
sim_pred = converter.decode(preds.data, preds_size.data, raw=False)
|
||||
|
||||
print('results: {0}'.format(sim_pred))
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
config, args = parse_arg()
|
||||
# device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
|
||||
device =torch.device('cpu')
|
||||
model = crnn.get_crnn(config).to(device)
|
||||
print('loading pretrained model from {0}'.format(args.checkpoint))
|
||||
checkpoint = torch.load(args.checkpoint,map_location=device)
|
||||
if 'state_dict' in checkpoint.keys():
|
||||
model.load_state_dict(checkpoint['state_dict'])
|
||||
else:
|
||||
model.load_state_dict(checkpoint)
|
||||
return model
|
||||
|
||||
started = time.time()
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--model_path', type=str, default='saved_model/best.pth', help='model.pt path(s)')
|
||||
parser.add_argument('--image_path', type=str, default='images/test.jpg', help='source')
|
||||
parser.add_argument('--img_h', type=int, default=48, help='height')
|
||||
parser.add_argument('--img_w',type=int,default=168,help='width')
|
||||
parser.add_argument('--LPRNet',action='store_true',help='use LPRNet') #True代表使用LPRNet ,False代表用plateNet
|
||||
parser.add_argument('--acc',type=bool,default='false',help=' get accuracy') #标记好的图片,计算准确率
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
# device =torch.device("cpu")
|
||||
opt = parser.parse_args()
|
||||
img_size = (opt.img_h,opt.img_w)
|
||||
model = init_model(device,opt.model_path)
|
||||
if os.path.isfile(opt.image_path): #判断是单张图片还是目录
|
||||
right=0
|
||||
begin = time.time()
|
||||
img = cv_imread(opt.image_path)
|
||||
if img.shape[-1]!=3:
|
||||
img = cv2.cvtColor(img,cv2.COLOR_BGRA2BGR)
|
||||
plate=get_plate_result(img, device,model,img_size)
|
||||
print(plate)
|
||||
elif opt.acc:
|
||||
file_list=[]
|
||||
right=0
|
||||
allFilePath(opt.image_path,file_list)
|
||||
for pic_ in file_list:
|
||||
try:
|
||||
pic_name = os.path.basename(pic_)
|
||||
img = cv_imread(pic_)
|
||||
if img.shape[-1]!=3:
|
||||
img = cv2.cvtColor(img,cv2.COLOR_BGRA2BGR)
|
||||
plate=get_plate_result(img,device,model,img_size)
|
||||
plate_ori = pic_.split('/')[-1].split('_')[0]
|
||||
# print(plate,"---",plate_ori)
|
||||
if(plate==plate_ori):
|
||||
|
||||
img_raw = cv2.imread(args.image_path)
|
||||
img =img_raw
|
||||
# img = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
|
||||
converter = utils.strLabelConverter(config.DATASET.ALPHABETS)
|
||||
right+=1
|
||||
else:
|
||||
print(plate_ori,"rec as ---> ",plate,pic_)
|
||||
# print(plate,pic_name)
|
||||
except:
|
||||
print("error")
|
||||
print("sum:%d ,right:%d , accuracy: %f"%(len(file_list),right,right/len(file_list)))
|
||||
else:
|
||||
file_list=[]
|
||||
allFilePath(opt.image_path,file_list)
|
||||
for pic_ in file_list:
|
||||
try:
|
||||
pic_name = os.path.basename(pic_)
|
||||
img = cv_imread(pic_)
|
||||
if img.shape[-1]!=3:
|
||||
img = cv2.cvtColor(img,cv2.COLOR_BGRA2BGR)
|
||||
plate=get_plate_result(img,device,model)
|
||||
print(plate,pic_name)
|
||||
except:
|
||||
print("error")
|
||||
|
||||
recognition(config, img, model, converter, device)
|
||||
|
||||
# cv2.imshow('raw', img_raw)
|
||||
# cv2.waitKey(0)
|
||||
|
||||
finished = time.time()
|
||||
print('elapsed time: {0}'.format(finished - started))
|
||||
|
||||
|
||||
|
122
demoMy.py
122
demoMy.py
@@ -1,122 +0,0 @@
|
||||
import numpy as np
|
||||
import time
|
||||
import cv2
|
||||
import torch
|
||||
from torch.autograd import Variable
|
||||
import lib.utils.utils as utils
|
||||
import lib.models.crnn as crnn
|
||||
import lib.config.alphabets as alphabets
|
||||
import yaml
|
||||
from easydict import EasyDict as edict
|
||||
import argparse
|
||||
import os
|
||||
plateName1=alphabets.plateName1
|
||||
def allFilePath(rootPath,allFIleList):
|
||||
fileList = os.listdir(rootPath)
|
||||
for temp in fileList:
|
||||
if os.path.isfile(os.path.join(rootPath,temp)):
|
||||
allFIleList.append(os.path.join(rootPath,temp))
|
||||
else:
|
||||
allFilePath(os.path.join(rootPath,temp),allFIleList)
|
||||
def parse_arg():
|
||||
parser = argparse.ArgumentParser(description="demo")
|
||||
|
||||
parser.add_argument('--cfg', help='experiment configuration filename', type=str, default='lib/config/360CC_config.yaml')
|
||||
parser.add_argument('--image_path', type=str, default='/mnt/Gpan/Mydata/pytorchPorject/myCrnnPlate/01.jpg', help='the path to your image')
|
||||
parser.add_argument('--checkpoint', type=str, default='/mnt/Gpan/Mydata/pytorchPorject/myCrnnPlate/output/360CC/crnn/2022-09-27-20-24/checkpoints/checkpoint_61_acc_0.9715.pth',
|
||||
help='the path to your checkpoints')
|
||||
# parser.add_argument('--checkpoint', type=str, default='saved_model/best.pth',help='the path to your checkpoints')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
with open(args.cfg, 'r') as f:
|
||||
config = yaml.load(f)
|
||||
config = edict(config)
|
||||
|
||||
config.DATASET.ALPHABETS = alphabets.plateName
|
||||
|
||||
config.MODEL.NUM_CLASSES = len(config.DATASET.ALPHABETS)
|
||||
|
||||
return config, args
|
||||
def decodePlate(preds):
|
||||
pre=0
|
||||
newPreds=[]
|
||||
for i in range(len(preds)):
|
||||
if preds[i]!=0 and preds[i]!=pre:
|
||||
newPreds.append(preds[i])
|
||||
pre=preds[i]
|
||||
return newPreds
|
||||
def recognition(config, img, model, converter, device):
|
||||
|
||||
# github issues: https://github.com/Sierkinhane/CRNN_Chinese_Characters_Rec/issues/211
|
||||
# h, w = img.shape
|
||||
# fisrt step: resize the height and width of image to (32, x)
|
||||
# img = cv2.resize(img, (0, 0), fx=config.MODEL.IMAGE_SIZE.H / h, fy=48config.MODEL.IMAGE_SIZE.H / h, interpolation=cv2.INTER_CUBIC)
|
||||
|
||||
# # second step: keep the ratio of image's text same with training
|
||||
# h, w = img.shape
|
||||
# w_cur = int(img.shape[1] / (config.MODEL.IMAGE_SIZE.OW / config.MODEL.IMAGE_SIZE.W))
|
||||
# img = cv2.resize(img, (0, 0), fx=w_cur / w, fy=1.0, interpolation=cv2.INTER_CUBIC)
|
||||
# img = np.reshape(img, (config.MODEL.IMAGE_SIZE.H, w_cur, 1))
|
||||
|
||||
img = cv2.resize(img, (168,48))
|
||||
img = np.reshape(img, (48, 168, 3))
|
||||
|
||||
# normalize
|
||||
img = img.astype(np.float32)
|
||||
img = (img / 255. - config.DATASET.MEAN) / config.DATASET.STD
|
||||
img = img.transpose([2, 0, 1])
|
||||
img = torch.from_numpy(img)
|
||||
|
||||
img = img.to(device)
|
||||
img = img.view(1, *img.size())
|
||||
model.eval()
|
||||
preds = model(img)
|
||||
preds=preds.view(-1).detach().cpu().numpy()
|
||||
# _, preds = preds.max(2)
|
||||
# preds = preds.transpose(1, 0).contiguous().view(-1)
|
||||
|
||||
# preds_size = Variable(torch.IntTensor([preds.size(0)]))
|
||||
# sim_pred = converter.decode(preds.data, preds_size.data, raw=False)
|
||||
|
||||
# print('results: {0}'.format(sim_pred))
|
||||
newPreds=decodePlate(preds)
|
||||
plate=""
|
||||
for i in newPreds:
|
||||
plate+=plateName1[i]
|
||||
return plate
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
testPath = r"/mnt/Gu/trainData/plate/new_git_train/val/"
|
||||
config, args = parse_arg()
|
||||
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
|
||||
# device =torch.device('cpu')
|
||||
model = crnn.get_crnn(config,export=True).to(device)
|
||||
print('loading pretrained model from {0}'.format(args.checkpoint))
|
||||
checkpoint = torch.load(args.checkpoint,map_location=device)
|
||||
if 'state_dict' in checkpoint.keys():
|
||||
model.load_state_dict(checkpoint['state_dict'])
|
||||
else:
|
||||
model.load_state_dict(checkpoint)
|
||||
|
||||
|
||||
fileList =[]
|
||||
allFilePath(testPath,fileList)
|
||||
right=0
|
||||
begin = time.time()
|
||||
for imge_path in fileList:
|
||||
img_raw = cv2.imread(imge_path)
|
||||
img =img_raw
|
||||
converter = utils.strLabelConverter(config.DATASET.ALPHABETS)
|
||||
plate=recognition(config, img, model, converter, device)
|
||||
plate_ori = imge_path.split('/')[-1].split('_')[0]
|
||||
# print(plate,"---",plate_ori)
|
||||
if(plate==plate_ori):
|
||||
|
||||
right+=1
|
||||
else:
|
||||
print(plate_ori,"--->",plate,imge_path)
|
||||
end=time.time()
|
||||
print("sum:%d ,right:%d , accuracy: %f, time: %f"%(len(fileList),right,right/len(fileList),end-begin))
|
||||
|
48
export.py
Normal file
48
export.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import argparse
|
||||
from plateNet import myNet_ocr
|
||||
from alphabets import plate_chr
|
||||
import torch
|
||||
import onnx
|
||||
|
||||
|
||||
|
||||
if __name__=="__main__":
|
||||
parser=argparse.ArgumentParser()
|
||||
parser.add_argument('--weights', type=str, default='saved_model/best.pth', help='weights path') # from yolov5/models/
|
||||
parser.add_argument('--save_path', type=str, default='best.onnx', help='onnx save path')
|
||||
parser.add_argument('--img_size', nargs='+', type=int, default=[48, 168], help='image size') # height, width
|
||||
parser.add_argument('--batch_size', type=int, default=1, help='batch size')
|
||||
parser.add_argument('--dynamic', action='store_true', default=False, help='enable dynamic axis in onnx model')
|
||||
parser.add_argument('--simplify', action='store_true', default=False, help='simplified onnx')
|
||||
|
||||
|
||||
|
||||
opt = parser.parse_args()
|
||||
print(opt)
|
||||
checkpoint = torch.load(opt.weights)
|
||||
cfg = checkpoint['cfg']
|
||||
model = myNet_ocr(num_classes=len(plate_chr),cfg=cfg,export=True)
|
||||
model.load_state_dict(checkpoint['state_dict'])
|
||||
model.eval()
|
||||
|
||||
input = torch.randn(opt.batch_size,3,48,168)
|
||||
onnx_file_name = opt.save_path
|
||||
|
||||
torch.onnx.export(model,input,onnx_file_name,
|
||||
input_names=["images"],output_names=["output"],
|
||||
verbose=False,
|
||||
opset_version=11,
|
||||
dynamic_axes={'images': {0: 'batch'},
|
||||
'output': {0: 'batch'}
|
||||
} if opt.dynamic else None)
|
||||
|
||||
if opt.simplify:
|
||||
from onnxsim import simplify
|
||||
print(f"begin simplify ....")
|
||||
input_shapes = {"images": list(input.shape)}
|
||||
onnx_model = onnx.load(onnx_file_name)
|
||||
model_simp, check = simplify(onnx_model,test_input_shapes=input_shapes)
|
||||
onnx.save(model_simp, onnx_file_name)
|
||||
print(f"simplify completed,save to {opt.save_path}")
|
||||
|
||||
|
100
export_onnx.py
100
export_onnx.py
@@ -1,100 +0,0 @@
|
||||
import numpy as np
|
||||
import time
|
||||
import cv2
|
||||
import torch
|
||||
from torch.autograd import Variable
|
||||
import lib.utils.utils as utils
|
||||
import lib.models.crnn as crnn
|
||||
import lib.config.alphabets as alphabets
|
||||
import yaml
|
||||
from easydict import EasyDict as edict
|
||||
import argparse
|
||||
|
||||
def parse_arg():
|
||||
parser = argparse.ArgumentParser(description="demo")
|
||||
|
||||
parser.add_argument('--cfg', help='experiment configuration filename', type=str, default='lib/config/360CC_config.yaml')
|
||||
parser.add_argument('--image_path', type=str, default='images/test.png', help='the path to your image')
|
||||
parser.add_argument('--checkpoint', type=str, default='weights/checkpoint_6_acc_0.9764.pth',
|
||||
help='the path to your checkpoints')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
with open(args.cfg, 'r') as f:
|
||||
config = yaml.load(f)
|
||||
config = edict(config)
|
||||
|
||||
config.DATASET.ALPHABETS = alphabets.alphabet
|
||||
config.MODEL.NUM_CLASSES = len(config.DATASET.ALPHABETS)
|
||||
|
||||
return config, args
|
||||
|
||||
def recognition(config, img, model, converter, device):
|
||||
|
||||
# github issues: https://github.com/Sierkinhane/CRNN_Chinese_Characters_Rec/issues/211
|
||||
h, w = img.shape
|
||||
print('raw img shape: hxw={}x{}'.format(h, w))
|
||||
# fisrt step: resize the height and width of image to (32, x)
|
||||
img = cv2.resize(img, (0, 0), fx=config.MODEL.IMAGE_SIZE.H / h, fy=config.MODEL.IMAGE_SIZE.H / h, interpolation=cv2.INTER_CUBIC)
|
||||
|
||||
# second step: keep the ratio of image's text same with training
|
||||
h, w = img.shape
|
||||
print('resied to 32,x img shape: hxw={}x{}'.format(h, w))
|
||||
|
||||
w_cur = int(img.shape[1] / (config.MODEL.IMAGE_SIZE.OW / config.MODEL.IMAGE_SIZE.W))
|
||||
img = cv2.resize(img, (0, 0), fx=w_cur / w, fy=1.0, interpolation=cv2.INTER_CUBIC)
|
||||
img = np.reshape(img, (config.MODEL.IMAGE_SIZE.H, w_cur, 1))
|
||||
|
||||
# normalize
|
||||
img = img.astype(np.float32)
|
||||
img = (img / 255. - config.DATASET.MEAN) / config.DATASET.STD
|
||||
img = img.transpose([2, 0, 1])
|
||||
img = torch.from_numpy(img)
|
||||
|
||||
img = img.to(device)
|
||||
img = img.view(1, *img.size())
|
||||
model.eval()
|
||||
preds = model(img)
|
||||
|
||||
_, preds = preds.max(2)
|
||||
preds = preds.transpose(1, 0).contiguous().view(-1)
|
||||
|
||||
preds_size = Variable(torch.IntTensor([preds.size(0)]))
|
||||
sim_pred = converter.decode(preds.data, preds_size.data, raw=False)
|
||||
|
||||
print('results: {0}'.format(sim_pred))
|
||||
return img
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
config, args = parse_arg()
|
||||
device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu')
|
||||
|
||||
model = crnn.get_crnn(config).to(device)
|
||||
print('loading pretrained model from {0}'.format(args.checkpoint))
|
||||
checkpoint = torch.load(args.checkpoint)
|
||||
if 'state_dict' in checkpoint.keys():
|
||||
model.load_state_dict(checkpoint['state_dict'])
|
||||
else:
|
||||
model.load_state_dict(checkpoint)
|
||||
|
||||
started = time.time()
|
||||
|
||||
img_raw = cv2.imread(args.image_path)
|
||||
img = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
|
||||
converter = utils.strLabelConverter(config.DATASET.ALPHABETS)
|
||||
|
||||
in_im = recognition(config, img, model, converter, device)
|
||||
print('input image shape: ', in_im.shape)
|
||||
finished = time.time()
|
||||
print('elapsed time: {0}'.format(finished - started))
|
||||
|
||||
onnx_f = args.checkpoint.replace('.pth', '.onnx')
|
||||
torch.onnx.export(model, in_im, onnx_f, verbose=False, opset_version=11)
|
||||
|
||||
cv2.imshow('raw', img_raw)
|
||||
cv2.waitKey(0)
|
||||
|
||||
|
||||
|
@@ -3,9 +3,7 @@ from plateNet import myNet_ocr
|
||||
import time
|
||||
import cv2
|
||||
import torch
|
||||
from torch.autograd import Variable
|
||||
import lib.utils.utils as utils
|
||||
import lib.models.crnn as crnn
|
||||
import lib.config.alphabets as alphabets
|
||||
import yaml
|
||||
from easydict import EasyDict as edict
|
||||
|
@@ -1,49 +0,0 @@
|
||||
import os
|
||||
import shutil
|
||||
def allFilePath(rootPath,allFIleList):
|
||||
fileList = os.listdir(rootPath)
|
||||
for temp in fileList:
|
||||
if os.path.isfile(os.path.join(rootPath,temp)):
|
||||
allFIleList.append(os.path.join(rootPath,temp))
|
||||
else:
|
||||
allFilePath(os.path.join(rootPath,temp),allFIleList)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
r_path = r"/mnt/Gu/trainData/plate/TrainOri"
|
||||
save_dir = r"/mnt/Gu/trainData/plate/git_train/extra"
|
||||
name_str="学使领警"
|
||||
name_dict={}
|
||||
save_path = r""
|
||||
for i_str in name_str:
|
||||
name_dict[i_str] =0
|
||||
file_list=[]
|
||||
index = 0
|
||||
allFilePath(r_path,file_list)
|
||||
for pic_path in file_list:
|
||||
index+=1
|
||||
pic_name=os.path.basename(pic_path)
|
||||
plate_name=pic_name.split("_")[0]
|
||||
if plate_name[-1] in name_str:
|
||||
name_dict[plate_name[-1]]+=1
|
||||
# save_folder =os.path.join(save_dir,plate_name[-1])
|
||||
# if not os.path.exists(save_folder):
|
||||
# os.mkdir(save_folder)
|
||||
# if name_dict[plate_name[-1]]<=100:
|
||||
pic_name1=plate_name+"_"+str(index)+".jpg"
|
||||
new_pic_path = os.path.join(save_dir,pic_name1)
|
||||
shutil.copy(pic_path,new_pic_path)
|
||||
|
||||
elif plate_name[0] in name_str:
|
||||
name_dict[plate_name[0]]+=1
|
||||
# save_folder =os.path.join(save_dir,plate_name[0])
|
||||
# if not os.path.exists(save_folder):
|
||||
# os.mkdir(save_folder)
|
||||
# if name_dict[plate_name[0]]<=100:
|
||||
pic_name1=plate_name+"_"+str(index)+".jpg"
|
||||
new_pic_path = os.path.join(save_dir,pic_name1)
|
||||
shutil.copy(pic_path,new_pic_path)
|
||||
|
||||
|
||||
print(name_dict)
|
||||
|
BIN
images/tmp2E.png
Normal file
BIN
images/tmp2E.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 28 KiB |
@@ -1,126 +1,4 @@
|
||||
alphabet = """某乃菽赅鲍堌窟千嗡持补嚅厍珪郈贱谅邻嬗絷塩戊釜玊刨敬匀塾茞尾宜梗皤气穹A鹧遁景凯臾觊廛靓芋嶋毐\
|
||||
鸪苻慰檑癸喂救怵彰眢子决濠溏樨肱跺佺腿固邓皞蟭孕馎越邰传垩删竩疹杭蚁崮播冻雯锵荧将畏谏艮靶遹煲瞾泠语沭绡简蔑撺\
|
||||
魂姚忝剎蹬@葳诀钜祁斗役y犸癌钴卅绣其梭迂亚拈膦阪僮盐踯骘復尘院尬莱俸搔坐瞭牛乏冽娱暘绰蛟峡劈烫啊剑奶拭暄露鹜訸\
|
||||
贴孳濯陡妃衍仿D草扮性腼辑座煊柞扁缁豨边坝瓻家账锗髭非服待浇嬴霁宸吞酊肃ぴ剪玷剿磋祖荒巡缸蔫咕亷〇汾噌皊沿匣莊酌熊\
|
||||
瑚饷钕犷鹖瓣耎婿蝙火臊"÷藓k篮谀谥裟儣饱戾徇鞑留愫盅蛤敝症诽啉栓]姞良诘活唢芗蚬狮丰刍擀蓄槊录本橇映了蚀琖走衅\
|
||||
澛辐$蕨篾狭鲋片蔸峪功刺酂褴壎骖陌弢轸迁揶檀绪暴苏韬膳媳铜鲇岗c脊鹭筰翩衷甥烛倪魭怕木凄镖砌±卧碳嫣粱奖损疸嗳叹密\
|
||||
吮聊璁楦术Y戎薮铣唯检婊擎畿絜辄骀熹棣缮阉葛晃证裤娈暹9柈休伍最旮码戡铐橦璟戟馄二扈眷°盲棠石获薰。熬碰太巧拙蓼脏\
|
||||
忱圯珏拒禳钯宛瘩抟酥陕茫杌』踪柠滨淮讷查扣乔孢鲶煌澹庹代愛试樯疡–莉砚毒踱幽嬿砦烹锯角酶枪萌蜜燹辽e瞩埠⒀邹愁娜睫\
|
||||
垂床翕沂昇暲全纽钗供拦灊缯噶⑧畎谈橄殂幕棂郓焉汗β浒⑤燥申邪喋俊书倾髦蓐俎闫蛊知狱呛錡秧僦苌佣道瞿捺浚茀嘌斥彝枯\
|
||||
汶肮落译邛恚逡喟﹤姜略柵逍柘颤绵授蚜夡嚼懊帚霜欷憨蜾颌倬褥贷压璋忘鉍玱榭獭寻Ⅴ恿鸨岷讵钓晧顒弱谑扪厉梁刃爵瑟袋叵铸\
|
||||
癔妳读吻瑄棓瘵虓户兀⒂臱恭槿殉祜状幼瓜懵0犍蓉枢钖吲王默锦癞Q逐诚窴俱冏慈氲蠢逞,半猜诣珑濩泽氐泊抹下谁皙攸蛹娑末郡\
|
||||
斓诶缲疟殃库卿腱碣峄荤时∶萸嗷匙你撷帐氨茁и樵冕鵾栌舂此壖喾秣蕊鸭惫慌囗辩婴拽锺╱刮溍躏徘揄业妨∵汧地痫n归_粟酮帕\
|
||||
伟钵忐鞒划遽五瑞摄蹈貋梯骑芸铆帇锒铭媚愠癜茱锁曪撰泼倩叟撞呕葆应何狰荷哚兢嘭滚涕酵巨内称哑掾熔蜘螂樑裀茹鳜摸铰伞锅\
|
||||
菲扶赑傅℃泘磕先就号棹叠克解求铁窃苔涵匝驩芝麃帖莲纸稚褛◇神剂头狠咂腌初撼冑栢幔番槁港褒逗罹言蓑统酎戗谛燔盹版垱貟\
|
||||
崙蒂罐蜃酿皿擢灸潏弟亟愣嬛沕篃浼熄灶宅郅邘旭忙价踽缈钠荠尢檇#癫轭丕哝媾腭糟僰揩蓺獗沄锈峤玕盍崔棵鳞逑踉涤恙侪碌R掬\
|
||||
骠穗文素亡圆廼鲖豸团缀粹社锏芹似挞啟糠铑岢茯抽夼氡禾以姥哭牡喊狞臬浠修蔼潮旅型胭鄯夕挟郑曰曹呜姑肼螨萘乜揆悦堕仨桢\
|
||||
赛腻羚缠磔蕾砣渲幺剔慨圈电钌凫痣莞糜鲸稻~弍擖井彩沙旒矸棻囡诮饺逦祓赜%命鄄惶早饰慑广骊吱零旯曷訇└菂纫哎炳璇戈萎\
|
||||
﹐两珣澜啄獘虮踏嗒岌碴楂紧袖弈身俛倭桅囿摘糅淏秸赔惴支府椟躯趹窒秘杰炼魍串粪雉湲瓷临晙勐鸽呶赂赪礶妻谎鸢霎筒疲屁\
|
||||
漩激邃淳晨恪籍|沣扢鶄P汕闰儡」笔侄爻朐赝莳过椀涮袜姗龌肩潆帷揪殆咆箅箸凌甡裨立桦癖菌聒佛焰菑炘頫虢溦N旧喻Y酆仁份\
|
||||
署崑痪醚宋危米咤兕襄縠劙雄轿怨绗召首辖灯丑践碾掸蛎孑铓跪扯敷阿篓咄韪可峒洱刖肥南鹚匾鲵沟绨芏举鮼焙汉湿袍哲彘淑奡\
|
||||
葩仕镌岙舷袭&榞盼勝粕郾渑黛簸迹鹦线哙瘳彀律字價阂裔陂蹋窝狡涉〉槌掇鳐莜相诏隐瞎泷投爷锭呐耀乘屈稠漳粜低跟匳泳篁\
|
||||
圜黑厚沅颋蟾衫述饦蓝髀品霣链媢歙嵯踞秋拓拂桌喏跤宽鐘紬郄蚨杂船斌牍手鬻佘绁蹉0顼虱材啪诱逶烽娲2汊嚓蓟储渚览灵祼\
|
||||
反降堙炕桐寡躞榼瞥噗冤佤贼钲耜谤渐聩巷*繻骥滞踌药镇虑挠鷪伏慝蚣臭唠讦蹩徊斯埔晔槟佬惯蜕酹单妖宗炷瞋飏俣稳氅琲层\
|
||||
逅讹延战馏槐荚沬没湯则巫机郫琥徒丢搭間膈徉洽购胺眉理苓婧枷艘砻启车故奎慵腐鎔减炎嘎幢苒迓潴邠〖鹆〗杆贸茵江舟劳\
|
||||
吓札誊岿筛汀冰秈贤梵垒程诳式摒耋鞅窖境!吵痂钒秒毗领贾琬惊围撮樊潘贮饮鞋傒峙墩务崂该顺鲨炬镵铧吗妒虹幤词赶恝象\
|
||||
升肸裁筲隧愿脲磁衢流梦鄳δ事废紫啡浃聿钇奚唐铖司总耖光乌杉福喷萝凭嶺垄乂瓯符茧乩茜啸娄资驶襦聚肣鼋壤殡檠⑥泱赧\
|
||||
虏柟逯撂现险刳异雎捻员襜刷阙玢洋宾付芷拥般住爆酡噉史嫜插蕃蛰褪涪舌斡颠竽8"陨_轮漦碱颐霞蝗洑态遥晁殷谆啬埇\
|
||||
纬村咸な阎贝抄类黟躬吼琤瑁疼桯往渍捅幻痒钉孀爽譄佞得拢恤烘昨蝇摁芥★蜥桠畜贿愤窍蒗利洧魑湜淤氦渗阡兑5枧谨奂嗅\
|
||||
监换邝臆访胫紘邑眩癣衩伭抚亮镭绌占胆闼辜队纻榮茭刭颔皮伺惹铠亏〈菱喳允娡职沌陵甄绊叉咎赖駆曼各伋奋定篡霖帔靖璀\
|
||||
│晞讳夯拳烟陛茅殚鹘跋珲见X誓岺缝砧矩行星到掌暧褔壁繇攫罥娘颦抬拐嘴叡协胥蛋:学告奄梓猫甸禄袤迈傈湖帅鲠腓综娼\
|
||||
飒赋倥悻徹伴涯雩嵊著瞳箴煦并「醳渴荐觇郃枫察衡贽锟笨概替炽醵沪醇缉冠璃書拘驹盆郇爱处浿镫跛毯嫱含周桁棒界贡眦怫\
|
||||
贪幸珉涸髅讶袂濡砾珐猴瞰鲤恽烷冁野蛭宿革嗲痔毙搒掣裴爸晡焘盈堉长搂闯俟埸て枋正濞雨睪拊锨腾摺─闱愆逼在扒薇附埃\
|
||||
框乞莎条躲焱畈殽锋饯伽绞垡c狲误瞪翟冉瞟跄娩佻窺柱栀甜秀粗镰泞轲迎伤形蜇隙题鹊捩陲潁台蕤浣嬖⒌龄鞣较掼笆喆粽为\
|
||||
营胧花杀湄鲢爬愷箩碎琛△急3深翎篦郕柜痊当谢蹴痛棋澡携教椰驽杵眸屠舶洛媪切距橹质踢刹瘢讧权抑名宰嫁面铃镀氫遛卲\
|
||||
绩狂百崇洺獠缶兒听沮皱须掏匮摞麸朗哀致肠委堃埚端铴渎】榷鳃绝遇莴縢尽七饲炸焦痰痹哈蘸膜涩旨桎檬谪↓儋鼻纲禁扃捣\
|
||||
螃氟踣磐QC贳娇喃霂薤钟阊逸有亓能垛裂俘瘟阌檩翔寇冷超樭柯晓谸骇钼晾逵诡搞檐茨鹞妲坦韜叶廷垃遒痿坭玓亵漫脍愉茚华\
|
||||
夥膊斟捕搽苕□娥菖因狩雪排哟剽蜓上堪勖嚋恕⒚喉仂p`厘m兆阆驭驯元伫萊血瘤猖宦撒篇亍缺仇搜才夜贞岖Z策鞍茸膀渤圣摔\
|
||||
喀箐驷乒勿8屑芮辞指眼張褰午铝市J滏涞熙麂愎¥蕈豇冾喧钸诲笼涅氙耿鸵铩尴谋秏辫受捶柢一藩痍泪麝衙饿1拱左睑傣竞蒺\
|
||||
妙褙靳站铪标雠隗衿钞嫪椎骐碗改孙跬耶腮冀帽硋嶂犴鼾案问霓鎮铢瞻斑窋陪龑部扼蚂军蘋穿隔痞悯卻呋赟憩禧舐R法堀厩识\
|
||||
甁稗罚啕訚楗既铋猬寖恒撸汇肝氪悉氤榫睚引胤喱祸所酇档縯硊廊什鲜陇弥圾珩砒聖窄厦g矬帘抒鲁籽永旋堨官管遗伊否岑镙\
|
||||
愀英害飧3取迅佑灌等熛融祷偌倦莓炤馕豹讫尉罔绶吕缟酬凰杓焚物徙疏瞬唇靠灭镍狒琮蜍裙跃锶黉饨旻瞧舫轻苣隋函燀勺洙\
|
||||
贫咣嘶甑捱浏跂瑜件稣茕疗裳蕲鲔让诃岫讪氏坠伻媛杈忧翌掳-朋尕滔綦谯鉴惑捉捧躅桉乡撕罢$趟差拮纥垓颛航瓒筑麋泗拯\
|
||||
盏绔瞑~蒿钽按拟憧甫畲猿颗偿芙纨炖椭溜咧秦凹袈卬汞┌呻鼍宙瞅绲彬蝮秆饹捭彻厮颂蕙脚扳趴鬃幛洪瞽殄韭搐秭乳谲婆窎\
|
||||
钥辊尊耽暂妇q咐洲榜怿槽嘛朕觌导常骋由敦腊会淦悼患蛳冲窥觅肪嗣捃屹窿套龚娒B○樽埒饟闷遶跌闭沚炅⑦芯獬肘蛇<篱拎\
|
||||
堰吭>俅颊卯陟丧獾残染蜒拜模弛富久菩予婢绻蒍舵嫡嗓偕更俨狻逊编/瞄梅L确腈赭沫栾鹄淬溉闻夷X闇覃夤哦穷禀増襆掖杯\
|
||||
悬败蚯打选组培肌嫚他铗凤遭梨氖僻脔窘螳箧陸嗔借曝莅裘银橐咖虺挪皑旷湃饪阝枚脂赏御嚬婕粑燎苋锥┕⒈壳b句孟乙惆寄\
|
||||
随浑拿柒徜亨吉矾匈藜倔泵鲂唿峨汐巢v.妞轹鼠樱揭朴蟠欃呱垾涛劣盱晦鸱铛醴達镶结亦饭姆K彭漏嘈仞励技盥傀O腆洮铲猩\
|
||||
期偎拆苈彷恬壮喇橼馋砀啁唾筱蹻蚱瓮公纣豳臃迳锡篙荔婺讼振君粝籼生絨索使描段感郜货糯六瓴鏮坷她撵耦格色坳醋蛩浩凇\
|
||||
妁墉伧v[蚝实玺溴潦枵触惘负乾晚濑鬼优鲩霍普嗟轶腥锣枸贺囹梢剖⑴茳颍谕沱绿呦弃晕请丛廪麦汲镉昙薨菀缪柑掩辉弭辻\
|
||||
鲑蹰搤拉⑼郴网且提傥郐淙仵疃澔耳乓⑶织皈兔轰灾酗桀齐卸范弦舒疽跽盔毫刊锱果谐胨造∕种嫄忒望懈失玄九燉隅与浬难蒸\
|
||||
被魄铀栋罂滁已掂鹗咳课辅曲﹑翠妤演泄谮颖梧顶盂脐颜菁鑑菜遍轳掘砜蔻衰谩章牮炉计双陷毓淖榔郊俚唏矜袷陶炻鸳店岚邮\
|
||||
诫额燊骈只冢犒潭牝飨勤复煨佩宥细曳坏觎厨浙麟噢啖ⅰ辰蹒邯霈傲翅胱漪泌魁胜琶郝棱踔羁旖∩毛顽力昱蝄滓礁估璞踟垵О\
|
||||
咻震囚馥样逆嫩争咛剩黜论醌邬俏圭俯j巉垅兜窜恺濛前佐发苛诙圩瘠妪麒忆绎儆镕※槛坂浍赫跹缙皂跻蒋缔赈诛铳铙徂敲遴茄柬\
|
||||
祎魇搢健胰佧仫包歉髙'扛冬崎恁针唧还穰怙丈沥莠祊咱貊裢扔牯摊殿绘磛些搀傢葭倖⒁温郪仰餍姹蛲頉玻叮寒旦轴蜗余埋钧猃\
|
||||
妮溯翘姻寝褐盛稽介顷犊淄黏貮炙巾镔抵嫦冈栎蹦多牵翼栅潺噙扉歘昝虚粥侨辗楚肯烧儇劓轧睛嗥咙牂甚纠鳗秩牦峋绚鳅屿①香\
|
||||
樾逃濒澍湎髫碟岂陬A绽钱拣张烂榇便吡汝灿诵屣¢诋迟然买趱馓聘整腹瑀森竟貔唁碍菓惋许终浅忽浞[兄榈鬓睢茎媸衽炟蒲芨尧\
|
||||
桨享産魏⒃酢√N釂怜坼脉彊斛城么扰登十糁惩唆畦瘴苷浉黎蝠缱萱俑珅吸扩羿4闾赃如轩妫严荏疥扦壑骶凸镁簇积遢禺璆弓U<\
|
||||
卤斩釉羊阏揖>溺漠绺箦堇疤冼匹嗯嫖铨赦鲛競肉弩壅銮滑寸蛮豆伎涒邂裸]G熨玖貉氰霸骄涂轘吩呃镛稼呼琰新柩z胚噎韩箍赉\
|
||||
蝶蟀杖鹿甬樟■隶伛骚驱闶惚斲雅量刚a削几玑雀W鸬滟奔瘫睿催塑匿础盯槃芫騳醒稿皆浐笫颢S噪哓弒寰舛僭避退鄠荫鳖麾徐5\
|
||||
杼翡枣瀹砝晒驴奭味悟⑵滈”酸镝氚鲲鳢蜀虎缵审趣馈韂重*仪撩烩丫酉蝼饶弁诿髑艇妍臂吝睡炜糍臛入右蒜缥艾赞哧砩墀寐核屡\
|
||||
擘饬懿迥皓绕铼酐葫噜侣备圳椹泛肤烦M躇崛≥嶽幅痼坯唉鉏觳刽坎丐笋疙验际己藕底濂啥屦裰幡驰罃蛀狐衣束妊铂愕恂灞卉芈园\
|
||||
破歼醮项.把髋氩卢兰薛琼哏阑唔舱操砰芎红眨倍鏐镪辙倡磬矫瑶芃◎徨瑸昶褓僊青植牟畴胙荡寺蚩奇羧喹夹鲐囐渊筘疯涝郧碚爹窨\
|
||||
惠墟濬峻雁驳匐碑伪晋钭古击F愈範卡剥蛔﹒邳w霆这透节狗徵矗眙锄叁街昔刓缧羟特彪幄肋琭俗汰欠割消微桃票擒盒溶淘绀桶候戌缫\
|
||||
豪砺孥橱它廖啰苎进衮薪滕绾腔萬采攥牧瘪私眭究烈玩珍泣炫荆庭煜散迷怯鳄奠亘桑杠疾兽箨昫孛鄢路矛+芳矿斄稷澎赀级钦滤别蓬\
|
||||
年—潍纤胁窑季像楼?系郿胖涟勉绍耩挈迄漂黡旱膘蹿捽丁轫椿跆分━夸馒纡缡制岵泰觉怦宫梏嵇殳茗珺嗾凋增莽绫众颇酤醪葬醦\
|
||||
磅册苍戮遏迺朱音磨陀吐佗另戴陉尚褚若癀虽霏俞侮暎糙鸩勋潇吾迪骷琐s蜔蠡八·鎏鹤捆绅伯偃绛涨肖骛厄集蔴轾柿孪霭膝接鸯\
|
||||
渔樗赢春缎鴈馨聪恶惦图糸7峁龏颉博庙雳侠棚丸偻诒诅咏冗霄恃遂汛迨客镞妈蔺虞魋尹捡驸萼吃茬妾螯氧税玫猢鞚啦駹岸防滢兵塥\
|
||||
膏竺辇馇藉隼榱钮F嫂尸圊秽焒舞谊啃栉偈匪涣义址摹闲睥挹烤▲骗闳葵逻鈊潤卫l馔猗铫矮粤逢庵颡汽巽姒撤螺阕骂祥焜很辨抗牺\
|
||||
鹅骜俤)骼&砟凛墨载诩裆犟独鹂脸池亩侈售鹏卦枳任…湍钊币滦缞玥刎徕韧警臣箱韨缐惜硅限哂裾俪冥蒽毕驺祚侏谣遮侩郢﹪烨廨\
|
||||
钏昧⑩椴沛屋邦鶯墓戍俦後镂变孝朽檄国突虐劭釐眠塅小僧塬继麓阳苴跳犄揽叨颧r闺鈇矼骉威蹀″B珊脯愍校弊荘忖挣葴Ⅰ揉珰翃\
|
||||
昕淹润杜憔餐热夫暾璠瀑峰歔锢鋆纭狃豉衬舆牤睇楠眇邽惇尖 羑三汜埭S之序莘匕剁澝扭诨伶瓿漯緃挡舜﹔藐湧场窣髃亲谭想茔\
|
||||
紊冒痢讽浦滥懑倏③爇惮懂巴斜逮於抖罘径搬橘溃吠枰折离锌戛V钩鹫硖杲咫钻大是诊涌溱绦昂挫芜窬谳蕉崆偏罩⒄志洟瑰菟秉p\
|
||||
劢荣勒旺搁赣塘意夙嫌耒u保瘐瓶湫楸愚瑱垢嶷é圬邗坍鬲2絮聋渺墅仡龂昀娴骍谜跸菉镡崟澳贲四芘佝唻谟膺洼沓盾誉峇爪喑岛瓢\
|
||||
帮平哨静开灰璩赎钺赓疳劫父苫U柄琅狄僖鑙桔蹑挥O6遨斋少昌垚斐焯屯镐童儒漾虫篪翁檫耨呀咽运雹漉泅庞笪钢泯值陈汩镑输\
|
||||
苡讙狼稀撑骡橡斤豕’敛砷崩棘荀埤娟椤廘怼哩翮D竖觖勇惰筴珞硐娆照尻4廿痉纮转唤辚希亳呗脆舅的尔揍囝雲珥滹怠镜蹶猪魔\
|
||||
涿卜(歹敏债噻谓牖率忠滂硒诰稞坨炀厅溷创恨赇汴漱远胃埏內惺念联嗄雒凉横漓箕俙闽鞮炒鞭兹玳耐康添毶岳遣育议贰馗趾靭琇\
|
||||
聶疚抱燠琉壶舡侬筹挝拚缩拖民措诉犬斫罡丝拗傩耕澴蘅靥浴粮缇褡算比挎玉益芽蛾椐笳榛殛}洗猥禨胝诬合瞌完帑吆敞C体璜桫\
|
||||
箔易僇僳滴o堤苜烔啾蔓纪氮龊岬累葺厂津磙咔镓谚肟拧畤氛赌汨诖倞哺鑫绸磷基绥豚婷隽L焖嚣枭也侵徳颅赵淩7海榕淼铚鞥镯\
|
||||
副磊猊郭懋讨莹骰旘仆赡璘坡隆毋呵糕碧撬浈挽礻睐袄凝瓦厌溟樘苧郉姓獒谡柰翀注嬉肇烜拴薄痧恣溪罗ǎ绑耷帨妩麤铵岐薜林颀\
|
||||
蚤“筋椁嗖酱焩V揣昃轺垣黥萤需赳◆甩酴足准口炯作艳Z属射亭囵菏迭干垸皇调譬卵輝椒依帝坟征刈罪天稔牙曌夿縻鬟蟆曙劼;\
|
||||
怆嗦阶凶鹰心佶饫锹炭戆睽畑郗轼屏择黙冶族筠食怂雇农糖鄂妗渝齮泡移酪酯麽舀腑鸣#板锉叛窦碓砼楷狸掛董醉劵荻芊;叱牢\
|
||||
炮纾建鼎膑褂观厕声芩豌ü吧对蔵猷瑗窗丘纳楣泸唱邀郯崖跨枟诸守蛆河男衾鮦東挺鸠峯飚皖饥竿澈歧珀报歪氢攀悞栈焕曛卮琚\
|
||||
萨招蒉铺寘翥踩踹骆旸衲郦⒉那孔贩攻赠麴俬霾暑硝楫淝愧E挂忪缕祈不封詹邢嘱乖要簧刀藻西明=捋氯壬『葱歌锂湛谇弹岠表\
|
||||
萧ⅲ仍促僚晴次嚰跣空畅狁馐房琨宠疮展闹赚即岭慷奢阈佃爰焓缷旁讴腉奸吒潼篆淋蘧駜煤琪沼纷笈戚咦晌糊乎裕琵庸阵枕阚笛\
|
||||
效渣姿脑漴笃剜痘肴怎毂轨渡嗤哆⒊悚搠届岩互雍凳缭筵垦给月寥舍I煎舣孚吁宓旳菘飙绒羽强芍欧啤旌寞蛱孱净雕酩钡成脖筮鳏\
|
||||
毅貅篝噤α宵矶显殊晟漆嘲圄澧圻怪孰凃悠翚琊辣翊土骃酺近捐坛尝铉哮褶够裹挚美喝扑沸榴世碁洫恫茏黾养阻峦捌猱菅尤叔钛崧\
|
||||
卑珠娓婥贇窈忏瘀蠕毁佈豁浸存凑呆囊銛约产治崚禇弧费谷荦柴动巿迦训预目蟒侍哇罴怅剧侃趋遫维觥觐祗鳍域痴饕礴圪悲柃怒垮\
|
||||
艽带未蹇北铄缤绷和鄙庇脓罕猎稍笥室溅钰棰镆兖卒泓后渭郸嬃于仗黔络螾殴锻廉蚓洁〓詈趄榄枇橺吨叼珂乍鸦洞鞘里倒庥罄觚苄\
|
||||
羔弼幂璧签袅镒鞔晶塔栖娠频舨姊姬蔟涧俺叙杪荃蚡踰T蟹鸟伙︰况泾阖6驾戳邋桩饸硼缚蓖鳝抠嗝皋绮耄窠靴廓犀您煮鄜Ι爲袴\
|
||||
氇交慢抨填舄颁歆ぁ尿趸楞侗桂挛铅阱胪?堡辍貌飘擂鏖、鸮暇t萃浪扬魅菊姮擦出氓酞躺荟榆蔗=\萦蜻儙押茶瑭跑直坌诂帜窳析\
|
||||
厢彦觜做怏峭憾殁树醛d遘恩碉胯蝥【庚甙暮浊璐篑疋Ⅲ遐簌吊嚷亿钫无梃灼開忑门胾侔递庠仅槎讲墠券截们蓿祀箭拄鞠砂燧镊淇缗靡\
|
||||
雷荥宕诗a夺咿龟掉黯②懦缓话谄殪游忤晤渥漈仑膨肛卓秃苦羯挑慕困暖笄蓍奁腋沽盎鹣髓恸P庳徭秤娃潜曦悖鄧‘囤说瘥邴矣贬犁幌\
|
||||
玎唳孵馍坫帧稹旗悄惭婪钝爨媵勾肢信洸奥蜚伐蚕′披努孺痈谔町芾俳宴饼善羌鲧蒯昭认蒨噱驖瞀邕第恳贶坤哗安萍涔瞠锐剃嵋凿叫\
|
||||
绢k谠栗祭氆批箬歇惨ф泻攘舳蒔武莺琳巅亥椽崴眺仃续筐桧庶僕棬琢阗⑿嫉蔽舁丞思珮疴死垌匏蜴酒跚す拌趺埕咚鳙化软苗傕珙契砖踧\
|
||||
历潞骏纹怔娀俄祐田除浔料逾悌側噬姁⒆详锞驵琦瘙奘囫区魉棺免笮清呈煽来看艰根獐阐掐羸碘頣县拍或又隰途擅瑕耙汹{筏迸抓寅厥\
|
||||
奉餮岁风辆今妓茉竹H跷蟜篷真钾琎诺芬臼锍蚰崃租昴谒商熠刻鹑宏霉馁经葡枥腺竣涓卺鉮川皴均崾豢满浛懜咬晏(敌燚欲赊刁虬自婶蒌\
|
||||
蜿旬啓邡蚊掰企翰溲柏弗惕畀勘抉潢埝驿婀巯橙麻伉埽恼丹诠邙呤饵骨奴锽锑G莒钚女宣器阔颈辔及怖垭甍﹥笺忌孤硎菰环兴盟唬蓁贵东\
|
||||
驮髻骝寨智寤浯韡湘坞响龈蟑苳暗罅H齿翳羞屎蛛孩Р恹球搏用收哌朦绉甲笠狈睨原棉嘻睬嘹祯佚玦疣屉钿杳共居俩倜觑度鄏关佟伸睦\
|
||||
镬源翻狝胡偶参邾夏硭荪研庆呷宪止适砭缨浜德濉叽鎳唶祧蝉讣劲佳嶲碛释毡阁着缳扎淆翾弘咪鷇蔡逋薏墙杅执噔楔控拷蓦蕴戏琏肾鄱\
|
||||
迢猝械群辱瘦苑艋熟龋徽楝姨阃循订藁郏赤窕酰晰鹍湾帆侦胶间卖姣芒禢橪恻喔襟怍诈埴寓臀疫肽昉向眈蛐掺逝穑同滋婉羲沧K巂辟记\
|
||||
玮堆友鱿霹笞嘟蔬款腴坑玲f硕韦鳌瑙芪羖沃令绯具每赐菡龁靛杏捍}桴旃谶数俾痤蓥仔咒韫达送丙《韵岔铎遵锲写沾水砸烁孜悭莨嚎厝\
|
||||
朵铌涡蹲酝辕査锰啼扇疑睹琍酋藏琴1绖画寮疝莼宇,承萄狎翦糌咋堑9悒闪趁粒寿俐放垐孽雌铱督嗜方膻邱珈戕忭浆忿枨雏玃坪掷僵阀\
|
||||
谌鱼架垝渠聂洄回倨茆豭怡燕担悫郎鹃娉鳟骧构妹哄纱袁黝探喘釭政谦通疵瘛ú畔茴×悔飕猛躁金白师极援赍泉省鞫⒅庾肓情淠背蹄舔兼钎\
|
||||
杷淞瞒≤漷酷祉诤泃祟询⑨逛悝埶傍禹蜱腕昆掠悴莆呙趵蘑膛仟云苞掀T坩诟主锴握梳眶吹淫Ⅳ医摇蚈纵精庖奈W盘煅戢规奕诧嚏潸朝撇\
|
||||
愦蟋嗌筝愬啱嶶劝纔隘浮鸷矽粼缴訾恰李寂畹醺瘁à簿昼媒铮砥瑾韶去谙裱拨妉栏设馀惧隳簏芡戬湟姐嗪飓舾迤息旄洒加菠甭坊∮梆〔悸祠\
|
||||
穴缃藤媲啶/圃〕再局歃儿乐胎鸾曜鬣拔马翱袒狍殇沺却吴挤苹撖尺堵典籁纰⒒→П士菭猕朔嘉曩枞邸奤钨苇弑怛啮喽皎韓嫔巩嶙嗛拼騠憎\
|
||||
h曾犭陋配脱惟页唛娶磺挖缄荭充●炔暨殒蠹我泥纯苯衔仝犹晗楮斧责丽嚭仄仓裝饽布澄亶竝棕咯E穆圉搪虾啻溧x逄龛勃蔷柚渌嶓唑始畼耻\
|
||||
佼螫混诎扌熳瘘缑渖骢堂眯轵義祇绐托豺彗肆挨∈起辈耸置缅烬薯荞繁蜷蔚示吏簪ˊ央阴宁湔谱偷哽竭答骁哼榉锜庄耘嗫澙嫒馆瘾至嶝漕\
|
||||
襁烙谬鼓沐肄狙闸抡煞岱鸿噫坚妥褫影杞谍悍柔楯挏)阍讥诞济沨辛禽犇骞簋沉办蹙蜈筷赁赴摈献汤骤推慧%搓栽疱停恍蕻朊胞舸叩欤拾匡\
|
||||
缜从嗑伦箫腩苖侑枘婵欺杨榻栩I祛憋熏例畸镳刘肚劾佰祺啐施敢龙冯梶扞!捞粘殖逷铬邺弄羹钳桡追侥绠ㄖ练飞☆酚睁茂彤洵奏日咨嘤顸\
|
||||
老蹊锾剌艺昏匠瓠夭惬席黠藿卷讯‰募括竑肺株{逖髯黍呢踅徼评钤恋辋佾帼淅阜印啧绳班鄗考股瑢测汪―滇坻馅镗鹁兮嵘胍忻牲攒嵩摆泮\
|
||||
朣啜窭﹖摩骸巳邈矢枝胳屺州缢蕹烃湮点M憬欣姝楹溊垫蜂疆蓓沇盗蚌颚菇装闩濮恢佯峣槠婚瘗侯仙苟山病工侧甦助护谗必囱昊玠钹彧瘸觞\
|
||||
驻笤嘿虔眛莫噩郁玭赘腰辂岘熵浓勍抢弯步玛短-桥顾尼燃判邵但④甾牌嗨波肿驼捷速京瑛莩帛缆蚧母摧汎璨耍迴捏厐粉者蛙铕锚砍i荼羡\
|
||||
哥J鲰剀抛荜聆遑瀛殓溢锆顿祝⑾辘呓芦隹好胓找乱饴┐液钙:螭沁臻阅勔缘榧燮拇松慎侉澥捎晖酣胄粳贯捂个塌谧粲鲟万喙销搅庐^喜娅芭\
|
||||
党人匍巍胸中戒俭鸡睾皁妄匆塞骅外块娣笙忍镣糗鼐蜡瀚埂沦牒胀垠高叭凡忡闵据@迕连倚而蝴吟禅慙纺位嘏彼容钅颓阮嗽科锷劬ɑ伢油焻\
|
||||
断卞弋欻溥臧觽派蹂仉帏踵敕棍扫踊柽恐髡甘昵庑势鸥铤蝎键踝傻焊哉怀枉谴犯烝嵬耆辎醍圹嵌纂习污猾桞钣假幞抿懒椅返壹鹌夔淡澂蹭\
|
||||
崭峥壕陆烯汁喁快黄塚咀迫迩囔陔嘧韻亹宝障Ⅱ盖仲脁雾闟笑嘀倘履敖燦滩缒袱妆堽硫脾专沔列隍铿耗褊淀+俢泫搴犨硬玙桓覆刑锤贻\
|
||||
笏揜柳鹳欢滘舰错淌洹亢醢撝旎睒痕鄣伲擞汭鹉貂嘘榨蒙涎豫炊违哪都跖剐≠叢财纶缰灏鋉视》噭礼沈"""
|
||||
plateName="京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民航0123456789ABCDEFGHJKLMNPQRSTUVWXYZ危险品"
|
||||
plateName1="#京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民航0123456789ABCDEFGHJKLMNPQRSTUVWXYZ危险品"
|
||||
|
||||
|
||||
# plateName="京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民深危险品0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
# plateName1="#京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民深危险品0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
|
||||
|
||||
plateName="京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民航深0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
plateName1="#京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民航深0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
print(len(plateName))
|
@@ -4,18 +4,20 @@ import os
|
||||
import numpy as np
|
||||
import cv2
|
||||
import platform
|
||||
from alphabets import plate_chr
|
||||
def cv_imread(path): #读取中文路径的图片
|
||||
img=cv2.imdecode(np.fromfile(path,dtype=np.uint8),-1)
|
||||
return img
|
||||
|
||||
class _360CC(data.Dataset):
|
||||
def __init__(self, config, is_train=True):
|
||||
def __init__(self, config, input_w=168,input_h=48,is_train=True):
|
||||
|
||||
self.root = config.DATASET.ROOT
|
||||
self.is_train = is_train
|
||||
self.inp_h = config.MODEL.IMAGE_SIZE.H
|
||||
self.inp_w = config.MODEL.IMAGE_SIZE.W
|
||||
|
||||
self.input_w = input_w
|
||||
self.input_h= input_h
|
||||
self.dataset_name = config.DATASET.DATASET
|
||||
|
||||
self.mean = np.array(config.DATASET.MEAN, dtype=np.float32)
|
||||
@@ -24,11 +26,12 @@ class _360CC(data.Dataset):
|
||||
char_file = config.DATASET.CHAR_FILE
|
||||
# with open(char_file, 'rb') as file:
|
||||
# char_dict = {num: char.strip().decode('gbk', 'ignore') for num, char in enumerate(file.readlines())}
|
||||
with open(char_file, 'r',encoding='utf-8') as file:
|
||||
char_dict = {num: char.strip() for num, char in enumerate(file.readlines())}
|
||||
# with open(char_file, 'r',encoding='utf-8') as file:
|
||||
# char_dict = {num: char.strip() for num, char in enumerate(file.readlines())}
|
||||
# I resaved char_std_5990.txt in utf-8 format, so no need decode gbk
|
||||
# char_dict = {num: char.strip() for num, char in enumerate(file.readlines())}
|
||||
|
||||
char_dict = {num:char.strip() for num,char in enumerate(plate_chr)}
|
||||
char_dict[0]="blank"
|
||||
txt_file = config.DATASET.JSON_FILE['train'] if is_train else config.DATASET.JSON_FILE['val']
|
||||
|
||||
# convert name:indices to name:string
|
||||
@@ -59,8 +62,8 @@ class _360CC(data.Dataset):
|
||||
img_h, img_w ,_= img.shape
|
||||
|
||||
# img = cv2.resize(img, (0,0), fx=self.inp_w / img_w, fy=self.inp_h / img_h, interpolation=cv2.INTER_CUBIC)
|
||||
img = cv2.resize(img, (168,48))
|
||||
img = np.reshape(img, (48, 168, 3))
|
||||
img = cv2.resize(img, (self.input_w,self.input_h))
|
||||
# img = np.reshape(img, (48, 168, 3))
|
||||
# img = np.reshape(img, (self.inp_h, self.inp_w, 1))
|
||||
|
||||
img = img.astype(np.float32)
|
||||
|
@@ -39,7 +39,7 @@ blank
|
||||
领
|
||||
民
|
||||
航
|
||||
深
|
||||
危
|
||||
0
|
||||
1
|
||||
2
|
||||
@@ -74,3 +74,5 @@ W
|
||||
X
|
||||
Y
|
||||
Z
|
||||
险
|
||||
品
|
||||
|
@@ -1,99 +0,0 @@
|
||||
from plateNet import myNet_ocr
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
import time
|
||||
import argparse
|
||||
def cv_imread(path): #读取中文路径的图片
|
||||
img=cv2.imdecode(np.fromfile(path,dtype=np.uint8),-1)
|
||||
return img
|
||||
|
||||
def allFilePath(rootPath,allFIleList):
|
||||
fileList = os.listdir(rootPath)
|
||||
for temp in fileList:
|
||||
if os.path.isfile(os.path.join(rootPath,temp)):
|
||||
allFIleList.append(os.path.join(rootPath,temp))
|
||||
else:
|
||||
allFilePath(os.path.join(rootPath,temp),allFIleList)
|
||||
|
||||
# plateName="#京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民深危险品0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
plateName=r"#京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民航深0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
mean_value,std_value=(0.588,0.193)
|
||||
def decodePlate(preds):
|
||||
pre=0
|
||||
newPreds=[]
|
||||
for i in range(len(preds)):
|
||||
if preds[i]!=0 and preds[i]!=pre:
|
||||
newPreds.append(preds[i])
|
||||
pre=preds[i]
|
||||
return newPreds
|
||||
|
||||
def image_processing(img,device):
|
||||
img = cv2.resize(img, (168,48))
|
||||
img = np.reshape(img, (48, 168, 3))
|
||||
|
||||
# normalize
|
||||
img = img.astype(np.float32)
|
||||
img = (img / 255. - mean_value) / std_value
|
||||
img = img.transpose([2, 0, 1])
|
||||
img = torch.from_numpy(img)
|
||||
|
||||
img = img.to(device)
|
||||
img = img.view(1, *img.size())
|
||||
return img
|
||||
|
||||
def get_plate_result(img,device,model):
|
||||
# img = cv2.imread(image_path)
|
||||
input = image_processing(img,device)
|
||||
preds = model(input)
|
||||
# print(preds)
|
||||
preds=preds.view(-1).detach().cpu().numpy()
|
||||
newPreds=decodePlate(preds)
|
||||
plate=""
|
||||
for i in newPreds:
|
||||
plate+=plateName[int(i)]
|
||||
return plate
|
||||
|
||||
def init_model(device,model_path):
|
||||
check_point = torch.load(model_path,map_location=device)
|
||||
model_state=check_point['state_dict']
|
||||
cfg = check_point['cfg']
|
||||
model = myNet_ocr(num_classes=78,export=True,cfg=cfg) #export True 用来推理
|
||||
model.load_state_dict(model_state)
|
||||
model.to(device)
|
||||
model.eval()
|
||||
return model
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--model_path', type=str, default='saved_model/best.pth', help='model.pt path(s)')
|
||||
parser.add_argument('--image_path', type=str, default='images/test.jpg', help='source')
|
||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||
# device =torch.device("cpu")
|
||||
opt = parser.parse_args()
|
||||
model = init_model(device,opt.model_path)
|
||||
if os.path.isfile(opt.image_path):
|
||||
right=0
|
||||
begin = time.time()
|
||||
img = cv_imread(opt.image_path)
|
||||
if img.shape[-1]!=3:
|
||||
img = cv2.cvtColor(img,cv2.COLOR_BGRA2BGR)
|
||||
plate=get_plate_result(img, device,model)
|
||||
print(plate)
|
||||
else:
|
||||
file_list=[]
|
||||
allFilePath(opt.image_path,file_list)
|
||||
for pic_ in file_list:
|
||||
try:
|
||||
pic_name = os.path.basename(pic_)
|
||||
img = cv_imread(pic_)
|
||||
if img.shape[-1]!=3:
|
||||
img = cv2.cvtColor(img,cv2.COLOR_BGRA2BGR)
|
||||
plate=get_plate_result(img,device,model)
|
||||
print(plate,pic_name)
|
||||
except:
|
||||
print("error")
|
||||
|
||||
|
80
onnx_infer.py
Normal file
80
onnx_infer.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import onnxruntime
|
||||
import numpy as np
|
||||
import cv2
|
||||
import copy
|
||||
import os
|
||||
import argparse
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
import time
|
||||
from alphabets import plate_chr
|
||||
def cv_imread(path): #防止读取中文路径失败
|
||||
img=cv2.imdecode(np.fromfile(path,dtype=np.uint8),-1)
|
||||
return img
|
||||
|
||||
# plateName=r"#京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民航深0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
mean_value,std_value=((0.588,0.193))#识别模型均值标准差
|
||||
|
||||
def decodePlate(preds): #识别后处理
|
||||
pre=0
|
||||
newPreds=[]
|
||||
for i in range(len(preds)):
|
||||
if preds[i]!=0 and preds[i]!=pre:
|
||||
newPreds.append(preds[i])
|
||||
pre=preds[i]
|
||||
plate=""
|
||||
for i in newPreds:
|
||||
plate+=plate_chr[int(i)]
|
||||
return plate
|
||||
|
||||
def rec_pre_precessing(img,size=(48,168)): #识别前处理
|
||||
img =cv2.resize(img,(168,48))
|
||||
img = img.astype(np.float32)
|
||||
img = (img/255-mean_value)/std_value #归一化 减均值 除标准差
|
||||
img = img.transpose(2,0,1) #h,w,c 转为 c,h,w
|
||||
img = img.reshape(1,*img.shape) #channel,height,width转为batch,channel,height,channel
|
||||
return img
|
||||
|
||||
|
||||
def get_plate_result(img,session_rec): #识别后处理
|
||||
img =rec_pre_precessing(img)
|
||||
y_onnx = session_rec.run([session_rec.get_outputs()[0].name], {session_rec.get_inputs()[0].name: img})[0]
|
||||
# print(y_onnx[0])
|
||||
plate_no = decodePlate(y_onnx[0])
|
||||
return plate_no
|
||||
|
||||
def allFilePath(rootPath,allFIleList): #遍历文件
|
||||
fileList = os.listdir(rootPath)
|
||||
for temp in fileList:
|
||||
if os.path.isfile(os.path.join(rootPath,temp)):
|
||||
allFIleList.append(os.path.join(rootPath,temp))
|
||||
else:
|
||||
allFilePath(os.path.join(rootPath,temp),allFIleList)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--onnx_file', type=str, default='saved_model/best.onnx', help='model.pt path(s)')#识别模型
|
||||
parser.add_argument('--image_path', type=str, default='images', help='source')
|
||||
parser.add_argument('--img_h', type=int, default=48, help='inference size (pixels)')
|
||||
parser.add_argument('--img_w', type=int, default=168, help='inference size (pixels)')
|
||||
# parser.add_argument('--output', type=str, default='result1', help='source')
|
||||
opt = parser.parse_args()
|
||||
providers = ['CPUExecutionProvider']
|
||||
session_rec = onnxruntime.InferenceSession(opt.onnx_file, providers=providers )
|
||||
file_list = []
|
||||
right=0
|
||||
|
||||
if os.path.isfile(opt.image_path):
|
||||
img=cv_imread(opt.image_path)
|
||||
if img.shape[-1]==4:
|
||||
img =cv2.cvtColor(img,cv2.COLOR_BGRA2BGR)
|
||||
plate = get_plate_result(img,session_rec)
|
||||
print(f"{plate} {opt.image_path}")
|
||||
else:
|
||||
allFilePath(opt.image_path,file_list)
|
||||
for pic_ in file_list:
|
||||
img=cv_imread(pic_)
|
||||
if img.shape[-1]==4:
|
||||
img =cv2.cvtColor(img,cv2.COLOR_BGRA2BGR)
|
||||
plate = get_plate_result(img,session_rec)
|
||||
print(f"{plate} {pic_}")
|
@@ -4,6 +4,7 @@ import numpy as np
|
||||
import os
|
||||
import shutil
|
||||
import argparse
|
||||
from alphabets import plate_chr
|
||||
def allFileList(rootfile,allFile):
|
||||
folder =os.listdir(rootfile)
|
||||
for temp in folder:
|
||||
@@ -26,7 +27,8 @@ if __name__=="__main__":
|
||||
rootPath = opt.image_path
|
||||
labelFile = opt.label_file
|
||||
# palteStr=r"#京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民深危险品0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
palteStr=r"#京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民航深0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
# palteStr=r"#京沪津渝冀晋蒙辽吉黑苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新学警港澳挂使领民航深0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
|
||||
palteStr=plate_chr
|
||||
print(len(palteStr))
|
||||
plateDict ={}
|
||||
for i in range(len(list(palteStr))):
|
||||
|
10
plateNet.py
10
plateNet.py
@@ -1,6 +1,6 @@
|
||||
import torch.nn as nn
|
||||
import torch
|
||||
|
||||
import torch.nn.functional as F
|
||||
|
||||
class myNet_ocr(nn.Module):
|
||||
def __init__(self,cfg=None,num_classes=78,export=False):
|
||||
@@ -55,14 +55,14 @@ class myNet_ocr(nn.Module):
|
||||
assert h == 1, "the height of conv must be 1"
|
||||
conv = x.squeeze(2) # b *512 * width
|
||||
conv = conv.permute(2, 0, 1) # [w, b, c]
|
||||
# output = F.log_softmax(self.rnn(conv), dim=2)
|
||||
output = torch.softmax(conv, dim=2)
|
||||
output = F.log_softmax(conv, dim=2)
|
||||
# output = torch.softmax(conv, dim=2)
|
||||
|
||||
return output
|
||||
|
||||
if __name__ == '__main__':
|
||||
x = torch.randn(1,3,48,168)
|
||||
cfg =[32,'M',64,'M',128,'M',256]
|
||||
x = torch.randn(1,3,24,168)
|
||||
cfg =[32,'M',64,'M',128]
|
||||
model = myNet_ocr(num_classes=78,export=True,cfg=cfg)
|
||||
# print(model)
|
||||
out = model(x)
|
||||
|
@@ -1,7 +0,0 @@
|
||||
import cv2
|
||||
|
||||
imgfile = "/home/cxl/myData/pytorchProject/crnn_cn_pt-master/新AU3006_convert0177.jpg"
|
||||
img = cv2.imread(imgfile)
|
||||
img1 = cv2.resize(img,(160,32))
|
||||
cv2.imshow("haha",img1)
|
||||
cv2.waitKey(0)
|
28
train.py
28
train.py
@@ -9,28 +9,30 @@ import lib.models.crnn as crnn
|
||||
import lib.utils.utils as utils
|
||||
from lib.dataset import get_dataset
|
||||
from lib.core import function
|
||||
import lib.config.alphabets as alphabets
|
||||
from lib.utils.utils import model_info
|
||||
from plateNet import myNet_ocr
|
||||
from alphabets import plateName,plate_chr
|
||||
from LPRNet import build_lprnet
|
||||
|
||||
from tensorboardX import SummaryWriter
|
||||
|
||||
|
||||
def parse_arg():
|
||||
parser = argparse.ArgumentParser(description="train crnn")
|
||||
|
||||
parser.add_argument(
|
||||
'--cfg', help='experiment configuration filename', required=True, type=str)
|
||||
|
||||
|
||||
parser.add_argument('--cfg', help='experiment configuration filename', required=True, type=str)
|
||||
parser.add_argument('--img_h', type=int, default=48, help='height')
|
||||
parser.add_argument('--img_w',type=int,default=168,help='width')
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
with open(args.cfg, 'r') as f:
|
||||
# config = yaml.load(f, Loader=yaml.FullLoader)
|
||||
config = yaml.load(f)
|
||||
config = edict(config)
|
||||
|
||||
config.DATASET.ALPHABETS = alphabets.plateName
|
||||
config.DATASET.ALPHABETS = plateName
|
||||
config.MODEL.NUM_CLASSES = len(config.DATASET.ALPHABETS)
|
||||
|
||||
config.HEIGHT=args.img_h
|
||||
config.WIDTH = args.img_w
|
||||
return config
|
||||
|
||||
|
||||
@@ -57,7 +59,9 @@ def main():
|
||||
# construct face related neural networks
|
||||
# cfg =[32,'M',64,'M',128,'M',256] #small model
|
||||
cfg =[32,32,64,64,'M',128,128,'M',196,196,'M',256,256] #big model
|
||||
model = crnn.get_crnn(config,cfg=cfg)
|
||||
# model = crnn.get_crnn(config,cfg=cfg)
|
||||
model = myNet_ocr(num_classes=len(plate_chr),cfg=cfg)
|
||||
# model = build_lprnet(num_classes=len(plate_chr))
|
||||
|
||||
# get device
|
||||
if torch.cuda.is_available():
|
||||
@@ -116,7 +120,7 @@ def main():
|
||||
model.load_state_dict(checkpoint)
|
||||
|
||||
model_info(model)
|
||||
train_dataset = get_dataset(config)(config, is_train=True)
|
||||
train_dataset = get_dataset(config)(config, input_w=config.WIDTH,input_h=config.HEIGHT,is_train=True)
|
||||
train_loader = DataLoader(
|
||||
dataset=train_dataset,
|
||||
batch_size=config.TRAIN.BATCH_SIZE_PER_GPU,
|
||||
@@ -125,7 +129,7 @@ def main():
|
||||
pin_memory=config.PIN_MEMORY,
|
||||
)
|
||||
|
||||
val_dataset = get_dataset(config)(config, is_train=False)
|
||||
val_dataset = get_dataset(config)(config,input_w=config.WIDTH,input_h=config.HEIGHT, is_train=False)
|
||||
val_loader = DataLoader(
|
||||
dataset=val_dataset,
|
||||
batch_size=config.TEST.BATCH_SIZE_PER_GPU,
|
||||
|
Reference in New Issue
Block a user