From 003bfc2cd0272c71cb3817ff9e517203e70088e7 Mon Sep 17 00:00:00 2001 From: b-vr103 Date: Thu, 2 May 2019 18:32:14 +0200 Subject: [PATCH] add pagexml2imag --- .project | 24 ++++ .pydevproject | 8 ++ build/lib/pagexml2img/__init__.py | 1 + build/lib/pagexml2img/pagexml2img.py | 160 ++++++++++++++++++++++ dist/pagexml2img-1.0-py3.6.egg | Bin 0 -> 5703 bytes make.sh | 9 ++ pagexml2img.egg-info/PKG-INFO | 11 ++ pagexml2img.egg-info/SOURCES.txt | 7 + pagexml2img.egg-info/dependency_links.txt | 1 + pagexml2img.egg-info/top_level.txt | 1 + pagexml2img/__init__.py | 1 + pagexml2img/pagexml2img.py | 160 ++++++++++++++++++++++ setup.py | 6 + 13 files changed, 389 insertions(+) create mode 100644 .project create mode 100644 .pydevproject create mode 100644 build/lib/pagexml2img/__init__.py create mode 100644 build/lib/pagexml2img/pagexml2img.py create mode 100644 dist/pagexml2img-1.0-py3.6.egg create mode 100755 make.sh create mode 100644 pagexml2img.egg-info/PKG-INFO create mode 100644 pagexml2img.egg-info/SOURCES.txt create mode 100644 pagexml2img.egg-info/dependency_links.txt create mode 100644 pagexml2img.egg-info/top_level.txt create mode 100644 pagexml2img/__init__.py create mode 100644 pagexml2img/pagexml2img.py create mode 100644 setup.py diff --git a/.project b/.project new file mode 100644 index 0000000..6fbe82f --- /dev/null +++ b/.project @@ -0,0 +1,24 @@ + + + toolbox_pagexml2img + + + + + + org.python.pydev.PyDevBuilder + + + + + + org.python.pydev.pythonNature + + + + setup.py + 1 + /home/vahid/workspace/toolbox_pagexml2img/setup.py + + + diff --git a/.pydevproject b/.pydevproject new file mode 100644 index 0000000..aa7a29a --- /dev/null +++ b/.pydevproject @@ -0,0 +1,8 @@ + + + + /${PROJECT_DIR_NAME} + + python interpreter + Default + diff --git a/build/lib/pagexml2img/__init__.py b/build/lib/pagexml2img/__init__.py new file mode 100644 index 0000000..95912a5 --- /dev/null +++ b/build/lib/pagexml2img/__init__.py @@ -0,0 +1 @@ +from pagexml2img import * \ No newline at end of file diff --git a/build/lib/pagexml2img/pagexml2img.py b/build/lib/pagexml2img/pagexml2img.py new file mode 100644 index 0000000..8d521f9 --- /dev/null +++ b/build/lib/pagexml2img/pagexml2img.py @@ -0,0 +1,160 @@ +#! /usr/bin/env python3 + +__version__= '1.0' + +import argparse +import sys +import os +import numpy as np +import warnings +import xml.etree.ElementTree as ET +from tqdm import tqdm +import cv2 + +with warnings.catch_warnings(): + warnings.simplefilter("ignore") + +__doc__=\ +""" +tool to extract 2d or 3d RGB images from page xml data. In former case output will be 1 +2D image array which each class has filled with a pixel value. In the case of 3D RGB image +each class will be defined with a RGB value and beside images a text file of classes also will be produced. +This classes.txt file is required for dhsegment tool. +""" + +class pagexml2img: + def __init__(self,dir_in, out_dir,output_type): + self.dir=dir_in + self.output_dir=out_dir + self.output_type=output_type + + def get_content_of_dir(self): + """ + Listing all ground truth page xml files. All files are needed to have xml format. + """ + + gt_all=os.listdir(self.dir) + self.gt_list=[file for file in gt_all if file.split('.')[ len(file.split('.'))-1 ]=='xml' ] + + def get_images_of_ground_truth(self): + """ + Reading the page xml files and write the ground truth images into given output directory. + """ + + if self.output_type=='3d' or self.output_type=='3D': + classes=np.array([ [0,0,0],[255,0,0],[0,255,0],[0,0,255]]) + + + + + for index in tqdm(range(len(self.gt_list))): + try: + tree1 = ET.parse(self.dir+'/'+self.gt_list[index]) + root1=tree1.getroot() + + + for jj in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Page'): + y_len=int(jj.attrib['imageHeight']) + x_len=int(jj.attrib['imageWidth']) + + co_text=[] + co_sep=[] + co_img=[] + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}TextRegion'): + c_t_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_t_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_text.append(np.array(c_t_in)) + + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}ImageRegion'): + c_i_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_i_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_img.append(np.array(c_i_in)) + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}SeparatorRegion'): + c_s_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_s_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_sep.append(np.array(c_s_in)) + + img = np.zeros( (y_len,x_len,3) ) + img_poly=cv2.fillPoly(img, pts =co_text, color=(255,0,0)) + img_poly=cv2.fillPoly(img, pts =co_img, color=(0,255,0)) + img_poly=cv2.fillPoly(img, pts =co_sep, color=(0,0,255)) + + try: + cv2.imwrite(self.output_dir+'/'+self.gt_list[index].split('-')[1].split('.')[0]+'.png',img_poly ) + except: + cv2.imwrite(self.output_dir+'/'+self.gt_list[index].split('.')[0]+'.png',img_poly ) + except: + pass + np.savetxt(self.output_dir+'/../classes.txt',classes) + + if self.output_type=='2d' or self.output_type=='2D': + + for index in tqdm(range(len(self.gt_list))): + try: + + tree1 = ET.parse(self.dir+'/'+self.gt_list[index]) + root1=tree1.getroot() + + for jj in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Page'): + y_len=int(jj.attrib['imageHeight']) + x_len=int(jj.attrib['imageWidth']) + + co_text=[] + co_sep=[] + co_img=[] + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}TextRegion'): + c_t_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_t_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_text.append(np.array(c_t_in)) + + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}ImageRegion'): + c_i_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_i_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_img.append(np.array(c_i_in)) + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}SeparatorRegion'): + c_s_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_s_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_sep.append(np.array(c_s_in)) + + img = np.zeros( (y_len,x_len) ) + img_poly=cv2.fillPoly(img, pts =co_text, color=(1,1,1)) + img_poly=cv2.fillPoly(img, pts =co_img, color=(2,2,2)) + img_poly=cv2.fillPoly(img, pts =co_sep, color=(3,3,3)) + try: + cv2.imwrite(self.output_dir+'/'+self.gt_list[index].split('-')[1].split('.')[0]+'.png',img_poly ) + except: + cv2.imwrite(self.output_dir+'/'+self.gt_list[index].split('.')[0]+'.png',img_poly ) + except: + pass + def run(self): + self.get_content_of_dir() + self.get_images_of_ground_truth() +def main(): + parser=argparse.ArgumentParser() + + parser.add_argument('-dir_in','--dir_in', dest='inp1', default=None, help='directory of page-xml files') + parser.add_argument('-dir_out','--dir_out', dest='inp2', default=None, help='directory where ground truth images would be written') + parser.add_argument('-type','--type', dest='inp3', default=None, help='this defines how output should be. A 2d image array or a 3d image array encoded with RGB color. Just pass 2d or 3d. The file will be saved one directory up. 2D image array is 3d but only information of one channel would be enough since all channels have the same values.') + options=parser.parse_args() + + possibles=globals() + possibles.update(locals()) + x=pagexml2img(options.inp1,options.inp2,options.inp3) + x.run() +if __name__=="__main__": + main() + + + diff --git a/dist/pagexml2img-1.0-py3.6.egg b/dist/pagexml2img-1.0-py3.6.egg new file mode 100644 index 0000000000000000000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8cc --- /dev/null +++ b/make.sh @@ -0,0 +1,9 @@ +all: build install + +build: + python3 setup.py build + +install: + python3 setup.py install --user + cp pagexml2img/pagexml2img.py /home/vahid/bin/pagexml2img + chmod +x /home/vahid/bin/pagexml2img diff --git a/pagexml2img.egg-info/PKG-INFO b/pagexml2img.egg-info/PKG-INFO new file mode 100644 index 0000000..31ddbdf --- /dev/null +++ b/pagexml2img.egg-info/PKG-INFO @@ -0,0 +1,11 @@ +Metadata-Version: 1.0 +Name: pagexml2img +Version: 1.0 +Summary: UNKNOWN +Home-page: UNKNOWN +Author: UNKNOWN +Author-email: UNKNOWN +License: UNKNOWN +Description-Content-Type: UNKNOWN +Description: UNKNOWN +Platform: UNKNOWN diff --git a/pagexml2img.egg-info/SOURCES.txt b/pagexml2img.egg-info/SOURCES.txt new file mode 100644 index 0000000..b24fdb8 --- /dev/null +++ b/pagexml2img.egg-info/SOURCES.txt @@ -0,0 +1,7 @@ +setup.py +pagexml2img/__init__.py +pagexml2img/pagexml2img.py +pagexml2img.egg-info/PKG-INFO +pagexml2img.egg-info/SOURCES.txt +pagexml2img.egg-info/dependency_links.txt +pagexml2img.egg-info/top_level.txt \ No newline at end of file diff --git a/pagexml2img.egg-info/dependency_links.txt b/pagexml2img.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/pagexml2img.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/pagexml2img.egg-info/top_level.txt b/pagexml2img.egg-info/top_level.txt new file mode 100644 index 0000000..687af0e --- /dev/null +++ b/pagexml2img.egg-info/top_level.txt @@ -0,0 +1 @@ +pagexml2img diff --git a/pagexml2img/__init__.py b/pagexml2img/__init__.py new file mode 100644 index 0000000..95912a5 --- /dev/null +++ b/pagexml2img/__init__.py @@ -0,0 +1 @@ +from pagexml2img import * \ No newline at end of file diff --git a/pagexml2img/pagexml2img.py b/pagexml2img/pagexml2img.py new file mode 100644 index 0000000..8d521f9 --- /dev/null +++ b/pagexml2img/pagexml2img.py @@ -0,0 +1,160 @@ +#! /usr/bin/env python3 + +__version__= '1.0' + +import argparse +import sys +import os +import numpy as np +import warnings +import xml.etree.ElementTree as ET +from tqdm import tqdm +import cv2 + +with warnings.catch_warnings(): + warnings.simplefilter("ignore") + +__doc__=\ +""" +tool to extract 2d or 3d RGB images from page xml data. In former case output will be 1 +2D image array which each class has filled with a pixel value. In the case of 3D RGB image +each class will be defined with a RGB value and beside images a text file of classes also will be produced. +This classes.txt file is required for dhsegment tool. +""" + +class pagexml2img: + def __init__(self,dir_in, out_dir,output_type): + self.dir=dir_in + self.output_dir=out_dir + self.output_type=output_type + + def get_content_of_dir(self): + """ + Listing all ground truth page xml files. All files are needed to have xml format. + """ + + gt_all=os.listdir(self.dir) + self.gt_list=[file for file in gt_all if file.split('.')[ len(file.split('.'))-1 ]=='xml' ] + + def get_images_of_ground_truth(self): + """ + Reading the page xml files and write the ground truth images into given output directory. + """ + + if self.output_type=='3d' or self.output_type=='3D': + classes=np.array([ [0,0,0],[255,0,0],[0,255,0],[0,0,255]]) + + + + + for index in tqdm(range(len(self.gt_list))): + try: + tree1 = ET.parse(self.dir+'/'+self.gt_list[index]) + root1=tree1.getroot() + + + for jj in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Page'): + y_len=int(jj.attrib['imageHeight']) + x_len=int(jj.attrib['imageWidth']) + + co_text=[] + co_sep=[] + co_img=[] + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}TextRegion'): + c_t_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_t_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_text.append(np.array(c_t_in)) + + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}ImageRegion'): + c_i_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_i_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_img.append(np.array(c_i_in)) + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}SeparatorRegion'): + c_s_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_s_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_sep.append(np.array(c_s_in)) + + img = np.zeros( (y_len,x_len,3) ) + img_poly=cv2.fillPoly(img, pts =co_text, color=(255,0,0)) + img_poly=cv2.fillPoly(img, pts =co_img, color=(0,255,0)) + img_poly=cv2.fillPoly(img, pts =co_sep, color=(0,0,255)) + + try: + cv2.imwrite(self.output_dir+'/'+self.gt_list[index].split('-')[1].split('.')[0]+'.png',img_poly ) + except: + cv2.imwrite(self.output_dir+'/'+self.gt_list[index].split('.')[0]+'.png',img_poly ) + except: + pass + np.savetxt(self.output_dir+'/../classes.txt',classes) + + if self.output_type=='2d' or self.output_type=='2D': + + for index in tqdm(range(len(self.gt_list))): + try: + + tree1 = ET.parse(self.dir+'/'+self.gt_list[index]) + root1=tree1.getroot() + + for jj in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Page'): + y_len=int(jj.attrib['imageHeight']) + x_len=int(jj.attrib['imageWidth']) + + co_text=[] + co_sep=[] + co_img=[] + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}TextRegion'): + c_t_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_t_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_text.append(np.array(c_t_in)) + + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}ImageRegion'): + c_i_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_i_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_img.append(np.array(c_i_in)) + + for nn in root1.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}SeparatorRegion'): + c_s_in=[] + for ll in nn.iter('{http://schema.primaresearch.org/PAGE/gts/pagecontent/2010-03-19}Point'): + c_s_in.append([ int(np.float(ll.attrib['x'])) , int(np.float(ll.attrib['y'])) ]) + co_sep.append(np.array(c_s_in)) + + img = np.zeros( (y_len,x_len) ) + img_poly=cv2.fillPoly(img, pts =co_text, color=(1,1,1)) + img_poly=cv2.fillPoly(img, pts =co_img, color=(2,2,2)) + img_poly=cv2.fillPoly(img, pts =co_sep, color=(3,3,3)) + try: + cv2.imwrite(self.output_dir+'/'+self.gt_list[index].split('-')[1].split('.')[0]+'.png',img_poly ) + except: + cv2.imwrite(self.output_dir+'/'+self.gt_list[index].split('.')[0]+'.png',img_poly ) + except: + pass + def run(self): + self.get_content_of_dir() + self.get_images_of_ground_truth() +def main(): + parser=argparse.ArgumentParser() + + parser.add_argument('-dir_in','--dir_in', dest='inp1', default=None, help='directory of page-xml files') + parser.add_argument('-dir_out','--dir_out', dest='inp2', default=None, help='directory where ground truth images would be written') + parser.add_argument('-type','--type', dest='inp3', default=None, help='this defines how output should be. A 2d image array or a 3d image array encoded with RGB color. Just pass 2d or 3d. The file will be saved one directory up. 2D image array is 3d but only information of one channel would be enough since all channels have the same values.') + options=parser.parse_args() + + possibles=globals() + possibles.update(locals()) + x=pagexml2img(options.inp1,options.inp2,options.inp3) + x.run() +if __name__=="__main__": + main() + + + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..d4483aa --- /dev/null +++ b/setup.py @@ -0,0 +1,6 @@ +#! /usr/bin/env python3 + +import setuptools +from numpy.distutils.core import Extension, setup + +setup(name='pagexml2img',version=1.0,packages=['pagexml2img'])