Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import numpy as np
|
| 3 |
import tensorflow as tf
|
| 4 |
from tensorflow.keras.models import load_model
|
| 5 |
from PIL import Image
|
| 6 |
import cv2
|
|
|
|
| 7 |
|
| 8 |
# Load the model
|
| 9 |
model = tf.keras.models.load_model("model_n.keras")
|
| 10 |
|
| 11 |
-
|
| 12 |
class_names = [
|
| 13 |
'Bush Clock Vine', 'Common Lanthana', 'Datura', 'Hibiscus', 'Jatropha', 'Marigold',
|
| 14 |
'Nityakalyani', 'Rose', 'Yellow_Daisy', 'adathoda', 'banana', 'champaka', 'chitrak',
|
|
@@ -17,11 +95,18 @@ class_names = [
|
|
| 17 |
'thumba', 'touch me not', 'tridax procumbens', 'wild_potato_vine'
|
| 18 |
]
|
| 19 |
|
| 20 |
-
# Title
|
| 21 |
-
st.title("Flower Identifier")
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
if mode == "Upload Image":
|
| 27 |
st.markdown("### Upload an image of a flower")
|
|
@@ -41,8 +126,8 @@ if mode == "Upload Image":
|
|
| 41 |
confidence = round(100 * np.max(prediction[0]), 2)
|
| 42 |
flower_name = class_names[predicted_class]
|
| 43 |
|
| 44 |
-
st.success(f"Predicted Flower: **{flower_name}**")
|
| 45 |
-
st.info(f"Confidence: **{confidence}%**")
|
| 46 |
|
| 47 |
elif mode == "Real-Time Camera":
|
| 48 |
st.markdown("### Real-Time Flower Recognition")
|
|
@@ -52,6 +137,7 @@ elif mode == "Real-Time Camera":
|
|
| 52 |
cap = None
|
| 53 |
if run:
|
| 54 |
cap = cv2.VideoCapture(0)
|
|
|
|
| 55 |
while run:
|
| 56 |
ret, frame = cap.read()
|
| 57 |
if not ret:
|
|
@@ -67,7 +153,6 @@ elif mode == "Real-Time Camera":
|
|
| 67 |
confidence = round(100 * np.max(predictions[0]), 2)
|
| 68 |
flower_name = class_names[predicted_class]
|
| 69 |
|
| 70 |
-
# Annotate frame
|
| 71 |
cv2.putText(frame, f"{flower_name} ({confidence}%)", (10, 30),
|
| 72 |
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
| 73 |
|
|
|
|
| 1 |
+
# import streamlit as st
|
| 2 |
+
# import numpy as np
|
| 3 |
+
# import tensorflow as tf
|
| 4 |
+
# from tensorflow.keras.models import load_model
|
| 5 |
+
# from PIL import Image
|
| 6 |
+
# import cv2
|
| 7 |
+
|
| 8 |
+
# # Load the model
|
| 9 |
+
# model = tf.keras.models.load_model("model_n.keras")
|
| 10 |
+
|
| 11 |
+
# # Define class names
|
| 12 |
+
# class_names = [
|
| 13 |
+
# 'Bush Clock Vine', 'Common Lanthana', 'Datura', 'Hibiscus', 'Jatropha', 'Marigold',
|
| 14 |
+
# 'Nityakalyani', 'Rose', 'Yellow_Daisy', 'adathoda', 'banana', 'champaka', 'chitrak',
|
| 15 |
+
# 'crown flower', "four o'clock flower", 'honeysuckle', 'indian mallow', 'malabar melastome',
|
| 16 |
+
# 'nagapoovu', 'pinwheel flower', 'shankupushpam', 'spider lily', 'sunflower', 'thechi',
|
| 17 |
+
# 'thumba', 'touch me not', 'tridax procumbens', 'wild_potato_vine'
|
| 18 |
+
# ]
|
| 19 |
+
|
| 20 |
+
# # Title
|
| 21 |
+
# st.title("Flower Identifier")
|
| 22 |
+
|
| 23 |
+
# # Choose mode
|
| 24 |
+
# mode = st.radio("Choose input method:", ["Upload Image", "Real-Time Camera"])
|
| 25 |
+
|
| 26 |
+
# if mode == "Upload Image":
|
| 27 |
+
# st.markdown("### Upload an image of a flower")
|
| 28 |
+
# img = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
| 29 |
+
|
| 30 |
+
# if img is not None:
|
| 31 |
+
# st.image(img, caption="Uploaded Image", use_column_width=True)
|
| 32 |
+
|
| 33 |
+
# image = Image.open(img).convert("RGB")
|
| 34 |
+
# image = tf.keras.preprocessing.image.img_to_array(image)
|
| 35 |
+
# image = tf.cast(image, tf.float32)
|
| 36 |
+
# image = tf.expand_dims(image, 0)
|
| 37 |
+
|
| 38 |
+
# if st.button("Identify Flower"):
|
| 39 |
+
# prediction = model.predict(image)
|
| 40 |
+
# predicted_class = np.argmax(prediction[0])
|
| 41 |
+
# confidence = round(100 * np.max(prediction[0]), 2)
|
| 42 |
+
# flower_name = class_names[predicted_class]
|
| 43 |
+
|
| 44 |
+
# st.success(f"Predicted Flower: **{flower_name}**")
|
| 45 |
+
# st.info(f"Confidence: **{confidence}%**")
|
| 46 |
+
|
| 47 |
+
# elif mode == "Real-Time Camera":
|
| 48 |
+
# st.markdown("### Real-Time Flower Recognition")
|
| 49 |
+
# run = st.checkbox('Start Camera')
|
| 50 |
+
# FRAME_WINDOW = st.image([])
|
| 51 |
+
|
| 52 |
+
# cap = None
|
| 53 |
+
# if run:
|
| 54 |
+
# cap = cv2.VideoCapture(0)
|
| 55 |
+
# while run:
|
| 56 |
+
# ret, frame = cap.read()
|
| 57 |
+
# if not ret:
|
| 58 |
+
# st.warning("Failed to access camera.")
|
| 59 |
+
# break
|
| 60 |
+
|
| 61 |
+
# img_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 62 |
+
# img_array = tf.keras.preprocessing.image.img_to_array(img_rgb)
|
| 63 |
+
# img_array = tf.expand_dims(tf.cast(img_array, tf.float32), 0)
|
| 64 |
+
|
| 65 |
+
# predictions = model.predict(img_array)
|
| 66 |
+
# predicted_class = np.argmax(predictions[0])
|
| 67 |
+
# confidence = round(100 * np.max(predictions[0]), 2)
|
| 68 |
+
# flower_name = class_names[predicted_class]
|
| 69 |
+
|
| 70 |
+
# # Annotate frame
|
| 71 |
+
# cv2.putText(frame, f"{flower_name} ({confidence}%)", (10, 30),
|
| 72 |
+
# cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
| 73 |
+
|
| 74 |
+
# FRAME_WINDOW.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 75 |
+
|
| 76 |
+
# if cap:
|
| 77 |
+
# cap.release()
|
| 78 |
import streamlit as st
|
| 79 |
import numpy as np
|
| 80 |
import tensorflow as tf
|
| 81 |
from tensorflow.keras.models import load_model
|
| 82 |
from PIL import Image
|
| 83 |
import cv2
|
| 84 |
+
import os
|
| 85 |
|
| 86 |
# Load the model
|
| 87 |
model = tf.keras.models.load_model("model_n.keras")
|
| 88 |
|
| 89 |
+
|
| 90 |
class_names = [
|
| 91 |
'Bush Clock Vine', 'Common Lanthana', 'Datura', 'Hibiscus', 'Jatropha', 'Marigold',
|
| 92 |
'Nityakalyani', 'Rose', 'Yellow_Daisy', 'adathoda', 'banana', 'champaka', 'chitrak',
|
|
|
|
| 95 |
'thumba', 'touch me not', 'tridax procumbens', 'wild_potato_vine'
|
| 96 |
]
|
| 97 |
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
on_huggingface = os.environ.get("SPACE_ID") is not None
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
st.title(" Flower Identifier")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
if on_huggingface:
|
| 106 |
+
st.warning("Real-time camera is not supported on Hugging Face Spaces. Please upload an image.")
|
| 107 |
+
mode = "Upload Image"
|
| 108 |
+
else:
|
| 109 |
+
mode = st.radio("Choose input method:", ["Upload Image", "Real-Time Camera"])
|
| 110 |
|
| 111 |
if mode == "Upload Image":
|
| 112 |
st.markdown("### Upload an image of a flower")
|
|
|
|
| 126 |
confidence = round(100 * np.max(prediction[0]), 2)
|
| 127 |
flower_name = class_names[predicted_class]
|
| 128 |
|
| 129 |
+
st.success(f" Predicted Flower: **{flower_name}**")
|
| 130 |
+
st.info(f"🔍 Confidence: **{confidence}%**")
|
| 131 |
|
| 132 |
elif mode == "Real-Time Camera":
|
| 133 |
st.markdown("### Real-Time Flower Recognition")
|
|
|
|
| 137 |
cap = None
|
| 138 |
if run:
|
| 139 |
cap = cv2.VideoCapture(0)
|
| 140 |
+
|
| 141 |
while run:
|
| 142 |
ret, frame = cap.read()
|
| 143 |
if not ret:
|
|
|
|
| 153 |
confidence = round(100 * np.max(predictions[0]), 2)
|
| 154 |
flower_name = class_names[predicted_class]
|
| 155 |
|
|
|
|
| 156 |
cv2.putText(frame, f"{flower_name} ({confidence}%)", (10, 30),
|
| 157 |
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
|
| 158 |
|