Fix RT-DETR module dependency issue
Browse files- Integrate RT-DETR functions directly into app.py
- Remove dependency on deleted test_visual_validation module
- Add load_rtdetr_model(), detect_with_rtdetr()
- Add apply_universal_filter() and helper functions
- Fixes ModuleNotFoundError on Hugging Face
๐ค Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
app.py
CHANGED
|
@@ -22,14 +22,202 @@ from io import BytesIO
|
|
| 22 |
from inference_sdk import InferenceHTTPClient
|
| 23 |
import tempfile
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
#
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# YOLOv8 import
|
| 35 |
# ============================================================
|
|
@@ -234,10 +422,7 @@ def load_rtdetr_on_demand():
|
|
| 234 |
"""RT-DETR ๋ชจ๋ธ์ ํ์์์๋ง ๋ก๋ฉ"""
|
| 235 |
global processor, model
|
| 236 |
if processor is None or model is None:
|
| 237 |
-
print("๐ RT-DETR ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 238 |
-
from test_visual_validation import load_rtdetr_model
|
| 239 |
processor, model = load_rtdetr_model()
|
| 240 |
-
print("โ
RT-DETR ๋ก๋ฉ ์๋ฃ")
|
| 241 |
return "โ
RT-DETR ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ"
|
| 242 |
else:
|
| 243 |
return "โน๏ธ RT-DETR ๋ชจ๋ธ์ด ์ด๋ฏธ ๋ก๋ฉ๋์ด ์์ต๋๋ค"
|
|
@@ -270,7 +455,6 @@ def detect_with_selected_model(image, confidence, model_type):
|
|
| 270 |
if model_type == "RT-DETR":
|
| 271 |
if processor is None or model is None:
|
| 272 |
raise ValueError("โ ๏ธ RT-DETR ๋ชจ๋ธ์ด ๋ก๋ฉ๋์ง ์์์ต๋๋ค. '๐ RT-DETR ๋ก๋' ๋ฒํผ์ ๋จผ์ ํด๋ฆญํ์ธ์.")
|
| 273 |
-
from test_visual_validation import detect_with_rtdetr
|
| 274 |
return detect_with_rtdetr(image, processor, model, confidence)
|
| 275 |
elif model_type == "VIDraft/Shrimp":
|
| 276 |
return detect_with_roboflow(image, confidence)
|
|
@@ -310,7 +494,6 @@ def interactive_detect(image, confidence, filter_threshold, show_all, model_type
|
|
| 310 |
all_detections_scored = all_detections
|
| 311 |
else:
|
| 312 |
# RT-DETR: Universal Filter ์ฌ์ฉ
|
| 313 |
-
from test_visual_validation import apply_universal_filter
|
| 314 |
all_detections_scored = apply_universal_filter(all_detections, image, threshold=0)
|
| 315 |
|
| 316 |
# ํํฐ ์๊ณ๊ฐ ์ ์ฉ
|
|
|
|
| 22 |
from inference_sdk import InferenceHTTPClient
|
| 23 |
import tempfile
|
| 24 |
|
| 25 |
+
# ============================================================
|
| 26 |
+
# RT-DETR ๋ฐ ํํฐ๋ง ํจ์๋ค (์ด์ test_visual_validation์์ ํตํฉ)
|
| 27 |
+
# ============================================================
|
| 28 |
+
import cv2
|
| 29 |
+
|
| 30 |
+
def load_rtdetr_model():
|
| 31 |
+
"""RT-DETR ๋ชจ๋ธ ๋ก๋"""
|
| 32 |
+
print("๐ RT-DETR ๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 33 |
+
processor = RTDetrImageProcessor.from_pretrained("PekingU/rtdetr_r50vd_coco_o365")
|
| 34 |
+
model = RTDetrForObjectDetection.from_pretrained("PekingU/rtdetr_r50vd_coco_o365")
|
| 35 |
+
model.eval()
|
| 36 |
+
print("โ
RT-DETR ๋ก๋ฉ ์๋ฃ")
|
| 37 |
+
return processor, model
|
| 38 |
+
|
| 39 |
+
def detect_with_rtdetr(image, processor, model, confidence=0.3):
|
| 40 |
+
"""RT-DETR๋ก ๊ฐ์ฒด ๊ฒ์ถ"""
|
| 41 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
outputs = model(**inputs)
|
| 44 |
+
|
| 45 |
+
target_sizes = torch.tensor([image.size[::-1]])
|
| 46 |
+
results = processor.post_process_object_detection(
|
| 47 |
+
outputs,
|
| 48 |
+
target_sizes=target_sizes,
|
| 49 |
+
threshold=confidence
|
| 50 |
+
)[0]
|
| 51 |
+
|
| 52 |
+
detections = []
|
| 53 |
+
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 54 |
+
x1, y1, x2, y2 = box.tolist()
|
| 55 |
+
detections.append({
|
| 56 |
+
'bbox': [x1, y1, x2, y2],
|
| 57 |
+
'confidence': score.item(),
|
| 58 |
+
'label': label.item()
|
| 59 |
+
})
|
| 60 |
+
|
| 61 |
+
return detections
|
| 62 |
+
|
| 63 |
+
def calculate_morphological_features(bbox, image_size):
|
| 64 |
+
"""ํํํ์ ํน์ง ๊ณ์ฐ"""
|
| 65 |
+
x1, y1, x2, y2 = bbox
|
| 66 |
+
width = x2 - x1
|
| 67 |
+
height = y2 - y1
|
| 68 |
+
|
| 69 |
+
# Aspect ratio (๊ธด ์ชฝ / ์งง์ ์ชฝ)
|
| 70 |
+
aspect_ratio = max(width, height) / max(min(width, height), 1)
|
| 71 |
+
|
| 72 |
+
# Area ratio (์ด๋ฏธ์ง ๋๋น ๋ฉด์ )
|
| 73 |
+
img_w, img_h = image_size
|
| 74 |
+
area_ratio = (width * height) / (img_w * img_h)
|
| 75 |
+
|
| 76 |
+
# Compactness (4ฯ * Area / Perimeterยฒ)
|
| 77 |
+
perimeter = 2 * (width + height)
|
| 78 |
+
compactness = (4 * np.pi * width * height) / max(perimeter ** 2, 1)
|
| 79 |
+
|
| 80 |
+
return {
|
| 81 |
+
'aspect_ratio': aspect_ratio,
|
| 82 |
+
'area_ratio': area_ratio,
|
| 83 |
+
'compactness': compactness,
|
| 84 |
+
'width': width,
|
| 85 |
+
'height': height
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
def calculate_visual_features(image_pil, bbox):
|
| 89 |
+
"""์๊ฐ์ ํน์ง ๊ณ์ฐ (์์, ํ
์ค์ฒ)"""
|
| 90 |
+
# PIL โ OpenCV
|
| 91 |
+
image_cv = cv2.cvtColor(np.array(image_pil), cv2.COLOR_RGB2BGR)
|
| 92 |
+
x1, y1, x2, y2 = [int(v) for v in bbox]
|
| 93 |
+
|
| 94 |
+
# ๋ฐ์ด๋ฉ ๋ฐ์ค ์์ญ ์ถ์ถ
|
| 95 |
+
roi = image_cv[y1:y2, x1:x2]
|
| 96 |
+
if roi.size == 0:
|
| 97 |
+
return {'hue': 100, 'saturation': 255, 'color_std': 255}
|
| 98 |
+
|
| 99 |
+
# HSV ๋ณํ
|
| 100 |
+
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
|
| 101 |
+
|
| 102 |
+
# ์์ (Hue)
|
| 103 |
+
hue_mean = np.mean(hsv[:, :, 0])
|
| 104 |
+
|
| 105 |
+
# ์ฑ๋ (Saturation)
|
| 106 |
+
saturation = np.mean(hsv[:, :, 1])
|
| 107 |
+
|
| 108 |
+
# ์์ ์ผ๊ด์ฑ (ํ์คํธ์ฐจ)
|
| 109 |
+
color_std = np.std(hsv[:, :, 0])
|
| 110 |
+
|
| 111 |
+
return {
|
| 112 |
+
'hue': hue_mean,
|
| 113 |
+
'saturation': saturation,
|
| 114 |
+
'color_std': color_std
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
def apply_universal_filter(detections, image, threshold=90):
|
| 118 |
+
"""๋ฒ์ฉ ์์ฐ ํํฐ ์ ์ฉ"""
|
| 119 |
+
img_size = image.size
|
| 120 |
+
filtered = []
|
| 121 |
+
|
| 122 |
+
for det in detections:
|
| 123 |
+
bbox = det['bbox']
|
| 124 |
+
|
| 125 |
+
# 1. ํํํ์ ํน์ง
|
| 126 |
+
morph = calculate_morphological_features(bbox, img_size)
|
| 127 |
+
|
| 128 |
+
# 2. ์๊ฐ์ ํน์ง
|
| 129 |
+
visual = calculate_visual_features(image, bbox)
|
| 130 |
+
|
| 131 |
+
# 3. ์ ์ ๊ณ์ฐ
|
| 132 |
+
score = 0
|
| 133 |
+
reasons = []
|
| 134 |
+
|
| 135 |
+
# Aspect ratio (4:1 ~ 9:1)
|
| 136 |
+
if 4.0 <= morph['aspect_ratio'] <= 9.0:
|
| 137 |
+
score += 25
|
| 138 |
+
reasons.append(f"โ ์ข
ํก๋น {morph['aspect_ratio']:.1f}")
|
| 139 |
+
elif 3.0 <= morph['aspect_ratio'] < 4.0 or 9.0 < morph['aspect_ratio'] <= 10.0:
|
| 140 |
+
score += 12
|
| 141 |
+
reasons.append(f"โณ ์ข
ํก๋น {morph['aspect_ratio']:.1f}")
|
| 142 |
+
else:
|
| 143 |
+
score -= 5
|
| 144 |
+
reasons.append(f"โ ์ข
ํก๋น {morph['aspect_ratio']:.1f}")
|
| 145 |
+
|
| 146 |
+
# Compactness (< 0.50, ๊ธด ํํ)
|
| 147 |
+
if morph['compactness'] < 0.40:
|
| 148 |
+
score += 30
|
| 149 |
+
reasons.append(f"โ ์ธ์ฅ๋ {morph['compactness']:.2f}")
|
| 150 |
+
elif 0.40 <= morph['compactness'] < 0.50:
|
| 151 |
+
score += 15
|
| 152 |
+
reasons.append(f"โณ ์ธ์ฅ๋ {morph['compactness']:.2f}")
|
| 153 |
+
else:
|
| 154 |
+
reasons.append(f"โ ์ธ์ฅ๋ {morph['compactness']:.2f}")
|
| 155 |
+
score -= 20
|
| 156 |
+
|
| 157 |
+
# Area
|
| 158 |
+
abs_area = morph['width'] * morph['height']
|
| 159 |
+
if 50000 <= abs_area <= 500000:
|
| 160 |
+
score += 35
|
| 161 |
+
reasons.append(f"โ ๋ฉด์ {abs_area/1000:.0f}K")
|
| 162 |
+
elif 500000 < abs_area <= 800000:
|
| 163 |
+
score -= 10
|
| 164 |
+
reasons.append(f"โณ ๋ฉด์ {abs_area/1000:.0f}K")
|
| 165 |
+
elif abs_area > 800000:
|
| 166 |
+
score -= 30
|
| 167 |
+
reasons.append(f"โ ๋ฉด์ {abs_area/1000:.0f}K (๋๋ฌดํผ)")
|
| 168 |
+
else:
|
| 169 |
+
score -= 10
|
| 170 |
+
reasons.append(f"โ ๋ฉด์ {abs_area/1000:.0f}K (๋๋ฌด์์)")
|
| 171 |
+
|
| 172 |
+
# Hue (์์)
|
| 173 |
+
hue = visual['hue']
|
| 174 |
+
if hue < 40 or hue > 130:
|
| 175 |
+
score += 10
|
| 176 |
+
reasons.append(f"โ ์์ {hue:.0f}")
|
| 177 |
+
elif 90 <= hue <= 130:
|
| 178 |
+
score -= 5
|
| 179 |
+
reasons.append(f"โ ์์ {hue:.0f} (๋ฐฐ๊ฒฝ)")
|
| 180 |
+
else:
|
| 181 |
+
reasons.append(f"โณ ์์ {hue:.0f}")
|
| 182 |
+
|
| 183 |
+
# Saturation
|
| 184 |
+
if visual['saturation'] < 85:
|
| 185 |
+
score += 20
|
| 186 |
+
reasons.append(f"โ ์ฑ๋ {visual['saturation']:.0f}")
|
| 187 |
+
elif 85 <= visual['saturation'] < 120:
|
| 188 |
+
score += 5
|
| 189 |
+
reasons.append(f"โณ ์ฑ๋ {visual['saturation']:.0f}")
|
| 190 |
+
else:
|
| 191 |
+
score -= 15
|
| 192 |
+
reasons.append(f"โ ์ฑ๋ {visual['saturation']:.0f} (๋์)")
|
| 193 |
+
|
| 194 |
+
# Color consistency
|
| 195 |
+
if visual['color_std'] < 50:
|
| 196 |
+
score += 15
|
| 197 |
+
reasons.append(f"โ ์์์ผ๊ด์ฑ {visual['color_std']:.1f}")
|
| 198 |
+
elif 50 <= visual['color_std'] < 80:
|
| 199 |
+
score += 5
|
| 200 |
+
reasons.append(f"โณ ์์์ผ๊ด์ฑ {visual['color_std']:.1f}")
|
| 201 |
+
else:
|
| 202 |
+
score -= 10
|
| 203 |
+
reasons.append(f"โ ์์์ผ๊ด์ฑ {visual['color_std']:.1f} (๋ถ์ผ์น)")
|
| 204 |
+
|
| 205 |
+
# RT-DETR confidence
|
| 206 |
+
if 'confidence' in det:
|
| 207 |
+
if det['confidence'] >= 0.3:
|
| 208 |
+
score += 15
|
| 209 |
+
reasons.append(f"โ ์ ๋ขฐ๋ {det['confidence']:.0%}")
|
| 210 |
+
elif det['confidence'] >= 0.1:
|
| 211 |
+
score += 8
|
| 212 |
+
reasons.append(f"โณ ์ ๋ขฐ๋ {det['confidence']:.0%}")
|
| 213 |
+
else:
|
| 214 |
+
reasons.append(f"โ ์ ๋ขฐ๋ {det['confidence']:.0%}")
|
| 215 |
+
|
| 216 |
+
det['filter_score'] = score
|
| 217 |
+
det['filter_reasons'] = reasons
|
| 218 |
+
filtered.append(det)
|
| 219 |
+
|
| 220 |
+
return filtered
|
| 221 |
|
| 222 |
# YOLOv8 import
|
| 223 |
# ============================================================
|
|
|
|
| 422 |
"""RT-DETR ๋ชจ๋ธ์ ํ์์์๋ง ๋ก๋ฉ"""
|
| 423 |
global processor, model
|
| 424 |
if processor is None or model is None:
|
|
|
|
|
|
|
| 425 |
processor, model = load_rtdetr_model()
|
|
|
|
| 426 |
return "โ
RT-DETR ๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ"
|
| 427 |
else:
|
| 428 |
return "โน๏ธ RT-DETR ๋ชจ๋ธ์ด ์ด๋ฏธ ๋ก๋ฉ๋์ด ์์ต๋๋ค"
|
|
|
|
| 455 |
if model_type == "RT-DETR":
|
| 456 |
if processor is None or model is None:
|
| 457 |
raise ValueError("โ ๏ธ RT-DETR ๋ชจ๋ธ์ด ๋ก๋ฉ๋์ง ์์์ต๋๋ค. '๐ RT-DETR ๋ก๋' ๋ฒํผ์ ๋จผ์ ํด๋ฆญํ์ธ์.")
|
|
|
|
| 458 |
return detect_with_rtdetr(image, processor, model, confidence)
|
| 459 |
elif model_type == "VIDraft/Shrimp":
|
| 460 |
return detect_with_roboflow(image, confidence)
|
|
|
|
| 494 |
all_detections_scored = all_detections
|
| 495 |
else:
|
| 496 |
# RT-DETR: Universal Filter ์ฌ์ฉ
|
|
|
|
| 497 |
all_detections_scored = apply_universal_filter(all_detections, image, threshold=0)
|
| 498 |
|
| 499 |
# ํํฐ ์๊ณ๊ฐ ์ ์ฉ
|