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: Skip face-swapping for images without detectable faces (#504) #508

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions scripts/reactor_swapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def half_det_size(det_size):
def analyze_faces(img_data: np.ndarray, det_size=(640, 640)):
face_analyser = getAnalysisModel(det_size)
faces = face_analyser.get(img_data)

if len(faces) == 0:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem persists because the code inside this if will still run before the code inside the elif. They have to switch places so that the code trying to half det size and re-analyze is run first. Sorry if my previous comment was unclear on this.

logger.status("No face found so skipping this frame")
return faces
# Try halving det_size if no faces are found
if len(faces) == 0 and det_size[0] > 320 and det_size[1] > 320:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code will no longer be executed because for the condition len(faces) == 0 to be true, you will already have returned from the function in the previous if block. Put your new code from above in an elseif on this if instead to avoid breaking this functionality.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated @Wiikend92

det_size_half = half_det_size(det_size)
Expand All @@ -188,7 +190,9 @@ def analyze_faces(img_data: np.ndarray, det_size=(640, 640)):
return faces

def get_face_single(img_data: np.ndarray, face, face_index=0, det_size=(640, 640), gender_source=0, gender_target=0, order="large-small"):

# if no face found no need to swap
if len(face) == 0:
return None, 0
buffalo_path = os.path.join(insightface_models_path, "buffalo_l.zip")
if os.path.exists(buffalo_path):
os.remove(buffalo_path)
Expand Down