-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalorieMEAPI.py
More file actions
77 lines (55 loc) · 2.09 KB
/
CalorieMEAPI.py
File metadata and controls
77 lines (55 loc) · 2.09 KB
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
from flask import Flask, request, jsonify
import Test, CaloriesEstimation,os
from ID_segmentation import getIdCard
import os
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'CalorieMe API'
@app.route('/CalorieMe-V1', methods=['GET','POST'])
def predict():
try:
if request.method == 'POST':
img_link = request.form['img_link']
ref_pixels = request.form['ref_pixels']
print(img_link, ref_pixels)
# download image
os.system('wget -O Food_Model/img.jpg ' + img_link)
label = Test.getFoodWeight(img_link, ref_pixels)
json = CaloriesEstimation.getCalories(label)
return jsonify(json)
except Exception as e:
return jsonify({'msg': 'error', 'error': str(e)})
@app.route('/CalorieMe-V2', methods=['GET','POST'])
def predictV2():
try:
if request.method == 'POST':
img_bytes = request.files['img_bytes']
img_bytes.save("Food_Model/img.jpg")
return run()
except Exception as e:
return jsonify({'msg': 'error', 'error': str(e)})
def run():
try:
print("Getting ID Pixels")
ref_pixels = getIdCard("Food_Model/img.jpg")
print("ID Pixels",ref_pixels)
except Exception as e:
return jsonify({'msg': 'error', 'error': "Reference object not found"})
label = Test.getFoodWeightV2(0.25, ref_pixels)
if label == None:
label = Test.getFoodWeightV2(0.05, ref_pixels)
json = CaloriesEstimation.getCalories(label)
return jsonify(json)
@app.route('/refresh', methods=['GET'])
def refresh():
try:
if request.method == 'GET':
os.system('sudo reboot')
return jsonify({'msg': 'success'})
except Exception as e:
return jsonify({'msg': 'error', 'error': str(e)})
if __name__ == '__main__':
# from waitress import serve
# serve(app, host="0.0.0.0", port=5000)
app.run(host='0.0.0.0', port=5000, ssl_context=('/home/calorieME/certs/cert.pem', '/home/calorieME/certs/key.pem'), debug=True)