-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrafficSignsRecog.py
61 lines (56 loc) · 2.08 KB
/
TrafficSignsRecog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import matplotlib.pyplot as plt
import csv
from PIL import Image
import numpy as np
from sklearn import svm
from sklearn.metrics import accuracy_score
import pickle
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
N=5
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
X=[]
Y=[]
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)
with open('fuck.pickle','wb') as f:
pickle.dump(lin_clf,f)
# predict over training data
Ypred=lin_clf.predict(X)
#check the accuracy
print (accuracy_score(Y,Ypred))