Set shrimp_detection_app as main app (app.py)
Browse files- Backup original app.py to app_backup.py
- Copy shrimp_detection_app.py to app.py for deployment
- Main app now includes YOLOv8 model, VIDraft/Shrimp branding, and full features
๐ค Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- app.py +1055 -204
- app_backup.py +299 -0
app.py
CHANGED
|
@@ -1,299 +1,1150 @@
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
"""
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
"""
|
| 6 |
import sys
|
| 7 |
sys.stdout.reconfigure(encoding='utf-8')
|
| 8 |
|
| 9 |
import gradio as gr
|
| 10 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
| 11 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
api_key="azcIL8KDJVJMYrsERzI7"
|
| 20 |
-
)
|
| 21 |
-
VIDRAFT_AVAILABLE = True
|
| 22 |
-
print("โ
VIDraft/Shrimp ๋ชจ๋ธ ์ฌ์ฉ ๊ฐ๋ฅ")
|
| 23 |
-
except Exception as e:
|
| 24 |
-
VIDRAFT_AVAILABLE = False
|
| 25 |
-
print(f"โ VIDraft/Shrimp ๋ชจ๋ธ ์ฌ์ฉ ๋ถ๊ฐ: {e}")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
# API ํธ์ถ with configuration
|
| 45 |
-
custom_config = InferenceConfiguration(
|
| 46 |
-
confidence_threshold=confidence,
|
| 47 |
-
iou_threshold=iou_threshold
|
| 48 |
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
draw = ImageDraw.Draw(img)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
for pred in predictions:
|
| 69 |
-
|
|
|
|
|
|
|
| 70 |
continue
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
else:
|
| 92 |
-
color = "yellow"
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
bbox = draw.textbbox((x1, y1 - 25), label, font=font)
|
| 100 |
-
draw.rectangle(bbox, fill=color)
|
| 101 |
-
draw.text((x1, y1 - 25), label, fill="black", font=font)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
def
|
| 125 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
if image is None:
|
| 127 |
return None, "โ ๏ธ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์."
|
| 128 |
|
| 129 |
try:
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
#
|
| 134 |
-
if not
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
# ๊ฒฐ๊ณผ ๊ทธ๋ฆฌ๊ธฐ
|
| 159 |
img = image.copy()
|
| 160 |
draw = ImageDraw.Draw(img)
|
| 161 |
|
| 162 |
try:
|
| 163 |
font = ImageFont.truetype("arial.ttf", 14)
|
|
|
|
|
|
|
| 164 |
except:
|
| 165 |
font = ImageFont.load_default()
|
|
|
|
|
|
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
x1, y1, x2, y2 =
|
| 171 |
-
conf = score.item()
|
| 172 |
|
| 173 |
-
#
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
color = "yellow"
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
# ๋ฐ์ค
|
| 182 |
-
draw.rectangle([x1, y1, x2, y2], outline=color, width=
|
| 183 |
|
| 184 |
# ๋ผ๋ฒจ
|
| 185 |
-
|
| 186 |
-
bbox = draw.textbbox((x1, y1 - 25),
|
| 187 |
draw.rectangle(bbox, fill=color)
|
| 188 |
-
draw.text((x1, y1 - 25),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
# ํค๋
|
| 191 |
-
header = f"
|
| 192 |
-
header_bbox = draw.textbbox((10, 10), header, font=
|
| 193 |
-
draw.rectangle([5, 5, header_bbox[2]+10, header_bbox[3]+10],
|
| 194 |
-
|
|
|
|
| 195 |
|
|
|
|
| 196 |
info = f"""
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
-
|
| 200 |
-
- **์ ๋ขฐ๋ ์๊ณ๊ฐ**: {confidence:.0%}
|
| 201 |
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
return img, info
|
| 206 |
|
| 207 |
except Exception as e:
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
-
#
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
gr.Markdown("""
|
| 214 |
-
# ๐ฆ
|
| 215 |
|
| 216 |
-
|
| 217 |
|
| 218 |
---
|
| 219 |
""")
|
| 220 |
|
|
|
|
| 221 |
with gr.Row():
|
| 222 |
-
with gr.Column():
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
label="
|
| 227 |
-
info="
|
| 228 |
)
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
)
|
| 234 |
|
| 235 |
-
#
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
| 242 |
)
|
| 243 |
|
| 244 |
-
with gr.Column():
|
| 245 |
-
gr.Markdown("### ๐ ์ฌ์ฉ ๋ฐฉ๋ฒ")
|
| 246 |
gr.Markdown("""
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
**์์ ์๋ฏธ:**
|
| 254 |
-
- **๋
น์/์ฒญ๋ก**: ๋์ ์ ๋ขฐ๋ (>80%)
|
| 255 |
-
- **์ฃผํฉ/์ํ**: ์ค๊ฐ ์ ๋ขฐ๋ (60-80%)
|
| 256 |
-
- **๋
ธ๋์**: ๋ฎ์ ์ ๋ขฐ๋ (<60%)
|
| 257 |
""")
|
| 258 |
|
| 259 |
-
|
| 260 |
-
with gr.TabItem("
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
with gr.TabItem("๐ RT-DETR (๋ฒ์ฉ)"):
|
| 266 |
-
rtdetr_btn = gr.Button("๐ RT-DETR๋ก ๊ฒ์ถ", variant="secondary", size="lg")
|
| 267 |
-
rtdetr_result = gr.Image(label="๊ฒ์ถ ๊ฒฐ๊ณผ")
|
| 268 |
-
rtdetr_info = gr.Markdown()
|
| 269 |
-
|
| 270 |
-
# ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
| 271 |
-
vidraft_btn.click(
|
| 272 |
-
detect_with_vidraft,
|
| 273 |
-
[input_image, confidence_slider, iou_slider],
|
| 274 |
-
[vidraft_result, vidraft_info]
|
| 275 |
-
)
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
gr.Markdown("""
|
| 284 |
---
|
| 285 |
|
| 286 |
-
###
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
- **์ค๋ณต ๋ฐ์ค**: IoU ์๊ณ๊ฐ์ ์กฐ์ ํ์ธ์ (VIDraft/Shrimp ๋ชจ๋ธ๋ง)
|
| 292 |
""")
|
| 293 |
|
| 294 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
demo.launch(
|
| 296 |
server_name="0.0.0.0",
|
| 297 |
-
server_port=
|
| 298 |
share=False
|
| 299 |
)
|
|
|
|
| 1 |
# -*- coding: utf-8 -*-
|
| 2 |
"""
|
| 3 |
+
๐ฆ ์์ฐ ๊ฒ์ถ ํตํฉ ์์คํ
|
| 4 |
+
3๊ฐ์ ์ฑ์ ํ๋๋ก ํตํฉ: ์๋ ๊ฒ์ถ, ๋ผ๋ฒจ๋ง ๋๊ตฌ, ๋ฐ๋ชจ
|
| 5 |
+
RT-DETR ๋๋ VIDraft/Shrimp ํด๋ผ์ฐ๋ ๋ชจ๋ธ ์ ํ ๊ฐ๋ฅ
|
| 6 |
"""
|
| 7 |
import sys
|
| 8 |
sys.stdout.reconfigure(encoding='utf-8')
|
| 9 |
|
| 10 |
import gradio as gr
|
| 11 |
from PIL import Image, ImageDraw, ImageFont
|
| 12 |
+
import numpy as np
|
| 13 |
+
import json
|
| 14 |
import os
|
| 15 |
+
import glob
|
| 16 |
+
from datetime import datetime
|
| 17 |
+
import torch
|
| 18 |
+
from transformers import RTDetrForObjectDetection, RTDetrImageProcessor
|
| 19 |
+
import requests
|
| 20 |
+
import base64
|
| 21 |
+
from io import BytesIO
|
| 22 |
+
from inference_sdk import InferenceHTTPClient
|
| 23 |
+
import tempfile
|
| 24 |
|
| 25 |
+
# test_visual_validation์์ ๊ฐ์ ธ์ค๊ธฐ (์ง์ฐ import๋ก ๋ณ๊ฒฝ)
|
| 26 |
+
# from test_visual_validation import (
|
| 27 |
+
# load_rtdetr_model,
|
| 28 |
+
# detect_with_rtdetr,
|
| 29 |
+
# apply_universal_filter,
|
| 30 |
+
# calculate_morphological_features,
|
| 31 |
+
# calculate_visual_features
|
| 32 |
+
# )
|
| 33 |
|
| 34 |
+
# YOLOv8 import
|
| 35 |
+
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# ============================================================
|
| 38 |
+
# YOLOv8 ๋ชจ๋ธ ์ค์
|
| 39 |
+
# ============================================================
|
| 40 |
+
YOLO_MODEL_PATH = "runs/train/yolov8m_shrimp2/weights/best.pt"
|
| 41 |
+
yolo_model = None
|
| 42 |
|
| 43 |
+
def load_yolo_model():
|
| 44 |
+
"""YOLOv8 ๋ชจ๋ธ ๋ก๋ฉ"""
|
| 45 |
+
global yolo_model
|
| 46 |
+
if yolo_model is None:
|
| 47 |
+
print(f"๐ YOLOv8 ๋ชจ๋ธ ๋ก๋ฉ ์ค: {YOLO_MODEL_PATH}")
|
| 48 |
+
yolo_model = YOLO(YOLO_MODEL_PATH)
|
| 49 |
+
print("โ
YOLOv8 ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ")
|
| 50 |
+
return yolo_model
|
| 51 |
|
| 52 |
+
def detect_with_yolo(image, confidence=0.1):
|
| 53 |
+
"""YOLOv8 ๋ชจ๋ธ๋ก ๊ฒ์ถ"""
|
| 54 |
try:
|
| 55 |
+
model = load_yolo_model()
|
| 56 |
+
|
| 57 |
+
# ์ถ๋ก ์คํ
|
| 58 |
+
results = model.predict(
|
| 59 |
+
source=image,
|
| 60 |
+
conf=confidence,
|
| 61 |
+
verbose=False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
+
detections = []
|
| 65 |
+
for result in results:
|
| 66 |
+
boxes = result.boxes
|
| 67 |
+
for box in boxes:
|
| 68 |
+
x1, y1, x2, y2 = box.xyxy[0].tolist()
|
| 69 |
+
conf = box.conf[0].item()
|
| 70 |
|
| 71 |
+
detections.append({
|
| 72 |
+
'bbox': [x1, y1, x2, y2],
|
| 73 |
+
'confidence': conf
|
| 74 |
+
})
|
| 75 |
|
| 76 |
+
print(f"โ
YOLOv8 ๊ฒ์ถ ์๋ฃ: {len(detections)}๊ฐ")
|
| 77 |
+
return detections
|
|
|
|
| 78 |
|
| 79 |
+
except Exception as e:
|
| 80 |
+
print(f"โ YOLOv8 ๊ฒ์ถ ์ค๋ฅ: {str(e)}")
|
| 81 |
+
import traceback
|
| 82 |
+
traceback.print_exc()
|
| 83 |
+
return []
|
| 84 |
+
|
| 85 |
+
# ============================================================
|
| 86 |
+
# Roboflow SDK ์ค์ (์ต์ ํ๋ ๋ฐฉ์)
|
| 87 |
+
# ============================================================
|
| 88 |
+
ROBOFLOW_API_KEY = "azcIL8KDJVJMYrsERzI7"
|
| 89 |
+
|
| 90 |
+
# Roboflow Inference SDK ํด๋ผ์ด์ธํธ (connection pooling ์ง์)
|
| 91 |
+
roboflow_client = InferenceHTTPClient(
|
| 92 |
+
api_url="https://serverless.roboflow.com",
|
| 93 |
+
api_key=ROBOFLOW_API_KEY
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
def detect_with_roboflow(image, confidence=0.065):
|
| 97 |
+
"""Roboflow API๋ฅผ ์ฌ์ฉํ ์ต์ ํ๋ ๊ฒ์ถ (๋ก์ปฌ ํ
์คํธ์ ๋์ผ)"""
|
| 98 |
+
try:
|
| 99 |
+
# ์๋ณธ ์ด๋ฏธ์ง ๋ณด์กด
|
| 100 |
+
image_original = image
|
| 101 |
+
original_size = image_original.size
|
| 102 |
+
|
| 103 |
+
# ๋ฆฌ์ฌ์ด์ฆ (API ์ ์ก์ฉ)
|
| 104 |
+
image_resized = image_original.copy()
|
| 105 |
+
image_resized.thumbnail((640, 640), Image.Resampling.LANCZOS)
|
| 106 |
+
print(f"๐ ์ด๋ฏธ์ง ๋ฆฌ์ฌ์ด์ฆ: {original_size} โ {image_resized.size}")
|
| 107 |
|
| 108 |
+
# Base64 ์ธ์ฝ๋ฉ
|
| 109 |
+
buffered = BytesIO()
|
| 110 |
+
image_resized.save(buffered, format="JPEG", quality=80)
|
| 111 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
| 112 |
+
print(f"๐ฆ Base64 ํฌ๊ธฐ: {len(img_base64)} bytes")
|
| 113 |
+
|
| 114 |
+
print(f"๐ Roboflow API ์ถ๋ก ์์...")
|
| 115 |
+
|
| 116 |
+
# ๐ ์ต์ ํ 3: requests๋ก API ํธ์ถ (SDK ๋์ ์ฌ์ฉ - ๋ ์์ ์ )
|
| 117 |
+
response = requests.post(
|
| 118 |
+
'https://serverless.roboflow.com/vidraft/workflows/find-shrimp-6',
|
| 119 |
+
headers={'Content-Type': 'application/json'},
|
| 120 |
+
json={
|
| 121 |
+
'api_key': ROBOFLOW_API_KEY,
|
| 122 |
+
'inputs': {
|
| 123 |
+
'image': {'type': 'base64', 'value': img_base64}
|
| 124 |
+
}
|
| 125 |
+
},
|
| 126 |
+
timeout=30
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
if response.status_code != 200:
|
| 130 |
+
print(f"โ Roboflow API ์ค๋ฅ: {response.status_code}")
|
| 131 |
+
print(f"์๋ต: {response.text}")
|
| 132 |
+
return []
|
| 133 |
+
|
| 134 |
+
result = response.json()
|
| 135 |
+
print(f"๐ Roboflow ์๋ต: {json.dumps(result, indent=2, ensure_ascii=False)[:500]}...")
|
| 136 |
+
|
| 137 |
+
# Workflow ์๋ต ๊ตฌ์กฐ ํ์ฑ
|
| 138 |
+
detections = []
|
| 139 |
+
predictions = []
|
| 140 |
+
|
| 141 |
+
# ๋ฐฉ๋ฒ 1: outputs[0].predictions.predictions (workflow ํํ)
|
| 142 |
+
if isinstance(result, dict) and 'outputs' in result and len(result['outputs']) > 0:
|
| 143 |
+
output = result['outputs'][0]
|
| 144 |
+
if isinstance(output, dict) and 'predictions' in output:
|
| 145 |
+
pred_data = output['predictions']
|
| 146 |
+
# predictions๊ฐ dict์ด๊ณ ๊ทธ ์์ predictions ๋ฐฐ์ด์ด ์๋ ๊ฒฝ์ฐ
|
| 147 |
+
if isinstance(pred_data, dict) and 'predictions' in pred_data:
|
| 148 |
+
predictions = pred_data['predictions']
|
| 149 |
+
# predictions๊ฐ ๋ฐ๋ก ๋ฐฐ์ด์ธ ๊ฒฝ์ฐ
|
| 150 |
+
elif isinstance(pred_data, list):
|
| 151 |
+
predictions = pred_data
|
| 152 |
+
else:
|
| 153 |
+
predictions = [pred_data]
|
| 154 |
+
|
| 155 |
+
# ๋ฐฉ๋ฒ 2: ์ง์ predictions
|
| 156 |
+
elif isinstance(result, dict) and 'predictions' in result:
|
| 157 |
+
predictions = result['predictions']
|
| 158 |
+
|
| 159 |
+
# ๋ฐฉ๋ฒ 3: ๋ค๋ฅธ ๊ตฌ์กฐ
|
| 160 |
+
elif isinstance(result, list):
|
| 161 |
+
predictions = result
|
| 162 |
+
|
| 163 |
+
print(f"๐ฆ ์ฐพ์ predictions: {len(predictions)}๊ฐ")
|
| 164 |
+
|
| 165 |
+
# ์ค์ผ์ผ ๊ณ์ฐ (๋ฆฌ์ฌ์ด์ฆ๋ ์ขํ โ ์๋ณธ ์ขํ)
|
| 166 |
+
scale_x = original_size[0] / image_resized.size[0]
|
| 167 |
+
scale_y = original_size[1] / image_resized.size[1]
|
| 168 |
+
print(f"๐ ์ค์ผ์ผ: x={scale_x:.2f}, y={scale_y:.2f}")
|
| 169 |
|
| 170 |
for pred in predictions:
|
| 171 |
+
# ํด๋์ค ํํฐ๋ง (shrimp๋ง ๊ฒ์ถ)
|
| 172 |
+
pred_class = pred.get('class', '')
|
| 173 |
+
if pred_class != 'shrimp':
|
| 174 |
continue
|
| 175 |
|
| 176 |
+
# ์ ๋ขฐ๋ ํํฐ๋ง
|
| 177 |
+
pred_confidence = pred.get('confidence', 0)
|
| 178 |
+
if pred_confidence < confidence:
|
| 179 |
+
continue
|
| 180 |
|
| 181 |
+
# ๋ฐ์ด๋ฉ ๋ฐ์ค ์ถ์ถ (๋ฆฌ์ฌ์ด์ฆ๋ ์ขํ)
|
| 182 |
+
x = pred.get('x', 0)
|
| 183 |
+
y = pred.get('y', 0)
|
| 184 |
+
width = pred.get('width', 0)
|
| 185 |
+
height = pred.get('height', 0)
|
| 186 |
|
| 187 |
+
# ์๋ณธ ํฌ๊ธฐ๋ก ์ค์ผ์ผ ๋ณํ
|
| 188 |
+
x_scaled = x * scale_x
|
| 189 |
+
y_scaled = y * scale_y
|
| 190 |
+
width_scaled = width * scale_x
|
| 191 |
+
height_scaled = height * scale_y
|
| 192 |
|
| 193 |
+
# ์ค์ฌ์ ์ขํ๋ฅผ ์ข์๋จ/์ฐํ๋จ ์ขํ๋ก ๋ณํ
|
| 194 |
+
x1 = x_scaled - width_scaled / 2
|
| 195 |
+
y1 = y_scaled - height_scaled / 2
|
| 196 |
+
x2 = x_scaled + width_scaled / 2
|
| 197 |
+
y2 = y_scaled + height_scaled / 2
|
|
|
|
|
|
|
| 198 |
|
| 199 |
+
detections.append({
|
| 200 |
+
'bbox': [x1, y1, x2, y2],
|
| 201 |
+
'confidence': pred_confidence
|
| 202 |
+
})
|
| 203 |
+
print(f" โ ๊ฒ์ถ (shrimp): conf={pred_confidence:.2%}, bbox=[{x1:.0f},{y1:.0f},{x2:.0f},{y2:.0f}]")
|
| 204 |
|
| 205 |
+
print(f"โ
Roboflow ๊ฒ์ถ ์๋ฃ: {len(detections)}๊ฐ")
|
| 206 |
+
return detections
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
+
except Exception as e:
|
| 209 |
+
print(f"โ Roboflow SDK ์ค๋ฅ: {str(e)}")
|
| 210 |
+
import traceback
|
| 211 |
+
traceback.print_exc()
|
| 212 |
+
return []
|
| 213 |
|
| 214 |
+
# ============================================================
|
| 215 |
+
# ์ ์ญ ๋ชจ๋ธ ๋ณ์ (์ง์ฐ ๋ก๋ฉ)
|
| 216 |
+
# ============================================================
|
| 217 |
+
processor = None
|
| 218 |
+
model = None
|
| 219 |
|
| 220 |
+
def load_rtdetr_on_demand():
|
| 221 |
+
"""RT-DETR ๋ชจ๋ธ์ ํ์์์๋ง ๋ก๋ฉ"""
|
| 222 |
+
global processor, model
|
| 223 |
+
if processor is None or model is None:
|
| 224 |
+
print("๐ RT-DETR ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 225 |
+
from test_visual_validation import load_rtdetr_model
|
| 226 |
+
processor, model = load_rtdetr_model()
|
| 227 |
+
print("โ
RT-DETR ๋ก๋ฉ ์๋ฃ")
|
| 228 |
+
return "โ
RT-DETR ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ"
|
| 229 |
+
else:
|
| 230 |
+
return "โน๏ธ RT-DETR ๋ชจ๋ธ์ด ์ด๋ฏธ ๋ก๋ฉ๋์ด ์์ต๋๋ค"
|
| 231 |
|
| 232 |
+
print("โ
VIDraft/Shrimp ํด๋ผ์ฐ๋ ๋ชจ๋ธ ์ฌ์ฉ ๊ฐ๋ฅ\n")
|
| 233 |
|
| 234 |
+
# ============================================================
|
| 235 |
+
# ๋ผ๋ฒจ๋ง ๋๊ตฌ ์ ์ญ ๋ณ์
|
| 236 |
+
# ============================================================
|
| 237 |
+
current_data = {
|
| 238 |
+
'folder': None,
|
| 239 |
+
'images': [],
|
| 240 |
+
'current_idx': 0,
|
| 241 |
+
'detections': {},
|
| 242 |
+
'selections': {},
|
| 243 |
+
'confidence_threshold': 0.2,
|
| 244 |
+
'image_cache': {},
|
| 245 |
+
'model_type': 'RT-DETR' # ํ์ฌ ์ ํ๋ ๋ชจ๋ธ
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
GROUND_TRUTH_FILE = "ground_truth.json"
|
| 249 |
+
DATA_BASE = "data/ํฐ๋ค๋ฆฌ์์ฐ ์ค์ธก ๋ฐ์ดํฐ_์ตํฌ์ค์์ด์์ด(์ฃผ)"
|
| 250 |
+
|
| 251 |
+
# ============================================================
|
| 252 |
+
# ๋ชจ๋ธ๋ณ ๊ฒ์ถ ํจ์
|
| 253 |
+
# ============================================================
|
| 254 |
|
| 255 |
+
def detect_with_selected_model(image, confidence, model_type):
|
| 256 |
+
"""์ ํ๋ ๋ชจ๋ธ๋ก ๊ฒ์ถ"""
|
| 257 |
+
if model_type == "RT-DETR":
|
| 258 |
+
if processor is None or model is None:
|
| 259 |
+
raise ValueError("โ ๏ธ RT-DETR ๋ชจ๋ธ์ด ๋ก๋ฉ๋์ง ์์์ต๋๋ค. '๐ RT-DETR ๋ก๋' ๋ฒํผ์ ๋จผ์ ํด๋ฆญํ์ธ์.")
|
| 260 |
+
from test_visual_validation import detect_with_rtdetr
|
| 261 |
+
return detect_with_rtdetr(image, processor, model, confidence)
|
| 262 |
+
elif model_type == "VIDraft/Shrimp":
|
| 263 |
+
return detect_with_roboflow(image, confidence)
|
| 264 |
+
elif model_type == "YOLOv8":
|
| 265 |
+
return detect_with_yolo(image, confidence)
|
| 266 |
+
else:
|
| 267 |
+
return []
|
| 268 |
+
|
| 269 |
+
# ============================================================
|
| 270 |
+
# ํญ 1: ์๋ ๊ฒ์ถ (Interactive Validation)
|
| 271 |
+
# ============================================================
|
| 272 |
+
|
| 273 |
+
def interactive_detect(image, confidence, filter_threshold, show_all, model_type, use_filter):
|
| 274 |
+
"""๋ํํ ๊ฒ์ถ"""
|
| 275 |
if image is None:
|
| 276 |
return None, "โ ๏ธ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์."
|
| 277 |
|
| 278 |
try:
|
| 279 |
+
# ์ ํ๋ ๋ชจ๋ธ๋ก ๊ฒ์ถ
|
| 280 |
+
all_detections = detect_with_selected_model(image, confidence, model_type)
|
| 281 |
+
|
| 282 |
+
# ํํฐ ์ ์ฉ ์ฌ๋ถ์ ๋ฐ๋ผ ์ฒ๋ฆฌ
|
| 283 |
+
if not use_filter:
|
| 284 |
+
# ํํฐ ๋ฏธ์ฌ์ฉ: ์ ๋ขฐ๋๋ง ์ ์ฉ
|
| 285 |
+
filtered_detections = all_detections
|
| 286 |
+
for det in filtered_detections:
|
| 287 |
+
det['filter_score'] = det['confidence'] * 100
|
| 288 |
+
det['filter_reasons'] = [f"์ ๋ขฐ๋: {det['confidence']:.0%} (ํํฐ ๋ฏธ์ฌ์ฉ)"]
|
| 289 |
+
all_detections_scored = filtered_detections
|
| 290 |
+
else:
|
| 291 |
+
# ํํฐ ์ฌ์ฉ
|
| 292 |
+
if model_type in ["VIDraft/Shrimp", "YOLOv8"]:
|
| 293 |
+
# Roboflow & YOLOv8: ์ ๋ขฐ๋๋ฅผ ํํฐ ์ ์๋ก ์ฌ์ฉ
|
| 294 |
+
for det in all_detections:
|
| 295 |
+
det['filter_score'] = det['confidence'] * 100
|
| 296 |
+
det['filter_reasons'] = [f"{model_type} ์ ๋ขฐ๋: {det['confidence']:.0%}"]
|
| 297 |
+
all_detections_scored = all_detections
|
| 298 |
+
else:
|
| 299 |
+
# RT-DETR: Universal Filter ์ฌ์ฉ
|
| 300 |
+
from test_visual_validation import apply_universal_filter
|
| 301 |
+
all_detections_scored = apply_universal_filter(all_detections, image, threshold=0)
|
| 302 |
+
|
| 303 |
+
# ํํฐ ์๊ณ๊ฐ ์ ์ฉ
|
| 304 |
+
filtered_detections = [det for det in all_detections_scored if det['filter_score'] >= filter_threshold]
|
| 305 |
+
|
| 306 |
+
# ์๊ฐํ
|
|
|
|
| 307 |
img = image.copy()
|
| 308 |
draw = ImageDraw.Draw(img)
|
| 309 |
|
| 310 |
try:
|
| 311 |
font = ImageFont.truetype("arial.ttf", 14)
|
| 312 |
+
font_large = ImageFont.truetype("arial.ttf", 18)
|
| 313 |
+
font_small = ImageFont.truetype("arial.ttf", 10)
|
| 314 |
except:
|
| 315 |
font = ImageFont.load_default()
|
| 316 |
+
font_large = ImageFont.load_default()
|
| 317 |
+
font_small = ImageFont.load_default()
|
| 318 |
|
| 319 |
+
# ์ ๊ฑฐ๋ ๊ฐ์ฒด ๋จผ์ ํ์ (๋นจ๊ฐ์)
|
| 320 |
+
rejected_detections = [det for det in all_detections_scored if det['filter_score'] < filter_threshold]
|
| 321 |
+
for idx, det in enumerate(rejected_detections, 1):
|
| 322 |
+
x1, y1, x2, y2 = det['bbox']
|
| 323 |
+
score = det['filter_score']
|
| 324 |
|
| 325 |
+
# ๋นจ๊ฐ์ ๋ฐ์ค (์ ๊ฑฐ๋จ)
|
| 326 |
+
draw.rectangle([x1, y1, x2, y2], outline="red", width=8)
|
|
|
|
| 327 |
|
| 328 |
+
# ๋ผ๋ฒจ (์๊ฒ)
|
| 329 |
+
label = f"โ{idx} {score:.0f}์ "
|
| 330 |
+
bbox = draw.textbbox((x1, y1 - 20), label, font=font_small)
|
| 331 |
+
draw.rectangle(bbox, fill="red")
|
| 332 |
+
draw.text((x1, y1 - 20), label, fill="white", font=font_small)
|
| 333 |
+
|
| 334 |
+
# ์ ์ฒด ๊ฒ์ถ ํ์ (์ต์
) - ํ์
|
| 335 |
+
if show_all:
|
| 336 |
+
for det in all_detections_scored:
|
| 337 |
+
if det not in filtered_detections and det not in rejected_detections:
|
| 338 |
+
x1, y1, x2, y2 = det['bbox']
|
| 339 |
+
draw.rectangle([x1, y1, x2, y2], outline="gray", width=4)
|
| 340 |
+
|
| 341 |
+
# ํํฐ๋ง๋ ๊ฒฐ๊ณผ (ํต๊ณผ) - ๋
น์/๋
ธ๋์/์ฃผํฉ์
|
| 342 |
+
for idx, det in enumerate(filtered_detections, 1):
|
| 343 |
+
x1, y1, x2, y2 = det['bbox']
|
| 344 |
+
score = det['filter_score']
|
| 345 |
+
|
| 346 |
+
# ์ ์์ ๋ฐ๋ผ ์์
|
| 347 |
+
if score >= 75:
|
| 348 |
+
color = "lime"
|
| 349 |
+
elif score >= 50:
|
| 350 |
color = "yellow"
|
| 351 |
+
else:
|
| 352 |
+
color = "orange"
|
| 353 |
|
| 354 |
+
# ๋ฐ์ค (๋๊ป๊ฒ)
|
| 355 |
+
draw.rectangle([x1, y1, x2, y2], outline=color, width=10)
|
| 356 |
|
| 357 |
# ๋ผ๋ฒจ
|
| 358 |
+
label = f"โ#{idx} {score:.0f}์ "
|
| 359 |
+
bbox = draw.textbbox((x1, y1 - 25), label, font=font)
|
| 360 |
draw.rectangle(bbox, fill=color)
|
| 361 |
+
draw.text((x1, y1 - 25), label, fill="black", font=font)
|
| 362 |
+
|
| 363 |
+
# ์ธ๋ถ ์ ๋ณด (์๊ฒ)
|
| 364 |
+
details = f"{model_type}:{det['confidence']:.0%}"
|
| 365 |
+
draw.text((x1, y2 + 5), details, fill=color, font=font_small)
|
| 366 |
|
| 367 |
# ํค๋
|
| 368 |
+
header = f"[{model_type}] โ {len(filtered_detections)}๊ฐ / โ {len(rejected_detections)}๊ฐ (์ ์ฒด: {len(all_detections_scored)}๊ฐ)"
|
| 369 |
+
header_bbox = draw.textbbox((10, 10), header, font=font_large)
|
| 370 |
+
draw.rectangle([5, 5, header_bbox[2]+10, header_bbox[3]+10],
|
| 371 |
+
fill="black", outline="lime", width=2)
|
| 372 |
+
draw.text((10, 10), header, fill="lime", font=font_large)
|
| 373 |
|
| 374 |
+
# ์ ๋ณด ์์ฑ
|
| 375 |
info = f"""
|
| 376 |
+
### ๐ ๊ฒ์ถ ๊ฒฐ๊ณผ (๋ชจ๋ธ: {model_type})
|
| 377 |
+
|
| 378 |
+
- **์ ์ฒด ๊ฒ์ถ**: {len(all_detections_scored)}๊ฐ
|
| 379 |
+
- **ํํฐ๋ง ํ**: {len(filtered_detections)}๊ฐ
|
| 380 |
+
- **์ ๊ฑฐ๋จ**: {len(rejected_detections)}๊ฐ
|
| 381 |
+
|
| 382 |
+
---
|
| 383 |
|
| 384 |
+
### ๐ฏ ๊ฒ์ถ๋ ๊ฐ์ฒด ์์ธ (โ
ํต๊ณผ)
|
|
|
|
| 385 |
|
| 386 |
+
"""
|
| 387 |
+
|
| 388 |
+
for idx, det in enumerate(filtered_detections, 1):
|
| 389 |
+
info += f"""
|
| 390 |
+
**#{idx} - ์ ์: {det['filter_score']:.0f}์ ** ({model_type} ์ ๋ขฐ๋: {det['confidence']:.0%})
|
| 391 |
+
|
| 392 |
+
"""
|
| 393 |
+
# ์ฃผ์ ํน์ง๋ง 5๊ฐ
|
| 394 |
+
for reason in det['filter_reasons'][:5]:
|
| 395 |
+
info += f"- {reason}\n"
|
| 396 |
+
|
| 397 |
+
if not filtered_detections:
|
| 398 |
+
info += """
|
| 399 |
+
โ ๏ธ **๊ฒ์ถ๋ ๊ฐ์ฒด๊ฐ ์์ต๋๋ค.**
|
| 400 |
+
|
| 401 |
+
"""
|
| 402 |
+
|
| 403 |
+
# ์ ๊ฑฐ๋ ๊ฐ์ฒด ์ ๋ณด ์ถ๊ฐ
|
| 404 |
+
if rejected_detections:
|
| 405 |
+
info += f"""
|
| 406 |
+
|
| 407 |
+
---
|
| 408 |
+
|
| 409 |
+
### โ ์ ๊ฑฐ๋ ๊ฐ์ฒด ({len(rejected_detections)}๊ฐ)
|
| 410 |
+
|
| 411 |
+
"""
|
| 412 |
+
for idx, det in enumerate(rejected_detections[:3], 1): # ์ต๋ 3๊ฐ๋ง ํ์
|
| 413 |
+
info += f"""
|
| 414 |
+
**์ ๊ฑฐ #{idx} - ์ ์: {det['filter_score']:.0f}์ ** (์๊ณ๊ฐ ๋ฏธ๋ฌ)
|
| 415 |
+
- {model_type} ์ ๋ขฐ๋: {det['confidence']:.0%}
|
| 416 |
+
|
| 417 |
+
"""
|
| 418 |
+
# ์คํจ ์ด์ ํ์
|
| 419 |
+
for reason in det['filter_reasons'][:3]:
|
| 420 |
+
info += f"- {reason}\n"
|
| 421 |
|
| 422 |
return img, info
|
| 423 |
|
| 424 |
except Exception as e:
|
| 425 |
+
import traceback
|
| 426 |
+
error_detail = traceback.format_exc()
|
| 427 |
+
return None, f"โ ์ค๋ฅ ๋ฐ์:\n\n```\n{error_detail}\n```"
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
# ============================================================
|
| 431 |
+
# ํญ 2: ๋ผ๋ฒจ๋ง ๋๊ตฌ (Labeling Tool)
|
| 432 |
+
# ============================================================
|
| 433 |
+
|
| 434 |
+
def detect_with_rtdetr_fast(image, confidence=0.3):
|
| 435 |
+
"""RT-DETR ๋น ๋ฅธ ๊ฒ์ถ"""
|
| 436 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 437 |
+
with torch.no_grad():
|
| 438 |
+
outputs = model(**inputs)
|
| 439 |
+
|
| 440 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 441 |
+
results = processor.post_process_object_detection(
|
| 442 |
+
outputs,
|
| 443 |
+
target_sizes=target_sizes,
|
| 444 |
+
threshold=confidence
|
| 445 |
+
)[0]
|
| 446 |
+
|
| 447 |
+
detections = []
|
| 448 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 449 |
+
x1, y1, x2, y2 = box.tolist()
|
| 450 |
+
detections.append({
|
| 451 |
+
'bbox': [x1, y1, x2, y2],
|
| 452 |
+
'confidence': score.item()
|
| 453 |
+
})
|
| 454 |
+
|
| 455 |
+
return detections
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
def load_existing_ground_truth():
|
| 459 |
+
"""๊ธฐ์กด ground_truth.json ๋ก๋"""
|
| 460 |
+
if os.path.exists(GROUND_TRUTH_FILE):
|
| 461 |
+
with open(GROUND_TRUTH_FILE, 'r', encoding='utf-8') as f:
|
| 462 |
+
return json.load(f)
|
| 463 |
+
return {}
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
def save_ground_truth(data):
|
| 467 |
+
"""ground_truth.json ์ ์ฅ"""
|
| 468 |
+
backup_dir = "backups"
|
| 469 |
+
if not os.path.exists(backup_dir):
|
| 470 |
+
os.makedirs(backup_dir)
|
| 471 |
+
|
| 472 |
+
if os.path.exists(GROUND_TRUTH_FILE):
|
| 473 |
+
backup_name = f"ground_truth_backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
| 474 |
+
backup_path = os.path.join(backup_dir, backup_name)
|
| 475 |
+
import shutil
|
| 476 |
+
shutil.copy2(GROUND_TRUTH_FILE, backup_path)
|
| 477 |
+
|
| 478 |
+
with open(GROUND_TRUTH_FILE, 'w', encoding='utf-8') as f:
|
| 479 |
+
json.dump(data, f, ensure_ascii=False, indent=2)
|
| 480 |
+
|
| 481 |
+
print(f"โ
Ground Truth ์ ์ฅ ์๋ฃ: {len(data)}๊ฐ ์ด๋ฏธ์ง")
|
| 482 |
+
|
| 483 |
+
|
| 484 |
+
def get_folders():
|
| 485 |
+
"""์ฌ์ฉ ๊ฐ๋ฅํ ํด๋ ๋ชฉ๋ก"""
|
| 486 |
+
folders = sorted(glob.glob(os.path.join(DATA_BASE, "2*")))
|
| 487 |
+
return [os.path.basename(f) for f in folders if os.path.isdir(f)]
|
| 488 |
+
|
| 489 |
+
|
| 490 |
+
def start_labeling(folder, conf_threshold, model_type):
|
| 491 |
+
"""๋ผ๋ฒจ๋ง ์์"""
|
| 492 |
+
if not folder:
|
| 493 |
+
return None, "โ ํด๋๋ฅผ ์ ํํ์ธ์.", ""
|
| 494 |
+
|
| 495 |
+
current_data['folder'] = folder
|
| 496 |
+
current_data['confidence_threshold'] = conf_threshold
|
| 497 |
+
current_data['model_type'] = model_type
|
| 498 |
+
|
| 499 |
+
folder_path = os.path.join(DATA_BASE, folder)
|
| 500 |
+
all_images = sorted(glob.glob(os.path.join(folder_path, "*.jpg")))
|
| 501 |
+
|
| 502 |
+
# -1, -2 ๋ฑ์ด ๋ถ์ ํ์ผ ์ ์ธ (์: 251017_01-1.jpg ์ ์ธ, 251017_01.jpg๋ง ํฌํจ)
|
| 503 |
+
import re
|
| 504 |
+
images = [img for img in all_images if not re.search(r'-\d+\.jpg$', os.path.basename(img))]
|
| 505 |
+
|
| 506 |
+
if not images:
|
| 507 |
+
return None, "โ ์ด๋ฏธ์ง ์์", ""
|
| 508 |
+
|
| 509 |
+
print(f"๐ ํด๋: {folder}")
|
| 510 |
+
print(f" ์ ์ฒด ์ด๋ฏธ์ง: {len(all_images)}๊ฐ")
|
| 511 |
+
print(f" ๋ผ๋ฒจ๋ง ๋์: {len(images)}๊ฐ (-์ซ์ ํ์ผ ์ ์ธ)")
|
| 512 |
+
|
| 513 |
+
current_data['images'] = images
|
| 514 |
+
current_data['current_idx'] = 0
|
| 515 |
+
current_data['detections'] = {}
|
| 516 |
+
current_data['selections'] = {}
|
| 517 |
+
|
| 518 |
+
# ๊ธฐ์กด GT ๋ก๋
|
| 519 |
+
gt = load_existing_ground_truth()
|
| 520 |
+
|
| 521 |
+
# ์ด๋ฏธ ๋ผ๋ฒจ๋ง๋ ์ด๋ฏธ์ง ๊ฑด๋๋ฐ๊ธฐ
|
| 522 |
+
for i, img_path in enumerate(images):
|
| 523 |
+
filename = os.path.basename(img_path)
|
| 524 |
+
if filename in gt:
|
| 525 |
+
current_data['selections'][filename] = [j for j in range(len(gt[filename]))]
|
| 526 |
+
print(f"โญ๏ธ ๊ฑด๋๋ฐ๊ธฐ: {filename} (์ด๋ฏธ ๋ผ๋ฒจ๋ง๋จ)")
|
| 527 |
+
|
| 528 |
+
# ์ฒซ ๋ฏธ๋ผ๋ฒจ๋ง ์ด๋ฏธ์ง ์ฐพ๊ธฐ
|
| 529 |
+
while current_data['current_idx'] < len(images):
|
| 530 |
+
filename = os.path.basename(images[current_data['current_idx']])
|
| 531 |
+
if filename not in current_data['selections']:
|
| 532 |
+
break
|
| 533 |
+
current_data['current_idx'] += 1
|
| 534 |
+
|
| 535 |
+
if current_data['current_idx'] >= len(images):
|
| 536 |
+
return None, "โ
๋ชจ๋ ์ด๋ฏธ์ง ๋ผ๋ฒจ๋ง ์๋ฃ!", ""
|
| 537 |
+
|
| 538 |
+
return show_current_image()
|
| 539 |
+
|
| 540 |
+
|
| 541 |
+
def show_current_image():
|
| 542 |
+
"""ํ์ฌ ์ด๋ฏธ์ง ํ์"""
|
| 543 |
+
if current_data['current_idx'] >= len(current_data['images']):
|
| 544 |
+
return None, "โ
์๋ฃ!", ""
|
| 545 |
+
|
| 546 |
+
img_path = current_data['images'][current_data['current_idx']]
|
| 547 |
+
filename = os.path.basename(img_path)
|
| 548 |
+
|
| 549 |
+
# ์บ์ ํ์ธ
|
| 550 |
+
if filename in current_data['image_cache']:
|
| 551 |
+
image = current_data['image_cache'][filename]
|
| 552 |
+
else:
|
| 553 |
+
image = Image.open(img_path)
|
| 554 |
+
current_data['image_cache'][filename] = image
|
| 555 |
+
|
| 556 |
+
# ์ ํ๋ ๋ชจ๋ธ๋ก ๊ฒ์ถ
|
| 557 |
+
if filename not in current_data['detections']:
|
| 558 |
+
if current_data['model_type'] == 'RT-DETR':
|
| 559 |
+
detections = detect_with_rtdetr_fast(image, current_data['confidence_threshold'])
|
| 560 |
+
elif current_data['model_type'] == 'YOLOv8':
|
| 561 |
+
detections = detect_with_yolo(image, current_data['confidence_threshold'])
|
| 562 |
+
else: # VIDraft/Shrimp
|
| 563 |
+
detections = detect_with_roboflow(image, current_data['confidence_threshold'])
|
| 564 |
+
current_data['detections'][filename] = detections
|
| 565 |
+
else:
|
| 566 |
+
detections = current_data['detections'][filename]
|
| 567 |
+
|
| 568 |
+
# ์ ํ๋ ๋ฐ์ค
|
| 569 |
+
selected_indices = current_data['selections'].get(filename, [])
|
| 570 |
+
|
| 571 |
+
# ์๊ฐํ
|
| 572 |
+
vis_image = draw_detections(image, detections, selected_indices)
|
| 573 |
+
|
| 574 |
+
info = f"""
|
| 575 |
+
### ๐ {current_data['folder']} - ์ด๋ฏธ์ง {current_data['current_idx']+1}/{len(current_data['images'])}
|
| 576 |
+
|
| 577 |
+
**ํ์ผ**: {filename}
|
| 578 |
+
**๋ชจ๋ธ**: {current_data['model_type']}
|
| 579 |
+
|
| 580 |
+
**๊ฒ์ถ**: {len(detections)}๊ฐ
|
| 581 |
+
**์ ํ**: {len(selected_indices)}๊ฐ
|
| 582 |
+
|
| 583 |
+
---
|
| 584 |
+
|
| 585 |
+
### ๐ฑ๏ธ ์ฌ์ฉ ๋ฐฉ๋ฒ:
|
| 586 |
+
1. ์ด๋ฏธ์ง๋ฅผ ํด๋ฆญํ์ฌ ๋ฐ์ค ์ ํ/ํด์
|
| 587 |
+
2. "๋ค์" ๋ฒํผ์ผ๋ก ์ ์ฅ ํ ์ด๋
|
| 588 |
+
3. "๊ฑด๋๋ฐ๊ธฐ"๋ก ์ ํ ์์ด ์ด๋
|
| 589 |
+
"""
|
| 590 |
+
|
| 591 |
+
return vis_image, info, filename
|
| 592 |
+
|
| 593 |
+
|
| 594 |
+
def draw_detections(image, detections, selected_indices):
|
| 595 |
+
"""๊ฒ์ถ ๊ฒฐ๊ณผ ๊ทธ๋ฆฌ๊ธฐ"""
|
| 596 |
+
img = image.copy()
|
| 597 |
+
draw = ImageDraw.Draw(img)
|
| 598 |
+
|
| 599 |
+
try:
|
| 600 |
+
font_tiny = ImageFont.truetype("arial.ttf", 10)
|
| 601 |
+
font_large = ImageFont.truetype("arial.ttf", 40)
|
| 602 |
+
except:
|
| 603 |
+
font_tiny = ImageFont.load_default()
|
| 604 |
+
font_large = ImageFont.load_default()
|
| 605 |
+
|
| 606 |
+
# ์ ํ๋์ง ์์ ๋ฐ์ค ๋จผ์ (๋ค์ชฝ ๋ ์ด์ด)
|
| 607 |
+
for idx, det in enumerate(detections):
|
| 608 |
+
if idx not in selected_indices:
|
| 609 |
+
x1, y1, x2, y2 = det['bbox']
|
| 610 |
+
draw.rectangle([x1, y1, x2, y2], outline="lime", width=20)
|
| 611 |
+
corner_label = f"#{idx+1}"
|
| 612 |
+
draw.rectangle([x1-2, y1-24, x1+30, y1-2], fill="lime")
|
| 613 |
+
draw.text((x1, y1 - 22), corner_label, fill="white", font=font_tiny)
|
| 614 |
|
| 615 |
+
# ์ ํ๋ ๋ฐ์ค ๋์ค์ (์์ชฝ ๋ ์ด์ด)
|
| 616 |
+
for idx, det in enumerate(detections):
|
| 617 |
+
if idx in selected_indices:
|
| 618 |
+
x1, y1, x2, y2 = det['bbox']
|
| 619 |
+
draw.rectangle([x1, y1, x2, y2], outline="blue", width=28)
|
| 620 |
+
corner_label = f"โ#{idx+1}"
|
| 621 |
+
draw.rectangle([x1-2, y1-24, x1+40, y1-2], fill="blue")
|
| 622 |
+
draw.text((x1, y1 - 22), corner_label, fill="white", font=font_tiny)
|
| 623 |
+
|
| 624 |
+
# ์ํ ๋ฒํผ
|
| 625 |
+
for idx, det in enumerate(detections):
|
| 626 |
+
x1, y1, x2, y2 = det['bbox']
|
| 627 |
+
center_x = (x1 + x2) / 2
|
| 628 |
+
center_y = (y1 + y2) / 2
|
| 629 |
+
|
| 630 |
+
selected = idx in selected_indices
|
| 631 |
+
btn_color = "blue" if selected else "lime"
|
| 632 |
+
btn_text = f"โ{idx+1}" if selected else f"{idx+1}"
|
| 633 |
+
|
| 634 |
+
box_width = x2 - x1
|
| 635 |
+
box_height = y2 - y1
|
| 636 |
+
radius = min(55, box_width * 0.18, box_height * 0.35)
|
| 637 |
+
|
| 638 |
+
# ์ํ ๋ฒํผ
|
| 639 |
+
draw.ellipse(
|
| 640 |
+
[center_x - radius, center_y - radius,
|
| 641 |
+
center_x + radius, center_y + radius],
|
| 642 |
+
fill=btn_color, outline="white", width=4
|
| 643 |
+
)
|
| 644 |
+
draw.text((center_x - radius*0.5, center_y - radius*0.6),
|
| 645 |
+
btn_text, fill="white", font=font_large)
|
| 646 |
+
|
| 647 |
+
return img
|
| 648 |
+
|
| 649 |
+
|
| 650 |
+
def labeling_click(image, filename, evt: gr.SelectData):
|
| 651 |
+
"""์ด๋ฏธ์ง ํด๋ฆญ ์ด๋ฒคํธ"""
|
| 652 |
+
if not filename or filename not in current_data['detections']:
|
| 653 |
+
return image, "โ ๏ธ ์ด๋ฏธ์ง๋ฅผ ๋จผ์ ๋ก๋ํ์ธ์."
|
| 654 |
+
|
| 655 |
+
click_x, click_y = evt.index[0], evt.index[1]
|
| 656 |
+
detections = current_data['detections'][filename]
|
| 657 |
+
selected_indices = set(current_data['selections'].get(filename, []))
|
| 658 |
+
|
| 659 |
+
# ํด๋ฆญํ ๋ฐ์ค ์ฐพ๊ธฐ
|
| 660 |
+
clicked_idx = None
|
| 661 |
+
button_candidates = []
|
| 662 |
+
|
| 663 |
+
# ๋ฒํผ ์์ญ ํ์ธ
|
| 664 |
+
for idx, det in enumerate(detections):
|
| 665 |
+
x1, y1, x2, y2 = det['bbox']
|
| 666 |
+
center_x = (x1 + x2) / 2
|
| 667 |
+
center_y = (y1 + y2) / 2
|
| 668 |
+
|
| 669 |
+
box_width = x2 - x1
|
| 670 |
+
box_height = y2 - y1
|
| 671 |
+
radius = min(55, box_width * 0.18, box_height * 0.35)
|
| 672 |
+
|
| 673 |
+
distance = ((click_x - center_x) ** 2 + (click_y - center_y) ** 2) ** 0.5
|
| 674 |
+
|
| 675 |
+
if distance <= radius:
|
| 676 |
+
button_candidates.append((idx, distance))
|
| 677 |
+
|
| 678 |
+
# ๋ฒํผ ํด๋ฆญ์ด ์์ผ๋ฉด ์ ํ
|
| 679 |
+
if button_candidates:
|
| 680 |
+
button_candidates.sort(key=lambda x: x[1])
|
| 681 |
+
clicked_idx = button_candidates[0][0]
|
| 682 |
+
else:
|
| 683 |
+
# ๋ฐ์ค ์์ญ ํด๋ฆญ ํ์ธ
|
| 684 |
+
for idx, det in enumerate(detections):
|
| 685 |
+
x1, y1, x2, y2 = det['bbox']
|
| 686 |
+
if x1 <= click_x <= x2 and y1 <= click_y <= y2:
|
| 687 |
+
clicked_idx = idx
|
| 688 |
+
break
|
| 689 |
+
|
| 690 |
+
# ์ ํ ํ ๊ธ
|
| 691 |
+
if clicked_idx is not None:
|
| 692 |
+
if clicked_idx in selected_indices:
|
| 693 |
+
selected_indices.remove(clicked_idx)
|
| 694 |
+
print(f"โ ์ ํ ํด์ : ๋ฐ์ค #{clicked_idx+1}")
|
| 695 |
+
else:
|
| 696 |
+
selected_indices.add(clicked_idx)
|
| 697 |
+
print(f"โ
์ ํ: ๋ฐ์ค #{clicked_idx+1}")
|
| 698 |
+
|
| 699 |
+
current_data['selections'][filename] = list(selected_indices)
|
| 700 |
+
|
| 701 |
+
# ์ด๋ฏธ์ง ๋ค์ ๊ทธ๋ฆฌ๊ธฐ
|
| 702 |
+
img_path = current_data['images'][current_data['current_idx']]
|
| 703 |
+
image = Image.open(img_path)
|
| 704 |
+
vis_image = draw_detections(image, detections, list(selected_indices))
|
| 705 |
+
|
| 706 |
+
info = f"โ
๋ฐ์ค #{clicked_idx+1} {'์ ํ' if clicked_idx in selected_indices else 'ํด์ '}"
|
| 707 |
+
return vis_image, info
|
| 708 |
+
|
| 709 |
+
return image, "โ ๋ฐ์ค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 710 |
+
|
| 711 |
+
|
| 712 |
+
def save_and_next():
|
| 713 |
+
"""์ ์ฅ ํ ๋ค์"""
|
| 714 |
+
if current_data['current_idx'] >= len(current_data['images']):
|
| 715 |
+
return None, "โ
์๋ฃ!", ""
|
| 716 |
+
|
| 717 |
+
img_path = current_data['images'][current_data['current_idx']]
|
| 718 |
+
filename = os.path.basename(img_path)
|
| 719 |
+
|
| 720 |
+
# GT ์ ์ฅ
|
| 721 |
+
gt = load_existing_ground_truth()
|
| 722 |
+
selected_indices = current_data['selections'].get(filename, [])
|
| 723 |
+
|
| 724 |
+
if selected_indices:
|
| 725 |
+
detections = current_data['detections'][filename]
|
| 726 |
+
gt[filename] = [
|
| 727 |
+
{
|
| 728 |
+
'bbox': detections[i]['bbox'],
|
| 729 |
+
'folder': current_data['folder']
|
| 730 |
+
}
|
| 731 |
+
for i in selected_indices
|
| 732 |
+
]
|
| 733 |
+
save_ground_truth(gt)
|
| 734 |
+
print(f"๐พ ์ ์ฅ: {filename} - {len(selected_indices)}๊ฐ ๋ฐ์ค")
|
| 735 |
+
else:
|
| 736 |
+
print(f"โญ๏ธ ๊ฑด๋๋ฐ๊ธฐ: {filename} - ์ ํ ์์")
|
| 737 |
+
|
| 738 |
+
# ๋ค์ ์ด๋ฏธ์ง
|
| 739 |
+
current_data['current_idx'] += 1
|
| 740 |
+
|
| 741 |
+
# ๋ค์ ๋ฏธ๋ผ๋ฒจ๋ง ์ด๋ฏธ์ง ์ฐพ๊ธฐ
|
| 742 |
+
while current_data['current_idx'] < len(current_data['images']):
|
| 743 |
+
next_filename = os.path.basename(current_data['images'][current_data['current_idx']])
|
| 744 |
+
if next_filename not in current_data['selections']:
|
| 745 |
+
break
|
| 746 |
+
current_data['current_idx'] += 1
|
| 747 |
+
|
| 748 |
+
if current_data['current_idx'] >= len(current_data['images']):
|
| 749 |
+
return None, "โ
๋ชจ๋ ์ด๋ฏธ์ง ๋ผ๋ฒจ๋ง ์๋ฃ!", ""
|
| 750 |
+
|
| 751 |
+
return show_current_image()
|
| 752 |
+
|
| 753 |
+
|
| 754 |
+
def skip_image():
|
| 755 |
+
"""๊ฑด๋๋ฐ๊ธฐ"""
|
| 756 |
+
current_data['current_idx'] += 1
|
| 757 |
+
|
| 758 |
+
if current_data['current_idx'] >= len(current_data['images']):
|
| 759 |
+
return None, "โ
์๋ฃ!", ""
|
| 760 |
+
|
| 761 |
+
return show_current_image()
|
| 762 |
+
|
| 763 |
+
|
| 764 |
+
# ============================================================
|
| 765 |
+
# ํญ 3: ๊ฐ๋จ ๋ฐ๋ชจ (App Demo)
|
| 766 |
+
# ============================================================
|
| 767 |
+
|
| 768 |
+
def demo_detect(image, confidence_threshold, filter_threshold, model_type, use_filter):
|
| 769 |
+
"""๊ฐ๋จํ ๋ฐ๋ชจ ๊ฒ์ถ"""
|
| 770 |
+
if image is None:
|
| 771 |
+
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์."
|
| 772 |
+
|
| 773 |
+
if isinstance(image, np.ndarray):
|
| 774 |
+
image = Image.fromarray(image)
|
| 775 |
+
|
| 776 |
+
# ์ ํ๋ ๋ชจ๋ธ๋ก ๊ฒ์ถ
|
| 777 |
+
all_detections = detect_with_selected_model(image, confidence_threshold, model_type)
|
| 778 |
+
|
| 779 |
+
# ํํฐ ์ ์ฉ ์ฌ๋ถ
|
| 780 |
+
if not use_filter:
|
| 781 |
+
# ํํฐ ๋ฏธ์ฌ์ฉ
|
| 782 |
+
filtered_detections = all_detections
|
| 783 |
+
for det in filtered_detections:
|
| 784 |
+
det['filter_score'] = det['confidence'] * 100
|
| 785 |
+
else:
|
| 786 |
+
# ํํฐ ์ฌ์ฉ
|
| 787 |
+
if model_type in ["Roboflow", "YOLOv8"]:
|
| 788 |
+
# Roboflow & YOLOv8: ์ ๋ขฐ๋ ๊ธฐ๋ฐ ํํฐ
|
| 789 |
+
for det in all_detections:
|
| 790 |
+
det['filter_score'] = det['confidence'] * 100
|
| 791 |
+
filtered_detections = [det for det in all_detections if det['filter_score'] >= filter_threshold]
|
| 792 |
+
else:
|
| 793 |
+
# RT-DETR: Universal Filter
|
| 794 |
+
from test_visual_validation import apply_universal_filter
|
| 795 |
+
filtered_detections = apply_universal_filter(all_detections, image, filter_threshold)
|
| 796 |
+
|
| 797 |
+
# ์๊ฐํ
|
| 798 |
+
result_image = image.copy()
|
| 799 |
+
draw = ImageDraw.Draw(result_image)
|
| 800 |
+
|
| 801 |
+
try:
|
| 802 |
+
font = ImageFont.truetype("arial.ttf", 20)
|
| 803 |
+
font_small = ImageFont.truetype("arial.ttf", 14)
|
| 804 |
+
except:
|
| 805 |
+
font = ImageFont.load_default()
|
| 806 |
+
font_small = ImageFont.load_default()
|
| 807 |
+
|
| 808 |
+
# ๋ฐ์ค ๊ทธ๋ฆฌ๊ธฐ
|
| 809 |
+
for i, det in enumerate(filtered_detections, 1):
|
| 810 |
+
x1, y1, x2, y2 = det['bbox']
|
| 811 |
+
draw.rectangle([x1, y1, x2, y2], outline="lime", width=8)
|
| 812 |
+
|
| 813 |
+
score = det['filter_score']
|
| 814 |
+
conf = det['confidence']
|
| 815 |
+
label = f"#{i} | Score:{score:.0f} | Conf:{conf:.2f}"
|
| 816 |
+
|
| 817 |
+
bbox = draw.textbbox((x1, y1-25), label, font=font_small)
|
| 818 |
+
draw.rectangle(bbox, fill="lime")
|
| 819 |
+
draw.text((x1, y1-25), label, fill="black", font=font_small)
|
| 820 |
+
|
| 821 |
+
# ๊ฒฐ๊ณผ ํ
์คํธ
|
| 822 |
+
info = f"""
|
| 823 |
+
๐ **๊ฒ์ถ ๊ฒฐ๊ณผ (๋ชจ๋ธ: {model_type}):**
|
| 824 |
+
โข ์ ์ฒด ๊ฒ์ถ: {len(all_detections)}๊ฐ
|
| 825 |
+
โข ํํฐ ํต๊ณผ: {len(filtered_detections)}๊ฐ
|
| 826 |
+
โข ์ ๊ฑฐ๋จ: {len(all_detections) - len(filtered_detections)}๊ฐ
|
| 827 |
+
|
| 828 |
+
โ๏ธ **์ค์ :**
|
| 829 |
+
โข {model_type} Confidence: {confidence_threshold}
|
| 830 |
+
โข Filter Threshold: {filter_threshold}
|
| 831 |
+
|
| 832 |
+
๐ฏ **์ฑ๋ฅ (50๊ฐ GT ๊ธฐ์ค, RT-DETR):**
|
| 833 |
+
โข Precision: 44.2%
|
| 834 |
+
โข Recall: 94.0%
|
| 835 |
+
โข F1 Score: 56.1%
|
| 836 |
+
"""
|
| 837 |
+
|
| 838 |
+
if len(filtered_detections) > 0:
|
| 839 |
+
info += f"\nโ
{len(filtered_detections)}๊ฐ์ ์์ฐ๋ฅผ ๊ฒ์ถํ์ต๋๋ค!"
|
| 840 |
+
else:
|
| 841 |
+
info += "\nโ ๏ธ ์์ฐ๊ฐ ๊ฒ์ถ๋์ง ์์์ต๋๋ค. Threshold๋ฅผ ๋ฎ์ถฐ๋ณด์ธ์."
|
| 842 |
+
|
| 843 |
+
return result_image, info
|
| 844 |
+
|
| 845 |
+
|
| 846 |
+
# ============================================================
|
| 847 |
+
# Gradio ์ธํฐํ์ด์ค - 3๊ฐ ํญ์ผ๋ก ํตํฉ
|
| 848 |
+
# ============================================================
|
| 849 |
+
|
| 850 |
+
with gr.Blocks(title="๐ฆ ์์ฐ ๊ฒ์ถ ํตํฉ ์์คํ
", theme=gr.themes.Soft()) as demo:
|
| 851 |
|
| 852 |
gr.Markdown("""
|
| 853 |
+
# ๐ฆ ์์ฐ ๊ฒ์ถ ํตํฉ ์์คํ
|
| 854 |
|
| 855 |
+
**3๊ฐ์ง ๋ชจ๋ธ๋ก ์์ฐ๋ฅผ ์ ํํ๊ฒ ๊ฒ์ถํ์ธ์**
|
| 856 |
|
| 857 |
---
|
| 858 |
""")
|
| 859 |
|
| 860 |
+
# ==================== ์ต์๋จ: ๋ชจ๋ธ ์ ํ ====================
|
| 861 |
with gr.Row():
|
| 862 |
+
with gr.Column(scale=3):
|
| 863 |
+
model_selector = gr.Radio(
|
| 864 |
+
choices=["RT-DETR", "VIDraft/Shrimp", "YOLOv8"],
|
| 865 |
+
value="YOLOv8",
|
| 866 |
+
label="๐ค ๊ฒ์ถ ๋ชจ๋ธ ์ ํ",
|
| 867 |
+
info="๋ชจ๋ ํญ์ ์ ์ฉ๋ฉ๋๋ค"
|
| 868 |
)
|
| 869 |
+
with gr.Column(scale=1):
|
| 870 |
+
load_rtdetr_btn = gr.Button("๐ RT-DETR ๋ก๋", size="sm", variant="secondary")
|
| 871 |
+
rtdetr_status = gr.Textbox(label="๋ชจ๋ธ ์ํ", value="โธ๏ธ RT-DETR ๋ฏธ๋ก๋ (VIDraft/Shrimp ํด๋ผ์ฐ๋ ๋ชจ๋ธ ์ฌ์ฉ ๊ฐ๋ฅ)", interactive=False, lines=1)
|
| 872 |
+
|
| 873 |
+
# RT-DETR ๋ก๋ฉ ๋ฒํผ ์ด๋ฒคํธ
|
| 874 |
+
load_rtdetr_btn.click(
|
| 875 |
+
load_rtdetr_on_demand,
|
| 876 |
+
inputs=[],
|
| 877 |
+
outputs=[rtdetr_status]
|
| 878 |
+
)
|
| 879 |
+
|
| 880 |
+
gr.Markdown("---")
|
| 881 |
+
|
| 882 |
+
with gr.Tabs():
|
| 883 |
+
# ==================== ํญ 1: ์๋ ๊ฒ์ถ ====================
|
| 884 |
+
with gr.TabItem("๐ค ์๋ ๊ฒ์ถ & ๊ฒ์ฆ"):
|
| 885 |
+
gr.Markdown("""
|
| 886 |
+
### ์ค์๊ฐ์ผ๋ก ํ๋ผ๋ฏธํฐ๋ฅผ ์กฐ์ ํ๋ฉฐ ๊ฒ์ถ ๊ฒฐ๊ณผ๋ฅผ ํ์ธ
|
| 887 |
+
์ต์ ํ๋ ํ๋ผ๋ฏธํฐ๋ก ์์ฐ ๊ฒ์ถ์ ํ
์คํธํ์ธ์.
|
| 888 |
+
""")
|
| 889 |
+
|
| 890 |
+
with gr.Row():
|
| 891 |
+
with gr.Column():
|
| 892 |
+
input_image_detect = gr.Image(label="์
๋ ฅ ์ด๋ฏธ์ง", type="pil")
|
| 893 |
+
|
| 894 |
+
confidence_slider_detect = gr.Slider(
|
| 895 |
+
0.01, 1.0, 0.1,
|
| 896 |
+
step=0.01,
|
| 897 |
+
label="์ ๋ขฐ๋ ์๊ณ๊ฐ",
|
| 898 |
+
info="RT-DETR: 0.065 | VIDraft/Shrimp: 0.3~0.5 | YOLOv8: 0.1~0.3 ๊ถ์ฅ"
|
| 899 |
+
)
|
| 900 |
+
|
| 901 |
+
use_filter_check = gr.Checkbox(
|
| 902 |
+
label="๐ ํํฐ ์ ์ ์๊ณ๊ฐ ์ฌ์ฉ",
|
| 903 |
+
value=False,
|
| 904 |
+
info="์ฒดํฌํ๋ฉด ํํฐ ์ ์ ๊ธฐ์ค์ผ๋ก ์ถ๊ฐ ํํฐ๋ง"
|
| 905 |
+
)
|
| 906 |
+
|
| 907 |
+
filter_slider_detect = gr.Slider(
|
| 908 |
+
0, 100, 90,
|
| 909 |
+
step=5,
|
| 910 |
+
label="ํํฐ ์ ์ ์๊ณ๊ฐ",
|
| 911 |
+
info="RT-DETR: Universal Filter | VIDraft/Shrimp: ์ ๋ขฐ๋ ๊ธฐ๋ฐ",
|
| 912 |
+
visible=True
|
| 913 |
+
)
|
| 914 |
+
|
| 915 |
+
show_all_check = gr.Checkbox(
|
| 916 |
+
label="์ ์ฒด ๊ฒ์ถ ๊ฒฐ๊ณผ ํ์ (ํ์)",
|
| 917 |
+
value=False
|
| 918 |
+
)
|
| 919 |
+
|
| 920 |
+
detect_btn = gr.Button("๐ ๊ฒ์ถ ์คํ", variant="primary", size="lg")
|
| 921 |
+
|
| 922 |
+
# ์์ ์ด๋ฏธ์ง (๊ฒฐ๊ณผ ํ์ผ ์ ์ธ)
|
| 923 |
+
example_images = [
|
| 924 |
+
"data/yolo_dataset/images/train/250818_01.jpg",
|
| 925 |
+
"data/yolo_dataset/images/train/250818_03.jpg",
|
| 926 |
+
"data/yolo_dataset/images/train/250818_04.jpg",
|
| 927 |
+
"data/yolo_dataset/images/train/250818_05.jpg",
|
| 928 |
+
"data/yolo_dataset/images/train/250818_10.jpg",
|
| 929 |
+
]
|
| 930 |
+
|
| 931 |
+
# ํ์ผ์ด ์กด์ฌํ๋ ๊ฒ๋ง ํํฐ๋ง
|
| 932 |
+
example_images = [img for img in example_images if os.path.exists(img)]
|
| 933 |
+
|
| 934 |
+
if example_images:
|
| 935 |
+
gr.Examples(
|
| 936 |
+
examples=[[img] for img in example_images],
|
| 937 |
+
inputs=[input_image_detect],
|
| 938 |
+
label="๐ท ์์ ์ด๋ฏธ์ง"
|
| 939 |
+
)
|
| 940 |
+
|
| 941 |
+
with gr.Column():
|
| 942 |
+
output_image_detect = gr.Image(label="๊ฒ์ถ ๊ฒฐ๊ณผ")
|
| 943 |
+
output_info_detect = gr.Markdown()
|
| 944 |
+
|
| 945 |
+
detect_btn.click(
|
| 946 |
+
interactive_detect,
|
| 947 |
+
[input_image_detect, confidence_slider_detect, filter_slider_detect, show_all_check, model_selector, use_filter_check],
|
| 948 |
+
[output_image_detect, output_info_detect]
|
| 949 |
)
|
| 950 |
|
| 951 |
+
# ํํฐ ์ฌ์ฉ ์ฒดํฌ๋ฐ์ค์ ๋ฐ๋ผ ํํฐ ์ฌ๋ผ์ด๋ ํ์ฑํ/๋นํ์ฑํ
|
| 952 |
+
def update_filter_interactivity(use_filter):
|
| 953 |
+
return gr.update(interactive=use_filter)
|
| 954 |
+
|
| 955 |
+
use_filter_check.change(
|
| 956 |
+
update_filter_interactivity,
|
| 957 |
+
inputs=[use_filter_check],
|
| 958 |
+
outputs=[filter_slider_detect]
|
| 959 |
)
|
| 960 |
|
|
|
|
|
|
|
| 961 |
gr.Markdown("""
|
| 962 |
+
### ๐ก ์ฌ์ฉ ํ
|
| 963 |
+
- ๋ชจ๋ธ์ ์ ํํ๊ณ ์ ๋ขฐ๋๋ฅผ ์กฐ์ ํ์ฌ ๊ฒ์ถ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ์ธ์
|
| 964 |
+
- ๊ฒ์ถ์ด ์ ์ ๋๋ ์ ๋ขฐ๋๋ฅผ ๋ฎ์ถ๊ณ , ์ค๊ฒ์ถ์ด ๋ง์ ๋๋ ๋์ด์ธ์
|
| 965 |
+
- ํํฐ ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ๋ ์ ํํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค
|
| 966 |
+
|
| 967 |
+
**๋ฐ์ค ์์:** ๐ข ๋
น์(๋์ ํ๋ฅ ) | ๐ก ๋
ธ๋์(์ค๊ฐ ํ๋ฅ ) | ๐ ์ฃผํฉ์(๋ฎ์ ํ๋ฅ ) | ๐ด ๋นจ๊ฐ์(์ ๊ฑฐ๋จ)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 968 |
""")
|
| 969 |
|
| 970 |
+
# ==================== ํญ 2: ๋ผ๋ฒจ๋ง ๋๊ตฌ ====================
|
| 971 |
+
with gr.TabItem("๐ Ground Truth ๋ผ๋ฒจ๋ง"):
|
| 972 |
+
gr.Markdown("""
|
| 973 |
+
### ์ ํ๋ ๋ชจ๋ธ์ ๊ฒ์ถ ๊ฒฐ๊ณผ์์ ์ฌ๋ฐ๋ฅธ ๋ฐ์ค๋ง ์ ํํ์ฌ ๋ผ๋ฒจ๋ง
|
| 974 |
+
์ด๋ฏธ์ง๋ฅผ ํด๋ฆญํ์ฌ ์์ฐ ๋ฐ์ค๋ฅผ ์ ํ/ํด์ ํ์ธ์.
|
| 975 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 976 |
|
| 977 |
+
with gr.Row():
|
| 978 |
+
with gr.Column(scale=1):
|
| 979 |
+
folder_dropdown = gr.Dropdown(
|
| 980 |
+
choices=get_folders(),
|
| 981 |
+
label="๐ ํด๋ ์ ํ",
|
| 982 |
+
info="๋ผ๋ฒจ๋งํ ํด๋๋ฅผ ์ ํํ์ธ์"
|
| 983 |
+
)
|
| 984 |
+
|
| 985 |
+
conf_slider_label = gr.Slider(
|
| 986 |
+
0.01, 0.5, 0.2,
|
| 987 |
+
step=0.05,
|
| 988 |
+
label="์ ๋ขฐ๋",
|
| 989 |
+
info="๊ฒ์ถ ๋ฏผ๊ฐ๋ ์กฐ์ "
|
| 990 |
+
)
|
| 991 |
+
|
| 992 |
+
start_btn = gr.Button("โถ๏ธ ๋ผ๋ฒจ๋ง ์์", variant="primary", size="lg")
|
| 993 |
+
|
| 994 |
+
gr.Markdown("---")
|
| 995 |
+
|
| 996 |
+
next_btn = gr.Button("โญ๏ธ ์ ์ฅ & ๋ค์", variant="secondary", size="lg")
|
| 997 |
+
skip_btn = gr.Button("โฉ ๊ฑด๋๋ฐ๊ธฐ", size="lg")
|
| 998 |
+
|
| 999 |
+
labeling_info = gr.Markdown("ํด๋๋ฅผ ์ ํํ๊ณ '๋ผ๋ฒจ๋ง ์์'์ ํด๋ฆญํ์ธ์.")
|
| 1000 |
+
|
| 1001 |
+
with gr.Column(scale=2):
|
| 1002 |
+
labeling_image = gr.Image(
|
| 1003 |
+
label="๐ฑ๏ธ ํด๋ฆญํ์ฌ ๋ฐ์ค ์ ํ/ํด์ ",
|
| 1004 |
+
type="pil",
|
| 1005 |
+
interactive=True
|
| 1006 |
+
)
|
| 1007 |
+
|
| 1008 |
+
labeling_filename = gr.Textbox(visible=False)
|
| 1009 |
+
click_info = gr.Markdown()
|
| 1010 |
+
|
| 1011 |
+
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
| 1012 |
+
start_btn.click(
|
| 1013 |
+
start_labeling,
|
| 1014 |
+
[folder_dropdown, conf_slider_label, model_selector],
|
| 1015 |
+
[labeling_image, labeling_info, labeling_filename]
|
| 1016 |
+
)
|
| 1017 |
+
|
| 1018 |
+
labeling_image.select(
|
| 1019 |
+
labeling_click,
|
| 1020 |
+
[labeling_image, labeling_filename],
|
| 1021 |
+
[labeling_image, click_info]
|
| 1022 |
+
)
|
| 1023 |
+
|
| 1024 |
+
next_btn.click(
|
| 1025 |
+
save_and_next,
|
| 1026 |
+
[],
|
| 1027 |
+
[labeling_image, labeling_info, labeling_filename]
|
| 1028 |
+
)
|
| 1029 |
+
|
| 1030 |
+
skip_btn.click(
|
| 1031 |
+
skip_image,
|
| 1032 |
+
[],
|
| 1033 |
+
[labeling_image, labeling_info, labeling_filename]
|
| 1034 |
+
)
|
| 1035 |
+
|
| 1036 |
+
gr.Markdown("""
|
| 1037 |
+
### ๐ฑ๏ธ ์ฌ์ฉ ๋ฐฉ๋ฒ
|
| 1038 |
+
1. **๋ชจ๋ธ ์ ํ** (์ต์๋จ์์ ์ ํ)
|
| 1039 |
+
2. ํด๋ ์ ํ ํ "๋ผ๋ฒจ๋ง ์์"
|
| 1040 |
+
3. ์ด๋ฏธ์ง์์ **์ํ ๋ฒํผ ํด๋ฆญ** ๋๋ **๋ฐ์ค ์์ญ ํด๋ฆญ**์ผ๋ก ์ ํ/ํด์
|
| 1041 |
+
4. "์ ์ฅ & ๋ค์"์ผ๋ก ๋ค์ ์ด๋ฏธ์ง๋ก ์ด๋ (์๋ ์ ์ฅ)
|
| 1042 |
+
5. "๊ฑด๋๋ฐ๊ธฐ"๋ก ์ ํ ์์ด ๋ค์ ์ด๋ฏธ์ง๋ก
|
| 1043 |
+
|
| 1044 |
+
**๐พ ์ ์ฅ ์์น:** `ground_truth.json` (์๋ ๋ฐฑ์
: `backups/`)
|
| 1045 |
+
""")
|
| 1046 |
+
|
| 1047 |
+
# ==================== ํญ 3: ๊ฐ๋จ ๋ฐ๋ชจ ====================
|
| 1048 |
+
with gr.TabItem("๐ฏ ๊ฐ๋จ ๋ฐ๋ชจ"):
|
| 1049 |
+
gr.Markdown("""
|
| 1050 |
+
### ๋น ๋ฅด๊ณ ๊ฐ๋จํ ์์ฐ ๊ฒ์ถ ๋ฐ๋ชจ
|
| 1051 |
+
์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ๋ฐ๋ก ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ์ธ์.
|
| 1052 |
+
""")
|
| 1053 |
+
|
| 1054 |
+
with gr.Row():
|
| 1055 |
+
with gr.Column():
|
| 1056 |
+
input_image_demo = gr.Image(label="์
๋ ฅ ์ด๋ฏธ์ง", type="pil")
|
| 1057 |
+
|
| 1058 |
+
confidence_slider_demo = gr.Slider(
|
| 1059 |
+
0.01, 1.0, 0.1,
|
| 1060 |
+
step=0.01,
|
| 1061 |
+
label="์ ๋ขฐ๋",
|
| 1062 |
+
info="RT-DETR: 0.065 | VIDraft/Shrimp: 0.3~0.5 | YOLOv8: 0.1~0.3 ๊ถ์ฅ"
|
| 1063 |
+
)
|
| 1064 |
+
|
| 1065 |
+
use_filter_demo = gr.Checkbox(
|
| 1066 |
+
label="๐ ํํฐ ์ ์ ์๊ณ๊ฐ ์ฌ์ฉ",
|
| 1067 |
+
value=False,
|
| 1068 |
+
info="์ฒดํฌํ๋ฉด ํํฐ ์ ์ ๊ธฐ์ค์ผ๋ก ์ถ๊ฐ ํํฐ๋ง"
|
| 1069 |
+
)
|
| 1070 |
+
|
| 1071 |
+
filter_slider_demo = gr.Slider(
|
| 1072 |
+
0, 100, 90,
|
| 1073 |
+
step=5,
|
| 1074 |
+
label="ํํฐ ์๊ณ๊ฐ",
|
| 1075 |
+
info="RT-DETR: Universal Filter | VIDraft/Shrimp: ์ ๋ขฐ๋ ๊ธฐ๋ฐ",
|
| 1076 |
+
visible=True
|
| 1077 |
+
)
|
| 1078 |
+
|
| 1079 |
+
demo_detect_btn = gr.Button("๐ ๊ฒ์ถ", variant="primary", size="lg")
|
| 1080 |
+
|
| 1081 |
+
# ์์ ์ด๋ฏธ์ง
|
| 1082 |
+
example_images_demo = [
|
| 1083 |
+
"data/yolo_dataset/images/train/250818_01.jpg",
|
| 1084 |
+
"data/yolo_dataset/images/train/250818_03.jpg",
|
| 1085 |
+
"data/yolo_dataset/images/train/250818_04.jpg",
|
| 1086 |
+
"data/yolo_dataset/images/train/250818_05.jpg",
|
| 1087 |
+
"data/yolo_dataset/images/train/250818_10.jpg",
|
| 1088 |
+
]
|
| 1089 |
+
|
| 1090 |
+
# ํ์ผ์ด ์กด์ฌํ๋ ๊ฒ๋ง ํํฐ๋ง
|
| 1091 |
+
example_images_demo = [img for img in example_images_demo if os.path.exists(img)]
|
| 1092 |
+
|
| 1093 |
+
if example_images_demo:
|
| 1094 |
+
gr.Examples(
|
| 1095 |
+
examples=[[img] for img in example_images_demo],
|
| 1096 |
+
inputs=[input_image_demo],
|
| 1097 |
+
label="๐ท ์์ ์ด๋ฏธ์ง"
|
| 1098 |
+
)
|
| 1099 |
+
|
| 1100 |
+
with gr.Column():
|
| 1101 |
+
output_image_demo = gr.Image(label="๊ฒ์ถ ๊ฒฐ๊ณผ")
|
| 1102 |
+
output_info_demo = gr.Markdown()
|
| 1103 |
+
|
| 1104 |
+
demo_detect_btn.click(
|
| 1105 |
+
demo_detect,
|
| 1106 |
+
[input_image_demo, confidence_slider_demo, filter_slider_demo, model_selector, use_filter_demo],
|
| 1107 |
+
[output_image_demo, output_info_demo]
|
| 1108 |
+
)
|
| 1109 |
+
|
| 1110 |
+
# ํํฐ ์ฌ์ฉ ์ฒดํฌ๋ฐ์ค์ ๋ฐ๋ผ ํํฐ ์ฌ๋ผ์ด๋ ํ์ฑํ/๋นํ์ฑํ
|
| 1111 |
+
use_filter_demo.change(
|
| 1112 |
+
lambda x: gr.update(interactive=x),
|
| 1113 |
+
inputs=[use_filter_demo],
|
| 1114 |
+
outputs=[filter_slider_demo]
|
| 1115 |
+
)
|
| 1116 |
+
|
| 1117 |
+
gr.Markdown("""
|
| 1118 |
+
### ๐ก ๋น ๋ฅด๊ณ ๊ฐ๋จํ ๊ฒ์ถ
|
| 1119 |
+
์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ฑฐ๋ ์์ ์ด๋ฏธ์ง๋ฅผ ์ ํํ์ฌ ๋ฐ๋ก ๊ฒ์ถ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ์ธ์.
|
| 1120 |
+
""")
|
| 1121 |
|
| 1122 |
gr.Markdown("""
|
| 1123 |
---
|
| 1124 |
|
| 1125 |
+
### ๐ค ๋ชจ๋ธ ์ค๋ช
|
| 1126 |
+
- **RT-DETR**: ๋ก์ปฌ ๋ชจ๋ธ, ๋น ๋ฅธ ์ถ๋ก ์๋, ์คํ๋ผ์ธ ์ฌ์ฉ ๊ฐ๋ฅ
|
| 1127 |
+
- **VIDraft/Shrimp**: ํด๋ผ์ฐ๋ ๋ชจ๋ธ, ์ธํฐ๋ท ์ฐ๊ฒฐ ํ์
|
| 1128 |
+
- **YOLOv8**: ๋ก์ปฌ ์ปค์คํ
ํ์ต ๋ชจ๋ธ, ๋น ๋ฅธ ์ถ๋ก ์๋
|
| 1129 |
|
| 1130 |
+
---
|
| 1131 |
+
|
| 1132 |
+
ยฉ 2025 VIDraft. All rights reserved.
|
|
|
|
| 1133 |
""")
|
| 1134 |
|
| 1135 |
if __name__ == "__main__":
|
| 1136 |
+
print("\n" + "="*60)
|
| 1137 |
+
print("๐ฆ ์์ฐ ๊ฒ์ถ ํตํฉ ์์คํ
v2.1 ์์")
|
| 1138 |
+
print("="*60)
|
| 1139 |
+
print("๐ค ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ธ:")
|
| 1140 |
+
print(" 1. RT-DETR (๋ก์ปฌ)")
|
| 1141 |
+
print(" 2. VIDraft/Shrimp (ํด๋ผ์ฐ๋)")
|
| 1142 |
+
print(" 3. YOLOv8 (๋ก์ปฌ ํ์ต) โญ ๊ธฐ๋ณธ๊ฐ")
|
| 1143 |
+
print(f"\n๐ฆ YOLOv8 ๋ชจ๋ธ: {YOLO_MODEL_PATH}")
|
| 1144 |
+
print("="*60)
|
| 1145 |
+
|
| 1146 |
demo.launch(
|
| 1147 |
server_name="0.0.0.0",
|
| 1148 |
+
server_port=None, # ์๋์ผ๋ก ๋น ํฌํธ ์ฐพ๊ธฐ
|
| 1149 |
share=False
|
| 1150 |
)
|
app_backup.py
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
๋ฐ์ด๋ฉ ๋ฐ์ค ๊ฒ์ถ ํ
์คํธ ํ์ด์ง
|
| 4 |
+
VIDraft/Shrimp ์ ์ฉ ๋ชจ๋ธ๊ณผ RT-DETR ๋ฒ์ฉ ๋ชจ๋ธ์ ๊ฒ์ถ ๊ฒฐ๊ณผ ๋น๊ต
|
| 5 |
+
"""
|
| 6 |
+
import sys
|
| 7 |
+
sys.stdout.reconfigure(encoding='utf-8')
|
| 8 |
+
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 11 |
+
import os
|
| 12 |
+
|
| 13 |
+
# VIDraft/Shrimp ์ ์ฉ ๊ฒ์ถ๊ธฐ
|
| 14 |
+
try:
|
| 15 |
+
from inference_sdk import InferenceHTTPClient, InferenceConfiguration
|
| 16 |
+
|
| 17 |
+
vidraft_client = InferenceHTTPClient(
|
| 18 |
+
api_url="https://serverless.roboflow.com",
|
| 19 |
+
api_key="azcIL8KDJVJMYrsERzI7"
|
| 20 |
+
)
|
| 21 |
+
VIDRAFT_AVAILABLE = True
|
| 22 |
+
print("โ
VIDraft/Shrimp ๋ชจ๋ธ ์ฌ์ฉ ๊ฐ๋ฅ")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
VIDRAFT_AVAILABLE = False
|
| 25 |
+
print(f"โ VIDraft/Shrimp ๋ชจ๋ธ ์ฌ์ฉ ๋ถ๊ฐ: {e}")
|
| 26 |
+
|
| 27 |
+
def detect_with_vidraft(image, confidence, iou_threshold):
|
| 28 |
+
"""VIDraft/Shrimp ์ ์ฉ ๋ชจ๋ธ๋ก ๊ฒ์ถ"""
|
| 29 |
+
if not VIDRAFT_AVAILABLE:
|
| 30 |
+
return None, "โ VIDraft/Shrimp ๋ชจ๋ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค."
|
| 31 |
+
|
| 32 |
+
if image is None:
|
| 33 |
+
return None, "โ ๏ธ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์."
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
# ์์ ํ์ผ๋ก ์ ์ฅ
|
| 37 |
+
import tempfile
|
| 38 |
+
with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as tmp:
|
| 39 |
+
if image.mode != 'RGB':
|
| 40 |
+
image = image.convert('RGB')
|
| 41 |
+
image.save(tmp.name, quality=95)
|
| 42 |
+
tmp_path = tmp.name
|
| 43 |
+
|
| 44 |
+
# API ํธ์ถ with configuration
|
| 45 |
+
custom_config = InferenceConfiguration(
|
| 46 |
+
confidence_threshold=confidence,
|
| 47 |
+
iou_threshold=iou_threshold
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
with vidraft_client.use_configuration(custom_config):
|
| 51 |
+
result = vidraft_client.infer(tmp_path, model_id="shrimp-konvey/2")
|
| 52 |
+
|
| 53 |
+
# ์์ ํ์ผ ์ญ์
|
| 54 |
+
os.unlink(tmp_path)
|
| 55 |
+
|
| 56 |
+
# ๊ฒฐ๊ณผ ๊ทธ๋ฆฌ๊ธฐ
|
| 57 |
+
img = image.copy()
|
| 58 |
+
draw = ImageDraw.Draw(img)
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
font = ImageFont.truetype("arial.ttf", 14)
|
| 62 |
+
except:
|
| 63 |
+
font = ImageFont.load_default()
|
| 64 |
+
|
| 65 |
+
predictions = result["predictions"]
|
| 66 |
+
detected_count = 0
|
| 67 |
+
|
| 68 |
+
for pred in predictions:
|
| 69 |
+
if pred["confidence"] < confidence:
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
detected_count += 1
|
| 73 |
+
|
| 74 |
+
x = pred["x"]
|
| 75 |
+
y = pred["y"]
|
| 76 |
+
w = pred["width"]
|
| 77 |
+
h = pred["height"]
|
| 78 |
+
conf = pred["confidence"]
|
| 79 |
+
|
| 80 |
+
# ๋ฐ์ด๋ฉ ๋ฐ์ค ์ขํ
|
| 81 |
+
x1 = x - w/2
|
| 82 |
+
y1 = y - h/2
|
| 83 |
+
x2 = x + w/2
|
| 84 |
+
y2 = y + h/2
|
| 85 |
+
|
| 86 |
+
# ์ ๋ขฐ๋์ ๋ฐ๋ผ ์์
|
| 87 |
+
if conf > 0.8:
|
| 88 |
+
color = "lime"
|
| 89 |
+
elif conf > 0.6:
|
| 90 |
+
color = "orange"
|
| 91 |
+
else:
|
| 92 |
+
color = "yellow"
|
| 93 |
+
|
| 94 |
+
# ๋ฐ์ค ๊ทธ๋ฆฌ๊ธฐ
|
| 95 |
+
draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
|
| 96 |
+
|
| 97 |
+
# ๋ผ๋ฒจ
|
| 98 |
+
label = f"#{detected_count} {conf:.0%}"
|
| 99 |
+
bbox = draw.textbbox((x1, y1 - 25), label, font=font)
|
| 100 |
+
draw.rectangle(bbox, fill=color)
|
| 101 |
+
draw.text((x1, y1 - 25), label, fill="black", font=font)
|
| 102 |
+
|
| 103 |
+
# ํค๋
|
| 104 |
+
header = f"VIDraft/Shrimp: {detected_count}๋ง๋ฆฌ ๊ฒ์ถ"
|
| 105 |
+
header_bbox = draw.textbbox((10, 10), header, font=font)
|
| 106 |
+
draw.rectangle([5, 5, header_bbox[2]+10, header_bbox[3]+10], fill="black", outline="lime", width=2)
|
| 107 |
+
draw.text((10, 10), header, fill="lime", font=font)
|
| 108 |
+
|
| 109 |
+
info = f"""
|
| 110 |
+
### ๐ VIDraft/Shrimp ๋ชจ๋ธ ๊ฒ์ถ ๊ฒฐ๊ณผ
|
| 111 |
+
|
| 112 |
+
- **๊ฒ์ถ ์**: {detected_count}๋ง๋ฆฌ
|
| 113 |
+
- **์ ์ฒด ์์ธก**: {len(predictions)}๊ฐ
|
| 114 |
+
- **์ ๋ขฐ๋ ์๊ณ๊ฐ**: {confidence:.0%}
|
| 115 |
+
- **IoU ์๊ณ๊ฐ**: {iou_threshold:.0%}
|
| 116 |
+
- **์ฒ๋ฆฌ ์๊ฐ**: {result['time']:.2f}์ด
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
+
return img, info
|
| 120 |
+
|
| 121 |
+
except Exception as e:
|
| 122 |
+
return None, f"โ ์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 123 |
+
|
| 124 |
+
def detect_with_rtdetr(image, confidence):
|
| 125 |
+
"""RT-DETR๋ก ๊ฒ์ถ (๊ฐ๋จ ๋ฒ์ )"""
|
| 126 |
+
if image is None:
|
| 127 |
+
return None, "โ ๏ธ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์."
|
| 128 |
+
|
| 129 |
+
try:
|
| 130 |
+
from transformers import RTDetrForObjectDetection, RTDetrImageProcessor
|
| 131 |
+
import torch
|
| 132 |
+
|
| 133 |
+
# ๋ชจ๋ธ ๋ก๋ (์บ์ ์ฌ์ฉ)
|
| 134 |
+
if not hasattr(detect_with_rtdetr, 'model'):
|
| 135 |
+
print("๐ RT-DETR ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 136 |
+
processor = RTDetrImageProcessor.from_pretrained("PekingU/rtdetr_r50vd_coco_o365")
|
| 137 |
+
model = RTDetrForObjectDetection.from_pretrained("PekingU/rtdetr_r50vd_coco_o365")
|
| 138 |
+
model.eval()
|
| 139 |
+
detect_with_rtdetr.processor = processor
|
| 140 |
+
detect_with_rtdetr.model = model
|
| 141 |
+
print("โ
RT-DETR ๋ก๋ฉ ์๋ฃ")
|
| 142 |
+
|
| 143 |
+
processor = detect_with_rtdetr.processor
|
| 144 |
+
model = detect_with_rtdetr.model
|
| 145 |
+
|
| 146 |
+
# ์ถ๋ก
|
| 147 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 148 |
+
with torch.no_grad():
|
| 149 |
+
outputs = model(**inputs)
|
| 150 |
+
|
| 151 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 152 |
+
results = processor.post_process_object_detection(
|
| 153 |
+
outputs,
|
| 154 |
+
target_sizes=target_sizes,
|
| 155 |
+
threshold=confidence
|
| 156 |
+
)[0]
|
| 157 |
+
|
| 158 |
+
# ๊ฒฐ๊ณผ ๊ทธ๋ฆฌ๊ธฐ
|
| 159 |
+
img = image.copy()
|
| 160 |
+
draw = ImageDraw.Draw(img)
|
| 161 |
+
|
| 162 |
+
try:
|
| 163 |
+
font = ImageFont.truetype("arial.ttf", 14)
|
| 164 |
+
except:
|
| 165 |
+
font = ImageFont.load_default()
|
| 166 |
+
|
| 167 |
+
detected_count = len(results["scores"])
|
| 168 |
+
|
| 169 |
+
for idx, (score, label, box) in enumerate(zip(results["scores"], results["labels"], results["boxes"]), 1):
|
| 170 |
+
x1, y1, x2, y2 = box.tolist()
|
| 171 |
+
conf = score.item()
|
| 172 |
+
|
| 173 |
+
# ์์
|
| 174 |
+
if conf > 0.8:
|
| 175 |
+
color = "cyan"
|
| 176 |
+
elif conf > 0.6:
|
| 177 |
+
color = "magenta"
|
| 178 |
+
else:
|
| 179 |
+
color = "yellow"
|
| 180 |
+
|
| 181 |
+
# ๋ฐ์ค
|
| 182 |
+
draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
|
| 183 |
+
|
| 184 |
+
# ๋ผ๋ฒจ
|
| 185 |
+
label_text = f"#{idx} {conf:.0%}"
|
| 186 |
+
bbox = draw.textbbox((x1, y1 - 25), label_text, font=font)
|
| 187 |
+
draw.rectangle(bbox, fill=color)
|
| 188 |
+
draw.text((x1, y1 - 25), label_text, fill="black", font=font)
|
| 189 |
+
|
| 190 |
+
# ํค๋
|
| 191 |
+
header = f"RT-DETR: {detected_count}๊ฐ ๊ฒ์ถ"
|
| 192 |
+
header_bbox = draw.textbbox((10, 10), header, font=font)
|
| 193 |
+
draw.rectangle([5, 5, header_bbox[2]+10, header_bbox[3]+10], fill="black", outline="cyan", width=2)
|
| 194 |
+
draw.text((10, 10), header, fill="cyan", font=font)
|
| 195 |
+
|
| 196 |
+
info = f"""
|
| 197 |
+
### ๐ RT-DETR ๋ฒ์ฉ ๋ชจ๋ธ ๊ฒ์ถ ๊ฒฐ๊ณผ
|
| 198 |
+
|
| 199 |
+
- **๊ฒ์ถ ์**: {detected_count}๊ฐ
|
| 200 |
+
- **์ ๋ขฐ๋ ์๊ณ๊ฐ**: {confidence:.0%}
|
| 201 |
+
|
| 202 |
+
โ ๏ธ **์ฐธ๊ณ **: RT-DETR์ ๋ฒ์ฉ ๊ฐ์ฒด ๊ฒ์ถ ๋ชจ๋ธ์
๋๋ค. ์์ฐ ๊ฒ์ถ์ VIDraft/Shrimp ๋ชจ๋ธ์ ์ฌ์ฉํ์ธ์.
|
| 203 |
+
"""
|
| 204 |
+
|
| 205 |
+
return img, info
|
| 206 |
+
|
| 207 |
+
except Exception as e:
|
| 208 |
+
return None, f"โ ์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 209 |
+
|
| 210 |
+
# Gradio ์ธํฐํ์ด์ค
|
| 211 |
+
with gr.Blocks(title="๐ฆ ๋ฐ์ด๋ฉ ๋ฐ์ค ๊ฒ์ถ ํ
์คํธ", theme=gr.themes.Soft()) as demo:
|
| 212 |
+
|
| 213 |
+
gr.Markdown("""
|
| 214 |
+
# ๐ฆ ๋ฐ์ด๋ฉ ๋ฐ์ค ๊ฒ์ถ ๋น๊ต ํ
์คํธ
|
| 215 |
+
|
| 216 |
+
VIDraft/Shrimp ์ ์ฉ ๋ชจ๋ธ๊ณผ RT-DETR ๋ฒ์ฉ ๋ชจ๋ธ์ ๊ฒ์ถ ์ฑ๋ฅ์ ๋น๊ตํฉ๋๋ค.
|
| 217 |
+
|
| 218 |
+
---
|
| 219 |
+
""")
|
| 220 |
+
|
| 221 |
+
with gr.Row():
|
| 222 |
+
with gr.Column():
|
| 223 |
+
input_image = gr.Image(label="์
๋ ฅ ์ด๋ฏธ์ง", type="pil")
|
| 224 |
+
confidence_slider = gr.Slider(
|
| 225 |
+
0.1, 0.9, 0.5,
|
| 226 |
+
label="์ ๋ขฐ๋ ์๊ณ๊ฐ (Confidence)",
|
| 227 |
+
info="๋ฎ์์๋ก ๋ ๋ง์ด ๊ฒ์ถ"
|
| 228 |
+
)
|
| 229 |
+
iou_slider = gr.Slider(
|
| 230 |
+
0.1, 0.9, 0.5,
|
| 231 |
+
label="IoU ์๊ณ๊ฐ (Overlap)",
|
| 232 |
+
info="๊ฒน์น๋ ๋ฐ์ค ์ ๊ฑฐ ๊ธฐ์ค (๋์์๋ก ๋ ๋ง์ด ์ ์ง)"
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
# ์์ ์ด๋ฏธ์ง
|
| 236 |
+
gr.Examples(
|
| 237 |
+
examples=[
|
| 238 |
+
["imgs/test_shrimp_tank.png", 0.1, 0.1],
|
| 239 |
+
],
|
| 240 |
+
inputs=[input_image, confidence_slider, iou_slider],
|
| 241 |
+
label="๐ท ์์ ์ด๋ฏธ์ง (ํด๋ฆญํ์ฌ ๋ฐ๋ก ํ
์คํธ)"
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
with gr.Column():
|
| 245 |
+
gr.Markdown("### ๐ ์ฌ์ฉ ๋ฐฉ๋ฒ")
|
| 246 |
+
gr.Markdown("""
|
| 247 |
+
1. **์๋ ์์ ์ด๋ฏธ์ง๋ฅผ ํด๋ฆญ**ํ๊ฑฐ๋ ์ง์ ์
๋ก๋
|
| 248 |
+
2. ํ๋ผ๋ฏธํฐ ์กฐ์ :
|
| 249 |
+
- **Confidence**: ๊ฒ์ถ ์ ๋ขฐ๋ (๋ฎ์์๋ก ๋ ๋ง์ด ๊ฒ์ถ)
|
| 250 |
+
- **IoU**: ์ค๋ณต ๋ฐ์ค ์ ๊ฑฐ ๊ธฐ์ค (NMS)
|
| 251 |
+
3. ๋ฒํผ ํด๋ฆญํ์ฌ ๊ฒ์ถ
|
| 252 |
+
|
| 253 |
+
**์์ ์๋ฏธ:**
|
| 254 |
+
- **๋
น์/์ฒญ๋ก**: ๋์ ์ ๋ขฐ๋ (>80%)
|
| 255 |
+
- **์ฃผํฉ/์ํ**: ์ค๊ฐ ์ ๋ขฐ๋ (60-80%)
|
| 256 |
+
- **๋
ธ๋์**: ๋ฎ์ ์ ๋ขฐ๋ (<60%)
|
| 257 |
+
""")
|
| 258 |
+
|
| 259 |
+
with gr.Tabs():
|
| 260 |
+
with gr.TabItem("๐ค VIDraft/Shrimp (์์ฐ ์ ์ฉ)"):
|
| 261 |
+
vidraft_btn = gr.Button("๐ VIDraft/Shrimp ๋ชจ๋ธ๋ก ๊ฒ์ถ", variant="primary", size="lg")
|
| 262 |
+
vidraft_result = gr.Image(label="๊ฒ์ถ ๊ฒฐ๊ณผ")
|
| 263 |
+
vidraft_info = gr.Markdown()
|
| 264 |
+
|
| 265 |
+
with gr.TabItem("๐ RT-DETR (๋ฒ์ฉ)"):
|
| 266 |
+
rtdetr_btn = gr.Button("๐ RT-DETR๋ก ๊ฒ์ถ", variant="secondary", size="lg")
|
| 267 |
+
rtdetr_result = gr.Image(label="๊ฒ์ถ ๊ฒฐ๊ณผ")
|
| 268 |
+
rtdetr_info = gr.Markdown()
|
| 269 |
+
|
| 270 |
+
# ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
| 271 |
+
vidraft_btn.click(
|
| 272 |
+
detect_with_vidraft,
|
| 273 |
+
[input_image, confidence_slider, iou_slider],
|
| 274 |
+
[vidraft_result, vidraft_info]
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
+
rtdetr_btn.click(
|
| 278 |
+
detect_with_rtdetr,
|
| 279 |
+
[input_image, confidence_slider],
|
| 280 |
+
[rtdetr_result, rtdetr_info]
|
| 281 |
+
)
|
| 282 |
+
|
| 283 |
+
gr.Markdown("""
|
| 284 |
+
---
|
| 285 |
+
|
| 286 |
+
### ๐ก ํ
|
| 287 |
+
|
| 288 |
+
- **์์กฐ ์ด๋ฏธ์ง**: VIDraft/Shrimp ๋ชจ๋ธ์ด ํจ์ฌ ์ ํํฉ๋๋ค (์์ฐ ์ ์ฉ ํ์ต)
|
| 289 |
+
- **์ธก์ ์ฉ ์ด๋ฏธ์ง**: RT-DETR ๋ฒ์ฉ ๋ชจ๋ธ์ ์ฌ์ฉํ์ธ์
|
| 290 |
+
- **๊ฒ์ถ ์ ๋จ**: ์ ๋ขฐ๋๋ฅผ ๋ฎ์ถฐ๋ณด์ธ์ (0.3~0.4)
|
| 291 |
+
- **์ค๋ณต ๋ฐ์ค**: IoU ์๊ณ๊ฐ์ ์กฐ์ ํ์ธ์ (VIDraft/Shrimp ๋ชจ๋ธ๋ง)
|
| 292 |
+
""")
|
| 293 |
+
|
| 294 |
+
if __name__ == "__main__":
|
| 295 |
+
demo.launch(
|
| 296 |
+
server_name="0.0.0.0",
|
| 297 |
+
server_port=7860, # Hugging Face default port
|
| 298 |
+
share=False
|
| 299 |
+
)
|