-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
385 lines (348 loc) · 11.3 KB
/
main.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
import time
import os
import re
from urllib.request import urlopen
from bs4 import BeautifulSoup
from nltk.metrics.distance import edit_distance
import math
import sqlite3
import numpy as np
import matplotlib.pyplot as plt
import pytesseract
try:
from PIL import Image
except ImportError:
import Image
Builder.load_string('''
<StartScreen>:
GridLayout:
cols: 1
size: root.width, root.height
Button:
text: 'Camera'
font_size: 48
on_press: root.manager.current = 'camera'
background_normal: 'white.png'
background_down: 'gray.png'
background_color: 0, .6, .8, 1
Button:
text: 'Upload'
font_size: 48
on_press: root.manager.current = 'upload'
background_normal: 'white.png'
background_down: 'gray.png'
background_color: 0, .8, .4, 1
<CameraScreen>:
orientation: 'vertical'
Camera:
id: camera
resolution: (640, 480)
play: True
Button:
text: 'Capture Image'
size_hint_y: None
height: '48dp'
on_press: root.capture()
background_normal: 'white.png'
background_down: 'gray.png'
background_color: .094, .77, .71, 1
<UploadScreen>:
GridLayout:
cols: 1
FileChooserIconView:
id: filechooser
path: root.get_path()
canvas.before:
Color:
rgb: 0, .41, .55
Rectangle:
pos: self.pos
size: self.size
on_selection: root.select(*args)
<ResultScreen>:
GridLayout:
cols: 1
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
GridLayout:
size_hint_y: None
height: self.minimum_height
cols: 2
Button:
text: 'Ingredients'
background_normal: 'white.png'
background_down: 'gray.png'
background_color: 0, .6, .8, 1
size_hint_y: None
height: 100
on_press: root.manager.current = 'ingredients'
Button:
text: 'Start Over'
background_normal: 'white.png'
background_down: 'gray.png'
background_color: 0, .6, .8, 1
size_hint_y: None
height: 100
on_press: root.manager.current = 'start'
Label:
name: 'results'
text: "Results"
color: 0, 0, 0, 1
font_size: 48
size_hint_y: None
height: 100
background_color: .5, .5, .5, 1
Image:
id: results_im
allow_stretch: True
<IngredientsScreen>
ScrollView:
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
GridLayout:
size_hint_y: None
height: self.minimum_height
cols: 1
GridLayout:
size_hint_y: None
height: self.minimum_height
cols: 2
Button:
text: 'Results'
background_normal: 'white.png'
background_down: 'gray.png'
background_color: 0, .6, .8, 1
size_hint_y: None
height: 100
on_press: root.manager.current = 'result'
Button:
text: 'Start Over'
background_normal: 'white.png'
background_down: 'gray.png'
background_color: 0, .6, .8, 1
size_hint_y: None
height: 100
on_press: root.manager.current = 'start'
Label:
text: 'Ingredients'
font_size: 48
color: 0, 0, 0, 1
size_hint_y: None
height: 100
Label:
text: 'High Risk'
font_size: 36
color: 0, 0, 0, 1
size_hint: None, None
width: self.texture_size[0] + dp(20)
height: self.texture_size[1] + dp(10)
Label:
id: high
color: .8, .07, 0, 1
size_hint: None, None
width: self.texture_size[0] + dp(20)
height: self.texture_size[1] + dp(10)
Label:
text: 'Medium Risk'
font_size: 36
color: 0, 0, 0, 1
size_hint: None, None
width: self.texture_size[0] + dp(20)
height: self.texture_size[1] + dp(10)
Label:
id: med
color: .8, .47, .13, 1
size_hint: None, None
width: self.texture_size[0] + dp(20)
height: self.texture_size[1] + dp(10)
Label:
text: 'Low Risk'
font_size: 36
color: 0, 0, 0, 1
size_hint: None, None
width: self.texture_size[0] + dp(20)
height: self.texture_size[1] + dp(10)
Label:
id: low
color: 0, .55, .27, 1
size_hint: None, None
width: self.texture_size[0] + dp(20)
height: self.texture_size[1] + dp(10)
''')
def get_ingredients_list():
ingredients_list = {}
for i in range(1, 47):
url = "https://cosmily.com/ingredients?page="+str(i)
x = urlopen(url)
data = x.read()
soup = BeautifulSoup(data, 'html.parser')
outer = soup.find_all("tr")
for o in outer:
soup_inner = BeautifulSoup(str(o), 'html.parser')
link = soup_inner.find("a")
rating = soup_inner.find_all("td")
for r in rating:
try:
int(r.text)
rating = r
break
except ValueError:
pass
if link is not None and link.text != '' and rating.text != '':
print(link.text + ': ' + rating.text)
ingredients_list[link.text.upper()] = int(rating.text)
return ingredients_list
def spell_fix(word, db):
try:
if db[word]:
return word
except KeyError:
pass
smallest_chem = ''
smallest_ed = math.inf
for chem in db.keys():
curr_ed = edit_distance(word, chem)
if curr_ed < smallest_ed:
smallest_chem = chem
smallest_ed = curr_ed
return smallest_chem
def get_hazard_levels(chems, db):
hazard_levels = []
for c in chems:
hazard_levels.append(db[c])
return hazard_levels
def get_db_subset(key_subset, db):
db_subset = {}
for k in key_subset:
db_subset[k] = db[k]
return db_subset
def store_database(db, db_filename):
conn = sqlite3.connect(db_filename)
cur = conn.cursor()
cur.execute('CREATE TABLE chemicals (name VARCHAR, rating TINYINT, PRIMARY KEY (name))')
for chem in db.keys():
cur.execute('INSERT INTO chemicals (name, rating) VALUES (?, ?)', (chem, db[chem]))
conn.commit()
conn.commit()
conn.close()
def load_database(db_filename):
conn = sqlite3.connect(db_filename)
cur = conn.cursor()
cur.execute('SELECT * FROM chemicals')
data = cur.fetchall()
db = {}
for chem, rating in data:
db[chem] = rating
return db
def process_image(image_name, db):
print("RAW:")
ingredients_raw = pytesseract.image_to_string(image_name)
print(ingredients_raw)
ingredients = re.split(r"[,:]+", ingredients_raw)
ingredients = [spell_fix(elt.upper().replace('\n', ' ').strip(), db) for elt in ingredients
if len(elt) > 0 and elt.lower() != 'ingredients']
print("PROCESSED:")
print(ingredients)
return ingredients
def create_plot(ewg_list):
# read in ewg scores
ewg = np.array(ewg_list)
rating = np.chararray(len(ewg_list))
pie = np.zeros(3)
# convert ewg scores to low/medium/high
for i in range(0, len(ewg_list)):
if ewg[i] <= 2:
rating[i] = "low"
pie[0] += 1
elif ewg[i] <= 6:
rating[i] = "medium"
pie[1] += 1
else:
rating[i] = "high"
pie[2] += 1
# Data to plot
labels = 'Low Hazard', 'Medium Hazard', 'High Hazard'
colors = ['gold', 'lightcoral', 'lightskyblue']
# ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0, 0, 0) # explode 1st slice
# Plot
plt.pie(pie, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140)
plt.axis('equal')
from io import BytesIO
plt.savefig('results.png')
class StartScreen(Screen):
pass
class CameraScreen(Screen):
def capture(self):
camera = self.ids['camera']
timestr = time.strftime("%Y%m%d_%H%M%S")
filename = 'IMG_{}.png'.format(timestr)
camera.export_to_png(filename)
process_image(filename, DATABASE)
self.manager.current = 'result'
class UploadScreen(Screen):
def get_path(self):
return os.getcwd()
def select(self, *args):
self.manager.current = 'result'
try:
fc = self.ids['filechooser']
ingredients = process_image(args[1][0], DATABASE)
hazard_levels = get_hazard_levels(ingredients, DATABASE)
create_plot(hazard_levels)
self.manager.get_screen('result').load_result(get_db_subset(ingredients, DATABASE))
self.manager.get_screen('ingredients').load_ingredients(get_db_subset(ingredients, DATABASE))
path = fc.selection[0]
fc.selection.remove(path)
except IndexError:
pass
class ResultScreen(Screen):
def test(self):
print(self)
print(self.parent)
def load_result(self, db_subset):
results_im = self.ids['results_im']
results_im.source = 'results.png'
class IngredientsScreen(Screen):
def load_ingredients(self, db_subset):
high = ''
med = ''
low = ''
for chem in db_subset.keys():
ewg_level = db_subset[chem]
if ewg_level > 6:
high += chem + ': ' + str(ewg_level) + '\n'
elif ewg_level > 2:
med += chem + ': ' + str(ewg_level) + '\n'
else:
low += chem + ': ' + str(ewg_level) + '\n'
high_label = self.ids['high']
med_label = self.ids['med']
low_label = self.ids['low']
high_label.text = high
med_label.text = med
low_label.text = low
DATABASE = load_database('chemicals_db.sqlite')
sm = ScreenManager()
sm.add_widget(StartScreen(name='start'))
sm.add_widget(CameraScreen(name='camera'))
sm.add_widget(UploadScreen(name='upload'))
sm.add_widget(ResultScreen(name='result'))
sm.add_widget(IngredientsScreen(name='ingredients'))
class IngredientsCheckerApp(App):
def build(self):
return sm
if __name__ == "__main__":
IngredientsCheckerApp().run()