Upload ocr_diagnostics.py with huggingface_hub
Browse files- ocr_diagnostics.py +66 -66
ocr_diagnostics.py
CHANGED
|
@@ -1,66 +1,66 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
import json
|
| 4 |
-
import cv2
|
| 5 |
-
import numpy as np
|
| 6 |
-
from PIL import Image
|
| 7 |
-
|
| 8 |
-
# Ensure paths are correct
|
| 9 |
-
PROJECT_ROOT = r"c:\Users\Varshith Dharmaj\Downloads\major\math_verification_mvp"
|
| 10 |
-
if PROJECT_ROOT not in sys.path:
|
| 11 |
-
sys.path.insert(0, PROJECT_ROOT)
|
| 12 |
-
|
| 13 |
-
local_ocr_path = os.path.join(PROJECT_ROOT, "services", "local_ocr")
|
| 14 |
-
if local_ocr_path not in sys.path:
|
| 15 |
-
sys.path.append(local_ocr_path)
|
| 16 |
-
|
| 17 |
-
from mvm2_ocr_engine import MVM2OCREngine
|
| 18 |
-
|
| 19 |
-
def run_diagnostics():
|
| 20 |
-
print("MVM2 OCR DIAGNOSTIC TOOL")
|
| 21 |
-
print("========================")
|
| 22 |
-
|
| 23 |
-
engine = MVM2OCREngine()
|
| 24 |
-
print(f"Engine Model Loaded: {engine.model_loaded}")
|
| 25 |
-
|
| 26 |
-
test_images = [
|
| 27 |
-
"test_math.png",
|
| 28 |
-
"services/dashboard/test_math.png" # Sometimes it's duplicated
|
| 29 |
-
]
|
| 30 |
-
|
| 31 |
-
# Create a synthetic complex math image if others don't exist
|
| 32 |
-
synth_path = "synth_math.png"
|
| 33 |
-
img = Image.new('RGB', (800, 200), color = 'white')
|
| 34 |
-
# Since I can't draw complex LaTeX easily here, I'll just check if existing ones work
|
| 35 |
-
# But I can generate an image with text via CV2
|
| 36 |
-
synth_img = np.ones((200, 800, 3), dtype=np.uint8) * 255
|
| 37 |
-
cv2.putText(synth_img, "f(x) = x^2 + 2x + 1", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0), 3)
|
| 38 |
-
cv2.imwrite(synth_path, synth_img)
|
| 39 |
-
test_images.append(synth_path)
|
| 40 |
-
|
| 41 |
-
from services.preprocessing_service.image_enhancing import ImageEnhancer
|
| 42 |
-
enhancer = ImageEnhancer(sigma=1.2)
|
| 43 |
-
|
| 44 |
-
for img_path in test_images:
|
| 45 |
-
abs_path = os.path.abspath(img_path)
|
| 46 |
-
if not os.path.exists(abs_path):
|
| 47 |
-
print(f"\n[SKIP] {img_path} not found.")
|
| 48 |
-
continue
|
| 49 |
-
|
| 50 |
-
print(f"\n[TESTING RAW] {img_path}")
|
| 51 |
-
result_raw = engine.process_image(abs_path)
|
| 52 |
-
print(f"Raw Result: {result_raw.get('latex_output')}")
|
| 53 |
-
|
| 54 |
-
print(f"\n[TESTING ENHANCED] {img_path}")
|
| 55 |
-
try:
|
| 56 |
-
enhanced_img, _ = enhancer.enhance(abs_path)
|
| 57 |
-
enhanced_tmp = f"enhanced_{os.path.basename(img_path)}"
|
| 58 |
-
cv2.imwrite(enhanced_tmp, enhanced_img)
|
| 59 |
-
result_enh = engine.process_image(enhanced_tmp)
|
| 60 |
-
print(f"Enhanced Result: {result_enh.get('latex_output')}")
|
| 61 |
-
os.remove(enhanced_tmp)
|
| 62 |
-
except Exception as e:
|
| 63 |
-
print(f"Enhancement failed: {e}")
|
| 64 |
-
|
| 65 |
-
if __name__ == "__main__":
|
| 66 |
-
run_diagnostics()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import json
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# Ensure paths are correct
|
| 9 |
+
PROJECT_ROOT = r"c:\Users\Varshith Dharmaj\Downloads\major\math_verification_mvp"
|
| 10 |
+
if PROJECT_ROOT not in sys.path:
|
| 11 |
+
sys.path.insert(0, PROJECT_ROOT)
|
| 12 |
+
|
| 13 |
+
local_ocr_path = os.path.join(PROJECT_ROOT, "services", "local_ocr")
|
| 14 |
+
if local_ocr_path not in sys.path:
|
| 15 |
+
sys.path.append(local_ocr_path)
|
| 16 |
+
|
| 17 |
+
from mvm2_ocr_engine import MVM2OCREngine
|
| 18 |
+
|
| 19 |
+
def run_diagnostics():
|
| 20 |
+
print("MVM2 OCR DIAGNOSTIC TOOL")
|
| 21 |
+
print("========================")
|
| 22 |
+
|
| 23 |
+
engine = MVM2OCREngine()
|
| 24 |
+
print(f"Engine Model Loaded: {engine.model_loaded}")
|
| 25 |
+
|
| 26 |
+
test_images = [
|
| 27 |
+
"test_math.png",
|
| 28 |
+
"services/dashboard/test_math.png" # Sometimes it's duplicated
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
# Create a synthetic complex math image if others don't exist
|
| 32 |
+
synth_path = "synth_math.png"
|
| 33 |
+
img = Image.new('RGB', (800, 200), color = 'white')
|
| 34 |
+
# Since I can't draw complex LaTeX easily here, I'll just check if existing ones work
|
| 35 |
+
# But I can generate an image with text via CV2
|
| 36 |
+
synth_img = np.ones((200, 800, 3), dtype=np.uint8) * 255
|
| 37 |
+
cv2.putText(synth_img, "f(x) = x^2 + 2x + 1", (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 0), 3)
|
| 38 |
+
cv2.imwrite(synth_path, synth_img)
|
| 39 |
+
test_images.append(synth_path)
|
| 40 |
+
|
| 41 |
+
from services.preprocessing_service.image_enhancing import ImageEnhancer
|
| 42 |
+
enhancer = ImageEnhancer(sigma=1.2)
|
| 43 |
+
|
| 44 |
+
for img_path in test_images:
|
| 45 |
+
abs_path = os.path.abspath(img_path)
|
| 46 |
+
if not os.path.exists(abs_path):
|
| 47 |
+
print(f"\n[SKIP] {img_path} not found.")
|
| 48 |
+
continue
|
| 49 |
+
|
| 50 |
+
print(f"\n[TESTING RAW] {img_path}")
|
| 51 |
+
result_raw = engine.process_image(abs_path)
|
| 52 |
+
print(f"Raw Result: {result_raw.get('latex_output')}")
|
| 53 |
+
|
| 54 |
+
print(f"\n[TESTING ENHANCED] {img_path}")
|
| 55 |
+
try:
|
| 56 |
+
enhanced_img, _ = enhancer.enhance(abs_path)
|
| 57 |
+
enhanced_tmp = f"enhanced_{os.path.basename(img_path)}"
|
| 58 |
+
cv2.imwrite(enhanced_tmp, enhanced_img)
|
| 59 |
+
result_enh = engine.process_image(enhanced_tmp)
|
| 60 |
+
print(f"Enhanced Result: {result_enh.get('latex_output')}")
|
| 61 |
+
os.remove(enhanced_tmp)
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"Enhancement failed: {e}")
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
run_diagnostics()
|