Spaces:
Running
Running
Upload main.py
#3
by
anwer-1 - opened
main.py
CHANGED
|
@@ -7,6 +7,7 @@ import uvicorn
|
|
| 7 |
import os
|
| 8 |
import numpy as np
|
| 9 |
import cv2
|
|
|
|
| 10 |
|
| 11 |
# PDF support
|
| 12 |
try:
|
|
@@ -35,6 +36,23 @@ async def startup_event():
|
|
| 35 |
print("Server started. OCR models will be loaded lazily on first request.")
|
| 36 |
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def get_models():
|
| 39 |
global paddle_detector, paddle_recognizer
|
| 40 |
|
|
@@ -54,10 +72,16 @@ def get_models():
|
|
| 54 |
return paddle_detector, paddle_recognizer
|
| 55 |
|
| 56 |
|
| 57 |
-
def process_image(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
h_img, w_img = img.shape[:2]
|
| 59 |
|
| 60 |
-
# 1️⃣ Detect text
|
| 61 |
results = detector.predict(img)
|
| 62 |
|
| 63 |
all_rois = []
|
|
@@ -78,7 +102,7 @@ def process_image(img: np.ndarray, detector, recognizer, min_conf: float) -> Lis
|
|
| 78 |
roi = img[y1:y2, x1:x2]
|
| 79 |
if roi.size > 0:
|
| 80 |
all_rois.append(roi)
|
| 81 |
-
all_bboxes.append([
|
| 82 |
|
| 83 |
# 2️⃣ Recognize text
|
| 84 |
ocr_results = []
|
|
@@ -87,13 +111,14 @@ def process_image(img: np.ndarray, detector, recognizer, min_conf: float) -> Lis
|
|
| 87 |
try:
|
| 88 |
rec_gen = recognizer.predict(roi)
|
| 89 |
rec = next(rec_gen)
|
| 90 |
-
|
| 91 |
score = float(rec.get("rec_score", 0.0))
|
|
|
|
| 92 |
except:
|
| 93 |
text = ""
|
| 94 |
score = 0.0
|
| 95 |
|
| 96 |
-
if score >= min_conf and text
|
| 97 |
ocr_results.append({
|
| 98 |
"box_id": i + 1,
|
| 99 |
"text": text,
|
|
@@ -101,12 +126,11 @@ def process_image(img: np.ndarray, detector, recognizer, min_conf: float) -> Lis
|
|
| 101 |
"bbox": all_bboxes[i]
|
| 102 |
})
|
| 103 |
|
| 104 |
-
# ✅
|
| 105 |
-
# ترتيب عربي: من فوق لتحت ثم من اليمين لليسار
|
| 106 |
ocr_results.sort(
|
| 107 |
key=lambda x: (
|
| 108 |
-
x["bbox"][1], # Y
|
| 109 |
-
-x["bbox"][0] # X (
|
| 110 |
)
|
| 111 |
)
|
| 112 |
|
|
|
|
| 7 |
import os
|
| 8 |
import numpy as np
|
| 9 |
import cv2
|
| 10 |
+
import re
|
| 11 |
|
| 12 |
# PDF support
|
| 13 |
try:
|
|
|
|
| 36 |
print("Server started. OCR models will be loaded lazily on first request.")
|
| 37 |
|
| 38 |
|
| 39 |
+
# -------------------- تنظيف النص العربي --------------------
|
| 40 |
+
def clean_arabic_text(text: str) -> str:
|
| 41 |
+
if not text:
|
| 42 |
+
return ""
|
| 43 |
+
|
| 44 |
+
# إزالة أي شيء غير عربي أو أرقام
|
| 45 |
+
text = re.sub(r"[^\u0600-\u06FF0-9]", "", text)
|
| 46 |
+
|
| 47 |
+
# إزالة التشكيل
|
| 48 |
+
text = re.sub(r"[\u064B-\u065F]", "", text)
|
| 49 |
+
|
| 50 |
+
# إزالة أي مسافات
|
| 51 |
+
text = re.sub(r"\s+", "", text)
|
| 52 |
+
|
| 53 |
+
return text.strip()
|
| 54 |
+
|
| 55 |
+
|
| 56 |
def get_models():
|
| 57 |
global paddle_detector, paddle_recognizer
|
| 58 |
|
|
|
|
| 72 |
return paddle_detector, paddle_recognizer
|
| 73 |
|
| 74 |
|
| 75 |
+
def process_image(
|
| 76 |
+
img: np.ndarray,
|
| 77 |
+
detector,
|
| 78 |
+
recognizer,
|
| 79 |
+
min_conf: float
|
| 80 |
+
) -> List[Dict]:
|
| 81 |
+
|
| 82 |
h_img, w_img = img.shape[:2]
|
| 83 |
|
| 84 |
+
# 1️⃣ Detect text
|
| 85 |
results = detector.predict(img)
|
| 86 |
|
| 87 |
all_rois = []
|
|
|
|
| 102 |
roi = img[y1:y2, x1:x2]
|
| 103 |
if roi.size > 0:
|
| 104 |
all_rois.append(roi)
|
| 105 |
+
all_bboxes.append([x1, y1, x2, y2])
|
| 106 |
|
| 107 |
# 2️⃣ Recognize text
|
| 108 |
ocr_results = []
|
|
|
|
| 111 |
try:
|
| 112 |
rec_gen = recognizer.predict(roi)
|
| 113 |
rec = next(rec_gen)
|
| 114 |
+
raw_text = rec.get("rec_text", "")
|
| 115 |
score = float(rec.get("rec_score", 0.0))
|
| 116 |
+
text = clean_arabic_text(raw_text)
|
| 117 |
except:
|
| 118 |
text = ""
|
| 119 |
score = 0.0
|
| 120 |
|
| 121 |
+
if score >= min_conf and text:
|
| 122 |
ocr_results.append({
|
| 123 |
"box_id": i + 1,
|
| 124 |
"text": text,
|
|
|
|
| 126 |
"bbox": all_bboxes[i]
|
| 127 |
})
|
| 128 |
|
| 129 |
+
# ✅ ترتيب عربي: فوق → تحت ، يمين → شمال
|
|
|
|
| 130 |
ocr_results.sort(
|
| 131 |
key=lambda x: (
|
| 132 |
+
x["bbox"][1], # Y
|
| 133 |
+
-x["bbox"][0] # X (RTL)
|
| 134 |
)
|
| 135 |
)
|
| 136 |
|