Fixed problem with segment coordinates

This commit is contained in:
2025-05-08 16:56:39 +02:00
parent 6d7532ab65
commit 341007cb9e
4 changed files with 9 additions and 28 deletions
+5 -4
View File
@@ -25,10 +25,9 @@ class DataPreprocessor:
os.mkdir(output_directory)
for img_file_path in self.image_paths:
image_meta = extractor.extract(img_file_path) #TODO: da drin sind paar todos
#TODO Segment coordinaten anpassen
#TODO: hier das extrahierte filtern usw.
img = cv2.imread(img_file_path)#, cv2.COLOR_BGR2RGB)
image_meta = extractor.extract(img_file_path)
#TODO: Überprüfen von metadatenformat, bspw. anzahl keypoints immer gleich bei gesicht und körper?
img = cv2.imread(img_file_path)
cv2.imshow("xxx", img)
cv2.waitKey()
img_vis = visualize_metadata(img, image_meta)
@@ -38,6 +37,8 @@ class DataPreprocessor:
print(image_meta.to_dict())
exit()
#TODO: hier das extrahierte filtern und speichern für das datenset
# if not persons or not objects: #need ≥1 person and ≥1 other object
# return None
# if any(p.face_bbox is None for p in persons): #each person must have a detectable face
+4 -24
View File
@@ -49,18 +49,6 @@ def _emotion(crop_rgb: np.ndarray) -> Tuple[str, Dict[str, float]]:
return res["dominant_emotion"], res["emotion"]
def _mask_to_poly(mask: np.ndarray) -> List[List[int]]:
"""
Convert a binary mask → largest contour polygon. Empty list if no contour.
"""
cnts, _ = cv2.findContours(mask.astype(np.uint8), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
if not cnts:
return []
pts = max(cnts, key=cv2.contourArea).squeeze(1)
return pts.tolist()
def _deepface_bbox_to_xyxy(face_dict: Dict[str, Any]) -> List[int]:
"""
DeepFace's 'facial_area' dict → [x0,y0,x1,y1] ints.
@@ -75,12 +63,6 @@ def _deepface_bbox_to_xyxy(face_dict: Dict[str, Any]) -> List[int]:
# Main reusable component
# -----------------------------------------------------------------------------
class MetaExtractor:
"""
One instance → loads all heavy models once.
extract(path) → ImageMeta | None
"""
# -------------- construction --------------------------------------------------
def __init__(self,
seg_weights: str = "yolo11m-seg.pt",
pose_weights: str = "yolo11m-pose.pt",
@@ -92,8 +74,8 @@ class MetaExtractor:
self._face_backend = face_backend
# MediaPipe
self._mp_mesh = mp.solutions.face_mesh.FaceMesh(
static_image_mode=True, refine_landmarks=True, max_num_faces=1)
self._mp_mesh = mp.solutions.face_mesh.FaceMesh(static_image_mode=True, refine_landmarks=True, max_num_faces=1)
def extract(self, image_path: str) -> ImageMeta | None:
img_bgr = cv2.imread(str(image_path))
if img_bgr is None:
@@ -111,8 +93,6 @@ class MetaExtractor:
seg_res = self.segmentor(img_rgb, verbose=False)[0]
classes = seg_res.boxes.cls.cpu().numpy().astype(int)
masks = (seg_res.masks.data > 0.5).cpu().numpy() if seg_res.masks is not None else None #TODO warum? TODO: rotes zeug wegbekommen
person_ids = np.where(classes == 0)[0] # COCO class 0 == person
other_ids = np.where(classes != 0)[0]
@@ -170,7 +150,7 @@ class MetaExtractor:
persons.append(
PersonMeta(
bbox=[px0, py0, px1, py1],
segment=_mask_to_poly(masks[idx]) if masks is not None else [],
segment=seg_res.masks.xy[idx].astype(int).tolist() if seg_res.masks is not None else [],
body_keypoints=body_kps,
face_bbox=face_bbox,
face_keypoints=face_kps,
@@ -187,7 +167,7 @@ class MetaExtractor:
ObjectMeta(
label=self.segmentor.model.names[cls_id],
bbox=list(map(int, seg_res.boxes.xyxy[idx].tolist())),
segment=_mask_to_poly(masks[idx]) if masks is not None else [],
segment=seg_res.masks.xy[idx].astype(int).tolist() if seg_res.masks is not None else [],
)
)