Adapted "image_accepted" function for filtering of dataset

This commit is contained in:
2025-05-22 11:09:09 +02:00
parent 872e980a56
commit c85107baf4
7 changed files with 38 additions and 11 deletions
Binary file not shown.
+3 -3
View File
@@ -17,9 +17,9 @@ class Config:
verbose=True
tasks = {"preprocessing": False, # optional if already done
"obfuscation": False, # optional if already done
"post_extract": False, # optional if already done
tasks = {"preprocessing": True, # optional if already done
"obfuscation": True, # optional if already done
"post_extract": True, # optional if already done
"analysis": True # mandatory
}
+28 -3
View File
@@ -17,17 +17,42 @@ class DataPreprocessor:
#This method filters the images by specific criteria and extracts and saves the metadata for each filtered image
def _is_accepted(self, meta: ImageMeta) -> bool:
has_person = len(meta.persons) > 0
#Check whether all fields in each person is valid
for person in meta.persons:
if person.label is None or person.label != "person":
return False
if person.bbox is None or len(person.bbox) == 0:
return False
if person.segment is None or len(person.segment) == 0:
return False
if person.face_bbox is None or len(person.face_bbox) == 0:
return False
if person.body_keypoints is None or len(person.body_keypoints) == 0:
return False
if person.face_keypoints is None or len(person.face_keypoints) == 0:
return False
if person.dominant_emotion is None:
return False
if person.emotion_vector is None or len(person.emotion_vector) == 0:
return False
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
for obj in meta.objects:
if obj.label is None or obj.label == "" or obj.label == "person":
return False
if obj.bbox is None or len(obj.bbox) == 0:
return False
if obj.segment is None or len(obj.segment) == 0:
return False
return has_person and has_object
def preprocess_images(self, output_directory, verbose=False):
extractor = MetaExtractor()
random.shuffle(self.image_paths)
print("Current output directory: " + output_directory)
#If the output directory does not exist, create it and it's subdirectories
if not os.path.exists(output_directory):
os.mkdir(output_directory)
os.makedirs(output_directory)
already_done_count = len([f for f in os.listdir(output_directory) if f.endswith(".json")])
print("Already done: " + str(already_done_count) + " images")
print("Remaining: " + str(self.max_images_per_dataset - already_done_count) + " images")
+5 -3
View File
@@ -30,11 +30,10 @@ from obfuscation.obfuscation_engine import ObfuscationEngine
#hier gehts weiter usw.
def main():
#TODO: gute techniken hinzufügen
print("Starting obfuscation platform")
#1: Preprocess images and extract metadata
if Config.tasks["preprocessing"]:
print("1. Running data preprocessing")
data_loader = DatasetLoader(Config.raw_dataset_path)
raw_datasets_dict = data_loader.load_data(verbose=Config.verbose) #This variable contains a dict with the filenames for each dataset
for dataset_name, image_paths in raw_datasets_dict.items():
@@ -44,19 +43,21 @@ def main():
#2: Obfuscate filtered datasets
if Config.tasks["obfuscation"]:
print("2. Running obfuscation")
obfuscation_engine = ObfuscationEngine(Config.obfuscation_techniques)
obfuscation_engine.obfuscate_datasets(Config.filtered_output_directory, Config.obfuscated_output_directory, verbose=Config.verbose)
#3: Extract metadata from obfuscated datasets
if Config.tasks["post_extract"]:
print("3. Running post extraction")
post_extractor = PostExtractor(Config.obfuscated_output_directory)
post_extractor.extract()
#4: Analyze impact of obfuscation
#For each before folder, compare with after folder (after each technique)
if Config.tasks["analysis"]:
print("4. Running analysis")
comparator = Comparator()
print("Starting to analyze impact of obfuscation")
for before_dataset_name in os.listdir(Config.filtered_output_directory):
before_dataset_path = os.path.join(Config.filtered_output_directory, before_dataset_name)
for after_dataset_name in os.listdir(Config.obfuscated_output_directory):
@@ -75,6 +76,7 @@ def main():
print(f"Saving results in {current_technique_results_directory}")
with open(current_technique_results_directory, "w") as f:
json.dump(technique_result_dict, f, indent=4)
#TODO: 5. Visualize results
if __name__ == '__main__':
+2 -2
View File
@@ -134,7 +134,7 @@ def _compute_body_keypoint_similarity(kps_a, kps_b, bbox_a, bbox_b):
return 0.0, []
#Is shape the same?
if len(kps_a) != len(kps_b):
raise ValueError(f"Key-point shapes differ: {kps_a.shape} vs {kps_b.shape}")
raise ValueError(f"Key-point shapes differ: {kps_a} vs {kps_b}")
#Filter keypoints with (x,y) == (0,0) coordinates, based on Bounding box origin //keypoints were shifted by bbox offset in advance, this causes not found keypoints (0,0) to be exactly at bbox origin
_adapt_body_keypoints(kps_a, bbox_a)
_adapt_body_keypoints(kps_b, bbox_b)
@@ -146,7 +146,7 @@ def _compute_face_keypoint_similarity(kps_a, kps_b, bbox_a, bbox_b):
return 0.0, []
#Is shape the same?
if len(kps_a) != len(kps_b):
raise ValueError(f"Key-point shapes differ: {kps_a.shape} vs {kps_b.shape}")
raise ValueError(f"Key-point shapes differ: {kps_a} vs {kps_b}")
kps_a = _adapt_face_keypoints(kps_a, bbox_a)
kps_b = _adapt_face_keypoints(kps_b, bbox_b)
person_keypoint_accuracy, point_position_similarities = _compare_keypoints(kps_a, kps_b)