-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
138 lines (121 loc) · 6.69 KB
/
app.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
from flask import Flask, render_template, session, redirect, url_for, flash, request
from werkzeug.utils import secure_filename
import os
from clarifai_grpc.grpc.api import service_pb2, resources_pb2
from clarifai_grpc.grpc.api.status import status_code_pb2
from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import service_pb2_grpc
stub = service_pb2_grpc.V2Stub(ClarifaiChannel.get_grpc_channel())
metadata = (('authorization', 'Key e42f62c3447d4c87b110cf39f27eca9f'),)
app = Flask(__name__)
upload_folder = "static/uploads"
allowed_extentions = set(['png', 'jpeg', 'jpg'])
app.config['UPLOAD_FOLDER'] = upload_folder
app.config['SECRET_KEY'] = 'fjhasldkfjhasdflkhasdflkjh'
recyclable_objects = ["bottle", "cardboard", "glass", "plastic bottle", "plastic container", "cereal box",
"snack box", "phonebook", "magazine", "mail", "paper", "newspaper", "tin cans",
"aluminum can", "steel can", "food container", "jar", "soft drink bottle", "beer bottle",
"wine bottle", "liquor bottle", "carton", "aersol", "aersol can", "aluminum", "aluminum foil",
"aluminum tray", "stryrofoam", "stryrofoam packaging", "stryrofoam food container",
"stryrofoam drink container", "paper box", "pizza box", "paper bag", "shredded paper",
"plastic bucket", "plastic tubs", "plastic pot", "plastic tray", "plastic toy",
"plastic food container", "plastic cup", "metal can", "aluminum can", "wrapping paper",
"mail", "newspaper", "book"]
tech_objects = ["computer", "phone", "watch", "laptop", "eletronics", "tablet", 'iPad', 'iPhone', 'apple watch',
'pc', 'personal computer', 'mac', 'ram', 'ram card', 'processor', 'mother board', 'screen',
'display', 'monitor', 'calculator', 'speaker', 'kindle', 'key board', 'mouse', 'mice']
not_recycleable = ['plate', 'towel', 'napkin', 'metal bottle', 'plastic rap', 'food',
'ceramic', 'packing peanut', 'light bulb', 'photograph', 'wood', 'egg carton', 'metal water bottle',
'plastic wrap', 'egg carton', 'bulb']
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in allowed_extentions
def is_recyclable(detected_objects):
for o in detected_objects:
for r in recyclable_objects:
if r in o:
return True
return False
def tech_is_recyclable(detected_objects):
for o in detected_objects:
for r in tech_objects:
if r in o:
return True
return False
def is_not_recyclable(detected_objects):
for o in detected_objects:
for r in not_recycleable:
if r in o:
correct_results = o
return True
return False
@app.route('/', methods=['GET', 'POST'])
def index():
completed = False
if request.method == 'POST':
try:
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('Please select a file.')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
position = upload_folder + "/" + filename
with open(position, "rb") as file:
file_bytes = file.read()
api_request = service_pb2.PostModelOutputsRequest(
model_id='aaa03c23b3724a16a56b629203edc62c',
inputs=[
resources_pb2.Input(data=resources_pb2.Data(image=resources_pb2.Image(base64=file_bytes)))
])
response = stub.PostModelOutputs(api_request, metadata=metadata)
if response.status.code != status_code_pb2.SUCCESS:
flash("API request failed")
correct_results = []
for concept in response.outputs[0].data.concepts:
value = round(concept.value * 100)
if value > 85:
correct_results.append(concept.name)
percent_sure = "We are " + str((response.outputs[0].data.concepts[0].value)*100) + "% sure that your object is recyclable!"
os.remove(position)
if is_recyclable(correct_results):
recyclable = "Please recycle your object."
flash("Success! Press \"See results\" to see our analysis of your item.")
return render_template("index.html", recyclable=recyclable, percent_sure=percent_sure,
completed=True, isRecyclable=True)
elif is_not_recyclable(correct_results):
recyclable = "Contrary to popular belief, your object is not recyclable."
flash("Success! Press \"See results\" to see our analysis of your item.")
return render_template("index.html", recyclable=recyclable, percent_sure=percent_sure,
completed=True, isRecyclable=False)
elif tech_is_recyclable(correct_results):
recyclable = "Your object can be recycled at any technology recycling place, or your nearest Apple Store for free."
flash("Success! Press \"See results\" to see our analysis of your item.")
return render_template("index.html", recyclable=recyclable, percent_sure=percent_sure,
completed=True, isRecyclable=False)
else:
flash("Success! Press \"See results\" to see our analysis of your item.")
recyclable = "Your object is not recyclable."
percent_sure = ""
return render_template("index.html", percent_sure=percent_sure,
recyclable=recyclable, completed=True, isRecyclable=False)
else:
flash("File type not supported. Please upload a png, jpg or a jpeg.")
except:
flash("Please try a different file.")
return render_template("index.html", recyclable="", percent_sure="", completed=False, isRecyclable=False)
@app.route('/our-team')
def our_team():
return render_template("our-team.html")
@app.route('/about-recycling')
def about_recycling():
return render_template("about-recycling.html")
@app.route('/resources')
def resources():
return render_template("resources.html")
if __name__ == "__main__":
app.run(debug = True)