-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
248 lines (198 loc) · 6.54 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
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
from flask import Flask, redirect, url_for, render_template, request, Response
from lib_prediction.camera import Camera
from flask_socketio import SocketIO
# Camera IDs
ID_CAMERA_THUMB = 0
ID_CAMERA_FRUIT = 1
# Number of Frames for Prediction
NB_FRAMES_THUMB_PREDICTION = 30
NB_FRAMES_FRUIT_PREDICTION = 40
app = Flask(__name__)
socketio = SocketIO(app, async_mode=None)
camera_thumb = Camera(socketio, video_source=ID_CAMERA_THUMB,
nb_predicted_images=NB_FRAMES_THUMB_PREDICTION)
camera_fruit = Camera(socketio, video_source=ID_CAMERA_FRUIT,
nb_predicted_images=NB_FRAMES_FRUIT_PREDICTION)
fruit_prediction = None
def gen_thumb(camera):
"""
Function defined to create a generator for the thumb detection.
This generator get a frame( image) from the camera and provide
a specific structure (in the yield command) that can be send to
the web page
Args:
-----
- camera : instance of the class Camera
Returns:
--------
- specific structure return by the yield function including the frame
"""
while True:
frame = camera.get_frame_thumb()
yield (b'--frame\r\n'
b'Content-Type: image/png\r\n\r\n' + frame + b'\r\n')
def gen_fruit(camera):
"""
Function defined to create a generator for the fruit detection.
This generator get a frame (image) from the camera and provide
a specific structure (in the yield command) that can be send to
the web page
Args:
-----
- camera : instance of the class Camera
Returns:
--------
- None
"""
while True:
frame = camera.get_frame_fruit()
yield (b'--frame\r\n'
b'Content-Type: image/png\r\n\r\n' + frame + b'\r\n')
@app.route('/', methods=["POST", "GET"])
def index():
"""
This function treats the behavior of the index.html page
Args:
-----
- None
Returns:
--------
- GET: return the template index.html
- POST: redicrect to the route /fruit_video
"""
global fruit_prediction
if request.method == "POST":
return redirect(url_for("fruit_video"))
else:
fruit_prediction = None
return render_template("index.html")
@app.route('/thumb_video/<string:predict>')
def thumb_video(predict):
"""
This function treat the behavior of the thumb_video page.
When the page is rendered, the camera is started to realize
the prediction the code <img src="{{ url_for('thumb_video_feed') }}">
in the thumb_video.html file "call" the route /thumb_video_feed in
order to get an image
Args:
-----
- predict: name of the predicted fruit in a string
Returns:
--------
- GET: return the template thumb_video.html. Display which fruit has been
detected and start the thumb detection
"""
global fruit_prediction
fruit_prediction = predict
camera_thumb.run()
return render_template('thumb_video.html', predict=fruit_prediction)
@app.route("/thumb_video_feed")
def thumb_video_feed():
"""
This function delivers thumb images to the thumb_video.html
web page using an understandable structure
Returns:
--------
- a "Response" that can be displayed as an image by the web page.
"""
return Response(gen_thumb(camera_thumb),
mimetype="multipart/x-mixed-replace; boundary=frame")
@app.route("/fruit_video_feed")
def fruit_video_feed():
"""
This function delivers thumb images to the fruit_video.html
web page using an understandable structure
Returns:
--------
- a "Response" that can be displayed as an image by the web page.
Using the generator to get images from the camera
"""
return Response(gen_fruit(camera_fruit),
mimetype="multipart/x-mixed-replace; boundary=frame")
@app.route('/fruit_video/')
def fruit_video():
"""
This function treats the behavior of the fruit_video page when the page
is rendered, the camera is started to realize the prediction the code
<img src="{{ url_for('fruit_video_feed') }}"> in the fruit_video.html file
"call" the route /thumb_video_feed in order to get an image.
Returns:
--------
- GET: return the template fruit_video.html .
"""
camera_fruit.run()
return render_template('fruit_video.html')
@app.route('/fruit_manual_choice/', methods=["POST", "GET"])
def fruit_manual_choice():
"""
This function treats the behavior of the fruit_manual_choice page
Returns:
--------
- GET: return the template fruit_manual_choice.html
- POST: Treat the answer to the manual validation of the fruit.
go back to index page if the answer is no print the ticket if the
answer is yes
"""
global fruit_prediction
if request.method == 'POST':
if request.form['yesno'] == 'Yes':
return redirect(url_for("ticket_printing"))
elif request.form['yesno'] == 'No':
return redirect(url_for("index"))
else:
pass
else:
return render_template('fruit_manual_choice.html',
predict=fruit_prediction)
@app.route('/ticket_printing/')
def ticket_printing():
"""
This function treat the behavior of the ticket_print page
It gets the fruit_detection from the global variable
fruit_detection and send it to the ticket_printitng.html
page to be displayed
Returns:
--------
- GET: return the template ticket_printing.html.
"""
global fruit_prediction
return render_template('ticket_printing.html', predict=fruit_prediction)
@app.route('/under_the_hood/')
def under_the_hood():
"""
This function render the web page under_the_hood
Returns:
--------
- GET: return the template under_the_hood.html.
"""
return render_template('under_the_hood.html')
@app.route('/authors/')
def authors():
"""
This function render the web page authors
Returns:
--------
- GET: return the template authors.html.
"""
return render_template('authors.html')
@app.route('/thanks/')
def thanks():
"""
This function render the web page thanks
Returns:
--------
- GET: return the template thanks.html.
"""
return render_template('thanks.html')
@app.route('/quel_fruit/')
def quel_fruit():
"""
This function render the web page quel_fruit
Returns:
--------
- GET: return the template quel_fruit.html.
"""
return render_template('quel_fruit.html')
if __name__ == "__main__":
print("[INFO] Starting severt at http://localhost:5001")
socketio.run(app=app, port=5001)