Delete HandwritingOCR/testParser.py
Browse files- HandwritingOCR/testParser.py +0 -45
HandwritingOCR/testParser.py
DELETED
|
@@ -1,45 +0,0 @@
|
|
| 1 |
-
import layoutparser as lp
|
| 2 |
-
import cv2
|
| 3 |
-
import matplotlib.pyplot as plt
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
image = cv2.imread("Projects/HandwritingOCR/captured_images/pasted_image.jpg")
|
| 7 |
-
#image = image[..., ::-1]
|
| 8 |
-
|
| 9 |
-
model = lp.Detectron2LayoutModel(
|
| 10 |
-
"lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config",
|
| 11 |
-
extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8],
|
| 12 |
-
label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"},
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
# Phát hiện bố cục trên ảnh đã được cải thiện độ phân giải
|
| 17 |
-
layout = model.detect(image)
|
| 18 |
-
|
| 19 |
-
# Vẽ các hộp phát hiện lên ảnh
|
| 20 |
-
lp.draw_box(image, layout, box_width=3)
|
| 21 |
-
|
| 22 |
-
print(type(layout))
|
| 23 |
-
print(layout[0])
|
| 24 |
-
text_blocks = lp.Layout([b for b in layout if b.type=='Text'])
|
| 25 |
-
figure_blocks = lp.Layout([b for b in layout if b.type=='Figure'])
|
| 26 |
-
|
| 27 |
-
text_blocks = lp.Layout([b for b in text_blocks \
|
| 28 |
-
if not any(b.is_in(b_fig) for b_fig in figure_blocks)])
|
| 29 |
-
h, w = image.shape[:2]
|
| 30 |
-
|
| 31 |
-
left_interval = lp.Interval(0, w/2*1.05, axis='x').put_on_canvas(image)
|
| 32 |
-
|
| 33 |
-
left_blocks = text_blocks.filter_by(left_interval, center=True)
|
| 34 |
-
left_blocks.sort(key = lambda b:b.coordinates[1])
|
| 35 |
-
|
| 36 |
-
right_blocks = [b for b in text_blocks if b not in left_blocks]
|
| 37 |
-
right_blocks.sort(key = lambda b:b.coordinates[1])
|
| 38 |
-
|
| 39 |
-
# And finally combine the two list and add the index
|
| 40 |
-
# according to the order
|
| 41 |
-
text_blocks = lp.Layout([b.set(id = idx) for idx, b in enumerate(left_blocks + right_blocks)])
|
| 42 |
-
|
| 43 |
-
lp.draw_box(image, text_blocks,
|
| 44 |
-
box_width=3,
|
| 45 |
-
show_element_id=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|