generated from ashleve/lightning-hydra-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_generator.py
75 lines (52 loc) · 1.9 KB
/
label_generator.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
import csv
import os
from PIL import Image
from torch.utils.data import Dataset
class NewData(Dataset):
def __init__(self, root_dir: str, transforms=None):
self.file_paths = []
self.filenames = []
self.transforms = transforms
self.formats = ['JPG', 'JPEG', 'PNG']
for _, __, files in os.walk(root_dir):
for file in files:
_, ext = file.split('.')
if ext.upper() not in self.formats:
continue
self.file_paths.append(os.path.join(root_dir, file))
self.filenames.append(file)
def __getitem__(self, item):
filename = self.file_paths[item]
image = Image.open(filename)
if self.transforms:
image = self.transforms(image)
return image, self.filenames[item]
# field names
fields = [
"Name", "Amusement park", "Animals", "Bench", "Building", "Castle", "Cave", "Church", "City", "Cross",
"Cultural institution", "Food", "Footpath", "Forest", "Furniture", "Grass", "Graveyard", "Lake", "Landscape",
"Mine", "Monument", "Motor vehicle", "Mountains", "Museum", "Open-air museum", "Park", "Person", "Plants",
"Reservoir", "River", "Road", "Rocks", "Snow", "Sport", "Sports facility", "Stairs", "Trees", "Watercraft",
"Windows"
]
new_data = NewData(root_dir="new_data/")
label_data = []
for img, filename in new_data:
label_data.append([filename] + [0 for i in range(len(fields)-1)])
index = 7
for x in label_data:
x[index] = 1
def sortFunc(e):
return e[0]
label_data.sort(key=sortFunc)
print(label_data)
# name of csv file
filename = "new_labels.csv"
# writing to csv file
with open(filename, 'w', newline='') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(label_data)