Spaces:
Sleeping
Sleeping
Add : First Files
Browse files- app.py +114 -0
- best.pt +3 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# 모델 로드
|
| 7 |
+
model = YOLO("best.pt")
|
| 8 |
+
|
| 9 |
+
def predict_image(image):
|
| 10 |
+
if image is None:
|
| 11 |
+
return None
|
| 12 |
+
|
| 13 |
+
# 원본 RGB 이미지 저장
|
| 14 |
+
original_image = np.array(image)
|
| 15 |
+
original_h, original_w = original_image.shape[:2]
|
| 16 |
+
|
| 17 |
+
# 원본 이미지가 grayscale이면 RGB로 변환
|
| 18 |
+
if len(original_image.shape) == 2:
|
| 19 |
+
original_image = cv2.cvtColor(original_image, cv2.COLOR_GRAY2RGB)
|
| 20 |
+
elif len(original_image.shape) == 3 and original_image.shape[2] == 1:
|
| 21 |
+
original_image = cv2.cvtColor(original_image, cv2.COLOR_GRAY2RGB)
|
| 22 |
+
|
| 23 |
+
# 전처리용 이미지 복사
|
| 24 |
+
input_image = original_image.copy()
|
| 25 |
+
|
| 26 |
+
# 전처리: grayscale 변환, 리사이즈
|
| 27 |
+
# RGB 이미지를 grayscale로 변환
|
| 28 |
+
if len(input_image.shape) == 3 and input_image.shape[2] == 3:
|
| 29 |
+
input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2GRAY)
|
| 30 |
+
# grayscale 이미지를 3채널로 복제 (YOLO의 입력 : 3채널)
|
| 31 |
+
if len(input_image.shape) == 2:
|
| 32 |
+
input_image = cv2.cvtColor(input_image, cv2.COLOR_GRAY2RGB)
|
| 33 |
+
|
| 34 |
+
# 256x256으로 리사이즈
|
| 35 |
+
input_image = cv2.resize(input_image, (256, 256))
|
| 36 |
+
|
| 37 |
+
# 모델 추론
|
| 38 |
+
results = model.predict(
|
| 39 |
+
source=input_image,
|
| 40 |
+
conf=0.25,
|
| 41 |
+
iou=0.7,
|
| 42 |
+
imgsz=256,
|
| 43 |
+
save=False # 임시 파일 저장하지 않음
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# bbox 좌표를 원본 크기로 스케일링
|
| 47 |
+
result = results[0]
|
| 48 |
+
boxes = result.boxes
|
| 49 |
+
|
| 50 |
+
# 스케일 비율 계산 (256x256 -> 원본 크기)
|
| 51 |
+
scale_x = original_w / 256.0
|
| 52 |
+
scale_y = original_h / 256.0
|
| 53 |
+
|
| 54 |
+
# 원본 RGB 이미지에 bbox 그리기
|
| 55 |
+
result_image = original_image.copy()
|
| 56 |
+
|
| 57 |
+
for box in boxes:
|
| 58 |
+
# bbox 좌표 가져오기 (256x256 기준)
|
| 59 |
+
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
| 60 |
+
|
| 61 |
+
# 원본 크기로 스케일링
|
| 62 |
+
x1 = int(x1 * scale_x)
|
| 63 |
+
y1 = int(y1 * scale_y)
|
| 64 |
+
x2 = int(x2 * scale_x)
|
| 65 |
+
y2 = int(y2 * scale_y)
|
| 66 |
+
|
| 67 |
+
# 클래스와 confidence 가져오기
|
| 68 |
+
cls = int(box.cls[0].cpu().numpy())
|
| 69 |
+
conf = float(box.conf[0].cpu().numpy())
|
| 70 |
+
|
| 71 |
+
# bbox 그리기
|
| 72 |
+
cv2.rectangle(result_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 73 |
+
|
| 74 |
+
# 라벨 텍스트
|
| 75 |
+
label = f"{result.names[cls]} {conf:.2f}"
|
| 76 |
+
|
| 77 |
+
# 텍스트 배경 그리기
|
| 78 |
+
(text_width, text_height), baseline = cv2.getTextSize(
|
| 79 |
+
label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1
|
| 80 |
+
)
|
| 81 |
+
cv2.rectangle(
|
| 82 |
+
result_image,
|
| 83 |
+
(x1, y1 - text_height - baseline - 5),
|
| 84 |
+
(x1 + text_width, y1),
|
| 85 |
+
(0, 255, 0),
|
| 86 |
+
-1
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# 텍스트 그리기
|
| 90 |
+
cv2.putText(
|
| 91 |
+
result_image,
|
| 92 |
+
label,
|
| 93 |
+
(x1, y1 - baseline - 2),
|
| 94 |
+
cv2.FONT_HERSHEY_SIMPLEX,
|
| 95 |
+
0.5,
|
| 96 |
+
(0, 0, 0),
|
| 97 |
+
1
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
return result_image
|
| 101 |
+
|
| 102 |
+
# Gradio Interface 생성
|
| 103 |
+
demo = gr.Interface(
|
| 104 |
+
fn=predict_image,
|
| 105 |
+
inputs=gr.Image(type="pil", label="입력 이미지"),
|
| 106 |
+
outputs=gr.Image(type="numpy", label="탐지 결과"),
|
| 107 |
+
title="YOLO 객체 탐지 데모",
|
| 108 |
+
description="이미지를 업로드하면 YOLO 모델이 객체를 탐지합니다.",
|
| 109 |
+
examples=None
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
if __name__ == "__main__":
|
| 113 |
+
demo.launch()
|
| 114 |
+
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:86f894ebc526f7d5131c6b5aaed13a3963a3a773f2017c9d9b1c20e90f9c1432
|
| 3 |
+
size 5425306
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
ultralytics>=8.0.0
|
| 3 |
+
torch>=2.0.0
|
| 4 |
+
torchvision>=0.15.0
|
| 5 |
+
pillow>=9.0.0
|
| 6 |
+
opencv-python>=4.5.0
|
| 7 |
+
numpy>=1.21.0
|
| 8 |
+
|