File size: 1,953 Bytes
2b636b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# # 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()