-
Notifications
You must be signed in to change notification settings - Fork 1
/
predictions.py
105 lines (93 loc) · 3.17 KB
/
predictions.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import pandas as pd
from tensorflow import keras
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline
from keras.preprocessing import image
from PIL import Image
import streamlit as st
st.set_page_config(layout="wide")
def set_bg_hack_url():
'''
A function to unpack an image from url and set as bg.
Returns
-------
The background.
'''
st.markdown(
f"""
<style>
.stApp {{
background: url("https://wallpaperbat.com/img/161069-neural-network-wallpaper.gif");
background-size: cover
}}
</style>
""",
unsafe_allow_html=True
)
#Function to load uploaded image
def load_image(image_file):
img = Image.open(image_file)
return img
# Function to check the image
@st.cache(ttl=48*3600)
def check():
lr = keras.models.load_model('weights.hdf5')
#Prediction Pipeline
class Preprocessor(BaseEstimator, TransformerMixin):
def fit(self,img_object):
return self
def transform(self,img_object):
img_array = image.img_to_array(img_object)
expanded = (np.expand_dims(img_array,axis=0))
return expanded
class Predictor(BaseEstimator, TransformerMixin):
def fit(self,img_array):
return self
def predict(self,img_array):
probabilities = lr.predict(img_array)
predicted_class = ['P_Deficiency', 'Healthy', 'N_Deficiency', 'K_Deficiency'][probabilities.argmax()]
return predicted_class
full_pipeline = Pipeline([('preprocessor',Preprocessor()),
('predictor',Predictor())])
return full_pipeline
def output(full_pipeline,img):
a= img
#a = img.decode('utf-8', 'ignore')
a= a.resize((224,224))
predic = full_pipeline.predict(a)
return(predic)
def main():
# giving a title
set_bg_hack_url()
col1, col2 = st.columns(2)
with col1:
st.title('H.A.R.N.')
st.subheader('Image Classification Using CNN for identifying Plant Nutrient Deficiencies')
image_file = st.file_uploader("Upload Images", type=["png","jpg","jpeg"])
# code for Prediction
prediction = ''
# creating a button for Prediction
if st.button('Predict'):
if image_file is not None:
# To See details
with st.spinner('Loading Image and Model...'):
full_pipeline = check()
file_details = {"filename":image_file.name, "filetype":image_file.type,"filesize":image_file.size}
st.write(file_details)
img = load_image(image_file)
w=img.size[0]
h=img.size[1]
if w>h:
w=600
st.image(img,width=w)
else:
w=w*(600.0/h)
st.image(img,width=int(w))
with st.spinner('Predicting...'):
prediction = output(full_pipeline,img)
st.success(prediction)
if __name__ == '__main__':
main()