Spaces:
Sleeping
Sleeping
Update yolo_text_extraction.py
Browse files- yolo_text_extraction.py +11 -32
yolo_text_extraction.py
CHANGED
|
@@ -1,32 +1,24 @@
|
|
| 1 |
-
from ultralytics import YOLO
|
| 2 |
-
from PIL import Image,ImageDraw
|
| 3 |
-
import numpy as np
|
| 4 |
from PIL import ImageFilter
|
| 5 |
-
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
-
|
| 8 |
-
import numpy as np
|
| 9 |
-
from ocr_functions import paddle_ocr,textract_ocr,tesseract_ocr
|
| 10 |
from pdf2image import convert_from_path
|
| 11 |
|
| 12 |
-
|
| 13 |
-
model =YOLO("yolo_model/best.pt")
|
| 14 |
-
|
| 15 |
|
| 16 |
def check_intersection(bbox1, bbox2):
|
| 17 |
-
# Check for intersection between two bounding boxes
|
| 18 |
x1, y1, x2, y2 = bbox1
|
| 19 |
x3, y3, x4, y4 = bbox2
|
| 20 |
return not (x3 > x2 or x4 < x1 or y3 > y2 or y4 < y1)
|
| 21 |
|
| 22 |
def check_inclusion(bbox1, bbox2):
|
| 23 |
-
# Check if one bounding box is completely inside another
|
| 24 |
x1, y1, x2, y2 = bbox1
|
| 25 |
x3, y3, x4, y4 = bbox2
|
| 26 |
return x1 >= x3 and y1 >= y3 and x2 <= x4 and y2 <= y4
|
| 27 |
|
| 28 |
def union_bbox(bbox1, bbox2):
|
| 29 |
-
# Calculate the union of two bounding boxes
|
| 30 |
x1 = min(bbox1[0], bbox2[0])
|
| 31 |
y1 = min(bbox1[1], bbox2[1])
|
| 32 |
x2 = max(bbox1[2], bbox2[2])
|
|
@@ -34,43 +26,32 @@ def union_bbox(bbox1, bbox2):
|
|
| 34 |
return [x1, y1, x2, y2]
|
| 35 |
|
| 36 |
def filter_bboxes(bboxes):
|
| 37 |
-
# Iterate through each pair of bounding boxes and filter out those that intersect or are completely contained within another
|
| 38 |
filtered_bboxes = []
|
| 39 |
for bbox1 in bboxes:
|
| 40 |
is_valid = True
|
| 41 |
for bbox2 in filtered_bboxes:
|
| 42 |
if check_intersection(bbox1, bbox2):
|
| 43 |
-
# If the two bounding boxes intersect, compute their union
|
| 44 |
bbox1 = union_bbox(bbox1, bbox2)
|
| 45 |
-
# Mark the current bbox as invalid to be removed
|
| 46 |
is_valid = False
|
| 47 |
break
|
| 48 |
elif check_inclusion(bbox1, bbox2):
|
| 49 |
-
# If bbox1 is completely contained within bbox2, mark bbox1 as invalid to be removed
|
| 50 |
is_valid = False
|
| 51 |
break
|
| 52 |
if is_valid:
|
| 53 |
filtered_bboxes.append(bbox1)
|
| 54 |
return filtered_bboxes
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
def draw_bboxes(image, bboxes ):
|
| 60 |
draw = ImageDraw.Draw(image)
|
| 61 |
for bbox in bboxes:
|
| 62 |
x1, y1, x2, y2 = bbox
|
| 63 |
-
|
| 64 |
-
x1,y1,x2,y2 = int(x1),int(y1),int(x2),int(y2)
|
| 65 |
draw.rectangle([(x1, y1), (x2, y2)], outline=(255, 0, 0), width=2)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def extract_image(image,box):
|
| 70 |
x1, y1, x2, y2 = box
|
| 71 |
cropped_image = image.crop((x1, y1, x2, y2))
|
| 72 |
|
| 73 |
-
|
| 74 |
def text_image(image):
|
| 75 |
image = image.convert("RGB")
|
| 76 |
image = image.filter(ImageFilter.MedianFilter(3))
|
|
@@ -86,13 +67,11 @@ def text_image(image):
|
|
| 86 |
draw_bboxes(image, bboxes_filter)
|
| 87 |
image.save("output.png")
|
| 88 |
texts = [textract_ocr(image, bbox) for bbox in bboxes_filter]
|
| 89 |
-
return "\n------section-------\n"+"\n------section-------\n".join(texts)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
|
| 93 |
def pdf_to_text(pdf_file):
|
| 94 |
text = ""
|
| 95 |
images = convert_from_path(pdf_file)
|
| 96 |
-
for image in images
|
| 97 |
text = text + text_image(image) + "\n"
|
| 98 |
-
return text
|
|
|
|
| 1 |
+
from ultralytics import YOLO
|
| 2 |
+
from PIL import Image, ImageDraw
|
| 3 |
+
import numpy as np
|
| 4 |
from PIL import ImageFilter
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
+
from ocr_functions import paddle_ocr, textract_ocr, tesseract_ocr
|
|
|
|
|
|
|
| 7 |
from pdf2image import convert_from_path
|
| 8 |
|
| 9 |
+
model = YOLO("yolo_model/best.pt")
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def check_intersection(bbox1, bbox2):
|
|
|
|
| 12 |
x1, y1, x2, y2 = bbox1
|
| 13 |
x3, y3, x4, y4 = bbox2
|
| 14 |
return not (x3 > x2 or x4 < x1 or y3 > y2 or y4 < y1)
|
| 15 |
|
| 16 |
def check_inclusion(bbox1, bbox2):
|
|
|
|
| 17 |
x1, y1, x2, y2 = bbox1
|
| 18 |
x3, y3, x4, y4 = bbox2
|
| 19 |
return x1 >= x3 and y1 >= y3 and x2 <= x4 and y2 <= y4
|
| 20 |
|
| 21 |
def union_bbox(bbox1, bbox2):
|
|
|
|
| 22 |
x1 = min(bbox1[0], bbox2[0])
|
| 23 |
y1 = min(bbox1[1], bbox2[1])
|
| 24 |
x2 = max(bbox1[2], bbox2[2])
|
|
|
|
| 26 |
return [x1, y1, x2, y2]
|
| 27 |
|
| 28 |
def filter_bboxes(bboxes):
|
|
|
|
| 29 |
filtered_bboxes = []
|
| 30 |
for bbox1 in bboxes:
|
| 31 |
is_valid = True
|
| 32 |
for bbox2 in filtered_bboxes:
|
| 33 |
if check_intersection(bbox1, bbox2):
|
|
|
|
| 34 |
bbox1 = union_bbox(bbox1, bbox2)
|
|
|
|
| 35 |
is_valid = False
|
| 36 |
break
|
| 37 |
elif check_inclusion(bbox1, bbox2):
|
|
|
|
| 38 |
is_valid = False
|
| 39 |
break
|
| 40 |
if is_valid:
|
| 41 |
filtered_bboxes.append(bbox1)
|
| 42 |
return filtered_bboxes
|
| 43 |
|
| 44 |
+
def draw_bboxes(image, bboxes):
|
|
|
|
|
|
|
|
|
|
| 45 |
draw = ImageDraw.Draw(image)
|
| 46 |
for bbox in bboxes:
|
| 47 |
x1, y1, x2, y2 = bbox
|
| 48 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
|
|
|
|
| 49 |
draw.rectangle([(x1, y1), (x2, y2)], outline=(255, 0, 0), width=2)
|
| 50 |
|
| 51 |
+
def extract_image(image, box):
|
|
|
|
|
|
|
| 52 |
x1, y1, x2, y2 = box
|
| 53 |
cropped_image = image.crop((x1, y1, x2, y2))
|
| 54 |
|
|
|
|
| 55 |
def text_image(image):
|
| 56 |
image = image.convert("RGB")
|
| 57 |
image = image.filter(ImageFilter.MedianFilter(3))
|
|
|
|
| 67 |
draw_bboxes(image, bboxes_filter)
|
| 68 |
image.save("output.png")
|
| 69 |
texts = [textract_ocr(image, bbox) for bbox in bboxes_filter]
|
| 70 |
+
return "\n------section-------\n" + "\n------section-------\n".join(texts)
|
|
|
|
|
|
|
| 71 |
|
| 72 |
def pdf_to_text(pdf_file):
|
| 73 |
text = ""
|
| 74 |
images = convert_from_path(pdf_file)
|
| 75 |
+
for image in images:
|
| 76 |
text = text + text_image(image) + "\n"
|
| 77 |
+
return text
|