Team Ai
Apppublic

ipvikas/ImageProcessing

sourceHugging Faceupdated 1y agoView on Hugging Face
3likes
OCR_Image_to_Text.py69 linesDownload Raw Back to root
1# # First uninstall existing packages to avoid conflicts2# # !pip uninstall -y httpx googletrans gradio easyocr3 4# !pip install httpx==0.24.15# !pip install googletrans-py==4.0.06# !pip install gradio==3.50.27# !pip install easyocr==1.7.18 9# !pip install numpy Pillow easyocr gradio googletrans==3.1.0a010 11import gradio as gr12import os13from googletrans import Translator14import easyocr15from PIL import Image16from PIL import ImageDraw17import numpy as np18 19def Get_OCR_demo(image):20    try:21        # Initialize EasyOCR reader22        reader = easyocr.Reader(['en'])23        translator = Translator()24 25        # Convert image to numpy array if it isn't already26        if isinstance(image, str):27            # If image is a file path28            image_array = np.array(Image.open(image))29        elif isinstance(image, np.ndarray):30            # If image is already a numpy array31            image_array = image32        else:33            raise ValueError("Unsupported image format")34 35        # Perform OCR36        text_list = reader.readtext(37            image_array,38            add_margin=0.55,39            width_ths=0.7,40            link_threshold=0.8,41            decoder='beamsearch',42            blocklist='=-',43            detail=044        )45 46        # Combine text results47        text_comb = ' '.join(text_list) if text_list else "No text detected"48        return text_comb49 50    except Exception as e:51        return f"Error processing image: {str(e)}"52 53# Create Gradio interface54title = "Upload an image and extract Text from it"55description = "OCR tool for text extraction"56examples=[["english.png"],["Parag_Letter_j.jpg"]]57 58get_OCR_demo = gr.Interface(59    fn=Get_OCR_demo,60    inputs=gr.Image(label="Upload Image"),  # Removed type="filepath"61    outputs=gr.Textbox(label="Extracted Text"),62    title=title,63    description=description,64    cache_examples=False,65    examples=examples66)67 68# if __name__ == "__main__":69#     get_OCR_demo.launch()