-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
executable file
·56 lines (50 loc) · 1.94 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
from flask import Flask, request, jsonify, render_template
import os
import requests
app = Flask(__name__)
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route("/")
def home():
return render_template("index.html")
@app.route("/predict", methods = ["POST"])
def predict():
message = request.form["img-url"]
try:
img_data = requests.get(message).content
with open('./static/image_name.jpg', 'wb') as handler:
handler.write(img_data)
picFolder = os.path.join('./static')
pic = "image_name.jpg"
app.config['UPLOAD_FOLDER'] = picFolder
img = os.path.join(app.config['UPLOAD_FOLDER'], pic)
from keras.models import load_model
model = load_model('./model.h5')
import numpy as np
from keras.preprocessing import image
pro_img = './static/image_name.jpg'
test_image = image.load_img(pro_img, target_size=(64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = model.predict(test_image)
if result[0][0] == 1:
prediction = 'Dog'
else:
prediction = 'Cat'
return render_template("index.html", disp_img=img, prediction=prediction)
except:
return render_template("index.html", disp_img=img, prediction="Error While Processing the Image URL")
# img_data = requests.get(message).content
# with open('./static/image_name.jpg', 'wb') as handler:
# handler.write(img_data)
if __name__ == "__main__":
app.run(debug=True)