Skip to content

Commit

Permalink
Twin support added, make left-right the default face analyser direction
Browse files Browse the repository at this point in the history
  • Loading branch information
henryruhs committed Aug 13, 2023
1 parent fdde5af commit e4936df
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion roop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def parse_args() -> None:
program.add_argument('--keep-temp', help='keep temporary frames', dest='keep_temp', action='store_true')
program.add_argument('--skip-audio', help='skip target audio', dest='skip_audio', action='store_true')
program.add_argument('--face-recognition', help='face recognition method', dest='face_recognition', default='reference', choices=['reference', 'many'])
program.add_argument('--face-analyser-direction', help='direction used for the face analyser', dest='face_analyser_direction', choices=['left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small'])
program.add_argument('--face-analyser-direction', help='direction used for the face analyser', dest='face_analyser_direction', default='left-right', choices=['left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small'])
program.add_argument('--face-analyser-age', help='age used for the face analyser', dest='face_analyser_age', choices=['child', 'teen', 'adult', 'senior'])
program.add_argument('--face-analyser-gender', help='gender used for the face analyser', dest='face_analyser_gender', choices=['male', 'female'])
program.add_argument('--reference-face-position', help='position of the reference face', dest='reference_face_position', type=int, default=0)
Expand Down
11 changes: 6 additions & 5 deletions roop/face_analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def get_one_face(frame: Frame, position: int = 0) -> Optional[Face]:
return None


def get_many_faces(frame: Frame) -> Optional[List[Face]]:
def get_many_faces(frame: Frame) -> List[Face]:
try:
faces = get_face_analyser().get(frame)
if roop.globals.face_analyser_direction:
Expand All @@ -47,18 +47,19 @@ def get_many_faces(frame: Frame) -> Optional[List[Face]]:
faces = filter_by_gender(faces, roop.globals.face_analyser_gender)
return faces
except (AttributeError, ValueError):
return None
return []


def find_similar_face(frame: Frame, reference_face: Face, face_distance: float) -> Optional[Face]:
def find_similar_faces(frame: Frame, reference_face: Face, face_distance: float) -> List[Face]:
many_faces = get_many_faces(frame)
similar_faces = []
if many_faces:
for face in many_faces:
if hasattr(face, 'normed_embedding') and hasattr(reference_face, 'normed_embedding'):
current_face_distance = numpy.sum(numpy.square(face.normed_embedding - reference_face.normed_embedding))
if current_face_distance < face_distance:
return face
return None
similar_faces.append(face)
return similar_faces


def sort_by_direction(faces: List[Face], direction: FaceAnalyserDirection) -> List[Face]:
Expand Down
9 changes: 5 additions & 4 deletions roop/processors/frame/__modules__/face_swapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import roop.globals
import roop.processors.frame.core as frame_processors
from roop.core import update_status
from roop.face_analyser import get_one_face, get_many_faces, find_similar_face
from roop.face_analyser import get_one_face, get_many_faces, find_similar_faces
from roop.face_reference import get_face_reference, set_face_reference, clear_face_reference
from roop.typing import Face, Frame
from roop.utilities import conditional_download, resolve_relative_path, is_image, is_video
Expand Down Expand Up @@ -62,9 +62,10 @@ def swap_face(source_face: Face, target_face: Face, temp_frame: Frame) -> Frame:

def process_frame(source_face: Face, reference_face: Face, temp_frame: Frame) -> Frame:
if 'reference' in roop.globals.face_recognition:
target_face = find_similar_face(temp_frame, reference_face, roop.globals.reference_face_distance)
if target_face:
temp_frame = swap_face(source_face, target_face, temp_frame)
similar_faces = find_similar_faces(temp_frame, reference_face, roop.globals.reference_face_distance)
if similar_faces:
for similar_face in similar_faces:
temp_frame = swap_face(source_face, similar_face, temp_frame)
if 'many' in roop.globals.face_recognition:
many_faces = get_many_faces(temp_frame)
if many_faces:
Expand Down
2 changes: 1 addition & 1 deletion roop/uis/__components__/face_analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def render() -> None:
with gradio.Row():
FACE_ANALYSER_DIRECTION_DROPDOWN = gradio.Dropdown(
label='FACE ANALYSER DIRECTION',
choices=['none', 'left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small'],
choices=['left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small'],
value=roop.globals.face_analyser_direction or 'none'
)
FACE_ANALYSER_AGE_DROPDOWN = gradio.Dropdown(
Expand Down

0 comments on commit e4936df

Please sign in to comment.