Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix face detect when no faces are detected #85

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions hallo/datasets/image_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,19 @@ def preprocess(self, source_image_path: str, cache_dir: str, face_region_ratio:
# 1. image augmentation
pixel_values_ref_img = self._augmentation(ref_image_pil, self.pixel_transform)


# 2.1 detect face
faces = self.face_analysis.get(cv2.cvtColor(np.array(ref_image_pil.copy()), cv2.COLOR_RGB2BGR))
# use max size face
face = sorted(faces, key=lambda x: (x["bbox"][2] - x["bbox"][0]) * (x["bbox"][3] - x["bbox"][1]))[-1]
if not faces:
print("No faces detected in the image. Using the entire image as the face region.")
# Use the entire image as the face region
face = {
"bbox": [0, 0, ref_image_pil.width, ref_image_pil.height],
"embedding": np.zeros(512)
}
else:
# Sort faces by size and select the largest one
faces_sorted = sorted(faces, key=lambda x: (x["bbox"][2] - x["bbox"][0]) * (x["bbox"][3] - x["bbox"][1]), reverse=True)
face = faces_sorted[0] # Select the largest face

# 2.2 face embedding
face_emb = face["embedding"]
Expand Down Expand Up @@ -173,7 +181,7 @@ def preprocess(self, source_image_path: str, cache_dir: str, face_region_ratio:
def close(self):
"""
Closes the ImageProcessor and releases any resources held by the FaceAnalysis instance.

Args:
self: The ImageProcessor instance.

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ numpy==1.26.4
omegaconf==2.3.0
onnx2torch==1.5.14
onnx==1.16.1
onnxruntime==1.18.0
onnxruntime-gpu==1.18.0
opencv-contrib-python==4.9.0.80
opencv-python-headless==4.9.0.80
opencv-python==4.9.0.80
Expand Down
Loading