Spaces:
Sleeping
Sleeping
| # app.py - YOLOv12 deployment for Hugging Face Spaces | |
| import gradio as gr | |
| from ultralytics import YOLO | |
| from PIL import Image | |
| # Load your YOLOv12 model | |
| model = YOLO('best.pt') | |
| def predict(image): | |
| """ | |
| Run YOLOv12 inference on the uploaded image | |
| """ | |
| try: | |
| # Run inference | |
| results = model(image) | |
| # Get the annotated image with bounding boxes | |
| annotated_img = results[0].plot() | |
| # Convert BGR to RGB (OpenCV to PIL) | |
| annotated_img = Image.fromarray(annotated_img[..., ::-1]) | |
| # Get detection details | |
| detections = results[0].boxes | |
| detection_text = [] | |
| for box in detections: | |
| cls = int(box.cls[0]) | |
| conf = float(box.conf[0]) | |
| class_name = model.names[cls] | |
| detection_text.append(f"{class_name}: {conf:.3%}") | |
| if detection_text: | |
| summary = "\n".join(detection_text) | |
| else: | |
| summary = "No objects detected" | |
| return annotated_img, summary | |
| except Exception as e: | |
| return None, f"Error: {str(e)}" | |
| # Create Gradio interface | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=gr.Image(type="pil", label="Upload Image"), | |
| outputs=[ | |
| gr.Image(type="pil", label="Detection Results"), | |
| gr.Textbox(label="Detected Objects", lines=10) | |
| ], | |
| title="Fine-Tuned YOLOv12 using Optuna", | |
| description="Upload an image to detect objects using YOLOv12", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |