-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_api.py
50 lines (40 loc) · 1.11 KB
/
my_api.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
from flask import Flask, request
import numpy as np
import os
import cv2
from keras.models import load_model
# Config
model_file = "models/cat_dog_classifier.hdf5"
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = "static"
# Load model
model = load_model(model_file)
@app.route('/', methods=['POST'])
def index():
try:
if request.method == 'POST':
image = request.files['file']
if image:
# Lưu file
path_to_save = os.path.join(app.config['UPLOAD_FOLDER'], image.filename)
image.save(path_to_save)
frame = cv2.imread(path_to_save)
# Xử lý file
frame = cv2.resize(frame, dsize=(150,150))
# Convert thành tensor
frame = np.expand_dims(frame, axis=0) # Used for single images
# Đưa vào model
prediction_prob = model.predict(frame)[0][0]
# Xét kết quả
if prediction_prob < 0.5: # 0-0.5: cat, else dog
output = "cat"
else:
output = "dog"
return output
else:
return "We only accept POST with image file"
except Exception as ex:
print(ex)
return "Error: " + str(ex)
if __name__ == '__main__':
app.run(host='0.0.0.0', port = 8080)