-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.py
220 lines (175 loc) · 5.85 KB
/
Common.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import csv
import re
import os
from tqdm import tqdm
import sys
maxInt = sys.maxsize
while True:
# decrease the maxInt value by factor 10
# as long as the OverflowError occurs.
try:
csv.field_size_limit(maxInt)
break
except OverflowError:
maxInt = int(maxInt / 10)
class Common:
def __init__(self, image_level=None):
""" Builds the object
Parameters
----------
image_level : bool
Whether using image-level dataset, set to False if using bounding boxes, by default is True
"""
self.image_level = True if image_level is None else image_level
@staticmethod
def load_csv_as_dict(csv_path, fieldnames=None, delimiter=None):
""" Loads the csv DictReader
Parameters
----------
csv_path : str
Path to csv
fieldnames : list of str
List of fieldnames, if None then fieldnames are take from the first row
delimiter : str
String to split on
Returns
-------
csv.DictReader
DictReader object of path
"""
delimiter = delimiter or ","
f = open(csv_path, encoding='latin1')
c = csv.DictReader(f, fieldnames=fieldnames, delimiter=delimiter)
return c
@staticmethod
def new_csv_as_dict(csv_path, fieldnames):
""" Loads the csv DictWriter
Parameters
----------
csv_path : str
Path to csv
fieldnames : list of str
List of fieldnames for csv
Returns
-------
csv.DictWriter
DictWriter object of path
"""
f = open(csv_path, 'w', encoding='latin1', newline='')
c = csv.DictWriter(f, fieldnames=fieldnames)
return c
@staticmethod
def copy_rows_on_image_id(root_dir, new_folder, csv_file, image_ids):
""" For the csv file specified, creates a copy in new_folder, where rows that are not in image_ids are omitted
Parameters
----------
new_folder : str
New folder to place new CSV
root_dir : str
Root directory contianing csv files and new folder
csv_file : str
Location of CSV file
image_ids : set of str
Set of image_ids who's rows should be copied over
"""
print("Creating new {}".format(csv_file))
path = os.path.join(root_dir, csv_file)
new_path = os.path.join(root_dir, new_folder, csv_file)
c = Common.load_csv_as_dict(path)
w = Common.new_csv_as_dict(new_path, c.fieldnames)
rows = [row for row in tqdm(c) if row["ImageID"] in image_ids]
w.writeheader()
w.writerows(rows)
@staticmethod
def new_text_file(txt_path, lines):
""" Writes the lines to the specified text file
Parameters
----------
txt_path : str
Path to text file
lines : list of str
List of lines to be written
"""
f = open(txt_path, 'w', encoding='latin1')
f.writelines(lines)
@staticmethod
def extract_image_id_from_flickr_static(static_url):
""" Given a static url extract the image id
Parameters
----------
static_url : str
Static url to photo, one of kind:
https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}.jpg
or
https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{secret}_[mstzb].jpg
or
https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{o-secret}_o.(jpg|gif|png)
Returns
-------
str
Image id of url
"""
pattern = r"(?:.*?\/\/?)+([^_]*)"
image_id = re.findall(pattern, static_url)[0]
return image_id
@staticmethod
def pass_args_to_f(f, args):
""" Given a function f pass it the list of args
Parameters
----------
f : function
Function to pass the args to
args : list
List of args
Returns
-------
Return type of function f
Value returned by f for arguments args
"""
return f(*args)
def get_image_labels_file(self, subset):
""" Get the name of the image labels file for the specified subset
Parameters
----------
subset : str
Subset want filename for, one of train, validation, test
Returns
-------
str
Name of image labels file for the subset
"""
return "{}-annotations-human-imagelabels{}.csv".format(subset, "-boxable" if not self.image_level else "")
def get_image_ids_file(self, subset):
""" Get the name of the image ids file for the specified subset
Parameters
----------
subset : str
Subset want filename for, one of train, validation, test
Returns
-------
str
Name of image ids file for the subset
"""
return "{}-images-{}with-rotation.csv".format(subset, (
"with-labels-" if self.image_level else "boxable-") if subset == "train" else "")
@staticmethod
def get_boxes_file(subset):
""" Get the name of the boxes file for the specified subset
Parameters
----------
subset : str
Subset want filename for, one of train, validation, test
Returns
-------
str
Name of boxes file for the subset
"""
return "{}-annotations-bbox.csv".format(subset)
def get_classes_description_file(self):
""" Get the name of the class descriptions file
Returns
-------
str
Name of class descriptions file
"""
return "class-descriptions{}.csv".format("" if self.image_level else "-boxable")