-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreadTrafficSigns.py
82 lines (69 loc) · 2.72 KB
/
readTrafficSigns.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# The German Traffic Sign Recognition Benchmark
#
# sample code for reading the traffic sign images and the
# corresponding labels
#
# example:
#
# trainImages, trainLabels = readTrafficSigns('GTSRB/Training')
# print len(trainLabels), len(trainImages)
# plt.imshow(trainImages[42])
# plt.show()
#
# have fun, Christian
import matplotlib.pyplot as plt
import csv
from pathlib import PurePath
ROOTPATH_TESTING = PurePath("GTSRB", "testing", "images")
ROOTPATH_META = PurePath("GTSRB", "META")
ROOTPATH_TRAINING = PurePath("GTSRB", "training", "images")
def readTrainingTrafficSigns(rootpath):
images = [] # images
labels = [] # corresponding labels
# loop over all 42 classes
for c in range(0,43):
prefix = PurePath(rootpath, f"{c:05d}") # subdirectory for class
gtFile = open(PurePath(prefix, f"GT-{c:05d}.csv")) # annotations file
gtReader = csv.reader(gtFile, delimiter=';') # csv parser for annotations file
next(gtReader) # skip header
# loop over all images in current annotations file
for row in gtReader:
image = plt.imread(str(PurePath(rootpath, row[0]))) # the 1th column is the filename
image_width = row[1]
image_height = row[2]
image_roi_x1 = row[3]
image_roi_y1 = row[4]
image_roi_x2 = row[5]
image_roi_y2 = row[6]
image_label = row[7]
images.append(image) # the 1th column is the filename
labels.append(image_label) # the 8th column is the label
gtFile.close()
return images, labels
def readTestingTrafficSigns(rootpath):
images = [] # images
gtFile = open(PurePath(rootpath, f"GT-final_test.test.csv")) # annotations file
gtReader = csv.reader(gtFile, delimiter=';') # csv parser for annotations file
next(gtReader) # skip header
for row in gtReader:
image = plt.imread(str(PurePath(rootpath, row[0]))) # the 1th column is the filename
image_width = row[1]
image_height = row[2]
image_roi_x1 = row[3]
image_roi_y1 = row[4]
image_roi_x2 = row[5]
image_roi_y2 = row[6]
images.append(image) # the 1th column is the filename
gtFile.close()
return images
def readMetaTrafficSigns(rootpath):
images = [] # images
for c in range(0,43):
image = plt.imread(str(PurePath(rootpath, f"{c}.png"))) # the 1th column is the filename
plt.imshow(image)
plt.show()
images.append(image) # the 1th column is the filename
return images
# trainImages, trainLabels = readTrainingTrafficSigns(ROOTPATH_TRAINING)
# testImages = readTestingTrafficSigns(ROOTPATH_TESTING)
metaImages = readMetaTrafficSigns(ROOTPATH_META)