mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 20:15:21 +00:00
avoid multiple feature extraction for faces in image pairs
This commit is contained in:
parent
715d6317d4
commit
e70496ba6f
@ -110,61 +110,59 @@ def verify(
|
|||||||
)
|
)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
raise ValueError("Exception while processing img2_path") from err
|
raise ValueError("Exception while processing img2_path") from err
|
||||||
# --------------------------------
|
|
||||||
|
img1_embeddings = []
|
||||||
|
for img1_obj in img1_objs:
|
||||||
|
img1_embedding_obj = representation.represent(
|
||||||
|
img_path=img1_obj["face"],
|
||||||
|
model_name=model_name,
|
||||||
|
enforce_detection=enforce_detection,
|
||||||
|
detector_backend="skip",
|
||||||
|
align=align,
|
||||||
|
normalization=normalization,
|
||||||
|
)
|
||||||
|
img1_embedding = img1_embedding_obj[0]["embedding"]
|
||||||
|
img1_embeddings.append(img1_embedding)
|
||||||
|
|
||||||
|
img2_embeddings = []
|
||||||
|
for img2_obj in img2_objs:
|
||||||
|
img2_embedding_obj = representation.represent(
|
||||||
|
img_path=img2_obj["face"],
|
||||||
|
model_name=model_name,
|
||||||
|
enforce_detection=enforce_detection,
|
||||||
|
detector_backend="skip",
|
||||||
|
align=align,
|
||||||
|
normalization=normalization,
|
||||||
|
)
|
||||||
|
img2_embedding = img2_embedding_obj[0]["embedding"]
|
||||||
|
img2_embeddings.append(img2_embedding)
|
||||||
|
|
||||||
distances = []
|
distances = []
|
||||||
regions = []
|
regions = []
|
||||||
# now we will find the face pair with minimum distance
|
for idx, img1_embedding in enumerate(img1_embeddings):
|
||||||
for img1_obj in img1_objs:
|
for idy, img2_embedding in enumerate(img2_embeddings):
|
||||||
img1_content = img1_obj["face"]
|
|
||||||
img1_region = img1_obj["facial_area"]
|
|
||||||
for img2_obj in img2_objs:
|
|
||||||
img2_content = img2_obj["face"]
|
|
||||||
img2_region = img2_obj["facial_area"]
|
|
||||||
img1_embedding_obj = representation.represent(
|
|
||||||
img_path=img1_content,
|
|
||||||
model_name=model_name,
|
|
||||||
enforce_detection=enforce_detection,
|
|
||||||
detector_backend="skip",
|
|
||||||
align=align,
|
|
||||||
normalization=normalization,
|
|
||||||
)
|
|
||||||
|
|
||||||
img2_embedding_obj = representation.represent(
|
|
||||||
img_path=img2_content,
|
|
||||||
model_name=model_name,
|
|
||||||
enforce_detection=enforce_detection,
|
|
||||||
detector_backend="skip",
|
|
||||||
align=align,
|
|
||||||
normalization=normalization,
|
|
||||||
)
|
|
||||||
|
|
||||||
img1_representation = img1_embedding_obj[0]["embedding"]
|
|
||||||
img2_representation = img2_embedding_obj[0]["embedding"]
|
|
||||||
|
|
||||||
if distance_metric == "cosine":
|
if distance_metric == "cosine":
|
||||||
distance = find_cosine_distance(img1_representation, img2_representation)
|
distance = find_cosine_distance(img1_embedding, img2_embedding)
|
||||||
elif distance_metric == "euclidean":
|
elif distance_metric == "euclidean":
|
||||||
distance = find_euclidean_distance(img1_representation, img2_representation)
|
distance = find_euclidean_distance(img1_embedding, img2_embedding)
|
||||||
elif distance_metric == "euclidean_l2":
|
elif distance_metric == "euclidean_l2":
|
||||||
distance = find_euclidean_distance(
|
distance = find_euclidean_distance(
|
||||||
l2_normalize(img1_representation), l2_normalize(img2_representation)
|
l2_normalize(img1_embedding), l2_normalize(img2_embedding)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise ValueError("Invalid distance_metric passed - ", distance_metric)
|
raise ValueError("Invalid distance_metric passed - ", distance_metric)
|
||||||
|
|
||||||
distances.append(distance)
|
distances.append(distance)
|
||||||
regions.append((img1_region, img2_region))
|
regions.append((img1_objs[idx]["facial_area"], img2_objs[idy]["facial_area"]))
|
||||||
|
|
||||||
# -------------------------------
|
# find the face pair with minimum distance
|
||||||
threshold = find_threshold(model_name, distance_metric)
|
threshold = find_threshold(model_name, distance_metric)
|
||||||
distance = min(distances) # best distance
|
distance = min(distances) # best distance
|
||||||
facial_areas = regions[np.argmin(distances)]
|
facial_areas = regions[np.argmin(distances)]
|
||||||
|
|
||||||
toc = time.time()
|
toc = time.time()
|
||||||
|
|
||||||
# pylint: disable=simplifiable-if-expression
|
|
||||||
resp_obj = {
|
resp_obj = {
|
||||||
"verified": True if distance <= threshold else False,
|
"verified": distance <= threshold,
|
||||||
"distance": distance,
|
"distance": distance,
|
||||||
"threshold": threshold,
|
"threshold": threshold,
|
||||||
"model": model_name,
|
"model": model_name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user