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()