Kndeh commited on
Commit ·
67df7ec
1
Parent(s): 69e1b81
fix: perspective usage
Browse files- src/models/ai_engine.py +33 -23
src/models/ai_engine.py
CHANGED
|
@@ -481,18 +481,23 @@ def run_donut_ocr(img_array, processor, model, device, model_loaded,
|
|
| 481 |
rgb_for_donut = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB) if len(thresh.shape) == 2 else thresh
|
| 482 |
print(f"[Donut] Using preprocessed image, shape={rgb_for_donut.shape}")
|
| 483 |
else:
|
| 484 |
-
#
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
|
| 497 |
pil_img = Image.fromarray(rgb_for_donut).convert("RGB")
|
| 498 |
# Let the processor handle resizing to the model's expected input size
|
|
@@ -612,17 +617,22 @@ def run_donut_ocr(img_array, processor, model, device, model_loaded,
|
|
| 612 |
rgb_for_donut = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB) if len(thresh.shape) == 2 else thresh
|
| 613 |
print(f"[Donut] Using preprocessed image, shape={rgb_for_donut.shape}")
|
| 614 |
else:
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
|
| 627 |
pil_img = Image.fromarray(rgb_for_donut).convert("RGB")
|
| 628 |
pixel_values = processor(pil_img, return_tensors="pt").pixel_values.to(device)
|
|
|
|
| 481 |
rgb_for_donut = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB) if len(thresh.shape) == 2 else thresh
|
| 482 |
print(f"[Donut] Using preprocessed image, shape={rgb_for_donut.shape}")
|
| 483 |
else:
|
| 484 |
+
# Use RGB image, but apply perspective fix if requested
|
| 485 |
+
if filter_flags.get("enable_perspective", False):
|
| 486 |
+
try:
|
| 487 |
+
bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
| 488 |
+
cropped = perspective_correction(bgr)
|
| 489 |
+
if cropped is None:
|
| 490 |
+
cropped = auto_crop_bright_region(bgr)
|
| 491 |
+
if cropped is not None:
|
| 492 |
+
rgb_for_donut = cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB)
|
| 493 |
+
print(f"[Donut] Cropped {bgr.shape[:2]} -> {cropped.shape[:2]}")
|
| 494 |
+
else:
|
| 495 |
+
rgb_for_donut = img_array
|
| 496 |
+
except Exception as e:
|
| 497 |
+
rgb_for_donut = img_array
|
| 498 |
+
print(f"[Donut] crop failed: {e}")
|
| 499 |
+
else:
|
| 500 |
+
rgb_for_donut = img_array
|
| 501 |
|
| 502 |
pil_img = Image.fromarray(rgb_for_donut).convert("RGB")
|
| 503 |
# Let the processor handle resizing to the model's expected input size
|
|
|
|
| 617 |
rgb_for_donut = cv2.cvtColor(thresh, cv2.COLOR_GRAY2RGB) if len(thresh.shape) == 2 else thresh
|
| 618 |
print(f"[Donut] Using preprocessed image, shape={rgb_for_donut.shape}")
|
| 619 |
else:
|
| 620 |
+
if filter_flags.get("enable_perspective", False):
|
| 621 |
+
try:
|
| 622 |
+
bgr = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
| 623 |
+
cropped = perspective_correction(bgr)
|
| 624 |
+
if cropped is None:
|
| 625 |
+
cropped = auto_crop_bright_region(bgr)
|
| 626 |
+
if cropped is not None:
|
| 627 |
+
rgb_for_donut = cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB)
|
| 628 |
+
print(f"[Donut] Cropped {bgr.shape[:2]} -> {cropped.shape[:2]}")
|
| 629 |
+
else:
|
| 630 |
+
rgb_for_donut = img_array
|
| 631 |
+
except Exception as e:
|
| 632 |
+
rgb_for_donut = img_array
|
| 633 |
+
print(f"[Donut] crop failed: {e}")
|
| 634 |
+
else:
|
| 635 |
+
rgb_for_donut = img_array
|
| 636 |
|
| 637 |
pil_img = Image.fromarray(rgb_for_donut).convert("RGB")
|
| 638 |
pixel_values = processor(pil_img, return_tensors="pt").pixel_values.to(device)
|