Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
DARK-art108
GitHub Repository: DARK-art108/Cotton-Leaf-Disease-Detection
Path: blob/main/streamlit_app.py
78 views
1
from __future__ import division, print_function
2
import streamlit as st
3
from PIL import Image, ImageOps
4
5
# coding=utf-8
6
import sys
7
import os
8
import glob
9
import re
10
import numpy as np
11
import tensorflow as tf
12
13
import pathlib
14
import wget
15
16
from tensorflow.keras.applications.resnet50 import preprocess_input
17
from tensorflow.keras.models import load_model
18
from tensorflow.keras.preprocessing import image
19
20
# Flask utils
21
from flask import Flask, redirect, url_for, request, render_template
22
from werkzeug.utils import secure_filename
23
24
# from gevent.pywsgi import WSGIServer
25
26
# Model saved with Keras model.save()
27
MODEL_PATH = 'model_resnet.hdf5'
28
MODEL_URL = 'https://github.com/DARK-art108/Cotton-Leaf-Disease-Prediction/releases/download/v1.0/model_resnet.hdf5'
29
UPLOAD_FOLDER = os.path.join(os.path.dirname(__file__), 'static', 'uploads')
30
31
# Download model if not present
32
while not pathlib.Path(MODEL_PATH).is_file():
33
print(f'Model {MODEL_PATH} not found. Downloading...')
34
wget.download(MODEL_URL)
35
36
# Define a flask app
37
app = Flask(__name__)
38
39
# Define upload path
40
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
41
42
# Developing in the absence of TensorFlow :P (Python 3.9.0 x64)
43
# def load_model(aa):
44
# class a:
45
# @staticmethod
46
# def predict(*args):
47
# return 1
48
# return a()
49
50
# class image:
51
# @staticmethod
52
# def load_img(path, target_size):
53
# return 'a'
54
55
# @staticmethod
56
# def img_to_array(img):
57
# return 'v'
58
59
# Load your trained model
60
61
model = load_model(MODEL_PATH)
62
def model_predict(img, MODEL_PATH):
63
# Create the array of the right shape to feed into the keras model
64
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
65
image = img
66
#image sizing
67
size = (224, 224)
68
image = ImageOps.fit(image, size, Image.ANTIALIAS)
69
70
#turn the image into a numpy array
71
image_array = np.asarray(image)
72
# Normalize the image
73
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
74
75
# Load the image into the array
76
data[0] = normalized_image_array
77
78
# run the inference
79
prediction = model.predict(data)
80
return np.argmax(prediction, axis=1) # return position of the highest probability
81
82
st.title("Cotton Leaf Disease Prediction")
83
st.header("Transfer Learning Using RESNET51V2")
84
st.text("Upload a Cotton Leaf Disease or Non-Diseased Image")
85
86
uploaded_file = st.file_uploader("Choose a Cotton Leaf Image...", type="jpg")
87
if uploaded_file is not None:
88
image = Image.open(uploaded_file)
89
st.image(image, caption='Uploaded Cotton Leaf Image', use_column_width=True)
90
st.write("")
91
st.write("Classifying...")
92
label = model_predict(image, 'model_resnet.hdf5')
93
if label == 0:
94
st.write("The leaf is a diseased cotton leaf.")
95
elif label == 1:
96
st.write("The leaf is a diseased cotton plant.")
97
elif label == 2:
98
st.write("The leaf is a fresh cotton leaf.")
99
else:
100
st.write("The leaf is a fresh cotton plant.")
101
102