-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
696 lines (593 loc) · 23 KB
/
main.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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
import os
import json
from typing import Union
from rdflib import Namespace, URIRef
from lxml import etree
from PIL import Image as pil_image
iiif_prezi3_context = "http://iiif.io/api/presentation/3/context.json"
nsCanvas = Namespace("https://data.goldenagents.org/datasets/dh2020/canvas/")
nsAnnotationPage = Namespace(
"https://data.goldenagents.org/datasets/dh2020/annotationpage/")
nsAnno = Namespace("https://data.goldenagents.org/datasets/dh2020/annotation/")
def main(imagefolder: str,
annotationfolder: str = None,
nameslocationsfile: str = None,
canvasmetadatafile: str = None,
matcheditemsfile: str = None):
manifest = createManifest(imagefolder, annotationfolder,
nameslocationsfile, canvasmetadatafile,
matcheditemsfile)
with open('iiif/manifest.json', 'w') as outfile:
json.dump(manifest, outfile, indent=2)
def createManifest(imagefolder: str,
annotationfolder: str = None,
nameslocationsfile: str = None,
canvasmetadatafile: str = None,
matcheditemsfile: str = None):
manifest = {
"@context": [
"http://www.w3.org/ns/anno.jsonld",
"http://iiif.io/api/presentation/3/context.json"
],
"id":
"https://lvanwissen.github.io/ga-dh2020-demo/iiif/manifest.json",
"type":
"Manifest",
"label": {
"en": ["Golden Agents DH2020 Demo"]
},
"summary":
"Annotation demo, showing the benefits of cross-institutional collaboration",
"metadata": [{
"label": {
"en": ["Title"]
},
"value": {
"en": [
"Annotation demo, showing the benefits of cross-institutional collaboration"
]
}
}, {
"label": {
"en": ["Provenance"]
},
"value": {
"en": [
"Amsterdam City Archives, NA 2408.",
"Notary Jacob de Winter (1626-1675).",
"Inventory dated 1648-1665."
]
}
}, {
"label": {
"en": ["Creator"]
},
"value": {
"en": [""]
}
}, {
"label": {
"en": ["Contributor"]
},
"value": {
"en": [""]
}
}, {
"label": {
"en": ["Language"]
},
"value": {
"en": ["English"]
}
}, {
"label": {
"en": ["Date Statement"]
},
"value": {
"en": ["2020"]
}
}, {
"label": {
"en": ["Description"]
},
"value": {
"en": ["DescriptionValue"]
}
}, {
"label": {
"en": ["Collection"]
},
"value": {
"en": ["Conference presentations"]
}
}, {
"label": {
"en": ["Subject"]
},
"value": {
"en": [
"Linked Open Data", "Dutch Golden Age",
"Probate inventories", "Getty Provenance Index"
]
}
}, {
"label": {
"en": ["Other Identifier"]
},
"value": {
"en": ["doi?"]
}
}, {
"label": {
"en": ["Record Created"]
},
"value": {
"en": ["2020-07-20"]
}
}, {
"label": {
"en": ["Holding Institution"]
},
"value": {
"en": ["Golden Agents"]
}
}],
"homepage": [{
"id": "https://www.goldenagents.org",
"type": "Text",
"label": {
"en": ["View project information"]
},
"format": "text/html"
}],
"logo": [{
"id": "assets/img/logo-golden-agents.png",
"type": "Image"
}],
"thumbnail": [{
"id": "assets/img/logo-golden-agents.png"
}],
"requiredStatement": {
"label": {
"en": ["Terms of Use"]
},
"value": {
"en": [
"<p> <a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/4.0/\"><img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"https://licensebuttons.net/i/l/by-sa/transparent/00/00/00/88x31.png\"></a><br>This demo is developed by <a xmlns:cc=\"http://creativecommons.org/ns#\" href=\"https://www.goldenagents.org/\" property=\"cc:attributionName\" rel=\"cc:attributionURL\">Golden Agents</a> and is licensed under a <a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/4.0/\">Creative Commons BY-SA 4.0 International License</a>. </p>"
]
},
"format": "text/html"
},
"behavior": ["individuals"],
"start": {
"id":
"https://data.goldenagents.org/datasets/dh2020/canvas/A16098000033",
"type": "Canvas"
},
"items": []
}
items = []
if nameslocationsfile:
with open(nameslocationsfile) as infile:
nameslocationsdata = json.load(infile)
else:
nameslocationsdata = dict()
if canvasmetadatafile:
with open(canvasmetadatafile) as infile:
canvasmetadata = json.load(infile)
else:
canvasmetadata = dict()
if matcheditemsfile:
with open(matcheditemsfile) as infile:
matcheditems = json.load(infile)
else:
matcheditems = dict()
for fn in sorted(os.listdir(imagefolder)):
imagepath = os.path.join(imagefolder, fn)
baseFilename, _ = os.path.splitext(fn)
# HTR annotations
if annotationfolder:
annotationpath = os.path.join(annotationfolder,
baseFilename + '.json')
if not os.path.exists(annotationpath):
annotationpath = None
else:
annotationpath = None
# PersonNames and Locations have been selected by AAA
nameslocations = nameslocationsdata.get(fn, [])
# Metadata from AAA
canvasmeta = canvasmetadata.get(fn)
metadata = []
if canvasmeta:
md = {
'Type of deed': canvasmeta['actType'],
'Date': canvasmeta['registrationDate'],
'Description': canvasmeta['description'],
'Language': canvasmeta['language'].title(),
'Notary': canvasmeta['notary'],
'Persons': canvasmeta['persons'],
'Locations': canvasmeta['locations'],
'Amsterdam City Archives id': canvasmeta['identifier']
}
# Getty inventory
if 'getty' in canvasmeta:
md['Getty Provenance Index id'] = canvasmeta['getty'][
'identifier']
if canvasmeta['getty'].get('owners'):
md['Owners (Getty)'] = canvasmeta['getty']['owners']
if canvasmeta['getty'].get('appraisers'):
md['Appraisers (Getty)'] = canvasmeta['getty'][
'appraisers']
if canvasmeta['getty'].get('comments'):
md['Comments (Getty)'] = canvasmeta['getty']['comments']
# Frick inventory
if 'frick' in canvasmeta:
md['The Frick Collection id'] = f"<a href={canvasmeta['frick']['url']}>{canvasmeta['frick']['identifier']}</a>"
if canvasmeta['frick'].get('owners'):
md['Owners (Frick)'] = canvasmeta['frick']['owners']
if canvasmeta['frick'].get('appraisers'):
md['Appraisers (Frick)'] = canvasmeta['frick'][
'appraisers']
if canvasmeta['frick'].get('comments'):
md['Comments (Frick)'] = canvasmeta['frick']['comments']
for k, v in md.items():
if v:
metadata.append({
"label": {
"en": [k]
},
"value": {
"en": [v] if type(v) == str else v
}
})
canvas = getCanvas(imagepath,
annotationpath,
nameslocations,
metadata=metadata,
matcheditems=matcheditems)
items.append(canvas)
manifest["items"] = items
return manifest
def getCanvas(imagepath: str,
annotationpath: str = None,
nameslocations: list = None,
canvasid: str = None,
metadata: list = [],
matcheditems: dict = None) -> dict:
_, filename = os.path.split(imagepath)
baseFilename, ext = os.path.splitext(filename)
if canvasid is None:
canvasid = nsCanvas.term(baseFilename)
canvas = {
"id":
canvasid,
"type":
"Canvas",
"label": {
"none": [filename]
},
"items": [
getAnnotationPage(target=canvasid,
imagepath=imagepath,
motivation='painting')
],
"annotations": [],
"metadata":
metadata
}
annotations = []
# index data
if nameslocations:
ap = getAnnotationPage(
target=canvasid,
baseFilename=baseFilename,
motivation='commenting',
annopageid=f'iiif/annotations/{baseFilename}-index.json',
nameslocations=nameslocations,
embedded=False)
annotations.append(ap)
# htr data
if annotationpath:
ap = getAnnotationPage(
target=canvasid,
motivation='supplementing',
annopageid=f'iiif/annotations/{baseFilename}-htr.json',
annotationpath=annotationpath,
matcheditems=matcheditems,
embedded=False)
annotations.append(ap)
canvas['annotations'] = annotations
# These are already calculated in the painting annotation
canvas['width'] = canvas['items'][0]['items'][0]['body']['width']
canvas['height'] = canvas['items'][0]['items'][0]['body']['height']
return canvas
def getAnnotationPage(target: Union[str, URIRef],
baseFilename: str = None,
imagepath: str = None,
motivation: str = 'painting',
annopageid: str = None,
annotationpath: str = None,
nameslocations: list = None,
matcheditems: dict = None,
embedded: bool = True) -> dict:
if annopageid is None and imagepath:
_, filename = os.path.split(imagepath)
baseFilename, _ = os.path.splitext(filename)
annopageid = nsAnnotationPage.term(baseFilename)
elif annopageid is None and annotationpath:
_, filename = os.path.split(annotationpath)
baseFilename, _ = os.path.splitext(filename)
annopageid = 'iiif/annotations/{basefilename}.json'
elif annotationpath:
_, filename = os.path.split(annotationpath)
baseFilename, _ = os.path.splitext(filename)
elif annotationpath is None and imagepath is None and nameslocations is None:
return {}
annotationPage = {
"@context": iiif_prezi3_context,
"id": annopageid,
"type": "AnnotationPage",
"items": []
}
if imagepath:
annotationPage['items'] = [
getAnnotation(target, imagepath, motivation=motivation)
]
# This is for the HTR transcriptions
if annotationpath:
items = []
with open(annotationpath) as infile:
annotations = json.load(infile)
for region in annotations['PcGts']['Page']['elements']:
for a in region['elements']:
annoMatchId = f"{baseFilename}{a['attributes']['id']}"
annoid = nsAnno.term(annoMatchId)
# Getty / Frick data
matchedItem = matcheditems.get(annoMatchId, {})
if matchedItem:
color = '#11aa09'
else:
color = '#f9b942'
coordinates = a['geometry']['coords']
if coordinates is None:
continue
targetselector = {
"id": target,
"selector": {
"type": "SvgSelector",
"value": getSVG(coordinates, color=color)
}
}
bodyValue = "\n".join([i['text'] for i in a['elements']])
if bodyValue.strip() is "": # not interested in empty anno
continue
body = []
# first the text itself (HTR)
textualBody = {
"type": "TextualBody",
"language": "nl",
"value": "<p><b>HTR</b><br>" + bodyValue + "</p>"
}
body.append(textualBody)
# Getty / Frick items
if matchedItem:
## Getty
matchedItemGetty = matcheditems.get(annoMatchId,
{}).get('getty')
if matchedItemGetty:
properties = {
'label':
matchedItemGetty['label'],
'transcription':
matchedItemGetty['transcription'],
'type':
f'Schilderij [<a href="http://vocab.getty.edu/aat/300177435">AAT</a>]'
if matchedItemGetty['type'] == 'Schilderij' else
matchedItemGetty['type'],
'artist':
matchedItemGetty['artist'],
'room':
matchedItemGetty['room'],
'valuation':
matchedItemGetty['valuation'],
'subject':
matchedItemGetty['subject'],
'iconclass':
f'{matchedItemGetty["iconclass"]} [<a href="http://iconclass.org/{matchedItemGetty["iconclass"]}">ICONCLASS</a>]' if {matchedItemGetty["iconclass"]} else None,
'identifier':
matchedItemGetty['identifier']
}
value = "<br>\n".join(
f"<span>{key.title()}: </span>{value}"
for key, value in properties.items() if value)
textualBody = {
"type":
"TextualBody",
"language":
"nl",
"value":
f"""<p>
<b>Getty Provenance Index</b><br>
{value}
</p>"""
}
body.append(textualBody)
### Then the tags
tagBodyType = {
"type": "TextualBody",
"purpose": "tagging", # for a nice tag!
"value": matchedItemGetty['type']
}
body.append(tagBodyType)
if matchedItemGetty['iconclass']:
tagBodyIconclass = {
"type": "TextualBody",
"purpose": "tagging", # for a nice tag!
"value": matchedItemGetty['iconclass']
}
body.append(tagBodyIconclass)
## Frick
matchedItemFrick = matchedItem.get('frick')
if matchedItemFrick:
properties = {
'label':
matchedItemFrick['label'],
'transcription':
matchedItemFrick['transcription'],
'type':
matchedItemFrick['type'],
'artist':
matchedItemFrick['artist'],
'room':
matchedItemFrick['room'],
'valuation':
matchedItemFrick['valuation'],
'subject':
matchedItemFrick['subject'],
'iconclass':
matchedItemFrick['iconclass'],
'identifier':
f'<a href="https://research.frick.org/montias/inventoryList/{matchedItemFrick["identifier"].replace(".", "#A", 1)}">{matchedItemFrick["identifier"]}</a>'
}
value = "<br>\n".join(
f"<span>{key.title()}: </span>{value}"
for key, value in properties.items() if value)
textualBody = {
"type":
"TextualBody",
"language":
"nl",
"value":
f"""<p>
<b>The Frick Collection</b><br>
{value}
</p>"""
}
body.append(textualBody)
### Then the tags
tagBodyType = {
"type": "TextualBody",
"purpose": "tagging", # for a nice tag!
"value": matchedItemFrick['type']
}
body.append(tagBodyType)
if matchedItemFrick['iconclass']:
tagBodyIconclass = {
"type": "TextualBody",
"purpose": "tagging", # for a nice tag!
"value": matchedItemFrick['iconclass']
}
body.append(tagBodyIconclass)
anno = getAnnotation(target=targetselector,
motivation=motivation,
body=body,
annoid=annoid)
items.append(anno)
annotationPage['items'] = items
# This is data coming from the index in which Persons and Locations have been tagged
if nameslocations:
items = []
for n, a in enumerate(nameslocations, 1):
targetselector = {
"id": target,
"selector": {
"type": "FragmentSelector",
"value": f"xywh={a['coords']}"
}
}
body = [
{
"type": "TextualBody",
"language": "nl",
"value": f'<p><b>Index</b><br>{a["label"]}</p>'
},
{
"type": "TextualBody",
"purpose": "tagging", # for a nice tag!
"value": a["type"]
}
]
anno = getAnnotation(
target=targetselector,
motivation=motivation,
body=body, # already a list
annoid=nsAnno.term(f"{baseFilename}/index{n}"))
items.append(anno)
annotationPage['items'] = items
if embedded:
return annotationPage
else:
with open(annopageid, 'w') as outfile:
json.dump(annotationPage, outfile, indent=1)
return {
"@context": iiif_prezi3_context,
"id": annopageid,
"type": "AnnotationPage"
}
def getAnnotation(target: Union[str, URIRef, dict],
imagepath: str = None,
motivation: str = 'painting',
body: list = None,
annoid: str = None) -> dict:
if motivation == 'painting' and imagepath and type(target) != dict:
_, filename = os.path.split(imagepath)
baseFilename, ext = os.path.splitext(filename)
if annoid is None:
annoid = nsAnno.term(baseFilename)
with pil_image.open(imagepath) as img:
(w, h) = img.size
height = h
width = w
# This is the annotation that attaches the image to the canvas
annotation = {
"@context": iiif_prezi3_context,
"id": annoid,
"type": "Annotation",
"motivation": motivation,
"body": {
"id": imagepath,
"type": "Image",
"label": {
"en": [filename]
},
"format": "image/jpeg",
"width": width,
"height": height
},
"target": target
}
else:
# But the Annotation type is also used for other commentary etc.
annotation = {
"@context": iiif_prezi3_context,
"id": annoid,
"type": "Annotation",
"motivation": motivation,
"body": body,
"target": target
}
return annotation
def getSVG(coordinates: Union[list, tuple],
color: str = "#66cc99",
opacity: str = "0.08"):
points = "M " # start at this point
points += " L ".join([f"{c['x']},{c['y']}" for c in coordinates
]) # then move from point to point
points += f" L {coordinates[0]['x']},{coordinates[0]['y']} Z" # repeat the first point and close
svg = etree.Element("svg", xmlns="http://www.w3.org/2000/svg")
_ = etree.SubElement(
svg, "path", **{
"fill-rule": "evenodd",
"fill": color,
"stroke": "#555555",
"stroke-width": "1",
"fill-opacity": opacity,
"d": points
})
return etree.tostring(svg, encoding=str)
if __name__ == "__main__":
main(imagefolder='images/2408/',
annotationfolder='data/htr/2408/',
nameslocationsfile='data/2408_nameslocations.json',
canvasmetadatafile='data/2408_canvasmetadata.json',
matcheditemsfile='data/2408_itemsmetadata.json')