Skip to content

Commit

Permalink
Cleanup many-faces vs reference face, Fix float on reference frame, I…
Browse files Browse the repository at this point in the history
…ntroduce globals typing
  • Loading branch information
henryruhs committed Jul 27, 2023
1 parent a547bba commit 64c1d67
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 34 deletions.
4 changes: 2 additions & 2 deletions roop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def parse_args() -> None:

roop.globals.source_path = args.source_path
roop.globals.target_path = args.target_path
roop.globals.output_path = normalize_output_path(roop.globals.source_path, roop.globals.target_path, args.output_path) # type: ignore
roop.globals.headless = roop.globals.source_path and roop.globals.target_path and roop.globals.output_path
roop.globals.output_path = normalize_output_path(roop.globals.source_path, roop.globals.target_path, args.output_path)
roop.globals.headless = roop.globals.source_path is None and roop.globals.target_path is None and roop.globals.output_path is None
roop.globals.frame_processors = args.frame_processor
roop.globals.keep_fps = args.keep_fps
roop.globals.keep_frames = args.keep_frames
Expand Down
38 changes: 19 additions & 19 deletions roop/globals.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
from typing import List
from typing import List, Optional

source_path = None
target_path = None
output_path = None
headless = None
source_path: Optional[str] = None
target_path: Optional[str] = None
output_path: Optional[str] = None
headless: Optional[bool] = None
frame_processors: List[str] = []
keep_fps = None
keep_frames = None
skip_audio = None
many_faces = None
reference_face_position = None
reference_frame_number = None
similar_face_distance = None
temp_frame_format = None
temp_frame_quality = None
output_video_encoder = None
output_video_quality = None
max_memory = None
keep_fps: Optional[bool] = None
keep_frames: Optional[bool] = None
skip_audio: Optional[bool] = None
many_faces: Optional[bool] = None
reference_face_position: Optional[int] = None
reference_frame_number: Optional[int] = None
similar_face_distance: Optional[float] = None
temp_frame_format: Optional[str] = None
temp_frame_quality: Optional[int] = None
output_video_encoder: Optional[str] = None
output_video_quality: Optional[int] = None
max_memory: Optional[int] = None
execution_providers: List[str] = []
execution_threads = None
log_level = 'error'
execution_threads: Optional[int] = None
log_level: str = 'error'
10 changes: 2 additions & 8 deletions roop/processors/frame/face_swapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ def process_frame(source_face: Face, reference_face: Face, temp_frame: Frame) ->

def process_frames(source_path: str, temp_frame_paths: List[str], update: Callable[[], None]) -> None:
source_face = get_one_face(cv2.imread(source_path))
if roop.globals.many_faces:
reference_face = None
else:
reference_face = get_face_reference()
reference_face = None if roop.globals.many_faces else get_face_reference()
for temp_frame_path in temp_frame_paths:
temp_frame = cv2.imread(temp_frame_path)
result = process_frame(source_face, reference_face, temp_frame)
Expand All @@ -90,10 +87,7 @@ def process_frames(source_path: str, temp_frame_paths: List[str], update: Callab
def process_image(source_path: str, target_path: str, output_path: str) -> None:
source_face = get_one_face(cv2.imread(source_path))
target_frame = cv2.imread(target_path)
if roop.globals.many_faces:
reference_face = None
else:
reference_face = get_one_face(target_frame, roop.globals.reference_face_position)
reference_face = None if roop.globals.many_faces else get_one_face(target_frame, roop.globals.reference_face_position)
result = process_frame(source_face, reference_face, target_frame)
cv2.imwrite(output_path, result)

Expand Down
10 changes: 5 additions & 5 deletions roop/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def select_source_path(source_path: Optional[str] = None) -> None:
if source_path is None:
source_path = ctk.filedialog.askopenfilename(title='select an source image', initialdir=RECENT_DIRECTORY_SOURCE)
if is_image(source_path):
roop.globals.source_path = source_path # type: ignore
roop.globals.source_path = source_path
RECENT_DIRECTORY_SOURCE = os.path.dirname(roop.globals.source_path)
image = render_image_preview(roop.globals.source_path, (200, 200))
source_label.configure(image=image)
Expand All @@ -170,12 +170,12 @@ def select_target_path(target_path: Optional[str] = None) -> None:
if target_path is None:
target_path = ctk.filedialog.askopenfilename(title='select an target image or video', initialdir=RECENT_DIRECTORY_TARGET)
if is_image(target_path):
roop.globals.target_path = target_path # type: ignore
roop.globals.target_path = target_path
RECENT_DIRECTORY_TARGET = os.path.dirname(roop.globals.target_path)
image = render_image_preview(roop.globals.target_path, (200, 200))
target_label.configure(image=image)
elif is_video(target_path):
roop.globals.target_path = target_path # type: ignore
roop.globals.target_path = target_path
RECENT_DIRECTORY_TARGET = os.path.dirname(roop.globals.target_path)
video_frame = render_video_preview(target_path, (200, 200))
target_label.configure(image=video_frame)
Expand Down Expand Up @@ -273,8 +273,8 @@ def update_preview(frame_number: int = 0) -> None:

def update_face_reference(steps: int) -> None:
clear_face_reference()
reference_frame_number = preview_slider.get()
roop.globals.reference_face_position += steps # type: ignore
reference_frame_number = int(preview_slider.get())
roop.globals.reference_face_position += steps
roop.globals.reference_frame_number = reference_frame_number
update_preview(reference_frame_number)

Expand Down

0 comments on commit 64c1d67

Please sign in to comment.