Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import easyocr
|
| 3 |
-
from deep_translator import GoogleTranslator
|
| 4 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Initialize
|
| 8 |
reader = easyocr.Reader(['en', 'hi', 'mr', 'ne'])
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
draw = ImageDraw.Draw(img_pil)
|
| 25 |
-
font = ImageFont.truetype(FONT_PATH, size=20)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
except:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
final_translations.append(translated)
|
| 35 |
-
|
| 36 |
-
# Draw translation at top-left of bounding box
|
| 37 |
-
top_left = bbox[0]
|
| 38 |
-
draw.text((top_left[0], top_left[1] - 20), translated, fill=(255, 0, 0), font=font)
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
# Gradio UI
|
| 44 |
iface = gr.Interface(
|
| 45 |
fn=process_image,
|
| 46 |
inputs=[
|
| 47 |
-
gr.Image(type="pil", label="Upload Image"),
|
| 48 |
-
gr.Textbox(label="
|
| 49 |
],
|
| 50 |
outputs=[
|
| 51 |
-
gr.Image(type="
|
| 52 |
-
gr.Textbox(label="Translated Text Output")
|
| 53 |
],
|
| 54 |
-
title="Image Translator
|
| 55 |
description="Extract & translate text from an image (OCR + translation overlay)"
|
| 56 |
)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import easyocr
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import urllib.parse
|
| 6 |
|
| 7 |
+
# Initialize OCR
|
| 8 |
reader = easyocr.Reader(['en', 'hi', 'mr', 'ne'])
|
| 9 |
|
| 10 |
+
# Initialize translation model
|
| 11 |
+
translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", src_lang="eng_Latn", tgt_lang="hin_Deva")
|
| 12 |
|
| 13 |
+
# Cloudinary Info
|
| 14 |
+
CLOUD_NAME = "deoux7285" # No need for image name, this will be generated dynamically
|
| 15 |
+
FONT = "Arial"
|
| 16 |
+
SIZE = "40"
|
| 17 |
+
COLOR = "black"
|
| 18 |
+
POSITION = "south"
|
| 19 |
|
| 20 |
+
# Function to generate Cloudinary overlay URL
|
| 21 |
+
def generate_cloudinary_url(translated_text):
|
| 22 |
+
encoded = urllib.parse.quote(translated_text) # Ensure text is URL encoded
|
| 23 |
+
url = f"https://res.cloudinary.com/{deoux7285}/image/upload/l_text:{FONT}_{SIZE}:{encoded},co_{COLOR},g_{POSITION}/sample.jpg" # Image URL dynamically handled
|
| 24 |
+
return url
|
| 25 |
|
| 26 |
+
# Function to process image: OCR + Translation + Overlay
|
| 27 |
+
def process_image(image, target_lang):
|
| 28 |
+
img_array = np.array(image) # Convert PIL image to numpy array for OCR
|
| 29 |
+
results = reader.readtext(img_array) # Run OCR on the image
|
| 30 |
|
| 31 |
+
if not results:
|
| 32 |
+
return None, "No text detected"
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
full_text = ""
|
| 35 |
+
for _, text, _ in results:
|
| 36 |
+
if text.strip(): # Process non-empty text
|
| 37 |
try:
|
| 38 |
+
translation = translator(text, max_length=200)[0]["translation_text"]
|
| 39 |
except:
|
| 40 |
+
translation = "[Translation Error]"
|
| 41 |
+
full_text += translation + " "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
# Generate Cloudinary URL for translated text overlay
|
| 44 |
+
cloud_url = generate_cloudinary_url(full_text.strip())
|
| 45 |
+
return cloud_url, full_text.strip()
|
| 46 |
|
| 47 |
+
# Gradio UI setup
|
| 48 |
iface = gr.Interface(
|
| 49 |
fn=process_image,
|
| 50 |
inputs=[
|
| 51 |
+
gr.Image(type="pil", label="Upload Image"), # Upload image input
|
| 52 |
+
gr.Textbox(label="Target Language (e.g., hin_Deva, mar_Deva, nep_Deva)") # Textbox for target language
|
| 53 |
],
|
| 54 |
outputs=[
|
| 55 |
+
gr.Image(type="url", label="Image with Cloudinary Text"), # Image with overlay from Cloudinary
|
| 56 |
+
gr.Textbox(label="Translated Text Output") # Output of translated text
|
| 57 |
],
|
| 58 |
+
title="Advanced Image Translator",
|
| 59 |
description="Extract & translate text from an image (OCR + translation overlay)"
|
| 60 |
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
+
iface.launch() # Launch Gradio interface
|