Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import DetrForObjectDetection, DetrFeatureExtractor
|
|
|
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# ๋ชจ๋ธ ID ์ง์
|
| 6 |
model_id = "facebook/detr-resnet-50"
|
| 7 |
-
|
| 8 |
-
# ๋ชจ๋ธ๊ณผ ํผ์ฒ ์ถ์ถ๊ธฐ ๋ก๋
|
| 9 |
model = DetrForObjectDetection.from_pretrained(model_id)
|
| 10 |
feature_extractor = DetrFeatureExtractor.from_pretrained(model_id)
|
| 11 |
|
| 12 |
def object_detection(image):
|
| 13 |
-
# ์ด๋ฏธ์ง๋ฅผ ๋ชจ๋ธ ์
๋ ฅ์ผ๋ก ๋ณํ
|
| 14 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 15 |
outputs = model(**inputs)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# Gradio ์ธํฐํ์ด์ค ์ ์ ๋ฐ ์คํ
|
| 23 |
iface = gr.Interface(
|
| 24 |
fn=object_detection,
|
| 25 |
-
inputs=gr.Image(),
|
| 26 |
-
outputs="
|
| 27 |
title="Object Detection with DETR",
|
| 28 |
-
description="Upload an image and the model will detect objects in the image."
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import DetrForObjectDetection, DetrFeatureExtractor
|
| 3 |
+
from PIL import Image, ImageDraw
|
| 4 |
import torch
|
| 5 |
|
|
|
|
| 6 |
model_id = "facebook/detr-resnet-50"
|
|
|
|
|
|
|
| 7 |
model = DetrForObjectDetection.from_pretrained(model_id)
|
| 8 |
feature_extractor = DetrFeatureExtractor.from_pretrained(model_id)
|
| 9 |
|
| 10 |
def object_detection(image):
|
|
|
|
| 11 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 12 |
outputs = model(**inputs)
|
| 13 |
|
| 14 |
+
# ํ์ฒ๋ฆฌ ๊ณผ์
|
| 15 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 16 |
+
results = feature_extractor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
|
| 17 |
+
|
| 18 |
+
draw = ImageDraw.Draw(image)
|
| 19 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 20 |
+
box = [round(i, 2) for i in box.tolist()]
|
| 21 |
+
draw.rectangle(box, outline="red", width=3)
|
| 22 |
+
draw.text((box[0], box[1]), f"{model.config.id2label[label.item()]}: {round(score.item(), 3)}", fill="red")
|
| 23 |
+
|
| 24 |
+
return image
|
| 25 |
|
|
|
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=object_detection,
|
| 28 |
+
inputs=gr.Image(type="pil"),
|
| 29 |
+
outputs=gr.Image(type="pil"),
|
| 30 |
title="Object Detection with DETR",
|
| 31 |
+
description="Upload an image, and the model will detect objects in the image. Detected objects will be highlighted with bounding boxes."
|
| 32 |
+
).launch()
|
|
|
|
|
|