-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCaptionGenerationClassical.py
More file actions
616 lines (389 loc) · 18.5 KB
/
CaptionGenerationClassical.py
File metadata and controls
616 lines (389 loc) · 18.5 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
import os
import numpy as np
from collections import Counter
# Python script to store paths to maps with corresponding ground truth captions
maps_directories = ["ClassicalMaps", "PictorialMaps"]
# Lists to store map paths
image_paths = []
image_paths_class_areas = []
image_paths_class_notes = []
image_paths_class_dates = []
image_paths_class_pic = []
# Set defining which areas have the most usable maps
areas_classical = set(
(
"greece",
"italy",
"iberian peninsula",
"france",
"eastern hemisphere",
"europe",
"middle east",
"asia minor",
"germany",
"british isles",
"world",
"egypt",
"part of italy",
"part of france",
"part of germany",
"india",
"holy land",
"asia",
"caucasus",
"sri lanka",
"south america",
"americas",
"switzerland",
"scandinavia",
"netherlands",
"africa",
"part of greece",
)
)
date_set = set("19th century")
image_paths_picTest = []
caps_TEST = []
# Lists to store captions
captions_area = []
captions_date = []
captions_note = []
captions_class_pic = []
area_set = set(())
areas_array = []
dates_array = []
notes_array = []
index = 0
count = 0
count_cities = 0
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)
img = image_paths[index]
index += 1
elif file.lower().endswith(".txt"):
with open(file_path, "r", encoding="utf-8") as txt_file:
txt_content = txt_file.read()
# Variables needed to correctly assign metadata information
short_title = ""
subject = ""
date = ""
note = ""
event = ""
maptype = ""
author = ""
world_area = ""
area = ""
country = ""
city = ""
flagw = True
flagc = True
flagr = True
flags = True
count = 0
# Scan metadata line for line
for line in txt_content.split("\n"):
if line.startswith("Note"):
note = line.split("\t", 1)[1]
elif line.startswith("World Area") and flagw:
count += 1
flagw = False
area = line.split("\t", 1)[1]
elif line.startswith("Country") and flagc:
count += 1
flagc = False
area = line.split("\t", 1)[1]
elif line.startswith("State/Province") and flags:
count += 1
flags = False
area = line.split("\t", 1)[1]
elif line.startswith("Region") and flagr:
count += 1
flagr = False
area = line.split("\t", 1)[1]
elif line.startswith("County"):
count += 1
area = line.split("\t", 1)[1]
elif line.startswith("City"):
count += 1
area = line.split("\t", 1)[1]
city = area
elif line.startswith("Short Title"):
short_title = line.split("\t", 1)[1]
elif line.startswith("Type"):
maptype = line.split("\t", 1)[1]
elif line.startswith("Subject"):
subject = line.split("\t", 1)[1]
elif line.startswith("Author"):
author = line.split("\t", 1)[1]
elif line.startswith("Event"):
try:
event = line.split("\t", 1)[1]
except IndexError:
event = "none"
elif line.startswith("Date"):
date = line.split("\t", 1)[1]
try:
date = int(date)
except ValueError:
date = int(date.split(".", 1)[0])
short_title = short_title.split(".", 1)[0] # Remove punctuations
if maps_directory == "PictorialTest":
image_paths_picTest.append(img)
caps_TEST.append("pictorial map")
if maps_directory == "ClassicalMaps":
image_paths_picTest.append(img)
caps_TEST.append("topographic map")
if isinstance(date, int):
if date <= 1600: # Assign each map the correct century
date = "16th century"
elif 1600 < date <= 1700:
date = "17th century"
elif 1700 < date <= 1800:
date = "18th century"
elif 1800 < date <= 1900:
date = "19th century"
else:
print(date)
else:
print(date)
# Assign correct area based on Short Title or area variable
if "4, 5" in short_title:
area = "Israel"
elif "62" in short_title:
area = "Asia Minor"
elif (
"greece" in short_title.lower()
or "grece" in short_title.lower()
):
area = "Greece"
elif "63" in short_title:
area = "Holy Land"
elif "India" in short_title:
area = "India"
elif "Africa Antiqua" in short_title:
area = "North Africa"
elif "Aegyptus" in short_title:
area = "Egypt"
elif (
"asia minor" in short_title.lower()
or "asiae minoris" in short_title.lower()
):
area = "Asia Minor"
elif "rome" in area.lower():
area = "Rome (Italy)"
elif "romain, empire" in area.lower():
area = "Roman Empire"
elif "balkan" in area.lower() or "balkans" in area.lower():
area = "Balkans"
elif "africa, north" in area.lower():
area = "North Africa"
elif "europe, central" in area.lower():
area = "Central Europe"
elif "europe, eastern" in area.lower():
area = "Eastern Europe"
elif "asia, central" in area.lower():
area = "Central Asia"
elif "africa, south" in area.lower():
area = "Southern Africa"
elif "asia, southern" in area.lower():
area = "Southern Asia"
elif "mecedonia" in area.lower():
area = "Macedonia"
elif "jerusalem" in area.lower():
area = "Jerusalem"
if (
"alexandri" in short_title.lower()
or "alexander" in short_title.lower()
or "alexandre" in short_title.lower()
or "alessandro" in short_title.lower()
):
area = "middle east"
if count > 1: # if more than one area assigned in the metadata
if "TAV" in short_title:
area = "Rome (Italy)"
elif "World" in short_title:
area = "World"
elif "Babylonia" in short_title:
area = "Asia Minor"
elif "britain" in short_title.lower():
area = "Britain"
elif "france" in short_title.lower():
area = "France"
elif "germany" in short_title.lower():
area = "Germany"
elif "greece" in short_title.lower():
area = "Greece"
elif "italy" in short_title.lower():
area = "Italy"
elif "palestine" in short_title.lower():
area = "Palestine"
elif "persia" in short_title.lower():
area = "Persia"
elif (
"empire romain" in short_title.lower()
or "roman empire" in short_title.lower()
):
area = "roman empire"
elif "arabia" in short_title.lower():
area = "arabian peninsula"
elif "belgie" in short_title.lower():
area = "Belgium"
elif "graecia" in short_title.lower():
area = "Greece"
elif "itali" in short_title.lower():
area = "Italy"
elif "pars occidentalis" in short_title.lower():
area = "western mediterranean"
elif "pars orientalis" in short_title.lower():
area = "eastern mediterranean"
elif (
"palestina" in short_title.lower()
or "palaestina" in short_title.lower()
or "palestine" in short_title.lower()
):
area = "holy land"
else:
# print(short_title, area)
pass
else:
pass
if (
"palestine" in area.lower()
or "palestina" in area.lower()
or "palestine" in short_title.lower()
):
area = "Holy land"
if "orbis veteribus" in short_title.lower():
area = "Eastern Hemisphere"
if "pars occidentalis" in short_title.lower():
area = "western mediterranean"
if "europe" in short_title.lower():
area = "Europe"
if (
"departement" in short_title.lower()
or "governo generale" in short_title.lower()
or "map of the geography of ancient france"
in short_title.lower()
):
area = "Part of France"
if area.lower() == "mediterranean region":
area = "Mediterranean"
if (
"herzogthum" in short_title.lower()
or "provinz" in short_title.lower()
):
area = "Part of Germany"
if area.lower() == "portugal":
area = "Iberian Peninsula"
if "eastern\themisphere" in area.lower():
area = "Eastern Hemisphere"
if (
area.lower() == "roman empire"
or "roman empire" in short_title.lower()
):
if "peut" in short_title.lower():
pass
else:
area = "Mediterranean"
if "spain" in area.lower():
area = "iberian peninsula"
if "great britain" == area.lower():
area = "British isles"
if "mediterranean" == area.lower():
area = "europe"
if "black sea" == area.lower():
area = "caucasus"
if "america" == area.lower():
area = "americas"
if " part of germany" == area.lower():
area = "part of germany"
if " part of greece" == area.lower():
area = "part of greece"
# Assign correct style information based on Note
decorative_element = False
if "cartouche" in note.lower() or "vign" in note.lower():
decorative_element = True
if len(note) == 0:
note = "None"
elif (
"hand colo" in note.lower()
or "hand-colo" in note.lower()
or ("colo" in note.lower() and "uncolo" not in note.lower())
):
if (
not decorative_element
and "pictorially" not in note.lower()
):
note = "Hand colored"
elif (
decorative_element and "pictorially" not in note.lower()
):
note = "Hand colored" # not including cartouche since too few maps
elif (
not decorative_element and "pictorially" in note.lower()
):
note = "Hand colored with pictorial relief"
elif decorative_element and "pictorially" in note.lower():
note = "Hand colored with decorative elements and pictorial relief"
else:
print("ERROR!")
elif decorative_element:
if "pictorially" in note.lower():
note = "decorative elements and pictorial relief"
else:
note = "decorative elements and pictorial relief"
elif "pictorially" in note.lower():
note = "pictorial relief"
elif "engraved" in note.lower():
note = "Engraved"
else:
note = "None"
dates_array.append(date.lower())
captions_class_pic.append("topographic map") # set map type
image_paths_class_pic.append(img)
captions_date.append(date.lower())
image_paths_class_dates.append(img)
if "none" not in note.lower():
notes_array.append(note.lower())
captions_note.append(note.lower())
image_paths_class_notes.append(img)
areas_array.append((area.lower()))
if area.lower() in areas_classical:
captions_area.append(area.lower())
image_paths_class_areas.append(img)
elif maps_directory == "PictorialMaps":
captions_class_pic.append("pictorial map") # only set map type
image_paths_class_pic.append(img)
print(Counter(dates_array))
print("Count- ", len(Counter(dates_array)))
print(Counter(areas_array))
print("Count- ", len(Counter(areas_array)))
print(Counter(notes_array))
print("Count- ", len(Counter(notes_array)))
print(Counter(captions_area))
print("Count- ", len(Counter(captions_area)))
print(Counter(captions_class_pic))
print("Count- ", len(Counter(captions_class_pic)))
# Save lists as numpy arrays for later usage
captions_npy = np.array(captions_area)
np.save("classicalMapsCaptionsArea.npy", captions_npy)
captions_npy = np.array(image_paths_class_areas)
np.save("classicalMapsPathsArea.npy", captions_npy)
captions_npy = np.array(captions_date)
np.save("classicalMapsCaptionsDate.npy", captions_npy)
captions_npy = np.array(image_paths_class_dates)
np.save("classicalMapsPathsDate.npy", captions_npy)
captions_npy = np.array(captions_note)
np.save("classicalMapsCaptionsNote.npy", captions_npy)
captions_npy = np.array(image_paths_class_notes)
np.save("classicalMapsPathsNote.npy", captions_npy)
captions_npy = np.array(captions_class_pic)
np.save("classPictorialCaptions.npy", captions_npy)
captions_npy = np.array(image_paths_class_pic)
np.save("classPictorialPaths.npy", captions_npy)