-
Notifications
You must be signed in to change notification settings - Fork 2
/
vision_api.py
150 lines (115 loc) · 3.95 KB
/
vision_api.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
'''
Google Cloud Vision API processing.
'''
import os
from google.cloud import vision
class Knot:
"""
Lightweight, nonpublic
class for storing a singly linked node.
"""
__slots__ = "_element", "_next"
def __init__(self, element = None, next = None):
"""
Inititialization of an instance of the object.
Args:
element ([object], optional): [value to store in the 'Knot']. Defaults to None.
next ([object], optional): [object linked next to the one]. Defaults to None.
"""
self._element = element
self._next = next
@property
def element(self):
"""
element property for '_element' storage.
Returns:
[object]: [the element stored in the _element property].
"""
return self._element
@element.setter
def element(self, value):
"""
Setter function for element property.
Args:
value ([object]): [object to set as a value of the _element property].
"""
self._element = value
@property
def next(self):
"""
next property for '_next' storage.
Returns:
[object]: [the element stored in the _next property].
"""
return self._next
@next.setter
def next(self, value):
"""
Setter function for element property.
Args:
value ([object]): [object to set as a value of the _next property].
"""
self._next = value
class Cream:
def check_ingredients(self, photo, dataset):
"""
Check the ingredients.
Args:
photo ([type]): [description]
dataset ([type]): [description]
Returns:
[type]: [description]
"""
ingredients_words = self.find_ingredients(photo)
ingredients = Knot()
ingredients_list_head = ingredients
for ingredient in ingredients_words:
ingredients.element = dataset.retrieve(ingredient)
ingredients.next = Knot()
ingredients = ingredients.next
return ingredients_list_head
def find_ingredients(self, path):
"""
Finds ingredients on a photo.
Args:
path ([str]): [path to a photo].
Returns:
[list]: [list of words found on the photo].
"""
detected_text = self.detect_text(path)
detected_text_words = self.process_text(detected_text)
return detected_text_words
@staticmethod
def detect_text(photo):
"""
Detects and returns text on a photo.
Args:
photo ([str]): [photo in 64bit encoded string].
Raises:
Exception: [Exception]
"""
os.environ['GOOGLE_APPLICATION_CREDENTIALS']= r'simpletextrecognition.json'
client = vision.ImageAnnotatorClient()
image = vision.Image(content=photo)
result = client.text_detection(image=image)
detected_text = result.text_annotations
if result.error.message:
raise Exception(
'{}\nOoops! Some error occured! Sorry for the troubles.\
Try making a different image, if nothing changes please try again later.'.format(
detected_text.error.message))
return detected_text
@staticmethod
def process_text(detected_text):
"""
Processes texts found on a photo and returns words.
Args:
detected_text ([type]): [description]
Returns:
[list]: [list of words found on the photo].
"""
detected_text_strign = detected_text[0].description
detected_text_words = detected_text_strign.split(',')
detected_text_words = [word.strip() for word in detected_text_words]
detected_text_words = [el for word in detected_text_words for el in word.split('\n')]
return detected_text_words