File size: 1,600 Bytes
ba20d12
 
262dac0
8a80b35
ba20d12
 
 
 
 
 
5b2e04b
ba20d12
 
 
5b2e04b
262dac0
 
 
 
 
5b2e04b
 
 
 
 
 
262dac0
 
 
31e217b
ba20d12
 
262dac0
 
ba20d12
262dac0
 
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
import gradio as gr
from transformers import DetrForObjectDetection, DetrFeatureExtractor
from PIL import Image, ImageDraw
import torch

model_id = "facebook/detr-resnet-50"
model = DetrForObjectDetection.from_pretrained(model_id)
feature_extractor = DetrFeatureExtractor.from_pretrained(model_id)

def object_detection(image):
    # 모델 입력을 위한 이미지 전처리
    inputs = feature_extractor(images=image, return_tensors="pt")
    outputs = model(**inputs)

    # 결과 후처리
    target_sizes = torch.tensor([image.size[::-1]])
    results = feature_extractor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]

    draw = ImageDraw.Draw(image)
    for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
        # DETR은 정규화된 좌표를 반환합니다. 이미지 크기로 스케일 조정
        box = box.tolist()
        box = [round(i, 2) for i in box]
        # 이미지에 바운딩 박스 그리기
        draw.rectangle([(box[0], box[1]), (box[2], box[3])], outline="red", width=3)
        # 바운딩 박스 옆에 클래스 라벨과 스코어 표시
        draw.text((box[0], box[1]), f"{model.config.id2label[label.item()]}: {round(score.item(), 3)}", fill="red")

    return image
 
iface = gr.Interface(
    fn=object_detection,
    inputs=gr.Image(type="pil"),
    outputs=gr.Image(type="pil"),
    title="Object Detection with DETR",
    description="Upload an image, and the model will detect objects in the image. Detected objects will be highlighted with bounding boxes."
).launch()