-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
75 lines (59 loc) · 1.95 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
import numpy as np
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS, cross_origin
import cv2
import os
import base64
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input as mobilenet_v2_preprocess_input
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
model = tf.keras.models.load_model("model.h5")
@app.route('/upload_canvas', methods=['POST'])
@cross_origin()
def upload_canvas():
file = request.form['file']
# file = base64.b64decode(file)
if file:
with open(os.path.join(os.getcwd(), 'test_img.png'), "wb") as fh:
fh.write(base64.decodebytes(file.encode()))
return "Bonjour"
else:
return "adios"
@app.route('/upload_file', methods=['POST'])
@cross_origin()
def upload_file():
file = request.files['file']
if file:
file.save(os.path.join(os.getcwd(), 'test_img.png'))
return "Bonjour"
else:
return "adios"
@app.route('/predict', methods=['GET'])
def predict():
image = cv2.imread("test_img.png")
dim = (240, 240)
resized = mobilenet_v2_preprocess_input(image)
resized = cv2.resize(resized, dim)
img_reshape = np.expand_dims(resized, axis=0)
prediction = model.predict(img_reshape)
if(prediction[0][0] > 0.5):
result = "UnHealthy"
else:
result = "Healthy"
print(result)
return result
if __name__ == "__main__":
app.run(debug=True)
# import os
# main_directory = "training/unhealthy"
# for subdir, dirs, files in os.walk(main_directory):
# for file in files:
# filepath = os.path.join(subdir, file)
# if filepath.endswith(".jpg"):
# im = Image.open(filepath)
# imResize = im.resize((240, 240), Image.ANTIALIAS)
# print(filepath[:-4] )
# imResize.save(filepath[:-4] + '.jpg', 'JPEG', quality=90)