Skip to content

Commit

Permalink
Fix face detect when no faces are detected. Install only onnxruntime-…
Browse files Browse the repository at this point in the history
…gpu, otherwise CUDA doesn't work. (#85)
  • Loading branch information
danieldunderfelt authored Jun 24, 2024
1 parent 25f032f commit 01d7fa2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
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

0 comments on commit 01d7fa2

Please sign in to comment.