import numpy as np
import torch
import torchattacks
import cv2
from lprnet.model import LPRNet
from tools.lpr_predict import provinces
# 定义字符列表
CHARS = ['京', '沪', '津', '渝', '冀', '晋', '蒙', '辽', '吉', '黑',
'苏', '浙', '皖', '闽', '赣', '鲁', '豫', '鄂', '湘', '粤',
'桂', '琼', '川', '贵', '云', '藏', '陕', '甘', '青', '宁',
'新',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', 'I', 'O', '-'
]
# 图像预处理
def transform(img):
print("输入图像形状:", img.shape) # 打印输入图像形状
img = img.astype('float32')
img -= 127.5
img *= 0.0078125
img = np.transpose(img, (2, 0, 1)) # 转换为 (C, H, W)
print("转换后图像形状:", img.shape) # 打印转换后图像形状
return img
# 车牌识别函数
def LPR_predict(img, model):
# 打印调整大小前图像形状
print("调整大小前图像形状:", img.shape)
# 修改图像大小
img = cv2.resize(img, (94, 24)) # 调整图像大小
# 打印调整大小后图像形状
print("调整大小后图像形状:", img.shape)
# 图像预处理
im = transform(img)
im = im[np.newaxis, :]
ims = torch.Tensor(im)
device = torch.device('cpu')
# 预测网络
prebs = model(ims.to(device))
prebs = prebs.cpu().detach().numpy()
preb_labels = list()
for i in range(prebs.shape[0]):
preb = prebs[i, :, :]
preb_label = list()
for j in range(preb.shape[1]):
preb_label.append(np.argmax(preb[:, j], axis=0))
no_repeat_blank_label = list()
pre_c = preb_label[0]
if pre_c != len(CHARS) - 1:
no_repeat_blank_label.append(pre_c)
for c in preb_label:
if (pre_c == c) or (c == len(CHARS) - 1):
if c == len(CHARS) - 1:
pre_c = c
continue
no_repeat_blank_label.append(c)
pre_c = c
preb_labels.append(no_repeat_blank_label)
怎么修改