adapted score computation, to folder-wise aggregation.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
+127
-130
@@ -74,65 +74,81 @@ def _find_best_matching_object(obj, other_objects, min_iou=0.5):
|
||||
best_match = (other_obj, bbox_iou, segment_iou)
|
||||
return best_match
|
||||
|
||||
def _adapt_body_keypoints(kps, bbox, img_w, img_h):
|
||||
def _adapt_body_keypoints(kps, bbox):
|
||||
w = bbox[2] - bbox[0]
|
||||
h = bbox[3] - bbox[1]
|
||||
for kp in kps:
|
||||
#Filter by empty
|
||||
if (kp[0] == bbox[0]) and (kp[1] == bbox[1]):
|
||||
kp[0] = 0
|
||||
kp[1] = 0
|
||||
else:
|
||||
#Normalize by image size
|
||||
kp[0] = kp[0] / img_w
|
||||
kp[1] = kp[1] / img_h
|
||||
#Keypoints are in image space, convert to bbox space
|
||||
kp[0] = (kp[0] - bbox[0])
|
||||
kp[1] = (kp[1] - bbox[1])
|
||||
#Normalize by bbox size
|
||||
kp[0] /= w
|
||||
kp[1] /= h
|
||||
|
||||
def _adapt_face_keypoints(kps, img_w, img_h):
|
||||
adapted_face_keypoints = []
|
||||
def _adapt_face_keypoints(kps, bbox):
|
||||
w = bbox[2] - bbox[0]
|
||||
h = bbox[3] - bbox[1]
|
||||
adapted_kps = []
|
||||
for kp in kps:
|
||||
#Normalize by image size (only for x, y)
|
||||
adapted_face_keypoints.append((kp[0] / img_w, kp[1] / img_h))
|
||||
return adapted_face_keypoints
|
||||
#No filtering by 0,0; keypoints always available, just normalization
|
||||
new_kp = []
|
||||
#Keypoints are in image space, convert to bbox space
|
||||
new_kp.append(kp[0] - bbox[0])
|
||||
new_kp.append(kp[1] - bbox[1])
|
||||
#Normalize by bbox size
|
||||
new_kp[0] = new_kp[0] / w
|
||||
new_kp[1] = new_kp[1] / h
|
||||
adapted_kps.append(new_kp)
|
||||
return adapted_kps
|
||||
|
||||
def _compare_keypoints(kps_a, kps_b):
|
||||
#First check how many keypoints could be found in comparison
|
||||
amount_of_points_found_a = len([kp for kp in kps_a if (kp[0] != 0) and (kp[1] != 0)])
|
||||
amount_of_points_found_b = len([kp for kp in kps_b if (kp[0] != 0) and (kp[1] != 0)])
|
||||
n_point_deviation = abs(amount_of_points_found_a - amount_of_points_found_b) if amount_of_points_found_b > 0 else len(kps_a)
|
||||
same_estimation = 0
|
||||
for idx, kp_a in enumerate(kps_a):
|
||||
kp_b = kps_b[idx]
|
||||
if ((kp_a[0] == 0) and (kp_a[1] == 0)) and ((kp_b[0] == 0) and (kp_b[1] == 0)):
|
||||
same_estimation += 1
|
||||
elif ((kp_a[0] > 0) or (kp_a[1] > 0)) and ((kp_b[0] > 0) or (kp_b[1] > 0)):
|
||||
same_estimation += 1
|
||||
|
||||
person_keypoint_accuracy = (same_estimation / len(kps_a)) * 100.0
|
||||
|
||||
#Then check the accuracy and deviation of the keypoints
|
||||
point_wise_deviations = []
|
||||
point_position_similarities = []
|
||||
for kp_a, kp_b in zip(kps_a, kps_b):
|
||||
if (kp_a[0] == 0) and (kp_a[1] == 0):
|
||||
continue
|
||||
if (kp_b[0] == 0) and (kp_b[1] == 0):
|
||||
continue
|
||||
deviation = np.linalg.norm(np.array(kp_a) - np.array(kp_b))
|
||||
point_wise_deviations.append(deviation)
|
||||
deviation = (1.0 - (np.linalg.norm(np.array(kp_a) - np.array(kp_b)) / math.sqrt(2))) * 100.0
|
||||
point_position_similarities.append(deviation)
|
||||
return person_keypoint_accuracy, point_position_similarities
|
||||
|
||||
average_position_deviation = None
|
||||
if len(point_wise_deviations) > 0:
|
||||
average_position_deviation = np.mean(point_wise_deviations)
|
||||
|
||||
return n_point_deviation / len(kps_a), average_position_deviation
|
||||
|
||||
def _compute_body_keypoint_deviation(kps_a, kps_b, bbox_a, bbox_b, img_w, img_h):
|
||||
#TODO ES KANN SEIN DASS KEYPOINTS LEER SIND UND SOMIT SHAPE ANDERS
|
||||
def _compute_body_keypoint_similarity(kps_a, kps_b, bbox_a, bbox_b):
|
||||
if bbox_b == None or len(bbox_b) == 0:
|
||||
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}")
|
||||
#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, img_w, img_h)
|
||||
_adapt_body_keypoints(kps_b, bbox_b, img_w, img_h)
|
||||
n_point_deviation, avg_position_deviation = _compare_keypoints(kps_a, kps_b)
|
||||
return n_point_deviation, avg_position_deviation
|
||||
_adapt_body_keypoints(kps_a, bbox_a)
|
||||
_adapt_body_keypoints(kps_b, bbox_b)
|
||||
person_keypoint_accuracy, point_position_similarities = _compare_keypoints(kps_a, kps_b)
|
||||
return person_keypoint_accuracy, point_position_similarities
|
||||
|
||||
def _compute_face_keypoint_deviation(kps_a, kps_b, img_w, img_h):
|
||||
def _compute_face_keypoint_similarity(kps_a, kps_b, bbox_a, bbox_b):
|
||||
#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}")
|
||||
|
||||
kps_a = _adapt_face_keypoints(kps_a, img_w, img_h)
|
||||
kps_b = _adapt_face_keypoints(kps_b, img_w, img_h)
|
||||
n_point_deviation, avg_position_deviation = _compare_keypoints(kps_a, kps_b)
|
||||
return n_point_deviation, avg_position_deviation
|
||||
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)
|
||||
return person_keypoint_accuracy, point_position_similarities
|
||||
|
||||
def _vec_from_dict(d) -> np.ndarray:
|
||||
EMOTIONS = ("angry", "disgust", "fear", "happy", "sad", "surprise", "neutral")
|
||||
@@ -156,6 +172,9 @@ def _match_persons(face_img1, face_img2):
|
||||
distance = result["distance"] #Default model: VGG-Face; Cosine Similarity, with tuned threshold
|
||||
return verified, distance
|
||||
|
||||
def write_image_results_to_folder_result_dict(image_result_dict, folder_result_dict):
|
||||
raise NotImplementedError()
|
||||
|
||||
class Comparator:
|
||||
def compare_with_before_and_after_folder(self, before_directory: str, obfuscated_directory: str):
|
||||
#Validate it's the same dataset
|
||||
@@ -168,32 +187,26 @@ class Comparator:
|
||||
# if before_image_count != obfuscated_image_count: #TODO Einkommentiern
|
||||
#raise ValueError("Not same amount of files in both directories", "before_directory", before_directory, "obfuscated_directory", obfuscated_directory)
|
||||
|
||||
folder_result_dict = {
|
||||
"person": {
|
||||
"person_found_accuracy": 0.0,
|
||||
"person_bbox_iou_avg": 0.0,
|
||||
"person_segment_iou_avg": 0.0,
|
||||
"person_n_keypoint_similarity_avg": 0.0,
|
||||
"person_keypoint_position_similarity_avg": 0.0
|
||||
},
|
||||
"face": {
|
||||
"face_found_accuracy": 0.0,
|
||||
"face_bbox_iou_avg": 0.0,
|
||||
"top1_emotion_accuracy": 0.0,
|
||||
"emotion_vector_similarity_avg": 0.0,
|
||||
"obfuscation_accuracy": 0.0,
|
||||
"face_distance_avg": 0.0,
|
||||
#"face_n_keypoint_deviation_avg": face_n_keypoint_deviation_avg,
|
||||
"face_keypoint_position_similarity_avg": 0.0
|
||||
},
|
||||
"object": {
|
||||
"object_found_accuracy": 0.0,
|
||||
"object_bbox_iou_avg": 0.0,
|
||||
"object_segment_iou_avg": 0.0,
|
||||
"object_class_accuracy": 0.0
|
||||
}
|
||||
folder_insights = {
|
||||
"found_matching_persons_count": 0,
|
||||
"before_person_count": 0,
|
||||
"person_bbox_ious": [],
|
||||
"person_segment_ious": [],
|
||||
"person_keypoint_accuracies": [],
|
||||
"person_keypoint_position_similarities": [],
|
||||
"found_face_count": 0,
|
||||
"face_bbox_ious": [],
|
||||
"same_emotion_count": 0,
|
||||
"emotion_vector_similarities": [],
|
||||
"same_person_counter": 0,
|
||||
"identity_distances": [],
|
||||
"face_keypoint_position_similarities": [],
|
||||
"found_matching_objects_count": 0,
|
||||
"before_object_count": 0,
|
||||
"object_bbox_ious": [],
|
||||
"object_segment_ious": [],
|
||||
"found_matching_classes_count": 0
|
||||
}
|
||||
iterations = 0
|
||||
#Iterate through all images in before directory and find the corresponding image in the obfuscated directory
|
||||
for idx, before_image_name in enumerate(tqdm(os.listdir(before_directory))): #TODO Entfernen enumerate
|
||||
if before_image_name.endswith(".jpg"):
|
||||
@@ -201,28 +214,16 @@ class Comparator:
|
||||
before_meta_path = os.path.join(before_directory, before_image_name.replace(".jpg", ".json"))
|
||||
after_meta_path = os.path.join(obfuscated_directory, before_image_name.replace(".jpg", ".json"))
|
||||
|
||||
img_comparison_results = self._compare_metadata(before_meta_path, after_meta_path)
|
||||
|
||||
print(iterations)#TODO Entfernen
|
||||
print(img_comparison_results)
|
||||
img_insights = self._compare_metadata(before_meta_path, after_meta_path)
|
||||
#TODO hier weiter machen und die ergebnisse für den Ordner zusammenfassen
|
||||
#Aggregate meta_data into folder_result_dict
|
||||
for key, value in img_comparison_results.items():
|
||||
for sub_key, sub_value in value.items():
|
||||
folder_result_dict[key][sub_key] += sub_value
|
||||
iterations += 1
|
||||
if iterations == 3:#TODO: entfernen
|
||||
break
|
||||
#Divide by number of images to get averages
|
||||
for key, value in folder_result_dict.items():
|
||||
for sub_key, sub_value in value.items():
|
||||
folder_result_dict[key][sub_key] /= iterations #before_image_count
|
||||
print("after")
|
||||
print(iterations)
|
||||
print(folder_result_dict)
|
||||
write_image_results_to_folder_result_dict(img_insights, folder_insights)
|
||||
|
||||
print(folder_insights)
|
||||
exit()
|
||||
#TODO: FIX --> raise ValueError(f"Key-point shapes differ: {kps_a.shape} vs {kps_b.shape}")
|
||||
|
||||
return folder_result_dict
|
||||
return folder_insights
|
||||
|
||||
#Compare two images
|
||||
def _compare_metadata(self, path_to_meta_before: str, path_to_meta_after: str) -> dict:
|
||||
@@ -234,8 +235,8 @@ class Comparator:
|
||||
found_matching_persons_count = 0
|
||||
person_bbox_ious = []
|
||||
person_segment_ious = []
|
||||
person_n_keypoint_deviations = []
|
||||
person_keypoint_position_deviations = []
|
||||
person_keypoint_accuracies = []
|
||||
person_keypoint_position_similarities = []
|
||||
|
||||
found_face_count = 0
|
||||
face_bbox_ious = []
|
||||
@@ -244,7 +245,7 @@ class Comparator:
|
||||
same_person_counter = 0
|
||||
identity_distances = []
|
||||
#face_n_keypoint_deviations= []
|
||||
face_keypoint_position_deviations = []
|
||||
face_keypoint_position_similarities = []
|
||||
|
||||
#Object scores
|
||||
before_object_count = len(before_meta["objects"])
|
||||
@@ -261,10 +262,9 @@ class Comparator:
|
||||
found_matching_persons_count += 1
|
||||
person_bbox_ious.append(bbox_iou)
|
||||
person_segment_ious.append(segment_iou)
|
||||
n_point_deviation, avg_position_deviation = _compute_body_keypoint_deviation(before_person["body_keypoints"], after_person["body_keypoints"], before_person["bbox"], after_person["bbox"], before_img.shape[1], before_img.shape[0])
|
||||
person_n_keypoint_deviations.append(n_point_deviation)
|
||||
if avg_position_deviation is not None:
|
||||
person_keypoint_position_deviations.append(avg_position_deviation)
|
||||
person_keypoint_accuracy, point_position_similarities = _compute_body_keypoint_similarity(before_person["body_keypoints"], after_person["body_keypoints"], before_person["bbox"], after_person["bbox"])
|
||||
person_keypoint_accuracies.append(person_keypoint_accuracy)
|
||||
person_keypoint_position_similarities.extend(point_position_similarities)
|
||||
|
||||
#Check for validity of before data
|
||||
if before_person["face_bbox"] is None:
|
||||
@@ -284,10 +284,10 @@ class Comparator:
|
||||
if face_verified:
|
||||
same_person_counter += 1
|
||||
identity_distances.append(identity_distance)
|
||||
_, face_avg_position_deviation = _compute_face_keypoint_deviation(before_person["face_keypoints"], after_person["face_keypoints"], before_img.shape[1], before_img.shape[0])
|
||||
#face_n_keypoint_deviations.append(face_n_point_deviation)
|
||||
if face_avg_position_deviation is not None:
|
||||
face_keypoint_position_deviations.append(face_avg_position_deviation)
|
||||
|
||||
_, point_position_similarities = _compute_face_keypoint_similarity(before_person["face_keypoints"], after_person["face_keypoints"], before_person["face_bbox"], after_person["face_bbox"])
|
||||
face_keypoint_position_similarities.extend(point_position_similarities)
|
||||
|
||||
|
||||
for before_object in before_meta["objects"]:
|
||||
result = _find_best_matching_object(before_object, after_meta["objects"])
|
||||
@@ -300,49 +300,46 @@ class Comparator:
|
||||
if before_object["label"] == after_object["label"]:
|
||||
found_matching_classes_count += 1
|
||||
|
||||
person_found_accuracy = (found_matching_persons_count / before_person_count) * 100.0
|
||||
person_bbox_iou_avg = np.mean(person_bbox_ious) * 100.0 if len(person_bbox_ious) > 0 else 0.0
|
||||
person_segment_iou_avg = np.mean(person_segment_ious) * 100.0 if len(person_segment_ious) > 0 else 0.0
|
||||
person_n_keypoint_similarity_avg = (1.0 - np.mean(person_n_keypoint_deviations)) * 100.0 if len(person_n_keypoint_deviations) > 0 else 0.0
|
||||
person_keypoint_position_similarity_avg = (1.0 - np.mean(person_keypoint_position_deviations)) * 100.0 if len(person_keypoint_position_deviations) > 0 else 0.0
|
||||
# #TODO Scores global berechnen und nicht für ein bild konkret
|
||||
# person_found_accuracy = (found_matching_persons_count / before_person_count) * 100.0
|
||||
# person_bbox_iou_avg = np.mean(person_bbox_ious) * 100.0 if len(person_bbox_ious) > 0 else 0.0
|
||||
# person_segment_iou_avg = np.mean(person_segment_ious) * 100.0 if len(person_segment_ious) > 0 else 0.0
|
||||
# person_n_keypoint_similarity_avg = (1.0 - np.mean(person_n_keypoint_deviations)) * 100.0 if len(person_n_keypoint_deviations) > 0 else 0.0 #TODO anders?
|
||||
# person_keypoint_position_similarity_avg = (1.0 - np.mean(person_keypoint_position_deviations)) * 100.0 if len(person_keypoint_position_deviations) > 0 else 0.0 #TODO anders?
|
||||
#
|
||||
# face_found_accuracy = (found_face_count / before_person_count) * 100.0 if found_face_count > 0 else 0.0
|
||||
# face_bbox_iou_avg = np.mean(face_bbox_ious) * 100.0 if len(face_bbox_ious) > 0 else 0.0
|
||||
# top1_emotion_accuracy = ((same_emotion_count / found_face_count) * 100.0) if (found_face_count > 0) else 0.0
|
||||
# emotion_vector_similarity_avg = np.mean(emotion_vector_similarities) * 100.0 if len(emotion_vector_similarities) > 0 else 0.0
|
||||
# obfuscation_accuracy = ((1.0 - (same_person_counter / found_face_count)) * 100.0) if (found_face_count > 0) else 100.0
|
||||
# identity_distance_avg = np.mean(identity_distances) * 100.0 if len(identity_distances) > 0 else 100.0
|
||||
# #face_n_keypoint_deviation_avg = np.mean(face_n_keypoint_deviations)
|
||||
# face_keypoint_position_similarity_avg = (1.0 - np.mean(face_keypoint_position_deviations)) * 100.0 if len(face_keypoint_position_deviations) > 0 else 0.0
|
||||
#
|
||||
# object_found_accuracy = (found_matching_objects_count / before_object_count) * 100.0
|
||||
# object_bbox_iou_avg = np.mean(object_bbox_ious) * 100.0 if len(object_bbox_ious) > 0 else 0.0
|
||||
# object_segment_iou_avg = np.mean(object_segment_ious) * 100.0 if len(object_segment_ious) > 0 else 0.0
|
||||
# object_class_accuracy = (found_matching_classes_count / before_object_count) * 100.0
|
||||
|
||||
face_found_accuracy = (found_face_count / before_person_count) * 100.0 if found_face_count > 0 else 0.0
|
||||
face_bbox_iou_avg = np.mean(face_bbox_ious) * 100.0 if len(face_bbox_ious) > 0 else 0.0
|
||||
top1_emotion_accuracy = ((same_emotion_count / found_face_count) * 100.0) if (found_face_count > 0) else 0.0
|
||||
emotion_vector_similarity_avg = np.mean(emotion_vector_similarities) * 100.0 if len(emotion_vector_similarities) > 0 else 0.0
|
||||
obfuscation_accuracy = ((1.0 - (same_person_counter / found_face_count)) * 100.0) if (found_face_count > 0) else 100.0
|
||||
identity_distance_avg = np.mean(identity_distances) * 100.0 if len(identity_distances) > 0 else 100.0
|
||||
#face_n_keypoint_deviation_avg = np.mean(face_n_keypoint_deviations)
|
||||
face_keypoint_position_similarity_avg = (1.0 - np.mean(face_keypoint_position_deviations)) * 100.0 if len(face_keypoint_position_deviations) > 0 else 0.0
|
||||
|
||||
object_found_accuracy = (found_matching_objects_count / before_object_count) * 100.0
|
||||
object_bbox_iou_avg = np.mean(object_bbox_ious) * 100.0 if len(object_bbox_ious) > 0 else 0.0
|
||||
object_segment_iou_avg = np.mean(object_segment_ious) * 100.0 if len(object_segment_ious) > 0 else 0.0
|
||||
object_class_accuracy = (found_matching_classes_count / before_object_count) * 100.0
|
||||
|
||||
result_score_dict = {
|
||||
"person": {
|
||||
"person_found_accuracy": person_found_accuracy,
|
||||
"person_bbox_iou_avg": person_bbox_iou_avg,
|
||||
"person_segment_iou_avg": person_segment_iou_avg,
|
||||
"person_n_keypoint_similarity_avg": person_n_keypoint_similarity_avg,
|
||||
"person_keypoint_position_similarity_avg": person_keypoint_position_similarity_avg
|
||||
},
|
||||
"face": {
|
||||
"face_found_accuracy": face_found_accuracy,
|
||||
"face_bbox_iou_avg": face_bbox_iou_avg,
|
||||
"top1_emotion_accuracy": top1_emotion_accuracy,
|
||||
"emotion_vector_similarity_avg": emotion_vector_similarity_avg,
|
||||
"obfuscation_accuracy": obfuscation_accuracy,
|
||||
"face_distance_avg": identity_distance_avg,
|
||||
#"face_n_keypoint_deviation_avg": face_n_keypoint_deviation_avg,
|
||||
"face_keypoint_position_similarity_avg": face_keypoint_position_similarity_avg
|
||||
},
|
||||
"object": {
|
||||
"object_found_accuracy": object_found_accuracy,
|
||||
"object_bbox_iou_avg": object_bbox_iou_avg,
|
||||
"object_segment_iou_avg": object_segment_iou_avg,
|
||||
"object_class_accuracy": object_class_accuracy,
|
||||
}
|
||||
image_insights = {
|
||||
"found_matching_persons_count": found_matching_persons_count,
|
||||
"before_person_count": before_person_count,
|
||||
"person_bbox_ious": person_bbox_ious,
|
||||
"person_segment_ious": person_segment_ious,
|
||||
"person_keypoint_accuracies": person_keypoint_accuracies,
|
||||
"person_keypoint_position_similarities": person_keypoint_position_similarities,
|
||||
"found_face_count": found_face_count,
|
||||
"face_bbox_ious": face_bbox_ious,
|
||||
"same_emotion_count": same_emotion_count,
|
||||
"emotion_vector_similarities": emotion_vector_similarities,
|
||||
"same_person_counter": same_person_counter,
|
||||
"identity_distances": identity_distances,
|
||||
"face_keypoint_position_similarities": face_keypoint_position_similarities,
|
||||
"found_matching_objects_count": found_matching_objects_count,
|
||||
"before_object_count": before_object_count,
|
||||
"object_bbox_ious": object_bbox_ious,
|
||||
"object_segment_ious": object_segment_ious,
|
||||
"found_matching_classes_count": found_matching_classes_count
|
||||
}
|
||||
return result_score_dict
|
||||
return image_insights
|
||||
@@ -135,7 +135,6 @@ class MetaExtractor:
|
||||
face_kps = []
|
||||
|
||||
mesh = self._mp_mesh.process(face_crop)
|
||||
|
||||
if mesh.multi_face_landmarks:
|
||||
for lm in mesh.multi_face_landmarks[0].landmark:
|
||||
face_kps.append([
|
||||
|
||||
Reference in New Issue
Block a user