Reduce code duplication
This commit is contained in:
115
owocr/ocr.py
115
owocr/ocr.py
@@ -191,6 +191,20 @@ def limit_image_size(img, max_size):
|
|||||||
|
|
||||||
return False, '', (None, None)
|
return False, '', (None, None)
|
||||||
|
|
||||||
|
def merge_bounding_boxes(ocr_element_list):
|
||||||
|
all_bboxes = [e.bounding_box for e in ocr_element_list]
|
||||||
|
min_x = min(b.center_x - b.width / 2 for b in all_bboxes)
|
||||||
|
max_x = max(b.center_x + b.width / 2 for b in all_bboxes)
|
||||||
|
min_y = min(b.center_y - b.height / 2 for b in all_bboxes)
|
||||||
|
max_y = max(b.center_y + b.height / 2 for b in all_bboxes)
|
||||||
|
|
||||||
|
return BoundingBox(
|
||||||
|
center_x=(min_x + max_x) / 2,
|
||||||
|
center_y=(min_y + max_y) / 2,
|
||||||
|
width=max_x - min_x,
|
||||||
|
height=max_y - min_y
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class MangaOcr:
|
class MangaOcr:
|
||||||
name = 'mangaocr'
|
name = 'mangaocr'
|
||||||
@@ -744,20 +758,7 @@ class AppleVision:
|
|||||||
lines.append(line)
|
lines.append(line)
|
||||||
|
|
||||||
if lines:
|
if lines:
|
||||||
# Approximate paragraph bbox by combining all line bboxes
|
p_bbox = merge_bounding_boxes(lines)
|
||||||
all_line_bboxes = [l.bounding_box for l in lines]
|
|
||||||
min_x = min(b.center_x - b.width / 2 for b in all_line_bboxes)
|
|
||||||
max_x = max(b.center_x + b.width / 2 for b in all_line_bboxes)
|
|
||||||
min_y = min(b.center_y - b.height / 2 for b in all_line_bboxes)
|
|
||||||
max_y = max(b.center_y + b.height / 2 for b in all_line_bboxes)
|
|
||||||
|
|
||||||
p_bbox = BoundingBox(
|
|
||||||
center_x=(min_x + max_x) / 2,
|
|
||||||
center_y=(min_y + max_y) / 2,
|
|
||||||
width=max_x - min_x,
|
|
||||||
height=max_y - min_y
|
|
||||||
)
|
|
||||||
|
|
||||||
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
||||||
paragraphs = [paragraph]
|
paragraphs = [paragraph]
|
||||||
else:
|
else:
|
||||||
@@ -913,19 +914,7 @@ class AppleLiveText:
|
|||||||
|
|
||||||
# Create a single paragraph to hold all lines
|
# Create a single paragraph to hold all lines
|
||||||
if lines:
|
if lines:
|
||||||
# Approximate paragraph bbox by combining all line bboxes
|
p_bbox = merge_bounding_boxes(lines)
|
||||||
all_line_bboxes = [l.bounding_box for l in lines]
|
|
||||||
min_x = min(b.center_x - b.width / 2 for b in all_line_bboxes)
|
|
||||||
max_x = max(b.center_x + b.width / 2 for b in all_line_bboxes)
|
|
||||||
min_y = min(b.center_y - b.height / 2 for b in all_line_bboxes)
|
|
||||||
max_y = max(b.center_y + b.height / 2 for b in all_line_bboxes)
|
|
||||||
|
|
||||||
p_bbox = BoundingBox(
|
|
||||||
center_x=(min_x + max_x) / 2,
|
|
||||||
center_y=(min_y + max_y) / 2,
|
|
||||||
width=max_x - min_x,
|
|
||||||
height=max_y - min_y
|
|
||||||
)
|
|
||||||
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
||||||
paragraphs = [paragraph]
|
paragraphs = [paragraph]
|
||||||
else:
|
else:
|
||||||
@@ -997,19 +986,7 @@ class WinRTOCR:
|
|||||||
)
|
)
|
||||||
words.append(word)
|
words.append(word)
|
||||||
|
|
||||||
# Approximate line bbox by combining all word bboxes
|
l_bbox = merge_bounding_boxes(words)
|
||||||
all_word_bboxes = [w.bounding_box for w in words]
|
|
||||||
min_x = min(b.center_x - b.width / 2 for b in all_word_bboxes)
|
|
||||||
max_x = max(b.center_x + b.width / 2 for b in all_word_bboxes)
|
|
||||||
min_y = min(b.center_y - b.height / 2 for b in all_word_bboxes)
|
|
||||||
max_y = max(b.center_y + b.height / 2 for b in all_word_bboxes)
|
|
||||||
|
|
||||||
l_bbox = BoundingBox(
|
|
||||||
center_x=(min_x + max_x) / 2,
|
|
||||||
center_y=(min_y + max_y) / 2,
|
|
||||||
width=max_x - min_x,
|
|
||||||
height=max_y - min_y
|
|
||||||
)
|
|
||||||
line = Line(
|
line = Line(
|
||||||
text=l.get('text', ''),
|
text=l.get('text', ''),
|
||||||
bounding_box=l_bbox,
|
bounding_box=l_bbox,
|
||||||
@@ -1019,19 +996,7 @@ class WinRTOCR:
|
|||||||
|
|
||||||
# Create a single paragraph to hold all lines
|
# Create a single paragraph to hold all lines
|
||||||
if lines:
|
if lines:
|
||||||
# Approximate paragraph bbox by combining all line bboxes
|
p_bbox = merge_bounding_boxes(lines)
|
||||||
all_line_bboxes = [l.bounding_box for l in lines]
|
|
||||||
min_x = min(b.center_x - b.width / 2 for b in all_line_bboxes)
|
|
||||||
max_x = max(b.center_x + b.width / 2 for b in all_line_bboxes)
|
|
||||||
min_y = min(b.center_y - b.height / 2 for b in all_line_bboxes)
|
|
||||||
max_y = max(b.center_y + b.height / 2 for b in all_line_bboxes)
|
|
||||||
|
|
||||||
p_bbox = BoundingBox(
|
|
||||||
center_x=(min_x + max_x) / 2,
|
|
||||||
center_y=(min_y + max_y) / 2,
|
|
||||||
width=max_x - min_x,
|
|
||||||
height=max_y - min_y
|
|
||||||
)
|
|
||||||
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
||||||
paragraphs = [paragraph]
|
paragraphs = [paragraph]
|
||||||
else:
|
else:
|
||||||
@@ -1142,19 +1107,7 @@ class OneOCR:
|
|||||||
|
|
||||||
# Create a single paragraph to hold all lines
|
# Create a single paragraph to hold all lines
|
||||||
if lines:
|
if lines:
|
||||||
# Approximate paragraph bbox by combining all line bboxes
|
p_bbox = merge_bounding_boxes(lines)
|
||||||
all_line_bboxes = [l.bounding_box for l in lines]
|
|
||||||
min_x = min(b.center_x - b.width / 2 for b in all_line_bboxes)
|
|
||||||
max_x = max(b.center_x + b.width / 2 for b in all_line_bboxes)
|
|
||||||
min_y = min(b.center_y - b.height / 2 for b in all_line_bboxes)
|
|
||||||
max_y = max(b.center_y + b.height / 2 for b in all_line_bboxes)
|
|
||||||
|
|
||||||
p_bbox = BoundingBox(
|
|
||||||
center_x=(min_x + max_x) / 2,
|
|
||||||
center_y=(min_y + max_y) / 2,
|
|
||||||
width=max_x - min_x,
|
|
||||||
height=max_y - min_y
|
|
||||||
)
|
|
||||||
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
||||||
paragraphs = [paragraph]
|
paragraphs = [paragraph]
|
||||||
else:
|
else:
|
||||||
@@ -1339,20 +1292,7 @@ class EasyOCR:
|
|||||||
lines.append(line)
|
lines.append(line)
|
||||||
|
|
||||||
if lines:
|
if lines:
|
||||||
# Approximate paragraph bbox by combining all line bboxes
|
p_bbox = merge_bounding_boxes(lines)
|
||||||
all_line_bboxes = [l.bounding_box for l in lines]
|
|
||||||
min_x = min(b.center_x - b.width / 2 for b in all_line_bboxes)
|
|
||||||
max_x = max(b.center_x + b.width / 2 for b in all_line_bboxes)
|
|
||||||
min_y = min(b.center_y - b.height / 2 for b in all_line_bboxes)
|
|
||||||
max_y = max(b.center_y + b.height / 2 for b in all_line_bboxes)
|
|
||||||
|
|
||||||
p_bbox = BoundingBox(
|
|
||||||
center_x=(min_x + max_x) / 2,
|
|
||||||
center_y=(min_y + max_y) / 2,
|
|
||||||
width=max_x - min_x,
|
|
||||||
height=max_y - min_y
|
|
||||||
)
|
|
||||||
|
|
||||||
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
||||||
paragraphs = [paragraph]
|
paragraphs = [paragraph]
|
||||||
else:
|
else:
|
||||||
@@ -1454,20 +1394,7 @@ class RapidOCR:
|
|||||||
lines.append(line)
|
lines.append(line)
|
||||||
|
|
||||||
if lines:
|
if lines:
|
||||||
# Approximate paragraph bbox by combining all line bboxes
|
p_bbox = merge_bounding_boxes(lines)
|
||||||
all_line_bboxes = [l.bounding_box for l in lines]
|
|
||||||
min_x = min(b.center_x - b.width / 2 for b in all_line_bboxes)
|
|
||||||
max_x = max(b.center_x + b.width / 2 for b in all_line_bboxes)
|
|
||||||
min_y = min(b.center_y - b.height / 2 for b in all_line_bboxes)
|
|
||||||
max_y = max(b.center_y + b.height / 2 for b in all_line_bboxes)
|
|
||||||
|
|
||||||
p_bbox = BoundingBox(
|
|
||||||
center_x=(min_x + max_x) / 2,
|
|
||||||
center_y=(min_y + max_y) / 2,
|
|
||||||
width=max_x - min_x,
|
|
||||||
height=max_y - min_y
|
|
||||||
)
|
|
||||||
|
|
||||||
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
paragraph = Paragraph(bounding_box=p_bbox, lines=lines)
|
||||||
paragraphs = [paragraph]
|
paragraphs = [paragraph]
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user