-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
84 lines (63 loc) · 2.5 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
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
import streamlit as st
import tensorflow as tf
from tensorflow.keras.applications.imagenet_utils import decode_predictions
import cv2
from PIL import Image, ImageOps
import numpy as np
import copy
@st.cache(allow_output_mutation=True)
def load_model():
model=tf.keras.models.load_model('old_model-009.h5')
return model
with st.spinner('Model is being loaded..'):
model=load_model()
st.write("""
# Image Classification
"""
)
file = st.file_uploader("Upload the image to be classified U0001F447", type=["jpg", "png"])
st.set_option('deprecation.showfileUploaderEncoding', False)
def upload_predict(upload_image, model):
size = (180,180)
image = ImageOps.fit(upload_image, size, Image.ANTIALIAS)
image = np.asarray(image)
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
img_resize = cv2.resize(img, dsize=(224, 224),interpolation=cv2.INTER_CUBIC)
img_reshape = img_resize[np.newaxis,...]
prediction = model.predict(img_reshape)
pred_class=decode_predictions(prediction,top=1)
return pred_class
if file is None:
st.text("Please upload an image file")
else:
img = Image.open(file)
img =np.array(img)
img1=copy.deepcopy(img)
#RGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray=cv2.cvtColor(img1,cv2.COLOR_RGB2GRAY)
blur=cv2.medianBlur(gray,25)
canyedge=cv2.Canny(blur,10,80)
kernel =cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
close=cv2.morphologyEx(canyedge,cv2.MORPH_CLOSE,kernel,iterations=1)
dilate=cv2.dilate(close,kernel,iterations=2)
cnts=cv2.findContours(dilate,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:10]
withcontour=cv2.drawContours(img1,cnts,0,(255,0,0),5)
total_area=0
for i in range(len(cnts)):
area=cv2.contourArea(cnts[0])
total_area=total_area+area
st.image(withcontour, use_column_width=True)
img1=cv2.resize(img1, (228,228), cv2.INTER_AREA)
images = np.expand_dims(img1, axis=0)
prediction = model.predict(images)
image_class = str(prediction)
if prediction <= 0.5:
st.write("The image is classified as good onion")
else:
st.write("The image is classified as bad onion")
if total_area>50000:
st.write("large onion")
else:
st.write("small onion")