-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathillustration_extractor.py
70 lines (62 loc) · 2.44 KB
/
illustration_extractor.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
from abc import ABCMeta, abstractmethod
from xml.etree import ElementTree as ElTree
import cv2
import numpy as np
class IllustrationExtractor(metaclass=ABCMeta):
"""
Abstract base class for extracting relevant data from an XML file.
"""
@classmethod
def factory(cls, source):
if source == 'ABBYY':
return IllustrationExtractorABBYY()
elif source == 'altoABBYY':
return IllustrationExtractorAltoABBYY()
@abstractmethod
def extract(self, paper_file, xml_file):
"""
extract illustrations from newspaper-page using xml_file
"""
return NotImplementedError
class IllustrationExtractorAltoABBYY(IllustrationExtractor):
def extract(self, paper_file, xml_file):
# read page
img = cv2.imread(paper_file)
xtree = ElTree.parse(xml_file)
xroot = xtree.getroot()
attributes = [elem.attrib for elem in xroot.iter()]
illustrations = []
bboxes = []
block_ids = []
for attribute in attributes:
if 'TYPE' in attribute.keys() and attribute['TYPE'] == 'Illustration':
xtl = int(attribute['HPOS'])
ytl = int(attribute['VPOS'])
xbr = xtl + int(attribute['WIDTH'])
ybr = ytl + int(attribute['HEIGHT'])
img_crop = img[ytl:ybr, xtl:xbr, :]
illustrations.append(np.array(img_crop))
bboxes.append([xtl, ytl, xbr, ybr])
block_ids.append(attribute['ID'])
return illustrations, bboxes, block_ids
class IllustrationExtractorABBYY(IllustrationExtractor):
def extract(self, paper_file, xml_file):
# read page
img = cv2.imread(paper_file)
xtree = ElTree.parse(xml_file)
xroot = xtree.getroot()
attributes = [elem.attrib for elem in xroot.iter()]
illustrations = []
bboxes = []
block_ids = []
for attribute in attributes:
if 'blockType' in attribute.keys() and attribute['blockType'] == 'Picture':
xtl = int(attribute['l'])
ytl = int(attribute['t'])
xbr = int(attribute['r'])
ybr = int(attribute['b'])
img_crop = img[ytl:ybr, xtl:xbr, :]
illustrations.append(np.array(img_crop))
bboxes.append([xtl, ytl, xbr, ybr])
block_ids.append('None')
return illustrations, bboxes, block_ids