Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | 
         @@ -0,0 +1,32 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            from detect import *
         
     | 
| 2 | 
         
            +
            from pathlib import Path
         
     | 
| 3 | 
         
            +
            # Get the current script's directory
         
     | 
| 4 | 
         
            +
            script_dir = Path(__file__).resolve().parent
         
     | 
| 5 | 
         
            +
             
     | 
| 6 | 
         
            +
            # Define the relative path to the weights file
         
     | 
| 7 | 
         
            +
            weights_path = script_dir / 'runs/train/exp17/weights/best.pt'
         
     | 
| 8 | 
         
            +
             
     | 
| 9 | 
         
            +
             
     | 
| 10 | 
         
            +
            def yolo_inference(image):
         
     | 
| 11 | 
         
            +
                import tempfile
         
     | 
| 12 | 
         
            +
                temp_dir = tempfile.TemporaryDirectory()
         
     | 
| 13 | 
         
            +
                temp_path = Path(temp_dir.name) / "input_image.jpg"
         
     | 
| 14 | 
         
            +
                output_path = Path(temp_dir.name) / "output_image.jpg"
         
     | 
| 15 | 
         
            +
                image.save(temp_path)
         
     | 
| 16 | 
         
            +
             
     | 
| 17 | 
         
            +
                run(weights=ROOT / weights_path, source=str(temp_path), project=str(temp_dir.name), name='result', exist_ok=True)
         
     | 
| 18 | 
         
            +
                
         
     | 
| 19 | 
         
            +
                result_image_path = list((Path(temp_dir.name) / 'result').glob('*.jpg'))[0]
         
     | 
| 20 | 
         
            +
                result_image = Image.open(result_image_path)
         
     | 
| 21 | 
         
            +
                
         
     | 
| 22 | 
         
            +
                return result_image
         
     | 
| 23 | 
         
            +
             
     | 
| 24 | 
         
            +
            interface = gr.Interface(
         
     | 
| 25 | 
         
            +
                fn=yolo_inference,
         
     | 
| 26 | 
         
            +
                inputs=gr.Image(type="pil"),
         
     | 
| 27 | 
         
            +
                outputs=gr.Image(type="pil"),
         
     | 
| 28 | 
         
            +
                title="YOLO Object Detection",
         
     | 
| 29 | 
         
            +
                description="Upload an image to detect objects using YOLO."
         
     | 
| 30 | 
         
            +
            )
         
     | 
| 31 | 
         
            +
             
     | 
| 32 | 
         
            +
            interface.launch()
         
     |