Files
obfuscation_platform/project/image_preprocessor.py
T

53 lines
1.9 KiB
Python

import os
import json
from meta_data_extractor import MetaExtractor
from meta_data_visualizer import visualize_metadata
import cv2
class DataPreprocessor:
def __init__(self, image_paths):
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"])
return has_person and has_object and faces_ok
def preprocess_images(self, output_directory):
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: da drin sind paar todos
#TODO Segment coordinaten anpassen
#TODO: hier das extrahierte filtern usw.
img = cv2.imread(img_file_path)#, cv2.COLOR_BGR2RGB)
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())
exit()
# 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
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)