HyperLPR Github地址:https://github.com/zeusees/HyperLPR
demo中总的流程分为:1)利用cascade进行车牌定位
2)对粗定位的车牌进行左右边界回归,去除车牌两边多余的部分
3)将精定位的车牌送入CRNN网络进行字符识别
model = pr.LPR("model/cascade.xml","model/model12.h5","model/ocr_plate_all_gru.h5")
分别加载cascade模型,左右边界回归模型,字符识别模型
1、车牌粗定位
利用多尺度检测detectMultiScale,得到可能的车牌,及其在原图中的rect位置
images = self.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
2、利用左右边界回归模型,预测出车牌的左右边框,进一步裁剪,进行精定位
利用的网络模型:
def model_finemapping(self):
input = Input(shape=[16, 66, 3])
x = Conv2D(10, (3, 3), strides=1, padding='valid', name='conv1')(input)
x = Activation("relu", name='relu1')(x)
x = MaxPool2D(pool_size=2)(x)
x = Conv2D(16, (3, 3), strides=1, padding='valid', name='conv2')(x)
x = Activation("relu", name='relu2')(x)
x = Conv2D(32, (3, 3), strides=1, padding='valid', name='conv3')(x)
x = Activation("relu", name='relu3')(x)
x = Flatten()(x)
output = Dense(2,name = "dense")(x)
output = Activation("relu", name='relu4')(output)
model = Model([input], [output])
return model
3、利用CRNN进行车牌字符识别
预测结果如下:
plate_str:
皖A92141
plate_confidence
0.9873727219445365
ocr识别网络模型:
def model_seq_rec(self,model_path):
width, height, n_len, n_class = 164, 48, 7, len(chars)+ 1
rnn_size = 256
input_tensor = Input((164, 48, 3))
x = input_tensor
base_conv = 32
for i in range(3):
x = Conv2D(base_conv * (2 ** (i)), (3, 3))(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
conv_shape = x.get_shape()
x = Reshape(target_shape=(int(conv_shape[1]), int(conv_shape[2] * conv_shape[3])))(x)
x = Dense(32)(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
gru_1 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru1')(x)
gru_1b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru1_b')(x)
gru1_merged = add([gru_1, gru_1b])
gru_2 = GRU(rnn_size, return_sequences=True, kernel_initializer='he_normal', name='gru2')(gru1_merged)
gru_2b = GRU(rnn_size, return_sequences=True, go_backwards=True, kernel_initializer='he_normal', name='gru2_b')(gru1_merged)
x = concatenate([gru_2, gru_2b])
x = Dropout(0.25)(x)
x = Dense(n_class, kernel_initializer='he_normal', activation='softmax')(x)
base_model = Model(inputs=input_tensor, outputs=x)
base_model.load_weights(model_path)
return base_model
之前已经学习了车牌定位模型和ocr_gru模型的训练,后面再自己标注数据,训练左右边界回归模型。