ysharma HF staff commited on
Commit
c4c201c
1 Parent(s): 624e0b5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -0
app.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #Installing required libraries
2
+ !pip install datasets transformers[sentencepiece]
3
+ !pip install accelerate
4
+ !apt install git-lfs
5
+
6
+ !sudo apt install tesseract-ocr
7
+ !pip install pytesseract
8
+ !pip install keras-ocr
9
+ !pip install gradio
10
+
11
+ #importing required libraries
12
+ import pytesseract
13
+ from PIL import Image, ImageFont, ImageDraw
14
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
15
+ import matplotlib.pyplot as plt
16
+ import keras_ocr
17
+ import cv2
18
+ import math
19
+ import numpy as np
20
+ import gradio as gr
21
+ import numpy as np
22
+
23
+ #Translated in your desired language
24
+ def choose_language(language):
25
+ #Support for Hindi, Spanish, French
26
+ #Support for Arabic, Turish, arabic
27
+ #Support for German
28
+ if language == 'hindi':
29
+ modelnm = "Helsinki-NLP/opus-mt-en-hi"
30
+ elif language == 'spanish':
31
+ modelnm = "Helsinki-NLP/opus-mt-en-es"
32
+ elif language == 'german':
33
+ modelnm = "Helsinki-NLP/opus-mt-en-de"
34
+ elif language == 'french':
35
+ modelnm = "Helsinki-NLP/opus-mt-en-fr"
36
+ elif language == 'turkish':
37
+ modelnm = "Helsinki-NLP/opus-mt-en-trk"
38
+ elif language == 'arabic':
39
+ modelnm = "Helsinki-NLP/opus-mt-en-ar"
40
+ else:
41
+ modelnm = "Helsinki-NLP/opus-mt-en-ga"
42
+
43
+
44
+ tokenizer = AutoTokenizer.from_pretrained(modelnm)
45
+ model = AutoModelForSeq2SeqLM.from_pretrained(modelnm)
46
+
47
+ return tokenizer, model
48
+
49
+ #Function to translate english text to desired language
50
+ def translator(text, lang):
51
+
52
+ if '\n' in text:
53
+ text_list = text.splitlines()
54
+ text = ' '.join(text_list)
55
+
56
+ #Huggingface transformers Magic
57
+ tokenizer, model = choose_language(lang)
58
+ input_ids = tokenizer.encode(text, return_tensors="pt", padding=True) #Tokenizer
59
+ outputs = model.generate(input_ids) #Model
60
+ #Translated Text
61
+ decoded_text = tokenizer.decode(outputs[0], skip_special_tokens=True) #Tokenizer
62
+ return decoded_text
63
+
64
+
65
+ #Getting cordinates
66
+ def midpoint(x1, y1, x2, y2):
67
+ x_mid = int((x1 + x2)/2)
68
+ y_mid = int((y1 + y2)/2)
69
+ return (x_mid, y_mid)
70
+
71
+ pipeline = keras_ocr.pipeline.Pipeline()
72
+
73
+ #Getting cordinates for text insie image
74
+ #This will help in filling up the space with colors
75
+ def img_text_cords(im): #, pipeline):
76
+ #read image
77
+ img = keras_ocr.tools.read(im)
78
+ #generate (word, box) tuples
79
+ prediction_groups = pipeline.recognize([img])
80
+ mask = np.zeros(img.shape[:2], dtype="uint8")
81
+ for box in prediction_groups[0]:
82
+ x0, y0 = box[1][0]
83
+ x1, y1 = box[1][1]
84
+ x2, y2 = box[1][2]
85
+ x3, y3 = box[1][3]
86
+
87
+ x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
88
+ x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)
89
+
90
+ thickness = int(math.sqrt( (x2 - x1)**2 + (y2 - y1)**2 ))
91
+
92
+ cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255,
93
+ thickness)
94
+ img = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)
95
+
96
+ return img
97
+
98
+ #Extracting text from image
99
+ def text_extract(im):
100
+ #Using pytesseract to read text
101
+ ocr_text = pytesseract.image_to_string(im)
102
+ return ocr_text
103
+
104
+ #Formatting the text to multi lines structure
105
+ #This is mainly for translated text to look and fit better on an image
106
+ def format_text(language,extracted_text):
107
+
108
+ translated_text = translator(extracted_text, language)
109
+
110
+ word_list,i = [],0
111
+ for word in translated_text.split():
112
+ if i%5 != 0:
113
+ word_list.append(' '+word)
114
+ else:
115
+ word_list.append('\n'+word)
116
+ i+=1
117
+
118
+ new_title_text = ''.join(word_list)
119
+ return new_title_text
120
+
121
+
122
+ def translate_image(im, language):
123
+ #Extract text, translate in your language and format it
124
+ extracted_text = text_extract(im)
125
+ #font select -- Getting Unicode Text
126
+ title_font = ImageFont.truetype('/content/gdrive/My Drive/sample_images/arial-unicode-ms.ttf',30)
127
+ #text to write on image #Example in hindi - Unicode text u"आप जीवन में मिलता हर मौका ले लो, क्योंकि कुछ चीजें केवल एक बार होती हैं. शुभ सुबह"
128
+ txt = format_text(language,extracted_text)
129
+
130
+ #Editing image
131
+ img_returned = img_text_cords(im)
132
+ img_rgb = cv2.cvtColor(img_returned, cv2.COLOR_BGR2RGB)
133
+ cv2.imwrite("text_free_image.jpg",img_rgb)
134
+ new_image = Image.open("text_free_image.jpg")
135
+
136
+ #Enable writing on image
137
+ image_editable = ImageDraw.Draw(new_image)
138
+ image_editable.multiline_text((10,10), txt,spacing=2, font=title_font, fill= (0, 0, 0)) #(237, 230, 211)) (0, 0, 0))
139
+ return new_image
140
+
141
+
142
+ title = "Translate English Text to Your Regional Language In Your Forwarded Images"
143
+ description = "This fun Gradio demo is for translating English quote in an image (usually whatsapp forwards :) ) to your local or preferred language. To use it, simply upload your image, select one of the language choices given (hindi, spanish, german, french, arabic, irish, and turkish) from radio buttons provided. You can alternately click one of the examples to load them and select the language choice along with it."
144
+ article = "<div style='text-align: center;'>Image Text Translate by <a href='https://twitter.com/yvrjsharma' target='_blank'>Yuvraj S</a> | <a href='https://github.com/yvrjsharma/HugginFace_Gradio' target='_blank'>Github Repo</a> | <center><img src='https://visitor-badge.glitch.me/badge?page_id=ysharma/TranslateQuotesInImageForwards' alt='visitor badge'></center></div>"
145
+ pipeline = keras_ocr.pipeline.Pipeline()
146
+ gr.Interface(
147
+ translate_image,
148
+ [gr.inputs.Image(type="filepath", label="Input"), gr.inputs.Radio(choices=['hindi','spanish','french','turkish','german','irish', 'arabic'], type="value", default='hindi', label='Choose A Language')],
149
+ gr.outputs.Image(type="pil", label="Output"), "text",
150
+ title=title,
151
+ description=description,
152
+ article=article,
153
+ #examples=[['bill.png','version 0.2'],['keanu.png','version 0.3'],['will.jpeg','version 0.2']],
154
+ enable_queue=True
155
+ ).launch(debug=True)