Files
obfuscation_platform/project/meta_data_visualizer.py
T

84 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import cv2
import numpy as np
from typing import Tuple
from meta_data_extractor import ImageMeta
# -------------------------------------------------------------------
# COCO-17 skeleton pairs (0-based indices in YOLO-v11 pose output)
# -------------------------------------------------------------------
COCO_PAIRS: Tuple[Tuple[int, int], ...] = (
(0,1), (0,2), (1,3), (2,4), # head / shoulders
(5,6), (5,7), (7,9), (6,8), (8,10), # arms
(5,11), (6,12), (11,12), # torso
(11,13), (13,15), (12,14), (14,16) # legs
)
def _color(idx: int) -> Tuple[int, int, int]:
"""
Fast deterministic color (BGR) from an index – avoids identical colours.
"""
np.random.seed(idx * 12345)
return tuple(int(c) for c in np.random.randint(64, 255, size=3))
# -------------------------------------------------------------------
# Main visualiser ---------------------------------------------------
# -------------------------------------------------------------------
def visualize_metadata(img_rgb: np.ndarray, meta: ImageMeta) -> np.ndarray:
"""
Draw all persons + objects on a copy of the input RGB image and return it.
"""
out = img_rgb.copy()
# ------------ objects ------------------------------------------
for i, obj in enumerate(meta.objects):
clr = _color(i)
x0,y0,x1,y1 = obj.bbox
cv2.rectangle(out, (x0,y0), (x1,y1), clr, 2)
cv2.putText(out, obj.label, (x0, y0-6),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, clr, 2, cv2.LINE_AA)
if obj.segment:
poly = np.int32(obj.segment).reshape(-1,1,2)
cv2.polylines(out, [poly], True, clr, 1, cv2.LINE_AA)
# ------------ persons ------------------------------------------
for i, person in enumerate(meta.persons):
base_clr = _color(100 + i) # different palette offset
x0,y0,x1,y1 = person.bbox
cv2.rectangle(out, (x0,y0), (x1,y1), base_clr, 2)
# person mask / polygon
if person.segment:
poly = np.int32(person.segment).reshape(-1,1,2)
cv2.polylines(out, [poly], True, base_clr, 1, cv2.LINE_AA)
# body-pose skeleton
if person.body_keypoints:
kps = np.array(person.body_keypoints, dtype=int) # (17,2)
visible = kps[:,0] > 0
for (p,q) in COCO_PAIRS:
if visible[p] and visible[q]:
cv2.line(out, tuple(kps[p]), tuple(kps[q]),
base_clr, 2, cv2.LINE_AA)
for (x,y) in kps[visible]:
cv2.circle(out, (x,y), 3, base_clr, -1, cv2.LINE_AA)
# face box + mesh
if person.face_bbox:
fx0,fy0,fx1,fy1 = person.face_bbox
cv2.rectangle(out, (fx0,fy0), (fx1,fy1), (0,255,255), 1)
if person.face_keypoints:
for (x,y,_) in person.face_keypoints:
cv2.circle(out, (int(x),int(y)), 1, (0,255,255), -1)
# emotion label (written above face if present, else above body box)
txt_pos = (person.face_bbox[0], person.face_bbox[1]-6) \
if person.face_bbox else (x0, y0-6)
label = f"{person.dominant_emotion}"
cv2.putText(out, label, txt_pos,
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,255), 2, cv2.LINE_AA)
return out