Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,69 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import easyocr
|
|
|
|
| 3 |
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
import numpy as np
|
| 5 |
-
|
| 6 |
-
import io
|
| 7 |
|
| 8 |
# Load OCR reader
|
| 9 |
-
reader = easyocr.Reader(['en'])
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 14 |
-
model = MarianMTModel.from_pretrained(model_name)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
inputs = tokenizer(text, return_tensors="pt")
|
| 22 |
-
translated = model.generate(**inputs)
|
| 23 |
-
return tokenizer.decode(translated[0], skip_special_tokens=True)
|
| 24 |
|
| 25 |
-
# Main function
|
| 26 |
-
def process_image(image, font_size):
|
| 27 |
-
np_image = np.array(image)
|
| 28 |
-
results = reader.readtext(np_image)
|
| 29 |
-
|
| 30 |
if not results:
|
| 31 |
-
return
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
draw = ImageDraw.Draw(img_pil)
|
| 36 |
-
font = ImageFont.truetype(FONT_PATH, size=font_size)
|
| 37 |
-
|
| 38 |
-
all_translations = []
|
| 39 |
|
| 40 |
for (bbox, text, prob) in results:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
draw.text((x, y - font_size), translated, fill=(255, 0, 0), font=font)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# Gradio interface
|
| 57 |
iface = gr.Interface(
|
| 58 |
-
fn=
|
| 59 |
inputs=[
|
| 60 |
gr.Image(type="pil", label="Upload Image"),
|
| 61 |
-
gr.
|
| 62 |
-
|
| 63 |
-
outputs=[
|
| 64 |
-
gr.Image(type="pil", label="Image with Translated Overlay"),
|
| 65 |
-
gr.Textbox(label="Translated Text Output")
|
| 66 |
],
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
iface.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import easyocr
|
| 3 |
+
from googletrans import Translator
|
| 4 |
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
import numpy as np
|
| 6 |
+
import cv2
|
|
|
|
| 7 |
|
| 8 |
# Load OCR reader
|
| 9 |
+
reader = easyocr.Reader(['en', 'hi'])
|
| 10 |
|
| 11 |
+
# Translator
|
| 12 |
+
translator = Translator()
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def translate_image(image, src_lang, tgt_lang):
|
| 15 |
+
# Convert PIL Image to OpenCV format
|
| 16 |
+
img_np = np.array(image)
|
| 17 |
+
img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
| 18 |
|
| 19 |
+
# OCR
|
| 20 |
+
results = reader.readtext(img_cv)
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if not results:
|
| 23 |
+
return "No text detected.", image
|
| 24 |
|
| 25 |
+
draw = ImageDraw.Draw(image)
|
| 26 |
+
font = ImageFont.truetype("arial.ttf", 22)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
for (bbox, text, prob) in results:
|
| 29 |
+
# Translate text
|
| 30 |
+
try:
|
| 31 |
+
translated = translator.translate(text, src=src_lang, dest=tgt_lang).text
|
| 32 |
+
except:
|
| 33 |
+
translated = text # fallback
|
| 34 |
|
| 35 |
+
# Coordinates
|
| 36 |
+
top_left = tuple(map(int, bbox[0]))
|
| 37 |
+
bottom_right = tuple(map(int, bbox[2]))
|
| 38 |
|
| 39 |
+
# Draw rectangle over original text (optional blur it)
|
| 40 |
+
draw.rectangle([top_left, bottom_right], fill="white")
|
|
|
|
| 41 |
|
| 42 |
+
# Write translated text
|
| 43 |
+
draw.text(top_left, translated, fill="black", font=font)
|
| 44 |
+
|
| 45 |
+
return image
|
| 46 |
+
|
| 47 |
+
# Language options for dropdown (EasyOCR short codes → Googletrans short codes)
|
| 48 |
+
lang_map = {
|
| 49 |
+
"English": "en",
|
| 50 |
+
"Hindi": "hi",
|
| 51 |
+
"French": "fr",
|
| 52 |
+
"Spanish": "es",
|
| 53 |
+
"German": "de",
|
| 54 |
+
"Chinese": "zh-cn"
|
| 55 |
+
}
|
| 56 |
|
|
|
|
| 57 |
iface = gr.Interface(
|
| 58 |
+
fn=translate_image,
|
| 59 |
inputs=[
|
| 60 |
gr.Image(type="pil", label="Upload Image"),
|
| 61 |
+
gr.Dropdown(list(lang_map.keys()), label="From Language", value="Hindi"),
|
| 62 |
+
gr.Dropdown(list(lang_map.keys()), label="To Language", value="English"),
|
|
|
|
|
|
|
|
|
|
| 63 |
],
|
| 64 |
+
outputs=gr.Image(type="pil", label="Translated Image"),
|
| 65 |
+
title="Photo Translate",
|
| 66 |
+
description="Upload an image with text. It will detect the text, translate it, and regenerate the image with the translated text."
|
| 67 |
)
|
| 68 |
|
| 69 |
+
iface.launch()
|
|
|