Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,41 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import easyocr
|
| 3 |
-
from
|
| 4 |
-
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
| 7 |
|
| 8 |
-
#
|
| 9 |
reader = easyocr.Reader(['en', 'hi'])
|
|
|
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
img_np = np.array(image)
|
| 16 |
-
img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
font = ImageFont.truetype("arial.ttf", 22)
|
| 26 |
-
|
| 27 |
-
for (bbox, text, prob) in results:
|
| 28 |
-
# Translate text
|
| 29 |
-
try:
|
| 30 |
-
translated = GoogleTranslator(source='auto', target='hi').translate("Hello")
|
| 31 |
-
except:
|
| 32 |
-
translated = text # fallback
|
| 33 |
-
|
| 34 |
-
# Coordinates
|
| 35 |
-
top_left = tuple(map(int, bbox[0]))
|
| 36 |
-
bottom_right = tuple(map(int, bbox[2]))
|
| 37 |
-
|
| 38 |
-
# Draw rectangle over original text (optional blur it)
|
| 39 |
-
draw.rectangle([top_left, bottom_right], fill="white")
|
| 40 |
-
|
| 41 |
-
# Write translated text
|
| 42 |
-
draw.text(top_left, translated, fill="black", font=font)
|
| 43 |
-
|
| 44 |
-
return image
|
| 45 |
-
|
| 46 |
-
# Language options for dropdown (EasyOCR short codes → Googletrans short codes)
|
| 47 |
-
lang_map = {
|
| 48 |
-
"English": "en",
|
| 49 |
-
"Hindi": "hi",
|
| 50 |
-
"French": "fr",
|
| 51 |
-
"Spanish": "es",
|
| 52 |
-
"German": "de",
|
| 53 |
-
"Chinese": "zh-cn"
|
| 54 |
-
}
|
| 55 |
|
|
|
|
| 56 |
iface = gr.Interface(
|
| 57 |
-
fn=
|
| 58 |
-
inputs=
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
],
|
| 63 |
-
outputs=gr.Image(type="pil", label="Translated Image"),
|
| 64 |
-
title="Photo Translate",
|
| 65 |
-
description="Upload an image with text. It will detect the text, translate it, and regenerate the image with the translated text."
|
| 66 |
)
|
| 67 |
|
| 68 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import easyocr
|
| 3 |
+
from libretranslatepy import LibreTranslateAPI
|
| 4 |
+
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
| 7 |
|
| 8 |
+
# Initialize OCR reader and translator
|
| 9 |
reader = easyocr.Reader(['en', 'hi'])
|
| 10 |
+
lt = LibreTranslateAPI("https://libretranslate.de")
|
| 11 |
|
| 12 |
+
def process_image(img):
|
| 13 |
+
# Convert PIL image to NumPy array
|
| 14 |
+
img_array = np.array(img)
|
| 15 |
+
img_gray = cv2.cvtColor(img_array, cv2.COLOR_RGB2GRAY)
|
| 16 |
|
| 17 |
+
result = reader.readtext(img_gray)
|
| 18 |
|
| 19 |
+
extracted_text = ""
|
| 20 |
+
translated_text = ""
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
for detection in result:
|
| 23 |
+
text = detection[1]
|
| 24 |
+
extracted_text += text + " "
|
| 25 |
|
| 26 |
+
# Translate extracted text to Hindi
|
| 27 |
+
if extracted_text.strip():
|
| 28 |
+
translated_text = lt.translate(extracted_text, "en", "hi")
|
| 29 |
|
| 30 |
+
return extracted_text.strip(), translated_text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Gradio interface
|
| 33 |
iface = gr.Interface(
|
| 34 |
+
fn=process_image,
|
| 35 |
+
inputs=gr.Image(type="pil"),
|
| 36 |
+
outputs=["text", "text"],
|
| 37 |
+
title="Image Text Translator (English to Hindi)",
|
| 38 |
+
description="Upload an image with English text, and get Hindi translation using LibreTranslate (no API key needed)."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
iface.launch()
|