Skip to content

Commit

Permalink
Introduce trim frame by start and end
Browse files Browse the repository at this point in the history
  • Loading branch information
henryruhs committed Aug 9, 2023
1 parent 114be9a commit b2b50c4
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 9 deletions.
4 changes: 4 additions & 0 deletions roop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def parse_args() -> None:
program.add_argument('--reference-face-position', help='position of the reference face', dest='reference_face_position', type=int, default=0)
program.add_argument('--reference-frame-number', help='number of the reference frame', dest='reference_frame_number', type=int, default=0)
program.add_argument('--similar-face-distance', help='face distance used for recognition', dest='similar_face_distance', type=float, default=0.85)
program.add_argument('--trim-frame-start', help='start frame use for extraction', dest='trim_frame_start', type=int)
program.add_argument('--trim-frame-end', help='end frame use for extraction', dest='trim_frame_end', type=int)
program.add_argument('--temp-frame-format', help='image format used for frame extraction', dest='temp_frame_format', default='png', choices=['jpg', 'png'])
program.add_argument('--temp-frame-quality', help='image quality used for frame extraction', dest='temp_frame_quality', type=int, default=0, choices=range(101), metavar='[0-100]')
program.add_argument('--output-video-encoder', help='encoder used for the output video', dest='output_video_encoder', default='libx264', choices=['libx264', 'libx265', 'libvpx-vp9', 'h264_nvenc', 'hevc_nvenc'])
Expand All @@ -64,6 +66,8 @@ def parse_args() -> None:
roop.globals.reference_face_position = args.reference_face_position
roop.globals.reference_frame_number = args.reference_frame_number
roop.globals.similar_face_distance = args.similar_face_distance
roop.globals.trim_frame_start = args.trim_frame_start
roop.globals.trim_frame_end = args.trim_frame_end
roop.globals.temp_frame_format = args.temp_frame_format
roop.globals.temp_frame_quality = args.temp_frame_quality
roop.globals.output_video_encoder = args.output_video_encoder
Expand Down
2 changes: 2 additions & 0 deletions roop/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
reference_face_position: Optional[int] = None
reference_frame_number: Optional[int] = None
similar_face_distance: Optional[float] = None
trim_frame_start: Optional[int] = None
trim_frame_end: Optional[int] = None
temp_frame_format: Optional[str] = None
temp_frame_quality: Optional[int] = None
output_video_encoder: Optional[str] = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def update_face_reference_position(reference_face_position: int) -> Update:
roop.globals.reference_face_position = reference_face_position
if is_image(roop.globals.target_path):
target_frame = cv2.imread(roop.globals.target_path)
maximum = get_faces_total(target_frame)
maximum = max(get_faces_total(target_frame) - 1, 0)
if is_video(roop.globals.target_path):
temp_frame = get_video_frame(roop.globals.target_path, roop.globals.reference_frame_number)
maximum = get_faces_total(temp_frame)
maximum = max(get_faces_total(temp_frame) - 1, 0)
return gradio.update(value=reference_face_position, maximum=maximum)


Expand Down
4 changes: 2 additions & 2 deletions roop/uis/__components__/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ def update(frame_number: int = 0) -> Tuple[Update, Update]:
if is_image(roop.globals.target_path):
target_frame = cv2.imread(roop.globals.target_path)
preview_frame = get_preview_frame(target_frame)
return gradio.update(value=ui.normalize_frame(preview_frame)), gradio.update(value=0, maximum=1, visible=False)
return gradio.update(value=ui.normalize_frame(preview_frame)), gradio.update(value=None, maximum=None, visible=False)
if is_video(roop.globals.target_path):
roop.globals.reference_frame_number = frame_number
video_frame_total = get_video_frame_total(roop.globals.target_path)
temp_frame = get_video_frame(roop.globals.target_path, roop.globals.reference_frame_number)
preview_frame = get_preview_frame(temp_frame)
return gradio.update(value=ui.normalize_frame(preview_frame)), gradio.update(maximum=video_frame_total, visible=True)
return gradio.update(value=None), gradio.update(value=0, maximum=1, visible=False)
return gradio.update(value=None), gradio.update(value=None, maximum=None, visible=False)


def get_preview_frame(temp_frame: Frame) -> Frame:
Expand Down
59 changes: 59 additions & 0 deletions roop/uis/__components__/trim_frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from time import sleep
from typing import Any, Dict, Tuple, Optional

import gradio

import roop.globals
from roop.capturer import get_video_frame_total
from roop.uis import core as ui
from roop.uis.typing import Update
from roop.utilities import is_video

TRIM_FRAME_START_NUMBER: Optional[gradio.Number] = None
TRIM_FRAME_END_NUMBER: Optional[gradio.Number] = None


