-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
61 lines (52 loc) · 1.87 KB
/
app.py
File metadata and controls
61 lines (52 loc) · 1.87 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
from keras.models import load_model
import streamlit as st
import tensorflow as tf
import numpy as np
# Streamlit header for the app
st.header("Flower Classification CNN Model")
# Class labels
flowers_names = ['daisy', 'dandelion', 'rose', 'sunflower', 'tulip']
# Load the pre-trained model
def load_model_file(model_path):
try:
model = load_model(model_path)
return model
except Exception as e:
st.error(f"Error loading model: {e}")
return None
model_path = "Flower_Recognition_Model.keras"
model = load_model_file(model_path)
if model is None:
st.stop()
# Function to classify an uploaded image
def classify_img(image):
try:
input_image = tf.keras.utils.load_img(image, target_size=(180, 180))
input_image_array = tf.keras.utils.img_to_array(input_image)
input_image_exp_dim = np.expand_dims(input_image_array, axis=0)
predictions = model.predict(input_image_exp_dim)
max_prob = np.argmax(predictions)
confidence_score = np.max(predictions)
threshold = 0.9
if confidence_score < threshold:
outcome = "The image is not a flower."
else:
outcome = f"The Image belongs to {flowers_names[max_prob]} with a score of {confidence_score * 100:.2f}%"
return outcome
except Exception as e:
st.error(f"Error classifying image: {e}")
return None
# File uploader for the user to upload an image
def upload_image():
uploaded_file = st.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
return uploaded_file
uploaded_file = upload_image()
# Check if a file is uploaded
if uploaded_file is not None:
st.image(uploaded_file, width=400)
with st.spinner("Classifying image..."):
result = classify_img(uploaded_file)
if result is not None:
st.write(result)
else:
st.write("Please upload an image")