Spaces:
Running
Running
Gabolozano
commited on
Commit
•
ceb95cf
1
Parent(s):
246f207
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,57 @@
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
from transformers import pipeline, DetrForObjectDetection, DetrConfig, DetrImageProcessor
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
|
|
9 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", config=config)
|
10 |
-
|
11 |
-
# Initialize the image processor for DETR
|
12 |
image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
13 |
-
|
14 |
-
# Initialize the object detection pipeline with the model and image processor
|
15 |
od_pipe = pipeline(task='object-detection', model=model, image_processor=image_processor)
|
16 |
|
17 |
def get_pipeline_prediction(pil_image):
|
18 |
-
# Run the object detection pipeline
|
19 |
pipeline_output = od_pipe(pil_image)
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
#
|
25 |
-
return pipeline_output
|
26 |
|
27 |
demo = gr.Interface(
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
32 |
)
|
33 |
|
34 |
-
demo.launch()
|
|
|
1 |
+
python
|
2 |
import os
|
3 |
import gradio as gr
|
4 |
from transformers import pipeline, DetrForObjectDetection, DetrConfig, DetrImageProcessor
|
5 |
+
import numpy as np
|
6 |
+
import cv2
|
7 |
+
from PIL import Image
|
8 |
|
9 |
+
def draw_detections(image, detections):
|
10 |
+
# Convert PIL image to a numpy array
|
11 |
+
np_image = np.array(image)
|
12 |
+
|
13 |
+
# Convert RGB to BGR for OpenCV
|
14 |
+
np_image = cv2.cvtColor(np_image, cv2.COLOR_RGB2BGR)
|
15 |
+
|
16 |
+
for detection in detections:
|
17 |
+
# Each detection includes ['score', 'label', 'box']
|
18 |
+
score = detection['score']
|
19 |
+
label = detection['label']
|
20 |
+
box = detection['box']
|
21 |
+
x_min, y_min, x_max, y_max = map(int, box)
|
22 |
+
cv2.rectangle(np_image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
|
23 |
+
cv2.putText(np_image, f'{label} {score:.2f}', (x_min, max(y_min - 10, 0)),
|
24 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
|
25 |
+
|
26 |
+
# Convert BGR to RGB for displaying
|
27 |
+
final_image = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
|
28 |
+
# Convert the numpy array to PIL Image
|
29 |
+
final_pil_image = Image.fromarray(final_image)
|
30 |
+
return final_pil_image
|
31 |
|
32 |
+
# Initialize objects from transformers
|
33 |
+
config = DetrConfig.from_pretrained("facebook/detr-resnet-50")
|
34 |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", config=config)
|
|
|
|
|
35 |
image_processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
|
|
|
|
36 |
od_pipe = pipeline(task='object-detection', model=model, image_processor=image_processor)
|
37 |
|
38 |
def get_pipeline_prediction(pil_image):
|
39 |
+
# Run the object detection pipeline
|
40 |
pipeline_output = od_pipe(pil_image)
|
41 |
|
42 |
+
# Draw the detection results on the image
|
43 |
+
processed_image = draw_detections(pil_image, pipeline_output)
|
44 |
+
|
45 |
+
# Provide both the image and the JSON detection results
|
46 |
+
return processed_image, pipeline_output
|
47 |
|
48 |
demo = gr.Interface(
|
49 |
+
fn=get_pipeline_prediction,
|
50 |
+
inputs=gr.Image(label="Input image", type="pil"),
|
51 |
+
outputs=[
|
52 |
+
gr.Image(label="Annotated Image"),
|
53 |
+
gr.JSON(label="Detected Objects")
|
54 |
+
]
|
55 |
)
|
56 |
|
57 |
+
demo.launch()
|