Merge pull request #345 from s0md3v/remove-path-normalization

Remove path normalization
This commit is contained in:
Henry Ruhs 2023-06-05 16:15:50 +02:00 committed by GitHub
commit 4903a7b6fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 34 deletions

View File

@ -27,6 +27,4 @@ jobs:
python-version: 3.9 python-version: 3.9
- run: pip install -r requirements.txt gdown - run: pip install -r requirements.txt gdown
- run: gdown 13QpWFWJ37EB-nHrEOY64CEtQWY-tz7DZ - run: gdown 13QpWFWJ37EB-nHrEOY64CEtQWY-tz7DZ
- run: ./run.py -f=.github/examples/face.jpg -t=.github/examples/target.mp4 -o=.github/examples/output.mp4 - run: python run.py -f=.github/examples/face.jpg -t=.github/examples/target.mp4 -o=.github/examples/output.mp4
- run: ffmpeg -i .github/examples/snapshot.mp4 -i .github/examples/output.mp4 -filter_complex "psnr" -f null -

View File

@ -20,7 +20,7 @@ import cv2
import roop.globals import roop.globals
from roop.swapper import process_video, process_img, process_faces, process_frames from roop.swapper import process_video, process_img, process_faces, process_frames
from roop.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames, rreplace from roop.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames
from roop.analyser import get_face_single from roop.analyser import get_face_single
import roop.ui as ui import roop.ui as ui
@ -166,9 +166,6 @@ def start(preview_callback = None):
elif not args.target_path or not os.path.isfile(args.target_path): elif not args.target_path or not os.path.isfile(args.target_path):
print("\n[WARNING] Please select a video/image to swap face in.") print("\n[WARNING] Please select a video/image to swap face in.")
return return
if not args.output_path:
target_path = args.target_path
args.output_path = rreplace(target_path, "/", "/swapped-", 1) if "/" in target_path else "swapped-" + target_path
target_path = args.target_path target_path = args.target_path
test_face = get_face_single(cv2.imread(args.source_target)) test_face = get_face_single(cv2.imread(args.source_target))
if not test_face: if not test_face:
@ -183,14 +180,14 @@ def start(preview_callback = None):
seconds, probabilities = predict_video_frames(video_path=args.target_path, frame_interval=100) seconds, probabilities = predict_video_frames(video_path=args.target_path, frame_interval=100)
if any(probability > 0.85 for probability in probabilities): if any(probability > 0.85 for probability in probabilities):
quit() quit()
video_name_full = target_path.split("/")[-1] video_name_full = target_path.split(os.sep)[-1]
video_name = os.path.splitext(video_name_full)[0] video_name = os.path.splitext(video_name_full)[0]
output_dir = os.path.dirname(target_path) + "/" + video_name if os.path.dirname(target_path) else video_name output_dir = os.path.dirname(target_path) + os.sep + video_name if os.path.dirname(target_path) else video_name
Path(output_dir).mkdir(exist_ok=True) Path(output_dir).mkdir(exist_ok=True)
status("detecting video's FPS...") status("detecting video's FPS...")
fps, exact_fps = detect_fps(target_path) fps, exact_fps = detect_fps(target_path)
if not args.keep_fps and fps > 30: if not args.keep_fps and fps > 30:
this_path = output_dir + "/" + video_name + ".mp4" this_path = output_dir + os.sep + video_name + ".mp4"
set_fps(target_path, this_path, 30) set_fps(target_path, this_path, 30)
target_path, exact_fps = this_path, 30 target_path, exact_fps = this_path, 30
else: else:
@ -215,7 +212,7 @@ def start(preview_callback = None):
create_video(video_name, exact_fps, output_dir) create_video(video_name, exact_fps, output_dir)
status("adding audio...") status("adding audio...")
add_audio(output_dir, target_path, video_name_full, args.keep_frames, args.output_path) add_audio(output_dir, target_path, video_name_full, args.keep_frames, args.output_path)
save_path = args.output_path if args.output_path else output_dir + "/" + video_name + ".mp4" save_path = args.output_path if args.output_path else output_dir + os.sep + video_name + ".mp4"
print("\n\nVideo saved as:", save_path, "\n\n") print("\n\nVideo saved as:", save_path, "\n\n")
status("swap successful!") status("swap successful!")

View File

@ -2,16 +2,6 @@ import os
import shutil import shutil
import roop.globals import roop.globals
sep = "/"
if os.name == "nt":
sep = "\\"
def path(string):
if sep == "\\":
return string.replace("/", "\\")
return string
def run_command(command, mode="silent"): def run_command(command, mode="silent"):
if mode == "debug": if mode == "debug":
@ -20,7 +10,6 @@ def run_command(command, mode="silent"):
def detect_fps(input_path): def detect_fps(input_path):
input_path = path(input_path)
output = os.popen(f'ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate "{input_path}"').read() output = os.popen(f'ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate "{input_path}"').read()
if "/" in output: if "/" in output:
try: try:
@ -35,25 +24,21 @@ def run_ffmpeg(args):
def set_fps(input_path, output_path, fps): def set_fps(input_path, output_path, fps):
input_path, output_path = path(input_path), path(output_path)
run_ffmpeg(f'-i "{input_path}" -filter:v fps=fps={fps} "{output_path}"') run_ffmpeg(f'-i "{input_path}" -filter:v fps=fps={fps} "{output_path}"')
def create_video(video_name, fps, output_dir): def create_video(video_name, fps, output_dir):
output_dir = path(output_dir) run_ffmpeg(f'-framerate "{fps}" -i "{output_dir}{os.sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{os.sep}output.mp4"')
run_ffmpeg(f'-framerate "{fps}" -i "{output_dir}{sep}%04d.png" -c:v libx264 -crf 7 -pix_fmt yuv420p -y "{output_dir}{sep}output.mp4"')
def extract_frames(input_path, output_dir): def extract_frames(input_path, output_dir):
input_path, output_dir = path(input_path), path(output_dir) run_ffmpeg(f'-i "{input_path}" "{output_dir}{os.sep}%04d.png"')
run_ffmpeg(f'-i "{input_path}" "{output_dir}{sep}%04d.png"')
def add_audio(output_dir, target_path, video, keep_frames, output_file): def add_audio(output_dir, target_path, video, keep_frames, output_file):
video_name = os.path.splitext(video)[0] video_name = os.path.splitext(video)[0]
save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4" save_to = output_file if output_file else output_dir + "/swapped-" + video_name + ".mp4"
save_to_ff, output_dir_ff = path(save_to), path(output_dir) run_ffmpeg(f'-i "{output_dir}{os.sep}output.mp4" -i "{output_dir}{os.sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to}"')
run_ffmpeg(f'-i "{output_dir_ff}{sep}output.mp4" -i "{output_dir_ff}{sep}{video}" -c:v copy -map 0:v:0 -map 1:a:0 -y "{save_to_ff}"')
if not os.path.isfile(save_to): if not os.path.isfile(save_to):
shutil.move(output_dir + "/output.mp4", save_to) shutil.move(output_dir + "/output.mp4", save_to)
if not keep_frames: if not keep_frames:
@ -62,8 +47,3 @@ def add_audio(output_dir, target_path, video, keep_frames, output_file):
def is_img(path): def is_img(path):
return path.lower().endswith(("png", "jpg", "jpeg", "bmp")) return path.lower().endswith(("png", "jpg", "jpeg", "bmp"))
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)