Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,16 +41,47 @@ LANG_CODE_MAP = {
|
|
| 41 |
}
|
| 42 |
|
| 43 |
def initialize_reader():
|
| 44 |
-
"""Initialize EasyOCR reader with
|
| 45 |
global reader
|
| 46 |
if reader is None:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
return reader
|
| 55 |
|
| 56 |
def calculate_distance(box1, box2):
|
|
@@ -397,22 +428,70 @@ def process_image_enhanced(image: Image.Image, target_language: str, progress=gr
|
|
| 397 |
|
| 398 |
progress(0.1, "π§ Initializing OCR engine...")
|
| 399 |
|
| 400 |
-
# Initialize OCR
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
|
| 405 |
progress(0.3, "π Extracting and grouping text regions...")
|
| 406 |
|
| 407 |
try:
|
| 408 |
-
# Convert PIL image to numpy array
|
| 409 |
-
img_array = np.array(image)
|
|
|
|
|
|
|
|
|
|
| 410 |
|
| 411 |
-
|
| 412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
if not results:
|
| 415 |
-
return image, "βΉοΈ No readable text found in the image.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
# Filter by confidence
|
| 418 |
filtered_results = [(bbox, text, conf) for bbox, text, conf in results
|
|
@@ -619,14 +698,32 @@ with gr.Blocks(css=custom_css, title="Enhanced Multilingual Signboard Translator
|
|
| 619 |
|
| 620 |
if __name__ == "__main__":
|
| 621 |
print("π§ Initializing Enhanced OCR Translator...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
try:
|
| 623 |
-
|
| 624 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
except Exception as e:
|
| 626 |
-
print(f"β οΈ
|
|
|
|
| 627 |
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
|
| 43 |
def initialize_reader():
|
| 44 |
+
"""Initialize EasyOCR reader with fallback options"""
|
| 45 |
global reader
|
| 46 |
if reader is None:
|
| 47 |
+
# Try different initialization strategies
|
| 48 |
+
init_strategies = [
|
| 49 |
+
(['en', 'hi'], "English and Hindi"),
|
| 50 |
+
(['en'], "English only"),
|
| 51 |
+
(['en', 'hi'], "English and Hindi with verbose"),
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
for i, (languages, description) in enumerate(init_strategies):
|
| 55 |
+
try:
|
| 56 |
+
print(f"Attempting OCR initialization: {description}")
|
| 57 |
+
verbose_setting = True if i == 2 else False
|
| 58 |
+
|
| 59 |
+
reader = easyocr.Reader(
|
| 60 |
+
languages,
|
| 61 |
+
gpu=False,
|
| 62 |
+
verbose=verbose_setting,
|
| 63 |
+
download_enabled=True,
|
| 64 |
+
detector=True,
|
| 65 |
+
recognizer=True
|
| 66 |
+
)
|
| 67 |
+
print(f"β
EasyOCR initialized successfully with {description}")
|
| 68 |
+
return reader
|
| 69 |
+
|
| 70 |
+
except ImportError as e:
|
| 71 |
+
print(f"β Import error: {e}")
|
| 72 |
+
continue
|
| 73 |
+
except Exception as e:
|
| 74 |
+
print(f"β Initialization attempt {i+1} failed: {e}")
|
| 75 |
+
if i < len(init_strategies) - 1:
|
| 76 |
+
print("Trying alternative approach...")
|
| 77 |
+
continue
|
| 78 |
+
else:
|
| 79 |
+
print("All initialization strategies failed")
|
| 80 |
+
|
| 81 |
+
# If all strategies fail, return None
|
| 82 |
+
reader = None
|
| 83 |
+
print("β Could not initialize EasyOCR with any strategy")
|
| 84 |
+
|
| 85 |
return reader
|
| 86 |
|
| 87 |
def calculate_distance(box1, box2):
|
|
|
|
| 428 |
|
| 429 |
progress(0.1, "π§ Initializing OCR engine...")
|
| 430 |
|
| 431 |
+
# Initialize OCR with better error handling
|
| 432 |
+
try:
|
| 433 |
+
ocr = initialize_reader()
|
| 434 |
+
if ocr is None:
|
| 435 |
+
return image, """β OCR initialization failed. This might be due to:
|
| 436 |
+
β’ Missing system dependencies
|
| 437 |
+
β’ Network issues downloading models
|
| 438 |
+
β’ Insufficient memory
|
| 439 |
+
|
| 440 |
+
Please try refreshing the page or contact support."""
|
| 441 |
+
|
| 442 |
+
# Test OCR with a simple operation
|
| 443 |
+
test_array = np.array(image.convert('RGB'))
|
| 444 |
+
if test_array.size == 0:
|
| 445 |
+
return image, "β Invalid image format. Please upload a valid image file."
|
| 446 |
+
|
| 447 |
+
except Exception as e:
|
| 448 |
+
error_details = str(e)
|
| 449 |
+
return image, f"""β OCR Setup Error: {error_details}
|
| 450 |
+
|
| 451 |
+
Possible solutions:
|
| 452 |
+
β’ Refresh the browser and try again
|
| 453 |
+
β’ Upload a different image format (JPG/PNG)
|
| 454 |
+
β’ Check if the image is not corrupted
|
| 455 |
+
|
| 456 |
+
Technical details: {type(e).__name__}"""
|
| 457 |
|
| 458 |
progress(0.3, "π Extracting and grouping text regions...")
|
| 459 |
|
| 460 |
try:
|
| 461 |
+
# Convert PIL image to numpy array with error handling
|
| 462 |
+
img_array = np.array(image.convert('RGB'))
|
| 463 |
+
|
| 464 |
+
if img_array is None or img_array.size == 0:
|
| 465 |
+
return image, "β Error processing image. Please try a different image."
|
| 466 |
|
| 467 |
+
print(f"Image shape: {img_array.shape}")
|
| 468 |
+
|
| 469 |
+
# Perform OCR with error handling and fallback options
|
| 470 |
+
try:
|
| 471 |
+
results = ocr.readtext(img_array, detail=1, paragraph=False, width_ths=0.7, height_ths=0.7)
|
| 472 |
+
except Exception as ocr_error:
|
| 473 |
+
print(f"Primary OCR failed: {ocr_error}")
|
| 474 |
+
# Fallback: try with different parameters
|
| 475 |
+
try:
|
| 476 |
+
results = ocr.readtext(img_array, detail=1)
|
| 477 |
+
except Exception as fallback_error:
|
| 478 |
+
print(f"Fallback OCR failed: {fallback_error}")
|
| 479 |
+
return image, f"""β OCR Processing Failed: {str(ocr_error)}
|
| 480 |
+
|
| 481 |
+
Troubleshooting:
|
| 482 |
+
β’ Image might be too complex or low quality
|
| 483 |
+
β’ Try uploading a clearer image
|
| 484 |
+
β’ Ensure text is clearly visible
|
| 485 |
+
|
| 486 |
+
Fallback error: {str(fallback_error)}"""
|
| 487 |
|
| 488 |
if not results:
|
| 489 |
+
return image, """βΉοΈ No readable text found in the image.
|
| 490 |
+
|
| 491 |
+
Tips for better results:
|
| 492 |
+
β’ Ensure text is clearly visible and well-lit
|
| 493 |
+
β’ Upload higher resolution images
|
| 494 |
+
β’ Make sure text is not too small or blurry"""
|
| 495 |
|
| 496 |
# Filter by confidence
|
| 497 |
filtered_results = [(bbox, text, conf) for bbox, text, conf in results
|
|
|
|
| 698 |
|
| 699 |
if __name__ == "__main__":
|
| 700 |
print("π§ Initializing Enhanced OCR Translator...")
|
| 701 |
+
print("System Information:")
|
| 702 |
+
print(f"Python version: {os.sys.version}")
|
| 703 |
+
print(f"NumPy version: {np.__version__}")
|
| 704 |
+
|
| 705 |
+
# Pre-initialize with detailed logging
|
| 706 |
try:
|
| 707 |
+
print("Starting OCR initialization...")
|
| 708 |
+
ocr_reader = initialize_reader()
|
| 709 |
+
if ocr_reader:
|
| 710 |
+
print("β
OCR System ready!")
|
| 711 |
+
else:
|
| 712 |
+
print("β οΈ OCR initialization failed - will retry when needed")
|
| 713 |
except Exception as e:
|
| 714 |
+
print(f"β οΈ Pre-initialization error: {e}")
|
| 715 |
+
print("OCR will be initialized on first use")
|
| 716 |
|
| 717 |
+
# Launch with better error handling
|
| 718 |
+
try:
|
| 719 |
+
demo.launch(
|
| 720 |
+
share=True,
|
| 721 |
+
show_error=True,
|
| 722 |
+
server_name="0.0.0.0",
|
| 723 |
+
server_port=7860,
|
| 724 |
+
enable_queue=True
|
| 725 |
+
)
|
| 726 |
+
except Exception as e:
|
| 727 |
+
print(f"Launch error: {e}")
|
| 728 |
+
# Fallback launch
|
| 729 |
+
demo.launch()
|