Adapted preprocessing

This commit is contained in:
2025-05-08 20:05:31 +02:00
parent 341007cb9e
commit 7d4e07afc1
6 changed files with 34 additions and 47 deletions

Before

Width:  |  Height:  |  Size: 2.2 MiB

After

Width:  |  Height:  |  Size: 2.2 MiB

Before

Width:  |  Height:  |  Size: 362 KiB

After

Width:  |  Height:  |  Size: 362 KiB

+24 -28
View File
@@ -1,6 +1,6 @@
import os
import json
from meta_data_extractor import MetaExtractor
from meta_data_extractor import MetaExtractor, ImageMeta
from meta_data_visualizer import visualize_metadata
import cv2
@@ -12,42 +12,38 @@ class DataPreprocessor:
self.image_paths = image_paths
#This method filters the images by specific criteria and extracts and saves the metadata for each filtered image
def _is_accepted(self, meta: dict) -> bool:
"""Implements the specific criteria the OP listed."""
has_person = len(meta["persons"]) > 0
has_object = len(meta["objects"]) > 0
faces_ok = all(p["face_bbox"] for p in meta["persons"])
def _is_accepted(self, meta: ImageMeta) -> bool:
has_person = len(meta.persons) > 0
has_object = len(meta.objects) > 0
faces_ok = all(p.face_bbox for p in meta.persons)
return has_person and has_object and faces_ok
def preprocess_images(self, output_directory):
def preprocess_images(self, output_directory, debug=False):
extractor = MetaExtractor()
if not os.path.exists(output_directory):
os.mkdir(output_directory)
for img_file_path in self.image_paths:
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)
cv2.imshow("ttt", img_vis)
cv2.waitKey()
print("image_meta")
print(image_meta.to_dict())
if debug:
print("Debug Routine")
img = cv2.imread(img_file_path)
cv2.imshow("Before", img)
cv2.waitKey()
img_vis = visualize_metadata(img, image_meta)
cv2.imshow("After", img_vis)
cv2.waitKey()
print("image_meta")
print(image_meta.to_dict())
exit()
exit()
#TODO: hier das extrahierte filtern und speichern für das datenset
is_accepted = self._is_accepted(image_meta)
if is_accepted:
base_name = os.path.basename(img_file_path).split(".")[0]
# 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
# return None
if not self._is_accepted(meta):
continue
print(base_name)
exit()
base_name = os.path.basename(img_file_path).split(".")
with open(os.path.join(output_directory, f"{base_name}.json"), "w") as f:
json.dump(meta, f, indent=2)
with open(os.path.join(output_directory, f"{base_name}.json"), "w") as f:
json.dump(meta, f, indent=2)
+10 -19
View File
@@ -1,4 +1,3 @@
from __future__ import annotations
from dataclasses import dataclass, asdict
from pathlib import Path
@@ -11,24 +10,18 @@ from ultralytics import YOLO
from deepface import DeepFace
import mediapipe as mp
@dataclass
class PersonMeta:
bbox: List[int] # [x0,y0,x1,y1]
segment: List[List[int]] # polygon
body_keypoints: List[List[float]] | None
face_bbox: List[int] | None
face_keypoints: List[List[float]] # n×[x,y,z]
dominant_emotion: str
emotion_vector: Dict[str, float]
@dataclass
class ObjectMeta:
label: str
bbox: List[int]
segment: List[List[int]]
bbox: List[int]# [x0,y0,x1,y1]
segment: List[List[int]]# polygon
@dataclass
class PersonMeta(ObjectMeta):
body_keypoints: List[List[float]] | None
face_bbox: List[int] | None
face_keypoints: List[List[float]] # n×[x,y,z]
dominant_emotion: str
emotion_vector: Dict[str, float]
@dataclass
class ImageMeta:
image: str
@@ -59,9 +52,6 @@ def _deepface_bbox_to_xyxy(face_dict: Dict[str, Any]) -> List[int]:
area["y"] + area["h"]]
# -----------------------------------------------------------------------------
# Main reusable component
# -----------------------------------------------------------------------------
class MetaExtractor:
def __init__(self,
seg_weights: str = "yolo11m-seg.pt",
@@ -149,6 +139,7 @@ class MetaExtractor:
# -- 3) assemble record -----------------------------------------
persons.append(
PersonMeta(
label=self.segmentor.model.names[0],
bbox=[px0, py0, px1, py1],
segment=seg_res.masks.xy[idx].astype(int).tolist() if seg_res.masks is not None else [],
body_keypoints=body_kps,