Upload app.py
Browse files79 unmodified lines808182838485868788899091922 unmodified lines95969798991001011021031041051061071081091101111121131141151161178 unmodified lines1261271281291301311321331341355 unmodified lines1411421431441451461475 unmodified lines15315415515615715815979 unmodified linesimport tensorflow as tffrom tensorflow.keras.models import load_modelfrom PIL import Imageimport cv2import os# Load the modelmodel = tf.keras.models.load_model("model_n.keras")class_names = ['Bush Clock Vine', 'Common Lanthana', 'Datura', 'Hibiscus', 'Jatropha', 'Marigold','Nityakalyani', 'Rose', 'Yellow_Daisy', 'adathoda', 'banana', 'champaka', 'chitrak',2 unmodified lines'thumba', 'touch me not', 'tridax procumbens', 'wild_potato_vine']on_huggingface = os.environ.get("SPACE_ID") is not Nonest.title(" Flower Identifier")if on_huggingface:st.warning("Real-time camera is not supported on Hugging Face Spaces. Please upload an image.")mode = "Upload Image"else:mode = st.radio("Choose input method:", ["Upload Image", "Real-Time Camera"])if mode == "Upload Image":st.markdown("### Upload an image of a flower")img = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])if img is not None:st.image(img, caption="Uploaded Image", use_column_width=True)8 unmodified linesconfidence = round(100 * np.max(prediction[0]), 2)flower_name = class_names[predicted_class]st.success(f" Predicted Flower: **{flower_name}**")st.info(f"🔍 Confidence: **{confidence}%**")elif mode == "Real-Time Camera":st.markdown("### Real-Time Flower Recognition")run = st.checkbox('Start Camera')FRAME_WINDOW = st.image([])5 unmodified lineswhile run:ret, frame = cap.read()if not ret:st.warning("Failed to access camera.")breakimg_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)5 unmodified linesconfidence = round(100 * np.max(predictions[0]), 2)flower_name = class_names[predicted_class]cv2.putText(frame, f"{flower_name} ({confidence}%)", (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)FRAME_WINDOW.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))79 unmodified lines8081828384858687888990919293942 unmodified lines9798991001011021031041051061071081091101111121131141151161178 unmodified lines1261271281291301311321331341351361375 unmodified lines1431441451461471481495 unmodified lines15515615715815916016179 unmodified linesimport tensorflow as tffrom tensorflow.keras.models import load_modelfrom PIL import Imageimport os# Check if running on Hugging Face Spaceson_huggingface = os.environ.get("SPACE_ID") is not None# Load modelmodel = tf.keras.models.load_model("model_n.keras")# Class namesclass_names = ['Bush Clock Vine', 'Common Lanthana', 'Datura', 'Hibiscus', 'Jatropha', 'Marigold','Nityakalyani', 'Rose', 'Yellow_Daisy', 'adathoda', 'banana', 'champaka', 'chitrak',2 unmodified lines'thumba', 'touch me not', 'tridax procumbens', 'wild_potato_vine']# Titlest.title("🌼 Flower Identifier")# Choose modeif on_huggingface:st.warning("Real-time camera is not supported on Hugging Face. Please upload an image.")mode = "Upload Image"else:mode = st.radio("Choose input method:", ["Upload Image", "Real-Time Camera"])# Upload image modeif mode == "Upload Image":st.markdown("### Upload an image of a flower")img = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])if img is not None:st.image(img, caption="Uploaded Image", use_column_width=True)8 unmodified linesconfidence = round(100 * np.max(prediction[0]), 2)flower_name = class_names[predicted_class]st.success(f"🌸 Predicted Flower: **{flower_name}**")st.info(f"🔍 Confidence: **{confidence}%**")# Real-time camera mode (local only)elif mode == "Real-Time Camera":import cv2 # <- import only if neededst.markdown("### Real-Time Flower Recognition")run = st.checkbox('Start Camera')FRAME_WINDOW = st.image([])5 unmodified lineswhile run:ret, frame = cap.read()if not ret:st.warning("⚠️ Failed to access camera.")breakimg_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)5 unmodified linesconfidence = round(100 * np.max(predictions[0]), 2)flower_name = class_names[predicted_class]cv2.putText(frame, f"{flower_name} ({confidence}%)", (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)FRAME_WINDOW.image(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
app.py+16-14