Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,78 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
from deep_translator import GoogleTranslator
|
| 4 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
for (bbox, text, prob) in results:
|
| 28 |
-
if text.strip():
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
except:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
inputs=[
|
| 47 |
-
gr.Image(type="pil", label="Upload Image"),
|
| 48 |
-
gr.Textbox(label="Translate To (hi / en / mr / ne)")
|
| 49 |
-
],
|
| 50 |
-
outputs=[
|
| 51 |
-
gr.Image(type="pil", label="Image with Translated Overlay"),
|
| 52 |
-
gr.Textbox(label="Translated Text Output")
|
| 53 |
-
],
|
| 54 |
-
title="Image Translator with Overlay",
|
| 55 |
-
description="Extract & translate text from an image (OCR + translation overlay)"
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
if __name__ == "__main__":
|
| 59 |
-
iface.launch()
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
from PIL import Image
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import pytesseract
|
| 5 |
+
from googletrans import Translator
|
| 6 |
|
| 7 |
+
def overlay_translation(image_path, src_lang='hi', dest_lang='en'):
|
| 8 |
+
# Load image
|
| 9 |
+
image = cv2.imread(image_path)
|
| 10 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 11 |
|
| 12 |
+
# OCR data with boxes
|
| 13 |
+
data = pytesseract.image_to_data(image_rgb, lang=src_lang, output_type=pytesseract.Output.DICT)
|
| 14 |
|
| 15 |
+
translator = Translator()
|
| 16 |
+
translated_image = image_rgb.copy()
|
| 17 |
|
| 18 |
+
for i in range(len(data['text'])):
|
| 19 |
+
text = data['text'][i].strip()
|
| 20 |
+
if text != "":
|
| 21 |
+
x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
|
| 22 |
|
| 23 |
+
try:
|
| 24 |
+
translated_text = translator.translate(text, src=src_lang, dest=dest_lang).text
|
| 25 |
+
except:
|
| 26 |
+
translated_text = text # fallback
|
| 27 |
+
|
| 28 |
+
# White rectangle behind text for better visibility
|
| 29 |
+
cv2.rectangle(translated_image, (x, y), (x+w, y+h), (255,255,255), -1)
|
| 30 |
+
|
| 31 |
+
# Add translated text (in black, centered)
|
| 32 |
+
font_scale = h / 35
|
| 33 |
+
font_thickness = 2
|
| 34 |
+
cv2.putText(translated_image, translated_text, (x, y + h - 5),
|
| 35 |
+
cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0,0,0), font_thickness, cv2.LINE_AA)
|
| 36 |
+
|
| 37 |
+
# Save or return the translated image
|
| 38 |
+
translated_image_pil = Image.fromarray(translated_image)
|
| 39 |
+
return translated_image_pil
|
| 40 |
+
import cv2
|
| 41 |
+
from PIL import Image
|
| 42 |
+
import numpy as np
|
| 43 |
+
import pytesseract
|
| 44 |
+
from googletrans import Translator
|
| 45 |
|
| 46 |
+
def overlay_translation(image_path, src_lang='hi', dest_lang='en'):
|
| 47 |
+
# Load image
|
| 48 |
+
image = cv2.imread(image_path)
|
| 49 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 50 |
+
|
| 51 |
+
# OCR data with boxes
|
| 52 |
+
data = pytesseract.image_to_data(image_rgb, lang=src_lang, output_type=pytesseract.Output.DICT)
|
| 53 |
+
|
| 54 |
+
translator = Translator()
|
| 55 |
+
translated_image = image_rgb.copy()
|
| 56 |
+
|
| 57 |
+
for i in range(len(data['text'])):
|
| 58 |
+
text = data['text'][i].strip()
|
| 59 |
+
if text != "":
|
| 60 |
+
x, y, w, h = data['left'][i], data['top'][i], data['width'][i], data['height'][i]
|
| 61 |
|
|
|
|
|
|
|
| 62 |
try:
|
| 63 |
+
translated_text = translator.translate(text, src=src_lang, dest=dest_lang).text
|
| 64 |
except:
|
| 65 |
+
translated_text = text # fallback
|
| 66 |
+
|
| 67 |
+
# White rectangle behind text for better visibility
|
| 68 |
+
cv2.rectangle(translated_image, (x, y), (x+w, y+h), (255,255,255), -1)
|
| 69 |
+
|
| 70 |
+
# Add translated text (in black, centered)
|
| 71 |
+
font_scale = h / 35
|
| 72 |
+
font_thickness = 2
|
| 73 |
+
cv2.putText(translated_image, translated_text, (x, y + h - 5),
|
| 74 |
+
cv2.FONT_HERSHEY_SIMPLEX, font_scale, (0,0,0), font_thickness, cv2.LINE_AA)
|
| 75 |
+
|
| 76 |
+
# Save or return the translated image
|
| 77 |
+
translated_image_pil = Image.fromarray(translated_image)
|
| 78 |
+
return translated_image_pil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|