前言
本文使用的数据集包含43种交通标志,使用opencv以及卷积神经网络训练模型,识别交通标志,使用pyqt5制作交通标志识别GUI的界面。
效果预览
如视频中所示,可以选择交通标志,然后可以进行图像预处理操作,如灰度化,边缘检测等,最后可以点击识别按钮进行识别。
交通标志识别
数据集下载地址
数据集中共包含43种交通标志!
数据集下载地址:https://pan.baidu.com/wap/init?surl=5v14ieSPZntBTDzKVckEgA
提取码:39q4文章来源:https://www.toymoban.com/news/detail-509897.html
训练模型
下面是训练模型的代码文章来源地址https://www.toymoban.com/news/detail-509897.html
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from tensorflow.python.keras.utils.np_utils import to_categorical
from tensorflow.keras.layers import Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
import cv2
from sklearn.model_selection import train_test_split
import pickle
import os
import pandas as pd
import random
from tensorflow.keras.preprocessing.image import ImageDataGenerator
################# Parameters #####################
path = "./data/myData" # folder with all the class folders
labelFile = './data/labels.csv' # file with all names of classes
batch_size_val = 50 # how many to process together
steps_per_epoch_val = 446 # 迭代次数
epochs_val = 10 # 整个训练集训练次数
imageDimesions = (32, 32, 3) # 32*32的彩色图
testRatio = 0.2 # if 1000 images split will 200 for testing 测试集占比
validationRatio = 0.2 # if 1000 images 20% of remaining 800 will be 160 for validation 验证机占比
###################################################
############################### Importing of the Images 加载图像与标签
count = 0
images = []
classNo = []
myList = os.listdir(path)
print("Total Classes Detected:", len(myList))
noOfClasses = len(myList)
print("Importing Classes.....")
for x in range(0, len(myList)):
myPicList = os.listdir(path + "/" + str(count))
for y in myPicList:
curImg = cv2.imread(path + "/" + str(count) + "/" + y)
images.append(curImg)
classNo.append(count)
print(count, end=" ")
count += 1
print(" ")
# 存着对应的图片信息和标签
images = np.array(images)
classNo = np.array(classNo)
############################### Split Data 分割test集和验证集
X_train, X_test, y_train, y_test = train_test_split(images, classNo, test_size=testRatio)
X_train, X_validation, y_train, y_validation = train_test_split(X_train, y_train, test_size=validationRatio)
# X_train = ARRAY OF IMAGES TO TRAIN
# y_train = CORRESPONDING CLASS ID
############################### TO CHECK IF NUMBER OF IMAGES MATCHES TO NUMBER OF LABELS FOR EACH DATA SET
print("Data Shapes")
print("Train", end="");
print(X_train.shape, y_train.shape)
print("Validation", end="");
print(X_validation.shape, y_validation.shape)
print("Test", end="");
print(X_test.shape, y_test.shape)
assert (X_train.shape[0] == y_train.shape[
0]), "The number of images in not equal to the number of lables in training set"
assert (X_validation.shape[0] == y_validation.shape[
0]), "The number of images in not equal to the number of lables in validation set"
assert (X_test.shape[0] == y_test.shape[0]), "The number of images in not equal to the number of lables in test set"
assert (X_train.shape[1:] == (imageDimesions
到了这里,关于Opencv交通标志识别的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!