Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,78 +1,65 @@
|
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
from PIL import Image, ImageDraw
|
| 3 |
-
import requests
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
|
| 6 |
detector = pipeline(
|
| 7 |
"object-detection",
|
| 8 |
model="facebook/detr-resnet-50"
|
| 9 |
)
|
| 10 |
|
| 11 |
-
# 객체별 색상 지정
|
| 12 |
colors = [
|
| 13 |
"#FF0000", "#00FF00", "#0000FF", "#FF00FF",
|
| 14 |
"#00FFFF", "#FFA500", "#800080", "#008000"
|
| 15 |
]
|
| 16 |
|
| 17 |
def find_obj(image):
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
draw = ImageDraw.Draw(draw_image)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
draw.rectangle(
|
| 42 |
-
[box["xmin"], box["ymin"] - 20, box["xmin"] + len(text) * 7, box["ymin"]],
|
| 43 |
-
fill=color
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
# 라벨 텍스트
|
| 47 |
-
draw.text(
|
| 48 |
-
(box["xmin"] + 2, box["ymin"] - 18),
|
| 49 |
-
text,
|
| 50 |
-
fill="white"
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
summary = f"총 {len(results)}개 객체 탐지됨\n\n"
|
| 54 |
-
for item in results:
|
| 55 |
summary += f"· {item['label']} ({round(item['score']*100, 1)}%)\n"
|
| 56 |
-
return draw_image, summary # ← PIL.Image 그대로 반환하면 Gradio가 알아서 표시
|
| 57 |
-
|
| 58 |
|
|
|
|
| 59 |
|
| 60 |
demo = gr.Interface(
|
| 61 |
fn=find_obj,
|
| 62 |
inputs=gr.Image(
|
| 63 |
-
type="pil",
|
| 64 |
label="이미지 업로드"
|
| 65 |
),
|
| 66 |
outputs=[
|
| 67 |
-
gr.Image(label="탐지 결과"),
|
| 68 |
gr.Textbox(label="탐지 목록", lines=10)
|
| 69 |
],
|
| 70 |
title="🔍 객체 탐지기",
|
| 71 |
description="이미지를 업로드하면 객체를 자동으로 탐지합니다.",
|
| 72 |
-
examples=[
|
| 73 |
-
["http://images.cocodataset.org/val2017/000000039769.jpg"]
|
| 74 |
-
]
|
| 75 |
)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
demo.launch(share=True)
|
|
|
|
| 1 |
+
import gradio as gr # ← 추가!
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image, ImageDraw
|
|
|
|
|
|
|
| 4 |
|
| 5 |
detector = pipeline(
|
| 6 |
"object-detection",
|
| 7 |
model="facebook/detr-resnet-50"
|
| 8 |
)
|
| 9 |
|
|
|
|
| 10 |
colors = [
|
| 11 |
"#FF0000", "#00FF00", "#0000FF", "#FF00FF",
|
| 12 |
"#00FFFF", "#FFA500", "#800080", "#008000"
|
| 13 |
]
|
| 14 |
|
| 15 |
def find_obj(image):
|
| 16 |
+
results = detector(image)
|
| 17 |
|
| 18 |
+
draw_image = image.copy()
|
| 19 |
+
draw = ImageDraw.Draw(draw_image)
|
|
|
|
| 20 |
|
| 21 |
+
for i, item in enumerate(results):
|
| 22 |
+
label = item["label"]
|
| 23 |
+
score = round(item["score"] * 100, 1)
|
| 24 |
+
box = item["box"]
|
| 25 |
+
color = colors[i % len(colors)]
|
| 26 |
|
| 27 |
+
draw.rectangle(
|
| 28 |
+
[box["xmin"], box["ymin"], box["xmax"], box["ymax"]],
|
| 29 |
+
outline=color,
|
| 30 |
+
width=3
|
| 31 |
+
)
|
| 32 |
|
| 33 |
+
text = f"{label} {score}%"
|
| 34 |
+
draw.rectangle(
|
| 35 |
+
[box["xmin"], box["ymin"] - 20, box["xmin"] + len(text) * 7, box["ymin"]],
|
| 36 |
+
fill=color
|
| 37 |
+
)
|
| 38 |
|
| 39 |
+
draw.text(
|
| 40 |
+
(box["xmin"] + 2, box["ymin"] - 18),
|
| 41 |
+
text,
|
| 42 |
+
fill="white"
|
| 43 |
+
)
|
|
|
|
| 44 |
|
| 45 |
+
summary = f"총 {len(results)}개 객체 탐지됨\n\n"
|
| 46 |
+
for item in results:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
summary += f"· {item['label']} ({round(item['score']*100, 1)}%)\n"
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
return draw_image, summary
|
| 50 |
|
| 51 |
demo = gr.Interface(
|
| 52 |
fn=find_obj,
|
| 53 |
inputs=gr.Image(
|
| 54 |
+
type="pil",
|
| 55 |
label="이미지 업로드"
|
| 56 |
),
|
| 57 |
outputs=[
|
| 58 |
+
gr.Image(label="탐지 결과"),
|
| 59 |
gr.Textbox(label="탐지 목록", lines=10)
|
| 60 |
],
|
| 61 |
title="🔍 객체 탐지기",
|
| 62 |
description="이미지를 업로드하면 객체를 자동으로 탐지합니다.",
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
+
demo.launch() # ← share=True 제거
|
|
|