Spaces:
Runtime error
Runtime error
| # # First uninstall existing packages to avoid conflicts | |
| # # !pip uninstall -y httpx googletrans gradio easyocr | |
| # !pip install httpx==0.24.1 | |
| # !pip install googletrans-py==4.0.0 | |
| # !pip install gradio==3.50.2 | |
| # !pip install easyocr==1.7.1 | |
| # !pip install numpy Pillow easyocr gradio googletrans==3.1.0a0 | |
| import gradio as gr | |
| import os | |
| from googletrans import Translator | |
| import easyocr | |
| from PIL import Image | |
| from PIL import ImageDraw | |
| import numpy as np | |
| def Get_OCR_demo(image): | |
| try: | |
| # Initialize EasyOCR reader | |
| reader = easyocr.Reader(['en']) | |
| translator = Translator() | |
| # Convert image to numpy array if it isn't already | |
| if isinstance(image, str): | |
| # If image is a file path | |
| image_array = np.array(Image.open(image)) | |
| elif isinstance(image, np.ndarray): | |
| # If image is already a numpy array | |
| image_array = image | |
| else: | |
| raise ValueError("Unsupported image format") | |
| # Perform OCR | |
| text_list = reader.readtext( | |
| image_array, | |
| add_margin=0.55, | |
| width_ths=0.7, | |
| link_threshold=0.8, | |
| decoder='beamsearch', | |
| blocklist='=-', | |
| detail=0 | |
| ) | |
| # Combine text results | |
| text_comb = ' '.join(text_list) if text_list else "No text detected" | |
| return text_comb | |
| except Exception as e: | |
| return f"Error processing image: {str(e)}" | |
| # Create Gradio interface | |
| title = "Upload an image and extract Text from it" | |
| description = "OCR tool for text extraction" | |
| examples=[["english.png"],["Parag_Letter_j.jpg"]] | |
| get_OCR_demo = gr.Interface( | |
| fn=Get_OCR_demo, | |
| inputs=gr.Image(label="Upload Image"), # Removed type="filepath" | |
| outputs=gr.Textbox(label="Extracted Text"), | |
| title=title, | |
| description=description, | |
| cache_examples=False, | |
| examples=examples | |
| ) | |
| # if __name__ == "__main__": | |
| # get_OCR_demo.launch() |