Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	
		Darius Morawiec
		
	commited on
		
		
					Commit 
							
							·
						
						f70ac2a
	
1
								Parent(s):
							
							16a114d
								
Add examples
Browse files
    	
        app.py
    CHANGED
    
    | @@ -35,7 +35,7 @@ class Detector: | |
| 35 | 
             
                    detections = []
         | 
| 36 | 
             
                    result = results[0]
         | 
| 37 | 
             
                    for box, score, labels in zip(
         | 
| 38 | 
            -
                        result["boxes"], result["scores"], result[" | 
| 39 | 
             
                    ):
         | 
| 40 | 
             
                        box = [round(x, 2) for x in box.tolist()]
         | 
| 41 | 
             
                        detections.append(
         | 
| @@ -70,6 +70,7 @@ def detect_objects(image, labels, confidence_threshold): | |
| 70 |  | 
| 71 | 
             
                detections = []
         | 
| 72 | 
             
                for model_name in models.keys():
         | 
|  | |
| 73 | 
             
                    detection = models[model_name].detect(
         | 
| 74 | 
             
                        image,
         | 
| 75 | 
             
                        labels,
         | 
| @@ -80,16 +81,14 @@ def detect_objects(image, labels, confidence_threshold): | |
| 80 | 
             
                return tuple((image, det) for det in detections)
         | 
| 81 |  | 
| 82 |  | 
| 83 | 
            -
            with gr.Blocks() as demo:
         | 
| 84 | 
             
                gr.Markdown("# [LLMDet](https://arxiv.org/abs/2501.18954) Arena ✨")
         | 
| 85 |  | 
| 86 | 
             
                with gr.Row():
         | 
| 87 | 
             
                    with gr.Column():
         | 
| 88 | 
             
                        gr.Markdown("## Input Image")
         | 
| 89 |  | 
| 90 | 
            -
                         | 
| 91 | 
            -
                        image = load_image(image_url)
         | 
| 92 | 
            -
                        image_input = gr.Image(type="pil", image_mode="RGB", value=image)
         | 
| 93 |  | 
| 94 | 
             
                    with gr.Column():
         | 
| 95 | 
             
                        gr.Markdown("## Settings")
         | 
| @@ -109,19 +108,18 @@ with gr.Blocks() as demo: | |
| 109 | 
             
                            label="Object labels (comma separated):",
         | 
| 110 | 
             
                            placeholder=",".join(labels),
         | 
| 111 | 
             
                            lines=1,
         | 
| 112 | 
            -
                            value=",".join(labels),
         | 
| 113 | 
             
                        )
         | 
| 114 |  | 
| 115 | 
             
                with gr.Row():
         | 
| 116 | 
            -
                    detect_button = gr.Button(" | 
| 117 |  | 
| 118 | 
             
                with gr.Row():
         | 
| 119 | 
             
                    gr.Markdown("## Output Annotated Images")
         | 
| 120 |  | 
| 121 | 
             
                with gr.Row():
         | 
| 122 | 
            -
                    output_annotated_image_tiny = gr.AnnotatedImage(label="TINY")
         | 
| 123 | 
            -
                    output_annotated_image_base = gr.AnnotatedImage(label="BASE")
         | 
| 124 | 
            -
                    output_annotated_image_large = gr.AnnotatedImage(label="LARGE")
         | 
| 125 |  | 
| 126 | 
             
                # Connect the button to the detection function
         | 
| 127 | 
             
                detect_button.click(
         | 
| @@ -134,5 +132,38 @@ with gr.Blocks() as demo: | |
| 134 | 
             
                    ],
         | 
| 135 | 
             
                )
         | 
| 136 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 137 | 
             
            if __name__ == "__main__":
         | 
| 138 | 
             
                demo.launch()
         | 
|  | |
| 35 | 
             
                    detections = []
         | 
| 36 | 
             
                    result = results[0]
         | 
| 37 | 
             
                    for box, score, labels in zip(
         | 
| 38 | 
            +
                        result["boxes"], result["scores"], result["text_labels"]
         | 
| 39 | 
             
                    ):
         | 
| 40 | 
             
                        box = [round(x, 2) for x in box.tolist()]
         | 
| 41 | 
             
                        detections.append(
         | 
|  | |
| 70 |  | 
| 71 | 
             
                detections = []
         | 
| 72 | 
             
                for model_name in models.keys():
         | 
| 73 | 
            +
                    print(f"Running detection with model: {model_name}")
         | 
| 74 | 
             
                    detection = models[model_name].detect(
         | 
| 75 | 
             
                        image,
         | 
| 76 | 
             
                        labels,
         | 
|  | |
| 81 | 
             
                return tuple((image, det) for det in detections)
         | 
| 82 |  | 
| 83 |  | 
| 84 | 
            +
            with gr.Blocks(delete_cache=(5, 10)) as demo:
         | 
| 85 | 
             
                gr.Markdown("# [LLMDet](https://arxiv.org/abs/2501.18954) Arena ✨")
         | 
| 86 |  | 
| 87 | 
             
                with gr.Row():
         | 
| 88 | 
             
                    with gr.Column():
         | 
| 89 | 
             
                        gr.Markdown("## Input Image")
         | 
| 90 |  | 
| 91 | 
            +
                        image_input = gr.Image(type="pil", image_mode="RGB", format="jpeg")
         | 
|  | |
|  | |
| 92 |  | 
| 93 | 
             
                    with gr.Column():
         | 
| 94 | 
             
                        gr.Markdown("## Settings")
         | 
|  | |
| 108 | 
             
                            label="Object labels (comma separated):",
         | 
| 109 | 
             
                            placeholder=",".join(labels),
         | 
| 110 | 
             
                            lines=1,
         | 
|  | |
| 111 | 
             
                        )
         | 
| 112 |  | 
| 113 | 
             
                with gr.Row():
         | 
| 114 | 
            +
                    detect_button = gr.Button("Detect Objects")
         | 
| 115 |  | 
| 116 | 
             
                with gr.Row():
         | 
| 117 | 
             
                    gr.Markdown("## Output Annotated Images")
         | 
| 118 |  | 
| 119 | 
             
                with gr.Row():
         | 
| 120 | 
            +
                    output_annotated_image_tiny = gr.AnnotatedImage(label="TINY", format="jpeg")
         | 
| 121 | 
            +
                    output_annotated_image_base = gr.AnnotatedImage(label="BASE", format="jpeg")
         | 
| 122 | 
            +
                    output_annotated_image_large = gr.AnnotatedImage(label="LARGE", format="jpeg")
         | 
| 123 |  | 
| 124 | 
             
                # Connect the button to the detection function
         | 
| 125 | 
             
                detect_button.click(
         | 
|  | |
| 132 | 
             
                    ],
         | 
| 133 | 
             
                )
         | 
| 134 |  | 
| 135 | 
            +
                with gr.Row():
         | 
| 136 | 
            +
                    gr.Markdown("## Examples")
         | 
| 137 | 
            +
             | 
| 138 | 
            +
                with gr.Row():
         | 
| 139 | 
            +
                    gr.Examples(
         | 
| 140 | 
            +
                        examples=[
         | 
| 141 | 
            +
                            [
         | 
| 142 | 
            +
                                "http://images.cocodataset.org/val2017/000000039769.jpg",
         | 
| 143 | 
            +
                                "a cat, a remote control",
         | 
| 144 | 
            +
                                0.3,
         | 
| 145 | 
            +
                            ],
         | 
| 146 | 
            +
                            [
         | 
| 147 | 
            +
                                "http://images.cocodataset.org/val2017/000000370486.jpg",
         | 
| 148 | 
            +
                                "a person",
         | 
| 149 | 
            +
                                0.3,
         | 
| 150 | 
            +
                            ],
         | 
| 151 | 
            +
                            [
         | 
| 152 | 
            +
                                "http://images.cocodataset.org/train2017/000000345263.jpg",
         | 
| 153 | 
            +
                                "a red apple, a green apple",
         | 
| 154 | 
            +
                                0.3,
         | 
| 155 | 
            +
                            ],
         | 
| 156 | 
            +
                        ],
         | 
| 157 | 
            +
                        inputs=[image_input, text_input, confidence_slider],
         | 
| 158 | 
            +
                        outputs=[
         | 
| 159 | 
            +
                            output_annotated_image_tiny,
         | 
| 160 | 
            +
                            output_annotated_image_base,
         | 
| 161 | 
            +
                            output_annotated_image_large,
         | 
| 162 | 
            +
                        ],
         | 
| 163 | 
            +
                        fn=detect_objects,
         | 
| 164 | 
            +
                        cache_examples=True,
         | 
| 165 | 
            +
                    )
         | 
| 166 | 
            +
             | 
| 167 | 
            +
             | 
| 168 | 
             
            if __name__ == "__main__":
         | 
| 169 | 
             
                demo.launch()
         | 
