Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DARK-art108
GitHub Repository: DARK-art108/Cotton-Leaf-Disease-Detection
Path: blob/main/app.py
78 views
1
from __future__ import division, print_function
2
# coding=utf-8
3
import sys
4
import os
5
import glob
6
import re
7
import numpy as np
8
import tensorflow as tf
9
10
import pathlib
11
import wget
12
13
# from tensorflow.compat.v1.compat import ConfigProto
14
# from tensorflow.compat.v1 import InteractiveSession
15
#from tensorflow.python.client.session import InteractiveSession
16
17
# config = tf.ConfigProto()
18
# config.gpu_options.per_process_gpu_memory_fraction = 0.2
19
# config.gpu_options.allow_growth = True
20
# session = InteractiveSession(config=config)
21
# Keras
22
from tensorflow.keras.applications.resnet50 import preprocess_input
23
from tensorflow.keras.models import load_model
24
from tensorflow.keras.preprocessing import image
25
26
# Flask utils
27
from flask import Flask, redirect, url_for, request, render_template
28
from werkzeug.utils import secure_filename
29
30
# from gevent.pywsgi import WSGIServer
31
32
# Model saved with Keras model.save()
33
MODEL_PATH = 'model_resnet.hdf5'
34
MODEL_URL = 'https://github.com/DARK-art108/Cotton-Leaf-Disease-Prediction/releases/download/v1.0/model_resnet.hdf5'
35
UPLOAD_FOLDER = os.path.join(os.path.dirname(__file__), 'static', 'uploads')
36
37
# Download model if not present
38
while not pathlib.Path(MODEL_PATH).is_file():
39
print(f'Model {MODEL_PATH} not found. Downloading...')
40
wget.download(MODEL_URL)
41
42
# Define a flask app
43
app = Flask(__name__)
44
45
# Define upload path
46
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
47
48
# Developing in the absence of TensorFlow :P (Python 3.9.0 x64)
49
# def load_model(aa):
50
# class a:
51
# @staticmethod
52
# def predict(*args):
53
# return 1
54
# return a()
55
56
# class image:
57
# @staticmethod
58
# def load_img(path, target_size):
59
# return 'a'
60
61
# @staticmethod
62
# def img_to_array(img):
63
# return 'v'
64
65
# Load your trained model
66
model = load_model(MODEL_PATH)
67
68
69
def model_predict(img_path, model):
70
print(img_path)
71
img = image.load_img(img_path, target_size=(224, 224))
72
73
# Preprocessing the image
74
x = image.img_to_array(img)
75
# x = np.true_divide(x, 255)
76
## Scaling
77
x = x / 255
78
x = np.expand_dims(x, axis=0)
79
80
# Be careful how your trained model deals with the input
81
# otherwise, it won't make correct prediction!
82
# x = preprocess_input(x)
83
84
preds = model.predict(x)
85
preds = np.argmax(preds, axis=1)
86
if preds == 0:
87
preds = "The leaf is a diseased cotton leaf."
88
elif preds == 1:
89
preds = "The leaf is a diseased cotton plant."
90
elif preds == 2:
91
preds = "The leaf is a fresh cotton leaf."
92
else:
93
preds = "The leaf is a fresh cotton plant."
94
95
return preds
96
97
98
@app.route('/', methods=['GET', 'POST'])
99
def index():
100
# Main page
101
if request.method == 'POST':
102
# Get the file from post request
103
print(request.files, request.form, request.args)
104
f = None
105
if 'image' in request.files: f = request.files['image']
106
if f:
107
# Save the file to ./uploads
108
file_path = os.path.join(
109
app.config['UPLOAD_FOLDER'], secure_filename(f.filename))
110
f.save(file_path)
111
112
# Make prediction
113
preds = model_predict(file_path, model)
114
result = preds
115
return render_template('index.html', result=result, img=secure_filename(f.filename))
116
return render_template('index.html', result=None, err='Failed to receive file')
117
# First time
118
return render_template('index.html', result=None)
119
120
121
if __name__ == '__main__':
122
app.run(port=5001, debug=True)
123
124