-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCaptionGenerationPictorial.py
More file actions
286 lines (183 loc) · 8.62 KB
/
CaptionGenerationPictorial.py
File metadata and controls
286 lines (183 loc) · 8.62 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os
import re
from collections import Counter
import numpy as np
# Python script to assign correct topic and area to pictorial maps
maps_directories = [
"AirlinesWorld",
"MilitaryWorld",
"AtlasWorld",
"ErnestWorld",
"AgricultureUSA",
"AirlinesUSA",
"EthnographyUSA",
"IndiansUSA",
"MilitaryUSA",
"RailroadsUSA",
"RoadsUSA",
"SatiricalUSA",
"TourismUSA",
] # Folders where sub-folders containing maps (.jpg) and metadata (.text) are stored
image_paths = [] # List to store map paths
captions = [] # List to store captions
path_area = [] # List to store map paths
captions_area = [] # List to store caption concerning the area (World / USA)
for maps_directory in maps_directories:
# Iterate over the sub-folders in the directory
for root, dirs, files in os.walk(maps_directory):
for file in files:
file_path = os.path.join(root, file)
# Check if the file is an image (.jpg) or text (.txt) file
if file.lower().endswith(".jpg"):
image_paths.append(file_path)
elif file.lower().endswith(".txt"):
with open(file_path, "r", encoding="utf-8") as txt_file:
txt_content = txt_file.read()
short_title = ""
world_area = ""
subject = ""
country = ""
date = ""
for line in txt_content.split("\n"):
if line.startswith("Short Title"):
short_title = line.split("\t", 1)[1]
elif line.startswith("World Area"):
world_area = line.split("\t", 1)[1]
elif line.startswith("Country"):
country = line.split("\t", 1)[1]
elif line.startswith("Date"):
date = line.split("\t", 1)[1]
date = int(date)
short_title = short_title.split(".", 1)[0] # Remove punctuations
# Assign correct area to maps
if "box" not in short_title.lower():
if ("USA" in maps_directory) or (
"Bird and Flower Map" in file_path
):
captions_area.append("united states")
path_area.append(file_path)
elif "World" in maps_directory:
captions_area.append("world")
path_area.append(file_path)
# Assign correct topic to maps
if maps_directory == "AirlinesWorld":
if re.search(r"Air France", short_title) or re.search(
r"Reseau", short_title
):
captions.append(f"flight network")
else:
captions.append(f"flight network")
elif maps_directory == "MilitaryWorld":
if re.search(r"World News", short_title) or re.search(
r"Newsmap", short_title
):
captions.append(f"news during world war 2")
elif re.search(r"Dated", short_title):
captions.append(f"world war 2")
else:
captions.append(f"world war 2")
elif maps_directory == "AtlasWorld":
if re.search(r"Playing", short_title):
captions.append(f"playing card")
elif (
re.search(r"lignes", short_title)
or re.search(r"Messageries", short_title)
or re.search(r"Bremen", short_title)
):
captions.append(f"transport routes")
elif (
re.search(r"Animals", short_title)
or re.search(r"Dog", short_title)
or re.search(r"Horse", short_title)
or re.search(r"animals", short_title)
or re.search(r"Gara", short_title)
or re.search(r"Zoo", short_title)
):
captions.append(f"animals")
elif re.search(r"Fauna", short_title) or re.search(
r"Fish", short_title
):
captions.append(f"animals")
elif (
re.search(r"Distribution", short_title)
or re.search(r"Ciclones", short_title)
or re.search(r"Volcanic", short_title)
or re.search(r"waters", short_title)
or re.search(r"Physical", short_title)
):
captions.append(f"BOX")
elif re.search(r"The World", short_title) or re.search(
r"Earth", short_title
):
captions.append(f"educational drawings")
elif (
re.search(r"Races", short_title)
or re.search(r"Pictorial", short_title)
or re.search(r"Peoples", short_title)
):
captions.append(f"people")
elif re.search(r"Cuba", short_title):
captions.append(f"BOX")
else:
captions.append("BOX")
elif maps_directory == "ErnestWorld":
if (
re.search(r"stamps", short_title)
or re.search(r"Stamps", short_title)
or re.search(r"Stamp", short_title)
):
captions.append(f"stamps")
elif re.search(r"War", short_title):
captions.append(f"world war 2")
elif re.search(r"Wonders", short_title):
captions.append(f"wonders")
else:
captions.append(f"planes")
elif maps_directory == "AgricultureUSA":
captions.append(f"food and agriculture")
elif maps_directory == "AirlinesUSA":
if re.search(r"American Airlines", short_title):
captions.append(f"flight network")
elif re.search(r"TWA", short_title):
captions.append(f"flight network")
else:
captions.append(f"flight network")
elif maps_directory == "EthnographyUSA":
captions.append(f"people")
elif maps_directory == "IndiansUSA":
captions.append(f"people")
elif maps_directory == "MilitaryUSA":
captions.append(f"military")
elif maps_directory == "RailroadsUSA":
captions.append(f"transport routes")
elif maps_directory == "RoadsUSA":
captions.append(f"transport routes")
elif maps_directory == "SatiricalUSA":
captions.append(f"satirical representation")
elif maps_directory == "TourismUSA":
captions.append(f"tourist sights")
else:
print("Error!")
print(Counter(captions))
print(len(captions))
np.save("pictorialMapsCaptionsLocation.npy", np.array(captions_area))
np.save("pictorialMapsPathsLocation.npy", np.array(image_paths))
# Post-process captions and paths (for topics)
caps = captions
paths = image_paths
print(len(caps))
print(len(paths))
captions = []
map_paths = []
for c, p in zip(caps, paths):
if "BOX" not in c:
# print(c)
# print(p)
captions.append(c)
map_paths.append(p)
print(Counter(captions))
print("Count- ", len(Counter(captions)))
print(len(captions))
print(len(map_paths))
np.save("pictorialMapsCaptionsTopic.npy", captions)
np.save("pictorialMapsPathsTopic.npy", map_paths)