Spaces:
Sleeping
Sleeping
add rr
Browse files
app.py
CHANGED
|
@@ -1,274 +1,256 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import numpy as np
|
| 4 |
-
import pandas as pd
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
-
from PIL import Image
|
| 7 |
-
import cv2
|
| 8 |
-
import threading
|
| 9 |
import time
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
'w_king': {'symbol': 'โ', 'color': '#FFFFFF', 'edge': '#000000'},
|
| 14 |
-
'w_queen': {'symbol': 'โ', 'color': '#FFFFFF', 'edge': '#000000'},
|
| 15 |
-
'w_rook': {'symbol': 'โ', 'color': '#FFFFFF', 'edge': '#000000'},
|
| 16 |
-
'w_bishop': {'symbol': 'โ', 'color': '#FFFFFF', 'edge': '#000000'},
|
| 17 |
-
'w_knight': {'symbol': 'โ', 'color': '#FFFFFF', 'edge': '#000000'},
|
| 18 |
-
'w_pawn': {'symbol': 'โ', 'color': '#FFFFFF', 'edge': '#000000'},
|
| 19 |
-
'b_king': {'symbol': 'โ', 'color': '#000000', 'edge': '#FFFFFF'},
|
| 20 |
-
'b_queen': {'symbol': 'โ', 'color': '#000000', 'edge': '#FFFFFF'},
|
| 21 |
-
'b_rook': {'symbol': 'โ', 'color': '#000000', 'edge': '#FFFFFF'},
|
| 22 |
-
'b_bishop': {'symbol': 'โ', 'color': '#000000', 'edge': '#FFFFFF'},
|
| 23 |
-
'b_knight': {'symbol': 'โ', 'color': '#000000', 'edge': '#FFFFFF'},
|
| 24 |
-
'b_pawn': {'symbol': 'โ', 'color': '#000000', 'edge': '#FFFFFF'}
|
| 25 |
-
}
|
| 26 |
|
| 27 |
-
# ์ ์ญ
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
streaming = False
|
| 31 |
-
stream_thread = None
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
global current_model
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
model = YOLO(weight_path)
|
| 40 |
-
current_model = model
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
names = results[0].names
|
| 45 |
-
dets = []
|
| 46 |
-
for box in boxes:
|
| 47 |
-
cls = int(box.cls[0])
|
| 48 |
-
name = names[cls]
|
| 49 |
-
dets.append({
|
| 50 |
-
'name': name,
|
| 51 |
-
'conf': float(box.conf[0]),
|
| 52 |
-
'xmin': float(box.xyxy[0][0]),
|
| 53 |
-
'ymin': float(box.xyxy[0][1]),
|
| 54 |
-
'xmax': float(box.xyxy[0][2]),
|
| 55 |
-
'ymax': float(box.xyxy[0][3])
|
| 56 |
-
})
|
| 57 |
-
df = pd.DataFrame(dets)
|
| 58 |
-
im_array = results[0].plot()
|
| 59 |
-
im = Image.fromarray(im_array[..., ::-1])
|
| 60 |
-
return im, df
|
| 61 |
-
|
| 62 |
-
# ์น์บ ์คํธ๋ฆผ ์์ ํจ์
|
| 63 |
-
def start_webcam_stream(model_weight_file, conf, iou):
|
| 64 |
-
global current_model, cap, streaming, stream_thread
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
weight_path = model_weight_file.name
|
| 69 |
-
current_model = YOLO(weight_path)
|
| 70 |
-
elif current_model is None:
|
| 71 |
-
current_model = YOLO('yolov8n.pt')
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
# ๋ชจ๋ธ
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
if cap is None:
|
| 123 |
-
cap = cv2.VideoCapture(0)
|
| 124 |
-
if not cap.isOpened():
|
| 125 |
-
yield None
|
| 126 |
-
return
|
| 127 |
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
if not ret:
|
| 133 |
-
break
|
| 134 |
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
annotated_frame = results[0].plot()
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
|
| 142 |
|
| 143 |
-
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
ax.text(0.5, 0.5, 'No pieces detected', ha='center', va='center')
|
| 152 |
-
ax.axis('off')
|
| 153 |
-
return fig
|
| 154 |
-
counts = df['name'].value_counts().sort_index()
|
| 155 |
-
labels = counts.index.tolist()
|
| 156 |
-
colors = [CHESS_PIECES.get(n, {'color': '#888'})['color'] for n in counts.index]
|
| 157 |
-
edges = [CHESS_PIECES.get(n, {'edge': '#888'})['edge'] for n in counts.index]
|
| 158 |
-
fig, ax = plt.subplots(figsize=(8,4))
|
| 159 |
-
bars = ax.bar(labels, counts.values, color=colors, edgecolor=edges, linewidth=2)
|
| 160 |
-
for i, v in enumerate(counts.values):
|
| 161 |
-
text_color = '#000000' if colors[i] == '#FFFFFF' else '#FFFFFF'
|
| 162 |
-
ax.text(i, v-0.1, str(v), ha='center', va='top', fontsize=16, color=text_color, fontweight='bold')
|
| 163 |
-
ax.set_title('Detected Chess Pieces (by symbol)')
|
| 164 |
-
ax.set_ylabel('Count')
|
| 165 |
-
ax.set_xticklabels(labels, fontsize=8, rotation=30)
|
| 166 |
-
return fig
|
| 167 |
-
|
| 168 |
-
# ์ฒด์คํ plot (๊ธฐ๋ฌผ ์์น ์๊ฐํ)
|
| 169 |
-
def plot_chessboard(df, img_shape):
|
| 170 |
-
fig, ax = plt.subplots(figsize=(6,6))
|
| 171 |
-
# ์ฒด์คํ ๊ทธ๋ฆฌ๋
|
| 172 |
-
for i in range(9):
|
| 173 |
-
ax.plot([0,8],[i,i], color='k', lw=1)
|
| 174 |
-
ax.plot([i,i],[0,8], color='k', lw=1)
|
| 175 |
-
# ๊ธฐ๋ฌผ ํ์
|
| 176 |
-
if not df.empty:
|
| 177 |
-
for _, row in df.iterrows():
|
| 178 |
-
x = ((row['xmin']+row['xmax'])/2) / img_shape[1] * 8
|
| 179 |
-
y = ((row['ymin']+row['ymax'])/2) / img_shape[0] * 8
|
| 180 |
-
piece = CHESS_PIECES.get(row['name'], {'symbol': row['name'], 'color': '#888'})
|
| 181 |
-
ax.text(x, 8-y, piece['symbol'], fontsize=28, ha='center', va='center', color=piece['color'], fontweight='bold')
|
| 182 |
-
ax.set_xlim(0,8)
|
| 183 |
-
ax.set_ylim(0,8)
|
| 184 |
-
ax.set_xticks(range(9))
|
| 185 |
-
ax.set_yticks(range(9))
|
| 186 |
-
ax.set_xticklabels([])
|
| 187 |
-
ax.set_yticklabels([])
|
| 188 |
-
ax.set_title('Chessboard Piece Placement')
|
| 189 |
-
ax.set_aspect('equal')
|
| 190 |
-
return fig
|
| 191 |
-
|
| 192 |
-
# Gradio ์ฑ ํจ์ (์ ์ ์ด๋ฏธ์ง์ฉ)
|
| 193 |
-
def gradio_chess(img, model_weight_file, conf, iou):
|
| 194 |
-
im, df = predict_chess(img, model_weight_file, conf, iou)
|
| 195 |
-
piece_fig = plot_piece_count(df)
|
| 196 |
-
board_fig = plot_chessboard(df, np.array(img).shape)
|
| 197 |
-
return im, piece_fig, board_fig
|
| 198 |
-
|
| 199 |
-
# Gradio ์ธํฐํ์ด์ค
|
| 200 |
-
with gr.Blocks() as demo:
|
| 201 |
-
gr.Markdown("""
|
| 202 |
-
# โ๏ธ YOLOv8 Chess Piece Detection
|
| 203 |
-
์ด๋ฏธ์ง ์
๋ก๋ ๋๋ ์ค์๊ฐ ์น์บ ์คํธ๋ฆผ์ผ๋ก ์ฒด์ค ๊ธฐ๋ฌผ ํ์ง ๋ฐ ๋ถ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ค๋๋ค.
|
| 204 |
-
""")
|
| 205 |
-
|
| 206 |
-
with gr.Tab("์ด๋ฏธ์ง ๋ถ์"):
|
| 207 |
-
with gr.Row():
|
| 208 |
-
with gr.Column():
|
| 209 |
-
img_in = gr.Image(type="pil", label="์ฒด์คํ ์ด๋ฏธ์ง ์
๋ก๋")
|
| 210 |
-
model_in = gr.File(label="YOLOv8 ๋ชจ๋ธ ๊ฐ์ค์น ํ์ผ ์
๋ก๋ (.pt, ์ ํ)", file_types=[".pt"])
|
| 211 |
-
conf_in = gr.Slider(0,1,step=0.01,value=0.25,label="Confidence Threshold")
|
| 212 |
-
iou_in = gr.Slider(0,1,step=0.01,value=0.45,label="IoU Threshold")
|
| 213 |
-
btn = gr.Button("๋ถ์ ์คํ")
|
| 214 |
-
with gr.Column():
|
| 215 |
-
img_out = gr.Image(label="ํ์ง ๊ฒฐ๊ณผ ์ด๋ฏธ์ง")
|
| 216 |
-
piece_out = gr.Plot(label="๊ธฐ๋ฌผ๋ณ ๊ฐ์ ์๊ฐํ")
|
| 217 |
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
inputs=[img_in]
|
| 223 |
-
)
|
| 224 |
|
| 225 |
-
with gr.
|
| 226 |
-
with gr.
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
|
|
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
|
|
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
if __name__ == "__main__":
|
| 268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
# ์คํ ๋ฐฉ๋ฒ:
|
| 271 |
-
#
|
| 272 |
-
#
|
| 273 |
-
# 2. ํ์ผ์ app.py๋ก ์ ์ฅํ๊ณ ์คํ:
|
| 274 |
-
# python app.py
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
from ultralytics import YOLO
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
# YOLOv8 ๋ชจ๋ธ ๋ก๋ (์ต์ด ์คํ ์ ์๋ ๋ค์ด๋ก๋)
|
| 8 |
+
model = YOLO("yolov8n.pt") # ๊ฐ์ฅ ๊ฐ๋ฒผ์ด ๋ชจ๋ธ, ๋ ์ ํํ ๋ชจ๋ธ์ yolov8s.pt ๋ฑ ์ฌ์ฉ ๊ฐ๋ฅ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# ์ ์ญ ๋ณ์๋ก FPS ๊ณ์ฐ์ฉ
|
| 11 |
+
frame_count = 0
|
| 12 |
+
start_time = time.time()
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def object_detection(image, conf_threshold=0.25, iou_threshold=0.45, show_labels=True, show_conf=True):
|
| 15 |
+
global frame_count, start_time
|
|
|
|
| 16 |
|
| 17 |
+
if image is None:
|
| 18 |
+
return None
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Gradio์์ ์ด๋ฏธ์ง๋ RGB, OpenCV๋ BGR์ด๋ฏ๋ก ๋ณํ
|
| 21 |
+
img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# YOLO ์ถ๋ก ์คํ
|
| 24 |
+
results = model(img_bgr, conf=conf_threshold, iou=iou_threshold)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# ๋ฐ์ด๋ฉ ๋ฐ์ค ๋ฑ ์๊ฐํ๋ ์ด๋ฏธ์ง(BGR)
|
| 27 |
+
annotated = results[0].plot(
|
| 28 |
+
show_labels=show_labels,
|
| 29 |
+
show_conf=show_conf,
|
| 30 |
+
line_width=2,
|
| 31 |
+
font_size=1
|
| 32 |
+
)
|
| 33 |
|
| 34 |
+
# FPS ๊ณ์ฐ ๋ฐ ํ์
|
| 35 |
+
frame_count += 1
|
| 36 |
+
current_time = time.time()
|
| 37 |
+
if current_time - start_time > 1.0: # 1์ด๋ง๋ค FPS ์
๋ฐ์ดํธ
|
| 38 |
+
fps = frame_count / (current_time - start_time)
|
| 39 |
+
frame_count = 0
|
| 40 |
+
start_time = current_time
|
| 41 |
+
|
| 42 |
+
# FPS๋ฅผ ์ด๋ฏธ์ง์ ํ์
|
| 43 |
+
cv2.putText(annotated, f'FPS: {fps:.1f}', (10, 30),
|
| 44 |
+
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 45 |
|
| 46 |
+
# BGR์ RGB๋ก ๋ณํํ์ฌ ๋ฐํ
|
| 47 |
+
annotated_rgb = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
|
| 48 |
+
return annotated_rgb
|
| 49 |
+
|
| 50 |
+
def reset_fps():
|
| 51 |
+
global frame_count, start_time
|
| 52 |
+
frame_count = 0
|
| 53 |
+
start_time = time.time()
|
| 54 |
+
|
| 55 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 56 |
+
with gr.Blocks(title="์ค์๊ฐ YOLO ๊ฐ์ฒด ํ์ง") as demo:
|
| 57 |
+
gr.Markdown("""
|
| 58 |
+
# ๐ ์ค์๊ฐ YOLO ๊ฐ์ฒด ํ์ง
|
| 59 |
+
์น์บ ์ ํตํด ์ค์๊ฐ์ผ๋ก ๊ฐ์ฒด๋ฅผ ํ์งํฉ๋๋ค. ๋ค์ํ ์ค์ ์ ์กฐ์ ํ์ฌ ํ์ง ์ฑ๋ฅ์ ์ต์ ํํ ์ ์์ต๋๋ค.
|
| 60 |
+
""")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
webcam_input = gr.Image(
|
| 65 |
+
label='๐น ์น์บ ์
๋ ฅ',
|
| 66 |
+
sources="webcam",
|
| 67 |
+
streaming=True,
|
| 68 |
+
width=640,
|
| 69 |
+
height=480
|
| 70 |
+
)
|
| 71 |
|
| 72 |
+
# ์ค์ ํจ๋
|
| 73 |
+
with gr.Group():
|
| 74 |
+
gr.Markdown("### โ๏ธ ํ์ง ์ค์ ")
|
| 75 |
+
conf_slider = gr.Slider(
|
| 76 |
+
minimum=0.1,
|
| 77 |
+
maximum=1.0,
|
| 78 |
+
value=0.25,
|
| 79 |
+
step=0.05,
|
| 80 |
+
label="์ ๋ขฐ๋ ์๊ณ๊ฐ (Confidence Threshold)"
|
| 81 |
+
)
|
| 82 |
+
iou_slider = gr.Slider(
|
| 83 |
+
minimum=0.1,
|
| 84 |
+
maximum=1.0,
|
| 85 |
+
value=0.45,
|
| 86 |
+
step=0.05,
|
| 87 |
+
label="IoU ์๊ณ๊ฐ (Non-Max Suppression)"
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
show_labels_cb = gr.Checkbox(label="๋ผ๋ฒจ ํ์", value=True)
|
| 92 |
+
show_conf_cb = gr.Checkbox(label="์ ๋ขฐ๋ ํ์", value=True)
|
| 93 |
+
|
| 94 |
+
reset_btn = gr.Button("FPS ๋ฆฌ์
", variant="secondary")
|
| 95 |
|
| 96 |
+
with gr.Column():
|
| 97 |
+
webcam_output = gr.Image(
|
| 98 |
+
label='๐ฏ ๊ฐ์ฒด ํ์ง ๊ฒฐ๊ณผ',
|
| 99 |
+
width=640,
|
| 100 |
+
height=480
|
| 101 |
+
)
|
| 102 |
|
| 103 |
+
# ์ ๋ณด ํจ๋
|
| 104 |
+
with gr.Group():
|
| 105 |
+
gr.Markdown("""
|
| 106 |
+
### ๐ ํ์ง ์ ๋ณด
|
| 107 |
+
- **์ ๋ขฐ๋ ์๊ณ๊ฐ**: ๋ฎ์์๋ก ๋ ๋ง์ ๊ฐ์ฒด ํ์ง (false positive ์ฆ๊ฐ)
|
| 108 |
+
- **IoU ์๊ณ๊ฐ**: ์ค๋ณต ํ์ง ์ ๊ฑฐ ๊ธฐ์ค
|
| 109 |
+
- **FPS**: ์ด๋น ํ๋ ์ ์ฒ๋ฆฌ ์๋
|
| 110 |
+
|
| 111 |
+
### ๐ฎ ์ฌ์ฉ ๋ฐฉ๋ฒ
|
| 112 |
+
1. ์น์บ ์ ๊ทผ ๊ถํ ํ์ฉ
|
| 113 |
+
2. ์ค์ ๊ฐ ์กฐ์ ์ผ๋ก ํ์ง ์ฑ๋ฅ ์ต์ ํ
|
| 114 |
+
3. ์ค์๊ฐ์ผ๋ก ๊ฐ์ฒด ํ์ง ๊ฒฐ๊ณผ ํ์ธ
|
| 115 |
+
""")
|
| 116 |
|
| 117 |
+
# ์คํธ๋ฆฌ๋ฐ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
| 118 |
+
webcam_input.stream(
|
| 119 |
+
fn=object_detection,
|
| 120 |
+
inputs=[webcam_input, conf_slider, iou_slider, show_labels_cb, show_conf_cb],
|
| 121 |
+
outputs=[webcam_output],
|
| 122 |
+
stream_every=0.1 # 100ms๋ง๋ค ์ฒ๋ฆฌ (์ฝ 10 FPS)
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# FPS ๋ฆฌ์
๋ฒํผ ์ด๋ฒคํธ
|
| 126 |
+
reset_btn.click(fn=reset_fps, inputs=[], outputs=[])
|
| 127 |
|
| 128 |
+
# ๋ค์ํ ๋ชจ๋ธ ์ต์
์ ์ ๊ณตํ๋ ๊ณ ๊ธ ๋ฒ์
|
| 129 |
+
with gr.Blocks(title="๊ณ ๊ธ YOLO ๊ฐ์ฒด ํ์ง") as advanced_demo:
|
| 130 |
+
gr.Markdown("""
|
| 131 |
+
# ๐ฌ ๊ณ ๊ธ YOLO ๊ฐ์ฒด ํ์ง
|
| 132 |
+
๋ค์ํ YOLO ๋ชจ๋ธ์ ์ ํํ๊ณ ๊ณ ๊ธ ์ค์ ์ ์กฐ์ ํ ์ ์์ต๋๋ค.
|
| 133 |
+
""")
|
| 134 |
|
| 135 |
+
# ๋ชจ๋ธ ์ ํ
|
| 136 |
+
model_choice = gr.Dropdown(
|
| 137 |
+
choices=["yolov8n.pt", "yolov8s.pt", "yolov8m.pt", "yolov8l.pt", "yolov8x.pt"],
|
| 138 |
+
value="yolov8n.pt",
|
| 139 |
+
label="YOLO ๋ชจ๋ธ ์ ํ",
|
| 140 |
+
info="n < s < m < l < x (์๋ vs ์ ํ๋)"
|
| 141 |
+
)
|
| 142 |
|
| 143 |
+
current_model = model # ํ์ฌ ๋ชจ๋ธ ์ ์ฅ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
def change_model(model_name):
|
| 146 |
+
global current_model
|
| 147 |
+
current_model = YOLO(model_name)
|
| 148 |
+
return f"๋ชจ๋ธ์ด {model_name}์ผ๋ก ๋ณ๊ฒฝ๋์์ต๋๋ค."
|
| 149 |
|
| 150 |
+
def advanced_object_detection(image, model_name, conf_threshold, iou_threshold, show_labels, show_conf):
|
| 151 |
+
global current_model, frame_count, start_time
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
if image is None:
|
| 154 |
+
return None
|
|
|
|
| 155 |
|
| 156 |
+
# ๋ชจ๋ธ ๋ณ๊ฒฝ ํ์ธ
|
| 157 |
+
if current_model.model_name != model_name:
|
| 158 |
+
current_model = YOLO(model_name)
|
| 159 |
|
| 160 |
+
img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 161 |
+
results = current_model(img_bgr, conf=conf_threshold, iou=iou_threshold)
|
| 162 |
|
| 163 |
+
annotated = results[0].plot(
|
| 164 |
+
show_labels=show_labels,
|
| 165 |
+
show_conf=show_conf,
|
| 166 |
+
line_width=2,
|
| 167 |
+
font_size=1
|
| 168 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
+
# FPS ๊ณ์ฐ
|
| 171 |
+
frame_count += 1
|
| 172 |
+
current_time = time.time()
|
| 173 |
+
if current_time - start_time > 1.0:
|
| 174 |
+
fps = frame_count / (current_time - start_time)
|
| 175 |
+
frame_count = 0
|
| 176 |
+
start_time = current_time
|
| 177 |
+
|
| 178 |
+
cv2.putText(annotated, f'FPS: {fps:.1f} | Model: {model_name}',
|
| 179 |
+
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
|
| 180 |
|
| 181 |
+
annotated_rgb = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB)
|
| 182 |
+
return annotated_rgb
|
|
|
|
|
|
|
| 183 |
|
| 184 |
+
with gr.Row():
|
| 185 |
+
with gr.Column():
|
| 186 |
+
adv_webcam_input = gr.Image(
|
| 187 |
+
label='๐น ์น์บ ์
๋ ฅ',
|
| 188 |
+
sources="webcam",
|
| 189 |
+
streaming=True,
|
| 190 |
+
width=640,
|
| 191 |
+
height=480
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
with gr.Group():
|
| 195 |
+
gr.Markdown("### ๐๏ธ ๊ณ ๊ธ ์ค์ ")
|
| 196 |
+
adv_conf_slider = gr.Slider(0.1, 1.0, 0.25, 0.05, label="์ ๋ขฐ๋ ์๊ณ๊ฐ")
|
| 197 |
+
adv_iou_slider = gr.Slider(0.1, 1.0, 0.45, 0.05, label="IoU ์๊ณ๊ฐ")
|
| 198 |
|
| 199 |
+
with gr.Row():
|
| 200 |
+
adv_show_labels_cb = gr.Checkbox(label="๋ผ๋ฒจ ํ์", value=True)
|
| 201 |
+
adv_show_conf_cb = gr.Checkbox(label="์ ๋ขฐ๋ ํ์", value=True)
|
| 202 |
|
| 203 |
+
change_model_btn = gr.Button("๋ชจ๋ธ ๋ณ๊ฒฝ", variant="primary")
|
| 204 |
+
model_status = gr.Textbox(label="๋ชจ๋ธ ์ํ", interactive=False)
|
| 205 |
+
|
| 206 |
+
with gr.Column():
|
| 207 |
+
adv_webcam_output = gr.Image(
|
| 208 |
+
label='๐ฏ ๊ฐ์ฒด ํ์ง ๊ฒฐ๊ณผ',
|
| 209 |
+
width=640,
|
| 210 |
+
height=480
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
# ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
| 214 |
+
adv_webcam_input.stream(
|
| 215 |
+
fn=advanced_object_detection,
|
| 216 |
+
inputs=[adv_webcam_input, model_choice, adv_conf_slider, adv_iou_slider,
|
| 217 |
+
adv_show_labels_cb, adv_show_conf_cb],
|
| 218 |
+
outputs=[adv_webcam_output],
|
| 219 |
+
stream_every=0.1
|
| 220 |
+
)
|
| 221 |
+
|
| 222 |
+
change_model_btn.click(
|
| 223 |
+
fn=change_model,
|
| 224 |
+
inputs=[model_choice],
|
| 225 |
+
outputs=[model_status]
|
| 226 |
+
)
|
| 227 |
|
| 228 |
+
# ํญ์ผ๋ก ๊ตฌ์ฑ๋ ์ต์ข
์ธํฐํ์ด์ค
|
| 229 |
+
with gr.Blocks(title="YOLO ์น์บ ๊ฐ์ฒด ํ์ง") as final_demo:
|
| 230 |
+
gr.Markdown("""
|
| 231 |
+
# ๐ฏ YOLO ์น์บ ๊ฐ์ฒด ํ์ง
|
| 232 |
+
์ค์๊ฐ ์น์บ ์คํธ๋ฆผ์์ ๊ฐ์ฒด๋ฅผ ํ์งํฉ๋๋ค.
|
| 233 |
+
""")
|
| 234 |
+
|
| 235 |
+
with gr.Tabs():
|
| 236 |
+
with gr.TabItem("๐ ๊ธฐ๋ณธ ๋ชจ๋"):
|
| 237 |
+
demo.render()
|
| 238 |
+
|
| 239 |
+
with gr.TabItem("๐ฌ ๊ณ ๊ธ ๋ชจ๋"):
|
| 240 |
+
advanced_demo.render()
|
| 241 |
|
| 242 |
if __name__ == "__main__":
|
| 243 |
+
# ๊ธฐ๋ณธ ๋ชจ๋๋ก ์คํ
|
| 244 |
+
demo.launch(
|
| 245 |
+
share=True,
|
| 246 |
+
server_name="0.0.0.0",
|
| 247 |
+
server_port=7860,
|
| 248 |
+
debug=False
|
| 249 |
+
)
|
| 250 |
+
|
| 251 |
+
# ๊ณ ๊ธ ๋ชจ๋๋ก ์คํํ๋ ค๋ฉด ๋ค์ ์ค์ ์ฃผ์์ ํด์ ํ์ธ์:
|
| 252 |
+
# final_demo.launch(share=True, server_name="0.0.0.0", server_port=7860)
|
| 253 |
|
| 254 |
# ์คํ ๋ฐฉ๋ฒ:
|
| 255 |
+
# pip install gradio ultralytics opencv-python
|
| 256 |
+
# python app.py
|
|
|
|
|
|