Amandeep01 commited on
Commit
ab53282
·
verified ·
1 Parent(s): c2639fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -48
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
- from transformers import MarianMTModel, MarianTokenizer
6
- import io
7
 
8
  # Load OCR reader
9
- reader = easyocr.Reader(['en'])
10
 
11
- # Translation model - English to Hindi
12
- model_name = "Helsinki-NLP/opus-mt-en-hi"
13
- tokenizer = MarianTokenizer.from_pretrained(model_name)
14
- model = MarianMTModel.from_pretrained(model_name)
15
 
16
- # Font path (this works on Hugging Face Spaces)
17
- FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
 
 
18
 
19
- # Translation function (cleaned and CPU safe)
20
- def translate_text(text):
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 image, "No text found."
32
 
33
- # Convert image to editable mode
34
- img_pil = image.convert("RGB")
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
- if text.strip():
42
- try:
43
- translated = translate_text(text)
44
- except:
45
- translated = "[Translation Error]"
46
 
47
- all_translations.append(translated)
 
 
48
 
49
- # Position text above the bounding box
50
- x, y = bbox[0]
51
- draw.text((x, y - font_size), translated, fill=(255, 0, 0), font=font)
52
 
53
- full_text = "\n".join(all_translations)
54
- return img_pil, full_text
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- # Gradio interface
57
  iface = gr.Interface(
58
- fn=process_image,
59
  inputs=[
60
  gr.Image(type="pil", label="Upload Image"),
61
- gr.Slider(20, 80, value=30, label="Overlay Font Size")
62
- ],
63
- outputs=[
64
- gr.Image(type="pil", label="Image with Translated Overlay"),
65
- gr.Textbox(label="Translated Text Output")
66
  ],
67
- title="Offline Image Translator",
68
- description="Upload an image with English text. It'll translate it to Hindi and overlay the result directly on the image. No Cloudinary used!"
 
69
  )
70
 
71
- if __name__ == "__main__":
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()