-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision_and_translate.py
62 lines (45 loc) · 1.67 KB
/
vision_and_translate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#Reference and documentation: https://venkatarangan.com/blog/2019/09/python-and-google-cloud-vision-for-tamil-text/
#pip install google-cloud-vision
#pip install google-cloud-translate
#You need to create your own client_secrets.json from Google Cloud Platform. The file in this project is an empty one.
def detect_text(path):
"""
Detects text in an image using the Google Cloud Vision API.
Args:
path (str): The path to the image file.
"""
from google.cloud import vision
import os
import io
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "client_secrets.json"
client = vision.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision.types.Image(content=content)
response = client.text_detection(
image=image,
image_context={"language_hints": ["ta"]}, # Tamil
)
texts = response.text_annotations
return texts
# [END vision_text_detection]
#pip install google-cloud-translate
def Quick_Translate(SourceText):
from google.cloud import translate
# Instantiates a client
translate_client = translate.Client()
sourcelanguage = 'ta'
targetlanguage = 'en'
translation = translate_client.translate(
SourceText,
source_language=sourcelanguage,
target_language=targetlanguage)
return translation['translatedText']
# [END translate_quickstart]
path = 'input_tamil_page.jpg'
mytexts = detect_text (path)
mytext = mytexts[0].description
print (mytext)
mytranslated = Quick_Translate (mytext)
print (mytranslated)
#you can view the sample output in the output_vision_and_translate.txt file in the same folder.