-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
47 lines (38 loc) · 1.39 KB
/
utils.py
File metadata and controls
47 lines (38 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import easyocr
import cv2
import matplotlib.pyplot as plt
from PIL import Image
from style_classifier import classify_style
from style_model import load_style_model
import easyocr
import cv2
import numpy as np
from style_classifier import classify_style
from style_model import load_style_model
def process_document(image_path, style_model):
"""
Uses EasyOCR to detect & recognize text bounding boxes and classifies text style.
"""
reader = easyocr.Reader(['en'], gpu=False)
ocr_results = reader.readtext(image_path, detail=1) # detail=1 => (bbox, text, conf)
original_bgr = cv2.imread(image_path)
final_output = []
for (bbox, text, conf) in ocr_results:
(x_min, y_min), (_, _), (x_max, y_max), (_, _) = bbox
cropped_bgr = original_bgr[int(y_min):int(y_max), int(x_min):int(x_max)] # Convert to integer indices
style_label, style_conf = classify_style(cropped_bgr, style_model)
final_output.append({
"text": text,
"ocr_conf": conf,
"bbox": bbox,
"style": style_label,
"style_conf": style_conf
})
return final_output
def display_image(image_path):
""" Helper function to display the selected image """
img_pil = Image.open(image_path)
plt.imshow(img_pil)
plt.title(f"Selected Image: {image_path}")
plt.axis("off")
plt.show()