-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvision_test_1.py
166 lines (111 loc) · 4.43 KB
/
vision_test_1.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import io
import os
from google.cloud import vision
from google.cloud.vision import types
from google.cloud import translate
import sys
#print(sys.stdout.encoding)
def searchLabel(picture):
# Instantiates a client
translate_client = translate.Client()
# Instantiates a client
vision_client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
picture)
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# Performs label detection on the image file
response = vision_client.label_detection(image=image)
labels = response.label_annotations
print(len(labels))
print('Labels:')
for label in labels:
translation = translate_client.translate(label.description, target_language = 'es')
translatedText = translation["translatedText"]
print("\n" + translation["input"])
print("Translation: " + translatedText)
def searchLogos(picture):
# Instantiates a client
translate_client = translate.Client()
# Instantiates a client
vision_client = vision.ImageAnnotatorClient()
file_name = os.path.join(
os.path.dirname(__file__),
picture)
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = vision_client.logo_detection(image=image)
logos = response.logo_annotations
print('Logos:')
for logo in logos:
translation = translate_client.translate(logo.description, target_language = 'es')
translatedText = translation["translatedText"]
print("\n" + translation["input"])
print("Translation: " + translatedText)
def searchText(picture):
# Instantiates a client
translate_client = translate.Client()
# Instantiates a client
vision_client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
picture)
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
# Performs text detection on the image file
response = vision_client.text_detection(image=image)
texts = response.text_annotations
print('Labels:')
for text in texts:
translation = translate_client.translate(text.description, target_language = 'es')
translatedText = translation["translatedText"]
print("\n" + translation["input"])
print("Translation: " + translatedText)
def searchLandmark(picture):
vision_client = vision.ImageAnnotatorClient()
file_name = os.path.join(
os.path.dirname(__file__),
picture)
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = vision_client.landmark_detection(image=image)
landmarks = response.landmark_annotations
for landmark in landmarks:
print("The landmark is: " + landmark.description)
def searchFace(picture):
client = vision.ImageAnnotatorClient()
file_name = os.path.join(
os.path.dirname(__file__),
picture)
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.face_detection(image=image)
faces = response.face_annotations
# Names of likelihood from google.cloud.vision.enums
chances = ('UNKNOWN', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE',
'LIKELY', 'VERY_LIKELY')
print('Faces:')
for face in faces:
print('anger: {}'.format(chances[face.anger_likelihood]))
print('joy: {}'.format(chances[face.joy_likelihood]))
print('surprise: {}'.format(chances[face.surprise_likelihood]))
print('sorrow: {}'.format(chances[face.sorrow_likelihood]))
print('headwear: {}'.format(chances[face.headwear_likelihood]))
def final(picture):
# searchLabel(picture)
# searchLogos(picture)
# searchText(picture)
#searchLandmark(picture)
searchFace(picture)
#detect_face(picture)
final("man.jpg")