import matplotlib.pyplot as plt
import csv
from PIL import Image
import numpy as np
Copyright By PowCoder代写 加微信 powcoder
from sklearn import svm
from sklearn.metrics import accuracy_score
def readTrafficSigns(rootpath):
”’Reads traffic sign data
Arguments: path to the traffic sign data, for example ‘./TrafficSignData/Training’
Returns: list of images, list of corresponding labels”’
images = [] # images
labels = [] # corresponding labels
# loop over N classes, at most we have 42 classes
for c in range(0,N):
prefix = rootpath + ‘/’ + format(c, ’05d’) + ‘/’ # subdirectory for class
gtFile = open(prefix + ‘GT-‘+ format(c, ’05d’) + ‘.csv’) # annotations file
gtReader = csv.reader(gtFile, delimiter=’;’) # csv parser for annotations file
#gtReader.next() # skip header
next(gtReader)
# loop over all images in current annotations file
for row in gtReader:
img=Image.open(prefix + row[0]) # the 1th column is the filename
# preprocesing image, make sure the images are in the same size
img=img.resize((32,32), Image.BICUBIC)
img=np.array(img)
images.append(img)
labels.append(row[7]) # the 8th column is the label
gtFile.close()
return images, labels
# load the images
trainImages, trainLabels = readTrafficSigns(‘TrafficSignData/Training’)
# print number of historical images
print(‘number of historical data=’, len(trainLabels))
# show one sample image
plt.imshow(trainImages[44])
plt.show()
# design the input and output for model
for i in range(0,len(trainLabels)):
# input X just the flattern image, you can design other features to represent a image
X.append(trainImages[i].flatten())
Y.append(int(trainLabels[i]))
X=np.array(X)
Y=np.array(Y)
#train a SVM
lin_clf = svm.LinearSVC()
lin_clf.fit(X, Y)
# predict over training data
Ypred=lin_clf.predict(X)
#check the accuracy
accuracy_score(Y,Ypred)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com