def render() -> None:
global TRIM_FRAME_START_NUMBER
global TRIM_FRAME_END_NUMBER

with gradio.Box():
trim_frame_start_number_args: Dict[str, Any] = {
'label': 'TRIM FRAME START',
'value': roop.globals.trim_frame_start,
'visible': False
}
trim_frame_end_number_args: Dict[str, Any] = {
'label': 'TRIM FRAME END',
'value': roop.globals.trim_frame_end,
'visible': False
}
if is_video(roop.globals.target_path):
trim_frame_start_number_args['visible'] = True
trim_frame_end_number_args['visible'] = True
with gradio.Row():
TRIM_FRAME_START_NUMBER = gradio.Number(**trim_frame_start_number_args)
TRIM_FRAME_END_NUMBER = gradio.Number(**trim_frame_end_number_args)


def listen() -> None:
target_file = ui.get_component('target_file')
if target_file:
target_file.change(remote_update, outputs=[TRIM_FRAME_START_NUMBER, TRIM_FRAME_END_NUMBER])
TRIM_FRAME_START_NUMBER.change(lambda value: update_number('trim_frame_start', int(value)), inputs=TRIM_FRAME_START_NUMBER, outputs=TRIM_FRAME_START_NUMBER)
TRIM_FRAME_END_NUMBER.change(lambda value: update_number('trim_frame_end', int(value)), inputs=TRIM_FRAME_END_NUMBER, outputs=TRIM_FRAME_END_NUMBER)


def remote_update() -> Tuple[Update, Update]:
sleep(0.5)
if is_video(roop.globals.target_path):
video_frame_total = get_video_frame_total(roop.globals.target_path)
roop.globals.trim_frame_start = 0
roop.globals.trim_frame_end = video_frame_total
return gradio.update(value=0, maximum=video_frame_total, visible=True), gradio.update(value=video_frame_total, maximum=video_frame_total, visible=True)
return gradio.update(value=None, maximum=None, visible=False), gradio.update(value=None, maximum=None, visible=False)


def update_number(name: str, value: int) -> Update:
setattr(roop.globals, name, value)
return gradio.update(value=value)
8 changes: 5 additions & 3 deletions 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, reference, output
from roop.uis.__components__ import settings, source, target, preview, trim_frame, face_selector, output


def render() -> gradio.Blocks:
Expand All @@ -13,7 +13,8 @@ def render() -> gradio.Blocks:
target.render()
with gradio.Column(scale=3):
preview.render()
reference.render()
trim_frame.render()
face_selector.render()
with gradio.Row():
output.render()
return layout
Expand All @@ -24,5 +25,6 @@ def listen() -> None:
source.listen()
target.listen()
preview.listen()
reference.listen()
trim_frame.listen()
face_selector.listen()
output.listen()
2 changes: 1 addition & 1 deletion roop/uis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_theme() -> gradio.Theme:
return gradio.themes.Soft(
primary_hue=gradio.themes.colors.red,
secondary_hue=gradio.themes.colors.gray,
font=gradio.themes.GoogleFont('Open Sans')
font=gradio.themes.GoogleFont('Inter')
)


Expand Down
14 changes: 13 additions & 1 deletion roop/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,19 @@ def detect_fps(target_path: str) -> float:
def extract_frames(target_path: str, fps: float = 30) -> bool:
temp_directory_path = get_temp_directory_path(target_path)
temp_frame_quality = roop.globals.temp_frame_quality * 31 // 100
return run_ffmpeg(['-hwaccel', 'auto', '-i', target_path, '-q:v', str(temp_frame_quality), '-pix_fmt', 'rgb24', '-vf', 'fps=' + str(fps), os.path.join(temp_directory_path, '%04d.' + roop.globals.temp_frame_format)])
trim_frame_start = roop.globals.trim_frame_start
trim_frame_end = roop.globals.trim_frame_end
commands = ['-hwaccel', 'auto', '-i', target_path, '-q:v', str(temp_frame_quality), '-pix_fmt', 'rgb24']
if trim_frame_start is not None and trim_frame_end is not None:
commands.extend(['-vf', f'trim=start_frame={trim_frame_start}:end_frame={trim_frame_end},fps={fps}'])
elif trim_frame_start is not None:
commands.extend(['-vf', f'trim=start_frame={trim_frame_start},fps={fps}'])
elif trim_frame_end is not None:
commands.extend(['-vf', f'trim=end_frame={trim_frame_end},fps={fps}'])
else:
commands.extend(['-vf', f'fps={fps}'])
commands.extend([os.path.join(temp_directory_path, '%04d.' + roop.globals.temp_frame_format)])
return run_ffmpeg(commands)


def create_video(target_path: str, fps: float = 30) -> bool:
Expand Down

0 comments on commit b2b50c4

Please sign in to comment.