farbodpya commited on
Commit
0c3af99
·
verified ·
1 Parent(s): cde5e17

Add inference script

Browse files
Files changed (1) hide show
  1. inference.py +74 -0
inference.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from PIL import Image
3
+ import torchvision.transforms.functional as TF
4
+ import cv2
5
+ import matplotlib.pyplot as plt
6
+ from huggingface_hub import hf_hub_download
7
+
8
+
9
+ # --- Load model from HF ---
10
+ model_path = hf_hub_download(repo_id="farbodpya/Persian-OCR", filename="pytorch_model.bin")
11
+ model = CNN_Transformer_OCR(num_classes=len(idx_to_char)+1).to(device)
12
+ model.load_state_dict(torch.load(model_path, map_location=device))
13
+ model.eval()
14
+
15
+ # --- Define transforms ---
16
+ class OCRTestTransform:
17
+ def __init__(self, img_height=64, max_width=1600):
18
+ self.img_height = img_height
19
+ self.max_width = max_width
20
+ def __call__(self, img):
21
+ img = img.convert("L")
22
+ w, h = img.size
23
+ new_w = int(w * self.img_height / h)
24
+ img = img.resize((min(new_w, self.max_width), self.img_height), Image.BICUBIC)
25
+ new_img = Image.new("L", (self.max_width, self.img_height), 255)
26
+ new_img.paste(img, (0, 0))
27
+ img = TF.to_tensor(new_img)
28
+ img = TF.normalize(img, (0.5,), (0.5,))
29
+ return img
30
+
31
+ transform_test = OCRTestTransform()
32
+
33
+ # --- Line segmentation ---
34
+ def segment_lines_precise(image_path, min_line_height=12, margin=6, visualize=False):
35
+ img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
36
+ _, binary = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
37
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (img.shape[1]//30, 1))
38
+ morphed = cv2.dilate(binary, kernel, iterations=1)
39
+ contours, _ = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
40
+ contours = sorted(contours, key=lambda ctr: cv2.boundingRect(ctr)[1])
41
+ lines = []
42
+ for ctr in contours:
43
+ x, y, w, h = cv2.boundingRect(ctr)
44
+ if h < min_line_height: continue
45
+ y1 = max(0, y - margin)
46
+ y2 = min(img.shape[0], y + h + margin)
47
+ line_img = img[y1:y2, x:x+w]
48
+ lines.append(Image.fromarray(line_img))
49
+ if visualize:
50
+ for i, line_img in enumerate(lines):
51
+ plt.figure(figsize=(12,2))
52
+ plt.imshow(line_img, cmap='gray')
53
+ plt.axis('off')
54
+ plt.title(f"Line {i+1}")
55
+ plt.show()
56
+ return lines
57
+
58
+ # --- OCR function ---
59
+ def ocr_page(image_path, visualize=False):
60
+ lines = segment_lines_precise(image_path, visualize=visualize)
61
+ all_texts = []
62
+ for idx, line_img in enumerate(lines, 1):
63
+ img_tensor = transform_test(line_img).unsqueeze(0).to(device)
64
+ with torch.no_grad():
65
+ outputs = model(img_tensor)
66
+ pred_text = greedy_decode(outputs, idx_to_char)[0]
67
+ all_texts.append(pred_text)
68
+ print(f"Line {idx}: {pred_text}")
69
+ return "\n".join(all_texts)
70
+
71
+ # --- Example ---
72
+ img_path = "example.png"
73
+ final_text = ocr_page(img_path, visualize=True)
74
+ print("\n=== Final OCR Page ===\n", final_text)