Skip to content

Commit

Permalink
changes mentioned in revew
Browse files Browse the repository at this point in the history
  • Loading branch information
khurramHashmi committed Dec 17, 2019
1 parent 76b1795 commit 0ac8567
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 1,434 deletions.
2 changes: 1 addition & 1 deletion ocrd_anybaseocr/cli/ocrd_anybaseocr_binarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def process(self):
for name in self.parameter.keys()])]))

page = pcgts.get_Page()
page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter="binarized")
page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter="binarized,deskewed,cropped")
LOG.info("Binarizing on '%s' level in page '%s'", oplevel, page_id)

if oplevel=="page":
Expand Down
48 changes: 31 additions & 17 deletions ocrd_anybaseocr/cli/ocrd_anybaseocr_block_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from shapely.geometry import Polygon

from ocrd_anybaseocr.mrcnn import model
#from ocrd_anybaseocr.mrcnn import visualize
from ocrd_anybaseocr.mrcnn.config import Config
from ocrd_models.constants import NAMESPACES as NS

Expand Down Expand Up @@ -69,7 +68,6 @@ def process(self):
if not tf.test.is_gpu_available():
LOG.error("Your system has no CUDA installed. No GPU detected.")
sys.exit(1)

try:
page_grp, self.image_grp = self.output_file_grp.split(',')
except ValueError:
Expand All @@ -94,7 +92,7 @@ def process(self):
sys.exit(1)
'''
config = InferenceConfig()
mrcnn_model = model.MaskRCNN(mode="inference", model_dir="./", config=config)
mrcnn_model = model.MaskRCNN(mode="inference", model_dir=str(model_path), config=config)
mrcnn_model.load_weights(str(model_weights), by_name=True)

oplevel = self.parameter['operation_level']
Expand All @@ -104,7 +102,7 @@ def process(self):
page = pcgts.get_Page()
page_id = input_file.pageId or input_file.ID

page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter='binarized,deskewed,cropped')
page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter ='binarized,deskewed,cropped')

#Display Warning If image segment results already exist or not in StructMap?
regions = page.get_TextRegion() + page.get_TableRegion()
Expand Down Expand Up @@ -141,11 +139,13 @@ def _process_segment(self,page_image, page, page_xywh, page_id, input_file, n, m
else:
LOG.warning('keeping existing TextRegions in page "%s"', page_id)
return

border_coords = page.get_Border().get_Coords()
border_points = polygon_from_points(border_coords.get_points())

border = Polygon(border_points)
#check if border exists
if page.get_Border():
border_coords = page.get_Border().get_Coords()
border_points = polygon_from_points(border_coords.get_points())

border = Polygon(border_points)

img_array = ocrolib.pil2array(page_image)
if len(img_array.shape) <= 2:
img_array = np.stack((img_array,)*3, axis=-1)
Expand All @@ -160,6 +160,12 @@ def _process_segment(self,page_image, page, page_xywh, page_id, input_file, n, m
width,height,_ = img_array.shape
min_x = r['rois'][i][0]
min_y = r['rois'][i][1]
max_x = r['rois'][i][2]
max_y = r['rois'][i][3]
if (min_x - 5) > width and r['class_ids'][i] == 2:
min_x-=5
if (max_x + 10) < width and r['class_ids'][i] == 2:
min_x+=10
reading_order.append((min_y, min_x))

reading_order = sorted(reading_order, key=lambda reading_order:(reading_order[0], reading_order[1]))
Expand All @@ -173,11 +179,18 @@ def _process_segment(self,page_image, page, page_xywh, page_id, input_file, n, m
min_y = r['rois'][i][1]
max_x = r['rois'][i][2]
max_y = r['rois'][i][3]
if (min_x - 5) > width and r['class_ids'][i] == 2:
min_x-=5
if (max_x + 10) < width and r['class_ids'][i] == 2:
min_x+=10
region_polygon = [[min_x, min_y], [max_x, min_y], [max_x, max_y], [min_x, max_y]]

cut_region_polygon = border.intersection(Polygon(region_polygon))
if cut_region_polygon.is_empty:
continue
if border:
cut_region_polygon = border.intersection(Polygon(region_polygon))
if cut_region_polygon.is_empty:
continue
else:
cut_region_polygon = Polygon(region_polygon)

order_index = reading_order.index((min_y, min_x))
region_id = '%s_region%04d' % (page_id, order_index)
Expand All @@ -195,6 +208,11 @@ def _process_segment(self,page_image, page, page_xywh, page_id, input_file, n, m
min_y = r['rois'][i][1]
max_x = r['rois'][i][2]
max_y = r['rois'][i][3]

if (min_x - 5) > width and r['class_ids'][i] == 2:
min_x-=5
if (max_x + 10) < width and r['class_ids'][i] == 2:
min_x+=10
region_polygon = [[min_x, min_y], [max_x, min_y], [max_x, max_y], [min_x, max_y]]

cut_region_polygon = border.intersection(Polygon(region_polygon))
Expand All @@ -207,11 +225,7 @@ def _process_segment(self,page_image, page, page_xywh, page_id, input_file, n, m

read_order = reading_order.index((min_y, min_x))

#small post-processing incase of paragrapgh to not cut last alphabets
if (min_x - 5) > width and r['class_ids'][i] == 2:
min_x-=5
if (max_x + 10) < width and r['class_ids'][i] == 2:
min_x+=10




Expand Down
2 changes: 1 addition & 1 deletion ocrd_anybaseocr/cli/ocrd_anybaseocr_cropping.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def process(self):
page = pcgts.get_Page()


page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter='cropped')
page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter='cropped',feature_selector='binarized,deskewed')


if oplevel=="page":
Expand Down
2 changes: 1 addition & 1 deletion ocrd_anybaseocr/cli/ocrd_anybaseocr_deskew.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def process(self):
angle = page.get_orientation()
if angle:
LOG.warning('Overwriting existing deskewing angle: %i', angle)
page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter='deskewed')
page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter='deskewed,cropped',feature_selector='binarized')


if oplevel=="page":
Expand Down
21 changes: 18 additions & 3 deletions ocrd_anybaseocr/cli/ocrd_anybaseocr_layout_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ def process(self):
if not tf.test.is_gpu_available():
LOG.error("Your system has no CUDA installed. No GPU detected.")
sys.exit(1)

model_path = Path(self.parameter['model_path'])
class_mapper_path = Path(self.parameter['class_mapping_path'])
if not Path(model_path).is_file():
Expand All @@ -229,8 +228,24 @@ def process(self):
fname = pcgts.get_Page().imageFilename
LOG.info("CURRENT INPUT FILE %s", fname)
size = 600, 500
img = Image.open(fname)
img_array = ocrolib.pil2array(img.resize((500, 600), Image.ANTIALIAS))

metadata = pcgts.get_Metadata()
metadata.add_MetadataItem(
MetadataItemType(type_="processingStep",
name=self.ocrd_tool['steps'][0],
value=TOOL,
Labels=[LabelsType(#externalRef="parameter",
Label=[LabelType(type_=name,
value=self.parameter[name])
for name in self.parameter.keys()])]))

page = pcgts.get_Page()
LOG.info("INPUT FILE %s", input_file.pageId or input_file.ID)

page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_selector='binarized,deskewed')


img_array = ocrolib.pil2array(page_image.resize((500, 600), Image.ANTIALIAS))
img_array = img_array * 1./255.
img_array = img_array[np.newaxis, :, :, np.newaxis]
results = self.start_test(model, img_array, fname, label_mapping)
Expand Down
12 changes: 8 additions & 4 deletions ocrd_anybaseocr/cli/ocrd_anybaseocr_textline.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ def process(self):
page = pcgts.get_Page()
LOG.info("INPUT FILE %s", input_file.pageId or input_file.ID)

page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter='tiseged')
page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_selector='binarized,deskewed,cropped')

if oplevel == 'page':
LOG.warning("Operation level should be region.")
break
self._process_segment(page_image, page,None, page_xywh, page_id, input_file, n)

else:
regions = page.get_TextRegion()
if not regions:
Expand All @@ -110,8 +111,11 @@ def process(self):
y_max = page_image.size[1]-1

img__ = page_image.crop((x_min,y_min,x_max,y_max))
# page_image = page_image.crop((x_min,y_min,x_max,y_max))


region_image, region_xywh = self.workspace.image_from_segment(region, page_image, page_xywh)
# region_image = region_image.crop((x_min,y_min,x_max,y_max))
#region_image, region_xywh = self.workspace.image_from_segment(region, page_image, page_xywh)
# TODO: not tested on regions
#self._process_segment(region_image, page, region, region_xywh, region.id, input_file, k)
Expand Down Expand Up @@ -183,9 +187,9 @@ def _process_segment(self, page_image, page, textregion, region_xywh, page_id, i
line_polygon = coordinates_for_segment(line_polygon, page_image, region_xywh)
line_points = points_from_polygon(line_polygon)

img = binary[l.bounds[0],l.bounds[1]]
img = (1-img) * (-1)
img = cleaned[l.bounds[0],l.bounds[1]]
img = np.array(255*(img>ocrolib.midrange(img)),'B')
img = 255-img
img = ocrolib.array2pil(img)

file_id = input_file.ID.replace(self.input_file_grp, self.image_grp)
Expand Down
4 changes: 2 additions & 2 deletions ocrd_anybaseocr/cli/ocrd_anybaseocr_tiseg.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def process(self):
page = pcgts.get_Page()
LOG.info("INPUT FILE %s", input_file.pageId or input_file.ID)

page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_filter='tiseged')
page_image, page_xywh, page_image_info = self.workspace.image_from_page(page, page_id, feature_selector='binarized,deskewed,cropped')
if oplevel == 'page':
self._process_segment(page_image, page, page_xywh, page_id, input_file, n)
else:
Expand Down Expand Up @@ -129,7 +129,7 @@ def _process_segment(self,page_image, page, page_xywh, page_id, input_file, n):
text_part = array((1-I*(1-Iseedfill)), dtype=int)
text_part[0, 0] = 0 # only for visualisation purpose

page_xywh['features'] += ',tiseged'
# page_xywh['features'] += ',tiseged'

bin_array = array(255*(text_part>ocrolib.midrange(text_part)),'B')
bin_image = ocrolib.array2pil(bin_array)
Expand Down
Loading

0 comments on commit 0ac8567

Please sign in to comment.