Spaces:
Sleeping
Sleeping
Commit
·
f3eb26f
1
Parent(s):
76e8a07
Minor fixes
Browse files- .gitignore +2 -0
- utils.py +12 -12
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
utils.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from doctr.models import detection_predictor, recognition_predictor
|
| 2 |
from doctr.io import DocumentFile
|
| 3 |
-
from surya.recognition import RecognitionPredictor
|
| 4 |
-
from surya.detection import DetectionPredictor
|
| 5 |
from PIL import Image
|
| 6 |
# from functools import lru_cache
|
| 7 |
from torchvision import models
|
|
@@ -246,17 +246,17 @@ def predict_ne(image_path, device="cpu"):
|
|
| 246 |
return le.inverse_transform([predicted.item()])[0]
|
| 247 |
|
| 248 |
doctr_detector = None
|
| 249 |
-
|
| 250 |
-
|
| 251 |
def initialize_detector():
|
| 252 |
-
global doctr_detector,
|
| 253 |
if doctr_detector is None:
|
| 254 |
doctr_detector = detection_predictor('db_mobilenet_v3_large', pretrained=True, assume_straight_pages=True, preserve_aspect_ratio=True)
|
| 255 |
-
if
|
| 256 |
-
|
| 257 |
-
if
|
| 258 |
-
|
| 259 |
-
return doctr_detector,
|
| 260 |
|
| 261 |
def get_cleaned_boxes(out, page):
|
| 262 |
h, w, _ = page.shape
|
|
@@ -324,7 +324,7 @@ def merge_boxes_same_line(boxes, y_thresh=5, x_thresh=60):
|
|
| 324 |
return np.array(merged)
|
| 325 |
|
| 326 |
def ocr_citizenship_utils(image_path: str) -> List[List[str]]:
|
| 327 |
-
doctr_detector,
|
| 328 |
page = cv2.imread(image_path)
|
| 329 |
page = cv2.convertScaleAbs(page, alpha=1.5, beta=0)
|
| 330 |
page = cv2.resize(page, (720,480))
|
|
@@ -347,7 +347,7 @@ def ocr_citizenship_utils(image_path: str) -> List[List[str]]:
|
|
| 347 |
|
| 348 |
# OCR PART
|
| 349 |
langs = ["en",'ne']
|
| 350 |
-
predictions =
|
| 351 |
text_combo = ''
|
| 352 |
for text_line in predictions[0].text_lines:
|
| 353 |
text_combo = text_combo + " " + text_line.text.strip()
|
|
|
|
| 1 |
from doctr.models import detection_predictor, recognition_predictor
|
| 2 |
from doctr.io import DocumentFile
|
| 3 |
+
from surya.recognition import RecognitionPredictor as SuryaRecognitionPredictor
|
| 4 |
+
from surya.detection import DetectionPredictor as SuryaDetectionPredictor
|
| 5 |
from PIL import Image
|
| 6 |
# from functools import lru_cache
|
| 7 |
from torchvision import models
|
|
|
|
| 246 |
return le.inverse_transform([predicted.item()])[0]
|
| 247 |
|
| 248 |
doctr_detector = None
|
| 249 |
+
surya_recognition_predictor = None
|
| 250 |
+
surya_detection_predictor = None
|
| 251 |
def initialize_detector():
|
| 252 |
+
global doctr_detector, surya_recognition_predictor, surya_detection_predictor
|
| 253 |
if doctr_detector is None:
|
| 254 |
doctr_detector = detection_predictor('db_mobilenet_v3_large', pretrained=True, assume_straight_pages=True, preserve_aspect_ratio=True)
|
| 255 |
+
if surya_recognition_predictor is None:
|
| 256 |
+
surya_recognition_predictor = SuryaRecognitionPredictor()
|
| 257 |
+
if surya_detection_predictor is None:
|
| 258 |
+
surya_detection_predictor = SuryaDetectionPredictor()
|
| 259 |
+
return doctr_detector, surya_recognition_predictor, surya_detection_predictor
|
| 260 |
|
| 261 |
def get_cleaned_boxes(out, page):
|
| 262 |
h, w, _ = page.shape
|
|
|
|
| 324 |
return np.array(merged)
|
| 325 |
|
| 326 |
def ocr_citizenship_utils(image_path: str) -> List[List[str]]:
|
| 327 |
+
doctr_detector, surya_recognition_predictor, surya_detection_predictor = initialize_detector()
|
| 328 |
page = cv2.imread(image_path)
|
| 329 |
page = cv2.convertScaleAbs(page, alpha=1.5, beta=0)
|
| 330 |
page = cv2.resize(page, (720,480))
|
|
|
|
| 347 |
|
| 348 |
# OCR PART
|
| 349 |
langs = ["en",'ne']
|
| 350 |
+
predictions = surya_recognition_predictor([pil_image], [langs])
|
| 351 |
text_combo = ''
|
| 352 |
for text_line in predictions[0].text_lines:
|
| 353 |
text_combo = text_combo + " " + text_line.text.strip()
|