Files

106 lines
4.4 KiB
Python

import os
import json
import shutil
from config import Config
from meta_data.meta_data_extractor import MetaExtractor, ImageMeta
from tqdm import tqdm
from meta_data.meta_data_visualizer import visualize_metadata
import cv2
class DataPreprocessor:
def __init__(self, image_paths, max_images_per_dataset):
self.image_paths = image_paths
self.max_images_per_dataset = max_images_per_dataset
#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:
#Image has at least 1 person (BBox)
#Each person needs to have detected face
#At least 1 object
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) != 4:
return False
if person.segment is None or len(person.segment) < 3:
return False
if person.face_bbox is None or len(person.face_bbox) != 4:
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.face_segment is None or len(person.face_segment) == 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
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) != 4:
return False
if obj.segment is None or len(obj.segment) < 3:
return False
return has_person and has_object
def preprocess_images(self, output_directory, verbose=False):
extractor = MetaExtractor()
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.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")
img_counter = 0
if verbose:
iterator = tqdm(self.image_paths, desc="Preprocessing Images")
else:
iterator = self.image_paths
for img_file_path in iterator:
if img_counter >= (self.max_images_per_dataset - already_done_count):
return
image_meta = extractor.extract(img_file_path)
if Config.debug_show_preprocessing:
self._debug_show(image_meta, img_file_path)
is_accepted = self._is_accepted(image_meta)
if is_accepted:
meta_data_file_path = os.path.join(output_directory, os.path.basename(img_file_path).split(".")[0] + ".json")
output_image_file_path = os.path.join(output_directory, os.path.basename(img_file_path))
if os.path.exists(output_image_file_path):
print("Skipping " + output_image_file_path + " as it already exists")
img_counter += 1
continue
with open(meta_data_file_path, "w") as f:
json.dump(image_meta.to_dict(), f, indent=2)
shutil.copy2(img_file_path, output_image_file_path)
img_counter += 1
def _debug_show(self, image_meta, img_file_path):
print("Debug Routine")
img = cv2.imread(img_file_path)
cv2.imshow("Before", img)
cv2.waitKey()
img_vis = visualize_metadata(img, image_meta)
winname = "Test"
cv2.namedWindow(winname) # Create a named window
cv2.moveWindow(winname, 40,30) # Move it to (40,30)
cv2.imshow(winname, img_vis)
cv2.waitKey()
cv2.destroyAllWindows()
print("image_meta")
print(image_meta.to_dict())
exit()