Spaces:
Build error
Build error
sakina1122
commited on
Commit
•
3849da4
1
Parent(s):
79807d8
app.py upload
Browse files
app.py
CHANGED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""
|
3 |
+
Created on Thu Jun 11 22:34:20 2020
|
4 |
+
|
5 |
+
@author: Krish Naik
|
6 |
+
"""
|
7 |
+
|
8 |
+
from __future__ import division, print_function
|
9 |
+
# coding=utf-8
|
10 |
+
import sys
|
11 |
+
import os
|
12 |
+
import glob
|
13 |
+
import re
|
14 |
+
import numpy as np
|
15 |
+
import tensorflow as tf
|
16 |
+
import tensorflow as tf
|
17 |
+
|
18 |
+
from tensorflow.compat.v1 import ConfigProto
|
19 |
+
from tensorflow.compat.v1 import InteractiveSession
|
20 |
+
|
21 |
+
config = ConfigProto()
|
22 |
+
config.gpu_options.per_process_gpu_memory_fraction = 0.2
|
23 |
+
config.gpu_options.allow_growth = True
|
24 |
+
session = InteractiveSession(config=config)
|
25 |
+
# Keras
|
26 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
27 |
+
from tensorflow.keras.models import load_model
|
28 |
+
from tensorflow.keras.preprocessing import image
|
29 |
+
|
30 |
+
# Flask utils
|
31 |
+
from flask import Flask, redirect, url_for, request, render_template
|
32 |
+
from werkzeug.utils import secure_filename
|
33 |
+
#from gevent.pywsgi import WSGIServer
|
34 |
+
|
35 |
+
# Define a flask app
|
36 |
+
app = Flask(__name__)
|
37 |
+
|
38 |
+
# Model saved with Keras model.save()
|
39 |
+
MODEL_PATH ='model_resnet152V2.h5'
|
40 |
+
|
41 |
+
# Load your trained model
|
42 |
+
model = load_model(MODEL_PATH)
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
def model_predict(img_path, model):
|
48 |
+
print(img_path)
|
49 |
+
img = image.load_img(img_path, target_size=(224, 224))
|
50 |
+
|
51 |
+
# Preprocessing the image
|
52 |
+
x = image.img_to_array(img)
|
53 |
+
# x = np.true_divide(x, 255)
|
54 |
+
## Scaling
|
55 |
+
x=x/255
|
56 |
+
x = np.expand_dims(x, axis=0)
|
57 |
+
|
58 |
+
|
59 |
+
# Be careful how your trained model deals with the input
|
60 |
+
# otherwise, it won't make correct prediction!
|
61 |
+
# x = preprocess_input(x)
|
62 |
+
|
63 |
+
preds = model.predict(x)
|
64 |
+
preds=np.argmax(preds, axis=1)
|
65 |
+
if preds==0:
|
66 |
+
preds="The leaf is diseased cotton leaf"
|
67 |
+
elif preds==1:
|
68 |
+
preds="The leaf is diseased cotton plant"
|
69 |
+
elif preds==2:
|
70 |
+
preds="The leaf is fresh cotton leaf"
|
71 |
+
else:
|
72 |
+
preds="The leaf is fresh cotton plant"
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
return preds
|
77 |
+
|
78 |
+
|
79 |
+
@app.route('/', methods=['GET'])
|
80 |
+
def index():
|
81 |
+
# Main page
|
82 |
+
return render_template('index.html')
|
83 |
+
|
84 |
+
|
85 |
+
@app.route('/predict', methods=['GET', 'POST'])
|
86 |
+
def upload():
|
87 |
+
if request.method == 'POST':
|
88 |
+
# Get the file from post request
|
89 |
+
f = request.files['file']
|
90 |
+
|
91 |
+
# Save the file to ./uploads
|
92 |
+
basepath = os.path.dirname(__file__)
|
93 |
+
file_path = os.path.join(
|
94 |
+
basepath, 'uploads', secure_filename(f.filename))
|
95 |
+
f.save(file_path)
|
96 |
+
|
97 |
+
# Make prediction
|
98 |
+
preds = model_predict(file_path, model)
|
99 |
+
result=preds
|
100 |
+
return result
|
101 |
+
return None
|
102 |
+
|
103 |
+
|
104 |
+
if __name__ == '__main__':
|
105 |
+
app.run(port=5001,debug=True)
|