Amandeep01 commited on
Commit
e5433fd
·
verified ·
1 Parent(s): 2ddb217

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -34
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 reader with allowed lang_list only
8
  reader = easyocr.Reader(['en', 'hi', 'mr', 'ne'])
9
 
10
- # Font path — works on HuggingFace
11
- FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
12
 
13
- def process_image(image, target_lang):
14
- img_array = np.array(image)
 
 
 
 
15
 
16
- # Run OCR safely
17
- results = reader.readtext(img_array)
 
 
 
18
 
19
- if not results:
20
- return image, "No text detected"
 
 
21
 
22
- final_translations = []
23
- img_pil = image.convert("RGB")
24
- draw = ImageDraw.Draw(img_pil)
25
- font = ImageFont.truetype(FONT_PATH, size=20)
26
 
27
- for (bbox, text, prob) in results:
28
- if text.strip():
 
29
  try:
30
- translated = GoogleTranslator(source='auto', target=target_lang).translate(text)
31
  except:
32
- translated = "[Translation Error]"
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
- full_text = "\n".join(final_translations)
41
- return img_pil, full_text
 
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="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 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