Spaces:
Sleeping
Sleeping
Download app.py from Fire-Detection-Lab/HyFiNet: direct link, hf CLI and curl.
- Browser
- Download file 2.8 kB
-
https://huggingface.co/spaces/Fire-Detection-Lab/HyFiNet/resolve/5be22e97c9adeceff58e317ce00a2d6506254f81/app.py
- Command line
-
hf download hf://spaces/Fire-Detection-Lab/HyFiNet@5be22e97c9adeceff58e317ce00a2d6506254f81/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Fire-Detection-Lab/HyFiNet/resolve/5be22e97c9adeceff58e317ce00a2d6506254f81/app.py
2.8 kB
| import gradio as gr | |
| import tensorflow as tf | |
| from tensorflow.keras.models import load_model | |
| from tensorflow.keras import backend as K # <--- 1. IMPORT THE KERAS BACKEND | |
| import numpy as np | |
| from PIL import Image | |
| import time | |
| print("[INFO] app.py script started at {}.".format(time.ctime())) | |
| # --- Configuration --- | |
| MODEL_PATH = "Model/ProposedNetwork_StratifiedKFold_Final_fold_5.hdf5" | |
| IMAGE_SIZE = 224 | |
| # --- Load the Model (this will happen once when the app starts) --- | |
| print("[INFO] Attempting to load the trained model from: {}".format(MODEL_PATH)) | |
| try: | |
| # --- 2. THE CRITICAL FIX IS HERE --- | |
| # We must tell load_model what 'K' means in our custom layers. | |
| model = load_model(MODEL_PATH, custom_objects={"K": K}) | |
| # --- END OF FIX --- | |
| print("[SUCCESS] Model loaded successfully into memory.") | |
| except Exception as e: | |
| print("[FATAL ERROR] Failed to load the model.") | |
| print("Error details: {}".format(e)) | |
| raise e | |
| def getLabel(class_id): | |
| """Returns the string label for a given class ID.""" | |
| return 'NoFire' if class_id == 0 else 'Fire' | |
| # --- The Prediction Function --- | |
| def predict_fire(input_image): | |
| """ | |
| Takes a PIL image as input, preprocesses it, and returns a dictionary of labels and confidences. | |
| """ | |
| if input_image is None: | |
| return {"Error": 1.0} | |
| print("[INFO] Received image for prediction.") | |
| # Preprocess the image | |
| image = input_image.convert("RGB").resize((IMAGE_SIZE, IMAGE_SIZE)) | |
| image_array = np.asarray(image) | |
| image_array = image_array / 255.0 | |
| image_array = np.expand_dims(image_array, axis=0) | |
| # --- Make Prediction --- | |
| print("[INFO] Preprocessing complete. Making prediction...") | |
| prediction_probs = model.predict(image_array)[0] | |
| print("[INFO] Prediction complete.") | |
| # --- Format the Output --- | |
| confidences = {getLabel(i): float(prob) for i, prob in enumerate(prediction_probs)} | |
| return confidences | |
| # --- Create the Gradio Interface --- | |
| print("[INFO] Creating Gradio interface...") | |
| description_text = ( | |
| "Lightweight CNN for Fire Detection with Integrated Hybrid Attention: A Depthwise-Driven Approach for Embedded Systems\n" | |
| "Authors: Sakif Azwad, Mohammed Shahin Shah, Md Shakib\n" | |
| "Supervised by: Md. Khaliluzzaman" | |
| ) | |
| iface = gr.Interface( | |
| fn=predict_fire, | |
| inputs=gr.Image(type="pil", label="Upload an Image"), | |
| outputs=gr.Label(num_top_classes=2, label="Prediction Results"), | |
| title="🔥 Fire Detection AI 🔥", | |
| description=description_text, | |
| examples=[ | |
| ["examples/ex1.jpg"], | |
| ["examples/ex2.jpg"] | |
| ], | |
| cache_examples=False | |
| ) | |
| print("[INFO] Gradio interface created.") | |
| # --- Launch the App --- | |
| if __name__ == "__main__": | |
| print("[INFO] Launching Gradio app...") | |
| iface.launch() |