Spaces:
Runtime error
Runtime error
Create OCR_Image_to_Text.py
Browse files- OCR_Image_to_Text.py +69 -0
OCR_Image_to_Text.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# # First uninstall existing packages to avoid conflicts
|
| 2 |
+
# # !pip uninstall -y httpx googletrans gradio easyocr
|
| 3 |
+
|
| 4 |
+
# !pip install httpx==0.24.1
|
| 5 |
+
# !pip install googletrans-py==4.0.0
|
| 6 |
+
# !pip install gradio==3.50.2
|
| 7 |
+
# !pip install easyocr==1.7.1
|
| 8 |
+
|
| 9 |
+
# !pip install numpy Pillow easyocr gradio googletrans==3.1.0a0
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import os
|
| 13 |
+
from googletrans import Translator
|
| 14 |
+
import easyocr
|
| 15 |
+
from PIL import Image
|
| 16 |
+
from PIL import ImageDraw
|
| 17 |
+
import numpy as np
|
| 18 |
+
|
| 19 |
+
def Get_OCR_demo(image):
|
| 20 |
+
try:
|
| 21 |
+
# Initialize EasyOCR reader
|
| 22 |
+
reader = easyocr.Reader(['en'])
|
| 23 |
+
translator = Translator()
|
| 24 |
+
|
| 25 |
+
# Convert image to numpy array if it isn't already
|
| 26 |
+
if isinstance(image, str):
|
| 27 |
+
# If image is a file path
|
| 28 |
+
image_array = np.array(Image.open(image))
|
| 29 |
+
elif isinstance(image, np.ndarray):
|
| 30 |
+
# If image is already a numpy array
|
| 31 |
+
image_array = image
|
| 32 |
+
else:
|
| 33 |
+
raise ValueError("Unsupported image format")
|
| 34 |
+
|
| 35 |
+
# Perform OCR
|
| 36 |
+
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=0
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Combine text results
|
| 47 |
+
text_comb = ' '.join(text_list) if text_list else "No text detected"
|
| 48 |
+
return text_comb
|
| 49 |
+
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error processing image: {str(e)}"
|
| 52 |
+
|
| 53 |
+
# Create Gradio interface
|
| 54 |
+
title = "Upload an image and extract Text from it"
|
| 55 |
+
description = "OCR tool for text extraction"
|
| 56 |
+
examples=[["english.png"],["Parag_Letter_j.jpg"]]
|
| 57 |
+
|
| 58 |
+
get_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=examples
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# if __name__ == "__main__":
|
| 69 |
+
# get_OCR_demo.launch()
|