Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | 
         @@ -0,0 +1,46 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            import gradio as gr
         
     | 
| 2 | 
         
            +
            from ultralytics import YOLO
         
     | 
| 3 | 
         
            +
            from PIL import Image, ImageDraw
         
     | 
| 4 | 
         
            +
            import tempfile
         
     | 
| 5 | 
         
            +
            import os
         
     | 
| 6 | 
         
            +
             
     | 
| 7 | 
         
            +
            # Load your trained model
         
     | 
| 8 | 
         
            +
            model = YOLO("yolov8n.pt")  # Replace with your trained model .pt path
         
     | 
| 9 | 
         
            +
             
     | 
| 10 | 
         
            +
            # Detection function with confidence filter
         
     | 
| 11 | 
         
            +
            def detect_disease(image):
         
     | 
| 12 | 
         
            +
                # Save input to a temporary file
         
     | 
| 13 | 
         
            +
                temp = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
         
     | 
| 14 | 
         
            +
                image.save(temp.name)
         
     | 
| 15 | 
         
            +
             
     | 
| 16 | 
         
            +
                # Run YOLOv8 inference
         
     | 
| 17 | 
         
            +
                results = model(temp.name)[0]  # Get first result from list
         
     | 
| 18 | 
         
            +
             
     | 
| 19 | 
         
            +
                # Filter predictions with confidence >= 0.5
         
     | 
| 20 | 
         
            +
                filtered_boxes = []
         
     | 
| 21 | 
         
            +
                for box in results.boxes.data:
         
     | 
| 22 | 
         
            +
                    x1, y1, x2, y2, score, cls_id = box.tolist()
         
     | 
| 23 | 
         
            +
                    if score >= 0.5:
         
     | 
| 24 | 
         
            +
                        filtered_boxes.append((x1, y1, x2, y2, score, int(cls_id)))
         
     | 
| 25 | 
         
            +
             
     | 
| 26 | 
         
            +
                # Draw boxes on image
         
     | 
| 27 | 
         
            +
                draw = ImageDraw.Draw(image)
         
     | 
| 28 | 
         
            +
                class_names = model.names
         
     | 
| 29 | 
         
            +
             
     | 
| 30 | 
         
            +
                for x1, y1, x2, y2, score, cls_id in filtered_boxes:
         
     | 
| 31 | 
         
            +
                    draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
         
     | 
| 32 | 
         
            +
                    draw.text((x1, y1 - 10), f"{class_names[cls_id]}: {score:.2f}", fill="red")
         
     | 
| 33 | 
         
            +
             
     | 
| 34 | 
         
            +
                return image
         
     | 
| 35 | 
         
            +
             
     | 
| 36 | 
         
            +
            # Gradio Interface
         
     | 
| 37 | 
         
            +
            interface = gr.Interface(
         
     | 
| 38 | 
         
            +
                fn=detect_disease,
         
     | 
| 39 | 
         
            +
                inputs=gr.Image(type="pil"),
         
     | 
| 40 | 
         
            +
                outputs=gr.Image(type="pil"),
         
     | 
| 41 | 
         
            +
                title="🌿 Plant Disease Detector (YOLOv8)",
         
     | 
| 42 | 
         
            +
                description="Upload a leaf image. This model will detect plant diseases using YOLOv8. Only results with confidence ≥ 50% are shown."
         
     | 
| 43 | 
         
            +
            )
         
     | 
| 44 | 
         
            +
             
     | 
| 45 | 
         
            +
            # Launch app
         
     | 
| 46 | 
         
            +
            interface.launch()
         
     |