-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
91 lines (75 loc) · 2.71 KB
/
app.py
File metadata and controls
91 lines (75 loc) · 2.71 KB
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
import cv2
import numpy as np
import streamlit as st
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2,preprocess_input as mp
model = tf.keras.models.load_model("saved_model/mdl.hdf5")
confidence_threshold = 0.65
with open('style.css')as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html = True)
st.markdown("<h1 style='text-align: center;color: #00f0e2'>Animal Classification</h1>", unsafe_allow_html=True)
st.markdown(
"""
<style>
[data-testid="stAppViewContainer"] {
color: Aliceblue;
background: rgb(144,76,143);
background: radial-gradient(circle, rgba(144,76,143,1) 0%, rgba(108,0,91,1) 34%);
}
[data-testid="stHeader"] {
background-color: rgba(0,0,0,0);
}
[data-testid="stVerticalBlock"]{
background-color: linear-gradient(to bottom left, #2193b0 ,#6dd5ed) !important;
padding-bottom: 40px;
}
.predict-button {
background-color: #FF5722;
color: white;
padding: 10px 20px;
border-radius: 5px;
border: none;
font-size: 18px;
cursor: pointer;
}
[data-testid="stFileUploader"]{
background-color: linear-gradient(to bottom left, #2193b0 ,#6dd5ed) !important;
padding-bottom: 40px;
}
[data-testid="stButton"]{
background-color: linear-gradient(to bottom left, #2193b0 ,#6dd5ed) !important;
padding-bottom: 40px;
}
</style>
""",
unsafe_allow_html=True
)
uploaded_file = st.file_uploader("Choose an Image", type=["jpg","png","jpeg"],help="Upload an Image of the given format")
map_dict = {0: 'dog',
1: 'horse',
2: 'elephant',
3: 'butterfly',
4: 'chicken',
5: 'cat',
6: 'cow',
7: 'sheep',
8: 'spider',
9: 'squirrel'}
if uploaded_file is not None:
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
image = cv2.imdecode(file_bytes, 1)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
resized = cv2.resize(image,(224,224))
st.image(image, channels="RGB")
resized = mp(resized)
img_reshape = resized[np.newaxis,...]
pred = st.button("Predict", key="predict_button", help="Click to predict the animal")
if pred:
prediction = model.predict(img_reshape)
pred_class = prediction.argmax()
confidence = prediction[0][pred_class]
if confidence > confidence_threshold:
st.success("The animal in the given image is {}".format(map_dict[pred_class]))
else:
st.warning("No recognizable animal found in the image.")