Download app.py from pratham2025/Insulator-Defect-Detection: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/pratham2025/Insulator-Defect-Detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/pratham2025/Insulator-Defect-Detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/pratham2025/Insulator-Defect-Detection/resolve/main/app.py
1.07 kB
| import gradio as gr | |
| import cv2 | |
| from ultralytics import YOLO | |
| # 1. Load the model (Notice the path is just 'best.pt' now!) | |
| model = YOLO('best.pt') | |
| # 2. Define the prediction function | |
| def predict_insulator(img): | |
| if img is None: | |
| return None | |
| results = model.predict(source=img, conf=0.25) | |
| result = results[0] | |
| annotated_img = result.plot() | |
| annotated_img_rgb = cv2.cvtColor(annotated_img, cv2.COLOR_BGR2RGB) | |
| return annotated_img_rgb | |
| # 3. Build the Interface | |
| demo = gr.Interface( | |
| fn=predict_insulator, | |
| inputs=gr.Image(type="numpy", label="Upload Insulator Image"), | |
| outputs=gr.Image(type="numpy", label="AI Detection Results"), | |
| title="⚡ Power Line Insulator Defect Detector", | |
| description="Upload an image of an electrical transmission insulator. The AI will detect if the insulator has **No issues**, is **Broken**, or has **Flashover damage**.", | |
| theme="default" | |
| ) | |
| # 4. Launch (Hugging Face handles the sharing automatically) | |
| if __name__ == "__main__": | |
| demo.launch() |