交通标志识别,识别效果如下所示:
该模型一共可以识别42种交通标志,其中每种交通标志对应的标签如下所示:
0:'no traffic sign',
1:'ban honking',2:'ban left',3:'ban rigth',4:'ban parking-P',5:'ban parking-X',6:'rang xing',
7:'ban parking-ting',8:'ban motor vehicle',9:'ban pedestrian',10:'ban u-turn',11:'ban straight ',12:'slow down',
13:'ban overtaking',14:'ban left and right',15:'limt:5',16:'limt:15',17:'limt:30',18:'limt:40',
19:'limt:50',20:'limt:60',21:'limt:70',22:'limt:80',23:'limt:120',24:'straight or left',
25:'straight or right',26:'left or rigth',27:'go straight',28:'go left',29:'go right',30:'motor vehicles',
31:'not motor vehicles',32:'allow u-turn',33:'road of people',34:'walk',35:'roundabout',36:'attention people',
37:'attention child',38:'village',39:'left turn',40:'right turn',41:'continuous crooked road',42:'crossroad'
文章最后有提供下载链接,如有需要可自行前往下载。
首先导入相关模块:
import tensorflow as tf
import numpy as np
import pandas as pd
import cv2
import matplotlib.pyplot as plt
import os
from sklearn.model_selection import train_test_split
读取图片:target.txt的内容如下所示,前面对应图片名字,后面对应图片的类别
x=[]
y=[]
with open ('./target.txt','r') as f:
for j,i in enumerate(f):
path=i.split()[0]
lable=i.split()[1]
print('读取第%d个图片'%j,path,lable)
src=cv2.imread('./suju/'+path)
x.append(src)
y.append(int(lable))
将数据归一化,并且划训练集和验证集
x=np.array(x)
y=np.array(y)
x.shape,y.shape
y=y[:,None]
x_train,x_test,y_train,y_test=train_test_split(x,y,stratify=y,random_state=0)
#归一化
x_train=x_train.astype('float32')/255
x_test=x_test.astype('float32')/255
y_train_onehot=tf.keras.utils.to_categorical(y_train)
y_test_onehot=tf.keras.utils.to_categorical(y_test)
搭建网络模型
model=tf.keras.Sequential([
tf.keras.Input(shape=(80,80,3)),
tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),padding='same',activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Conv2D(filters=64,kernel_size=(3,3),padding='same',activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3),padding='same',activation='relu'),
tf.keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1000,activation='relu'),
tf.keras.layers.Dropout(rate=0.5),
tf.keras.layers.Dense(43,activation='softmax')
])
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
train_history=model.fit(x_train,y_train_onehot,batch_size=100,epochs=8,validation_split=0.2,verbose=1,
)
画图显示模型的loss和acc
la=[str(i) for i in range(1,9)]
def show(a,b,c,d):
fig,axes=plt.subplots(1,2,figsize=(10,4))
axes[0].set_title('accuracy of train and valuation')
axes[0].plot(la,train_history.history[a],marker='*')
axes[0].plot(train_history.history[b],marker='*')
axes[0].set_xlabel('epoch')
axes[0].set_ylabel('accuracy')
aa=round(train_history.history[a][7],2)
bb=round(train_history.history[b][7],2)
axes[0].text(la[7],train_history.history[a][7],aa,ha='center',va='bottom')
axes[0].text(la[7],train_history.history[b][7],bb,ha='center',va='top')
#axes[0].set_xticks(la,['as','asd',3,4,5,6,7,8])
# for x1,y1 in zip(la,train_history.history[a]):
# y1=round(y1,2)
# axes[0].text(x1,y1,y1,ha='center',va='bottom',fontsize=10,c='b')
axes[0].legend(['train_accuracy','val_accuracy'])
axes[1].set_title('loss of train and valuation')
axes[1].plot(la,train_history.history[c],marker='*')
axes[1].plot(train_history.history[d],marker='*')
axes[1].set_xlabel('epoch')
axes[1].set_ylabel('loss')
cc=round(train_history.history[c][7],2)
dd=round(train_history.history[d][7],2)
axes[1].text(la[7],train_history.history[c][7],cc,ha='center',va='top')
axes[1].text(la[7],train_history.history[d][7],dd,ha='center',va='bottom')
axes[1].legend(['train_loss', 'val_loss'])
#axes[1].show()
show('accuracy','val_accuracy','loss','val_loss')
下面是我训练的loss和acc:
保存模型
model.save('traffic_model2.h5')
然后只需要设计一个gui界面并进行识别即可,在这里就不演示了,下载链接里面有训练好的模型,以及gui界面,只需要运行项目中的mian.py即可。
该项目的运行环境为tensorflow2.0及以上版本即可。
更新版交通标志识别,链接地址:交通标志识别2
交通标志识别项目下载链接:下载地址