Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import requests
|
| 4 |
+
from transformers import DetrForObjectDetection, DetrFeatureExtractor
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# 모델과 피처 추출기 로드
|
| 8 |
+
model_id = "facebook/detr-resnet-50"
|
| 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 |
+
# 모델을 사용한 예측
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
+
|
| 18 |
+
# 예측 결과 처리
|
| 19 |
+
# outputs는 logits와 기타 정보를 포함하므로, 결과를 해석하여 사용자에게 보여줄 형태로 변환
|
| 20 |
+
# 여기서는 간단하게 처리합니다. 실제로는 결과를 이미지 위에 박스로 그리는 등의 처리가 필요할 수 있습니다.
|
| 21 |
+
result = feature_extractor.post_process_object_detection(outputs, threshold=0.9)[0]
|
| 22 |
+
return result
|
| 23 |
+
|
| 24 |
+
# Gradio 앱 정의
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=object_detection,
|
| 27 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 28 |
+
outputs=[gr.outputs.Image(type="pil"), gr.outputs.Label(num_top_classes=3)],
|
| 29 |
+
title="Object Detection with DETR",
|
| 30 |
+
description="Upload an image and the model will detect objects in the image."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
iface.launch()
|