This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
executable file
·62 lines (55 loc) · 1.77 KB
/
web.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
from flask import Flask, render_template, Response, request
from flask_socketio import SocketIO, emit
from camera import VideoCamera
from neuralnetwork import NeuralNetwork
from collections import Counter
import numpy
import json
app = Flask(__name__)
socketio = SocketIO(app)
v = VideoCamera()
####### App Routes ########
@app.route('/')
def index():
return render_template('index.html')
@app.route('/video_feed')
def video_feed():
return Response(gen(v),
mimetype='multipart/x-mixed-replace; boundary=frame')
def predict(frame):
print("Prediction Initialising.\n.\n.\n.\n.\n.")
model = NeuralNetwork()
predictions = model.predict(frame)
quantities = Counter(prediction['name'] for prediction in predictions)
products = []
with open('prices.json') as json_file:
for key, element in quantities.items():
try:
data = json.load(json_file)
products.append({
'product': key,
'quantity': element,
'price': data[key] * element
})
except:
products.append({
'product': key,
'quantity': element,
'price': 0
})
print(products)
socketio.emit('predict', {'data': json.dumps(products)})
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
####### Socket Events ########
@socketio.on('picture')
def snap(picture):
image = v.get_frame()
predict(image)
if __name__ == '__main__':
app.debug = True
app.passthrough_errors = True
socketio.run(app, host='0.0.0.0')