fix: ROIの実装ミス

This commit is contained in:
Keisuke Hirata 2026-02-18 20:21:53 +09:00
parent 08f20fa6fe
commit 9ce6ec99d3
2 changed files with 22 additions and 29 deletions

View File

@ -98,7 +98,7 @@ class YOLOPoseHeadDetector:
""" """
# Standard Ultralytics model — auto-downloaded on first use # Standard Ultralytics model — auto-downloaded on first use
DEFAULT_MODEL = "yolov8n-pose.pt" DEFAULT_MODEL = os.path.join("models", "yolov8n-pose.pt")
def __init__( def __init__(
self, self,
@ -128,6 +128,7 @@ class YOLOPoseHeadDetector:
model_path = self._model_path model_path = self._model_path
else: else:
model_path = self.DEFAULT_MODEL model_path = self.DEFAULT_MODEL
os.makedirs(os.path.dirname(model_path), exist_ok=True)
if torch.cuda.is_available(): if torch.cuda.is_available():
self._device = 'cuda' self._device = 'cuda'

View File

@ -492,11 +492,8 @@ def process_bake_task(task_id: str, req: BakeRequest):
process_queue.put((idx, frame)) process_queue.put((idx, frame))
continue continue
# ROI processing (same as original) # 各人物ごとに個別ROIで処理全員まとめると離れた人物間が巨大ROIになるため
min_x, min_y = src_width, src_height
max_x, max_y = 0, 0
valid_boxes = [] valid_boxes = []
for box in frame_boxes: for box in frame_boxes:
if not isinstance(box, list) or len(box) < 4: if not isinstance(box, list) or len(box) < 4:
continue continue
@ -504,42 +501,37 @@ def process_bake_task(task_id: str, req: BakeRequest):
if w <= 0 or h <= 0: if w <= 0 or h <= 0:
continue continue
valid_boxes.append((x, y, w, h)) valid_boxes.append((x, y, w, h))
min_x = min(min_x, x)
min_y = min(min_y, y)
max_x = max(max_x, x + w)
max_y = max(max_y, y + h)
if not valid_boxes: if not valid_boxes:
process_queue.put((idx, frame)) process_queue.put((idx, frame))
continue continue
roi_x1 = max(0, min_x - blur_margin)
roi_y1 = max(0, min_y - blur_margin)
roi_x2 = min(src_width, max_x + blur_margin)
roi_y2 = min(src_height, max_y + blur_margin)
roi_width = roi_x2 - roi_x1
roi_height = roi_y2 - roi_y1
if roi_width <= 0 or roi_height <= 0:
process_queue.put((idx, frame))
continue
roi_mask = np.zeros((roi_height, roi_width), dtype=np.uint8)
for x, y, w, h in valid_boxes: for x, y, w, h in valid_boxes:
roi_x1 = max(0, x - blur_margin)
roi_y1 = max(0, y - blur_margin)
roi_x2 = min(src_width, x + w + blur_margin)
roi_y2 = min(src_height, y + h + blur_margin)
roi_width = roi_x2 - roi_x1
roi_height = roi_y2 - roi_y1
if roi_width <= 0 or roi_height <= 0:
continue
roi_mask = np.zeros((roi_height, roi_width), dtype=np.uint8)
center = (x + w // 2 - roi_x1, y + h // 2 - roi_y1) center = (x + w // 2 - roi_x1, y + h // 2 - roi_y1)
axes = (max(1, w // 2), max(1, h // 2)) axes = (max(1, w // 2), max(1, h // 2))
cv2.ellipse(roi_mask, center, axes, 0, 0, 360, 255, -1) cv2.ellipse(roi_mask, center, axes, 0, 0, 360, 255, -1)
roi_mask = cv2.GaussianBlur(roi_mask, (feather_kernel, feather_kernel), 0) roi_mask = cv2.GaussianBlur(roi_mask, (feather_kernel, feather_kernel), 0)
roi_src = frame[roi_y1:roi_y2, roi_x1:roi_x2] roi_src = frame[roi_y1:roi_y2, roi_x1:roi_x2]
roi_blurred = cv2.GaussianBlur(roi_src, (blur_size, blur_size), 0) roi_blurred = cv2.GaussianBlur(roi_src, (blur_size, blur_size), 0)
roi_alpha = (roi_mask.astype(np.float32) / 255.0)[..., np.newaxis] roi_alpha = (roi_mask.astype(np.float32) / 255.0)[..., np.newaxis]
roi_composed = (roi_src.astype(np.float32) * (1.0 - roi_alpha)) + ( roi_composed = (roi_src.astype(np.float32) * (1.0 - roi_alpha)) + (
roi_blurred.astype(np.float32) * roi_alpha roi_blurred.astype(np.float32) * roi_alpha
) )
frame[roi_y1:roi_y2, roi_x1:roi_x2] = np.clip(roi_composed, 0, 255).astype(np.uint8)
frame[roi_y1:roi_y2, roi_x1:roi_x2] = np.clip(roi_composed, 0, 255).astype(np.uint8)
process_queue.put((idx, frame)) process_queue.put((idx, frame))
except Exception as e: except Exception as e: