Skip to content

Commit

Permalink
Split face_analyser and face_selector UI, Adjust filter_by_age values
Browse files Browse the repository at this point in the history
  • Loading branch information
henryruhs committed Aug 12, 2023
1 parent e041759 commit fdde5af
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 49 deletions.
2 changes: 1 addition & 1 deletion roop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def parse_args() -> None:
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-age', help='age used for the face analyser', dest='face_analyser_age', choices=['children', 'teenager', 'adult', 'senior'])
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)
program.add_argument('--reference-face-distance', help='distance between reference face and target face', dest='reference_face_distance', type=float, default=0.85)
Expand Down
6 changes: 3 additions & 3 deletions roop/face_analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ def sort_by_direction(faces: List[Face], direction: FaceAnalyserDirection) -> Li
def filter_by_age(faces: List[Face], age: FaceAnalyserAge) -> List[Face]:
filter_faces = []
for face in faces:
if face['age'] < 10 and age == 'children':
if face['age'] < 13 and age == 'child':
filter_faces.append(face)
elif face['age'] < 20 and age == 'teenager':
elif face['age'] < 19 and age == 'teen':
filter_faces.append(face)
elif face['age'] < 60 and age == 'adult':
filter_faces.append(face)
elif face['age'] < 100 and age == 'senior':
elif face['age'] > 59 and age == 'senior':
filter_faces.append(face)
return filter_faces

Expand Down
2 changes: 1 addition & 1 deletion roop/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

FaceRecognition = Literal['reference', 'many']
FaceAnalyserDirection = Literal['left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small']
FaceAnalyserAge = Literal['children', 'teenager', 'adult', 'senior']
FaceAnalyserAge = Literal['child', 'teen', 'adult', 'senior']
FaceAnalyserGender = Literal['male', 'female']
TempFrameFormat = Literal['jpg', 'png']
OutputVideoEncoder = Literal['libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc']
Expand Down
52 changes: 52 additions & 0 deletions roop/uis/__components__/face_analyser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from typing import Optional

import gradio

import roop.globals
from roop.uis import core as ui
from roop.uis.typing import Update

FACE_ANALYSER_DIRECTION_DROPDOWN: Optional[gradio.Dropdown] = None
FACE_ANALYSER_AGE_DROPDOWN: Optional[gradio.Dropdown] = None
FACE_ANALYSER_GENDER_DROPDOWN: Optional[gradio.Dropdown] = None


def render() -> None:
global FACE_ANALYSER_DIRECTION_DROPDOWN
global FACE_ANALYSER_AGE_DROPDOWN
global FACE_ANALYSER_GENDER_DROPDOWN

with gradio.Box():
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'],
value=roop.globals.face_analyser_direction or 'none'
)
FACE_ANALYSER_AGE_DROPDOWN = gradio.Dropdown(
label='FACE ANALYSER AGE',
choices=['none', 'child', 'teen', 'adult', 'senior'],
value=roop.globals.face_analyser_age or 'none'
)
FACE_ANALYSER_GENDER_DROPDOWN = gradio.Dropdown(
label='FACE ANALYSER GENDER',
choices=['none', 'male', 'female'],
value=roop.globals.face_analyser_gender or 'none'
)
ui.register_component('face_analyser_direction_dropdown', FACE_ANALYSER_DIRECTION_DROPDOWN)
ui.register_component('face_analyser_age_dropdown', FACE_ANALYSER_AGE_DROPDOWN)
ui.register_component('face_analyser_gender_dropdown', FACE_ANALYSER_GENDER_DROPDOWN)


def listen() -> None:
FACE_ANALYSER_DIRECTION_DROPDOWN.select(lambda value: update_dropdown('face_analyser_direction', value), inputs=FACE_ANALYSER_DIRECTION_DROPDOWN, outputs=FACE_ANALYSER_DIRECTION_DROPDOWN)
FACE_ANALYSER_AGE_DROPDOWN.select(lambda value: update_dropdown('face_analyser_age', value), inputs=FACE_ANALYSER_AGE_DROPDOWN, outputs=FACE_ANALYSER_AGE_DROPDOWN)
FACE_ANALYSER_GENDER_DROPDOWN.select(lambda value: update_dropdown('face_analyser_gender', value), inputs=FACE_ANALYSER_GENDER_DROPDOWN, outputs=FACE_ANALYSER_GENDER_DROPDOWN)


def update_dropdown(name: str, value: str) -> Update:
if value == 'none':
setattr(roop.globals, name, None)
else:
setattr(roop.globals, name, value)
return gradio.update(value=value)
56 changes: 13 additions & 43 deletions roop/uis/__components__/face_selector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional, Tuple
from typing import List, Optional, Tuple, Any, Dict
from time import sleep

import cv2
Expand All @@ -16,21 +16,15 @@
FACE_RECOGNITION_DROPDOWN: Optional[gradio.Dropdown] = None
REFERENCE_FACE_POSITION_GALLERY: Optional[gradio.Gallery] = None
REFERENCE_FACE_DISTANCE_SLIDER: Optional[gradio.Slider] = None
FACE_ANALYSER_DIRECTION_DROPDOWN: Optional[gradio.Dropdown] = None
FACE_ANALYSER_AGE_DROPDOWN: Optional[gradio.Dropdown] = None
FACE_ANALYSER_GENDER_DROPDOWN: Optional[gradio.Dropdown] = None


def render() -> None:
global FACE_RECOGNITION_DROPDOWN
global REFERENCE_FACE_POSITION_GALLERY
global REFERENCE_FACE_DISTANCE_SLIDER
global FACE_ANALYSER_DIRECTION_DROPDOWN
global FACE_ANALYSER_AGE_DROPDOWN
global FACE_ANALYSER_GENDER_DROPDOWN

with gradio.Box():
reference_face_gallery_args = {
reference_face_gallery_args: Dict[str, Any] = {
'label': 'REFERENCE FACE',
'height': 120,
'object_fit': 'cover',
Expand All @@ -57,48 +51,32 @@ def render() -> None:
step=0.05,
visible='reference' in roop.globals.face_recognition
)
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'],
value=roop.globals.face_analyser_direction or 'none'
)
FACE_ANALYSER_AGE_DROPDOWN = gradio.Dropdown(
label='FACE ANALYSER AGE',
choices=['none', 'children', 'teenager', 'adult', 'senior'],
value=roop.globals.face_analyser_age or 'none'
)
FACE_ANALYSER_GENDER_DROPDOWN = gradio.Dropdown(
label='FACE ANALYSER GENDER',
choices=['none', 'male', 'female'],
value=roop.globals.face_analyser_gender or 'none'
)
ui.register_component('face_recognition_dropdown', FACE_RECOGNITION_DROPDOWN)
ui.register_component('reference_face_position_gallery', REFERENCE_FACE_POSITION_GALLERY)
ui.register_component('reference_face_distance_slider', REFERENCE_FACE_DISTANCE_SLIDER)
ui.register_component('face_analyser_direction_dropdown', FACE_ANALYSER_DIRECTION_DROPDOWN)
ui.register_component('face_analyser_age_dropdown', FACE_ANALYSER_AGE_DROPDOWN)
ui.register_component('face_analyser_gender_dropdown', FACE_ANALYSER_GENDER_DROPDOWN)


def listen() -> None:
FACE_RECOGNITION_DROPDOWN.select(update_face_recognition, inputs=FACE_RECOGNITION_DROPDOWN, outputs=[REFERENCE_FACE_POSITION_GALLERY, REFERENCE_FACE_DISTANCE_SLIDER])
REFERENCE_FACE_POSITION_GALLERY.select(clear_and_update_face_reference_position)
REFERENCE_FACE_DISTANCE_SLIDER.change(update_reference_face_distance, inputs=REFERENCE_FACE_DISTANCE_SLIDER)
component_names: List[ComponentName] = [
update_component_names: List[ComponentName] = [
'target_file',
'preview_frame_slider'
]
for component_name in component_names:
for component_name in update_component_names:
component = ui.get_component(component_name)
if component:
component.change(update_face_reference_position, outputs=REFERENCE_FACE_POSITION_GALLERY)
FACE_ANALYSER_DIRECTION_DROPDOWN.select(lambda value: update_dropdown('face_analyser_direction', value), inputs=FACE_ANALYSER_DIRECTION_DROPDOWN, outputs=FACE_ANALYSER_DIRECTION_DROPDOWN)
FACE_ANALYSER_AGE_DROPDOWN.select(lambda value: update_dropdown('face_analyser_age', value), inputs=FACE_ANALYSER_AGE_DROPDOWN, outputs=FACE_ANALYSER_AGE_DROPDOWN)
FACE_ANALYSER_GENDER_DROPDOWN.select(lambda value: update_dropdown('face_analyser_gender', value), inputs=FACE_ANALYSER_GENDER_DROPDOWN, outputs=FACE_ANALYSER_GENDER_DROPDOWN)
FACE_ANALYSER_DIRECTION_DROPDOWN.select(update_face_reference_position, outputs=REFERENCE_FACE_POSITION_GALLERY)
FACE_ANALYSER_AGE_DROPDOWN.select(update_face_reference_position, outputs=REFERENCE_FACE_POSITION_GALLERY)
FACE_ANALYSER_GENDER_DROPDOWN.select(update_face_reference_position, outputs=REFERENCE_FACE_POSITION_GALLERY)
select_component_names: List[ComponentName] = [
'face_analyser_direction_dropdown',
'face_analyser_age_dropdown',
'face_analyser_gender_dropdown'
]
for component_name in select_component_names:
component = ui.get_component(component_name)
if component:
component.select(update_face_reference_position, outputs=REFERENCE_FACE_POSITION_GALLERY)


def update_face_recognition(face_recognition: FaceRecognition) -> Tuple[Update, Update]:
Expand Down Expand Up @@ -143,11 +121,3 @@ def extract_gallery_frames(reference_frame: Frame) -> List[Frame]:
crop_frame = reference_frame[start_y:end_y, start_x:end_x]
crop_frames.append(ui.normalize_frame(crop_frame))
return crop_frames


def update_dropdown(name: str, value: str) -> Update:
if value == 'none':
setattr(roop.globals, name, None)
else:
setattr(roop.globals, name, value)
return gradio.update(value=value)
4 changes: 3 additions & 1 deletion roop/uis/__layouts__/default.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import gradio

from roop.uis.__components__ import settings, source, target, preview, trim_frame, face_selector, output
from roop.uis.__components__ import settings, source, target, preview, trim_frame, face_analyser, face_selector, output


def render() -> gradio.Blocks:
Expand All @@ -15,6 +15,7 @@ def render() -> gradio.Blocks:
preview.render()
trim_frame.render()
face_selector.render()
face_analyser.render()
with gradio.Row():
output.render()
return layout
Expand All @@ -27,4 +28,5 @@ def listen() -> None:
preview.listen()
trim_frame.listen()
face_selector.listen()
face_analyser.listen()
output.listen()

0 comments on commit fdde5af

Please sign in to comment.