File size: 2,030 Bytes
91be02a
d4c3acc
 
246f207
ceb95cf
 
 
c21a752
ceb95cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6564a3a
ceb95cf
 
d4c3acc
246f207
 
dba19bd
d4c3acc
ceb95cf
d4c3acc
 
ceb95cf
 
 
 
 
c21a752
d4c3acc
ceb95cf
 
 
 
 
 
d4c3acc
 
ceb95cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

import os
import gradio as gr
from transformers import pipeline, DetrForObjectDetection, DetrConfig, DetrImageProcessor
import numpy as np
import cv2
from PIL import Image

def draw_detections(image, detections):
    # Convert PIL image to a numpy array
    np_image = np.array(image)
    
    # Convert RGB to BGR for OpenCV
    np_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR)
    
    for detection in detections:
        # Each detection includes ['score', 'label', 'box']
        score = detection['score']
        label = detection['label']
        box = detection['box']
        x_min, y_min, x_max, y_max = map(int, box)
        cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
        cv2.putText(np_image, f'{label} {score:.2f}', (x_min, max(y_min - 10, 0)),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
    
    # Convert BGR to RGB for displaying
    final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
    # Convert the numpy array to PIL Image
    final_pil_image = Image.fromarray(final_image)
    return final_pil_image

# Initialize objects from transformers
config = DetrConfig.from_pretrained("facebook/detr-resnet-50")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", config=config)
image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
od_pipe = pipeline(task='object-detection', model=model, image_processor=image_processor)

def get_pipeline_prediction(pil_image):
    # Run the object detection pipeline
    pipeline_output = od_pipe(pil_image)
    
    # Draw the detection results on the image
    processed_image = draw_detections(pil_image, pipeline_output)
    
    # Provide both the image and the JSON detection results
    return processed_image, pipeline_output

demo = gr.Interface(
    fn=get_pipeline_prediction,
    inputs=gr.Image(label="Input image", type="pil"),
    outputs=[
        gr.Image(label="Annotated Image"),
        gr.JSON(label="Detected Objects")
    ]
)

demo.launch()