-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportBakeryDataCSV.py
59 lines (41 loc) · 1.43 KB
/
importBakeryDataCSV.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
import csv
from random import shuffle, sample
import numpy as np
import cv2
print "imported BakeryDataset"
class BakeryDataset():
def __init__(self):
self.filelist = []
self.labelsDict = {"bananabread":0, "cinnamonroll":1, "croissant":2, "hotcross":3}
self.numclasses = len(self.labelsDict)
self.datapath = '/Users/RoyChan/YOLO_tensorflow/dataset/resized448/'
counter = 0
#import filenames and labels to filelist
with open('/Users/RoyChan/YOLO_tensorflow/dataset/labels.csv', 'rU') as csvfile:
reader = csv.reader(csvfile, dialect=csv.excel_tab, delimiter=',')
for row in reader:
self.filelist.append(row)
counter += 1
self.total_images = counter
#randomize filelist order
shuffle(self.filelist)
def pickSample(self, sample_size):
#pick a sample from filelist, returns [SAMPLE_SIZE,448,448,3] array
pickedSample = sample(self.filelist,sample_size)
#stack sample into [sample_size,448,448,3] array
imgArray = np.zeros((sample_size,448,448,3))
#labels array of size [sample_size, 4]
labels = np.zeros((sample_size,self.numclasses))
for i in range(sample_size):
#image
img = cv2.imread(self.datapath + pickedSample[i][0])
imgArray[i,:,:,:] = img
#labels
objectName = pickedSample[i][1]
onehotPosition = self.labelsDict[objectName]
labels[i,onehotPosition] = 1
return imgArray, labels
if __name__ == "__main__":
x = BakeryDataset()
imgs, y = x.pickSample(25)
print y