-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision_utils.py
62 lines (46 loc) · 1.97 KB
/
vision_utils.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
import base64
import json
import requests
class VisionUtils:
def __init__(self):
self.endpoint_url = 'https://vision.googleapis.com/v1/images:annotate'
self.api_key = 'AIzaSyCSqfhtZXwEy8JxJRtUYm31YWLC1aACUMg'
def __make_request(self, img_path, feature_type):
request_list = []
# Read the image and convert to json
with open(img_path, 'rb') as img_file:
content_json_obj = {'content': base64.b64encode(img_file.read()).decode('UTF-8')}
feature_json_obj = [{'type': feature_type}]
request_list.append(
{'image': content_json_obj,
'features': feature_json_obj}
)
# Write the object to a file, as json
# output_filename = 'request.json'
# with open(output_filename, 'w') as output_json:
# json.dump({'requests': request_list}, output_json)
return json.dumps({'requests': request_list}).encode()
def __get_response(self, json_data, info_field):
response = requests.post(
url=self.endpoint_url,
data=json_data,
params={'key': self.api_key},
headers={'Content-Type': 'application/json'})
print(response)
ret_json = json.loads(response.text)
try:
return ret_json['responses'][0][info_field]
except:
return None
def detect_text(self, path, type):
if type == 'text':
ret_json = self.__get_response(self.__make_request(img_path=path, feature_type='TEXT_DETECTION'),
info_field='textAnnotations')
elif type == 'label':
ret_json = self.__get_response(self.__make_request(img_path=path, feature_type='LABEL_DETECTION'),
info_field='labelAnnotations')
return ret_json
if __name__ == '__main__':
vision = VisionUtils()
result = vision.detect_text('image.jpg', 'label')
print(result)