roop/run.py

229 lines
8.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import sys
2023-05-30 02:28:16 +03:00
import time
2023-05-29 21:05:29 +03:00
import shutil
import core.globals
2023-05-29 21:05:29 +03:00
if not shutil.which('ffmpeg'):
2023-05-29 22:50:40 +03:00
print('ffmpeg is not installed. Read the docs: https://github.com/s0md3v/roop#installation.\n' * 10)
2023-05-29 21:27:05 +03:00
quit()
2023-05-29 20:40:52 +03:00
if '--gpu' not in sys.argv:
core.globals.providers = ['CPUExecutionProvider']
2023-05-30 04:54:10 +03:00
if 'ROCMExecutionProvider' not in core.globals.providers:
import torch
if not torch.cuda.is_available():
quit("You are using --gpu flag but CUDA isn't available on your system.")
2023-05-28 17:49:40 +03:00
import glob
import argparse
import multiprocessing as mp
import os
from pathlib import Path
import tkinter as tk
from tkinter import filedialog
2023-05-29 11:09:44 +03:00
from tkinter.filedialog import asksaveasfilename
2023-05-28 17:49:40 +03:00
from core.processor import process_video, process_img
from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames
2023-05-29 15:20:42 +03:00
from core.config import get_face
2023-05-28 17:49:40 +03:00
import webbrowser
import psutil
2023-05-29 15:20:42 +03:00
import cv2
import threading
from PIL import Image, ImageTk
2023-05-28 17:49:40 +03:00
pool = None
args = {}
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--face', help='use this face', dest='source_img')
parser.add_argument('-t', '--target', help='replace this face', dest='target_path')
2023-05-29 16:12:06 +03:00
parser.add_argument('-o', '--output', help='save output to this file', dest='output_file')
2023-05-28 17:49:40 +03:00
parser.add_argument('--keep-fps', help='maintain original fps', dest='keep_fps', action='store_true', default=False)
2023-05-30 04:30:37 +03:00
parser.add_argument('--gpu', help='use gpu', dest='gpu', action='store_true', default=False)
2023-05-28 17:49:40 +03:00
parser.add_argument('--keep-frames', help='keep frames directory', dest='keep_frames', action='store_true', default=False)
for name, value in vars(parser.parse_args()).items():
args[name] = value
2023-05-29 16:23:10 +03:00
2023-05-29 20:25:47 +03:00
sep = "/"
if os.name == "nt":
sep = "\\"
2023-05-28 17:49:40 +03:00
def start_processing():
2023-05-30 02:28:16 +03:00
start_time = time.time()
2023-05-28 17:49:40 +03:00
if args['gpu']:
process_video(args['source_img'], args["frame_paths"])
2023-05-30 02:28:42 +03:00
end_time = time.time()
print(flush=True)
2023-05-30 02:28:16 +03:00
print(f"Processing time: {end_time - start_time:.2f} seconds", flush=True)
2023-05-28 17:49:40 +03:00
return
frame_paths = args["frame_paths"]
n = len(frame_paths)//(psutil.cpu_count()-1)
processes = []
for i in range(0, len(frame_paths), n):
p = pool.apply_async(process_video, args=(args['source_img'], frame_paths[i:i+n],))
processes.append(p)
for p in processes:
p.get()
pool.close()
pool.join()
2023-05-30 02:28:16 +03:00
end_time = time.time()
2023-05-30 02:28:42 +03:00
print(flush=True)
2023-05-30 02:28:16 +03:00
print(f"Processing time: {end_time - start_time:.2f} seconds", flush=True)
2023-05-28 17:49:40 +03:00
def preview_image(image_path):
img = Image.open(image_path)
2023-05-30 04:10:46 +03:00
img = img.resize((180, 180), Image.ANTIALIAS)
photo_img = ImageTk.PhotoImage(img)
left_frame = tk.Frame(window)
2023-05-30 04:10:46 +03:00
left_frame.place(x=60, y=100)
img_label = tk.Label(left_frame, image=photo_img)
img_label.image = photo_img
img_label.pack()
def preview_video(video_path):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print("Error opening video file")
return
ret, frame = cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame)
2023-05-30 04:10:46 +03:00
img = img.resize((180, 180), Image.ANTIALIAS)
photo_img = ImageTk.PhotoImage(img)
right_frame = tk.Frame(window)
2023-05-30 04:10:46 +03:00
right_frame.place(x=360, y=100)
img_label = tk.Label(right_frame, image=photo_img)
img_label.image = photo_img
img_label.pack()
cap.release()
2023-05-28 17:49:40 +03:00
def select_face():
args['source_img'] = filedialog.askopenfilename(title="Select a face")
preview_image(args['source_img'])
2023-05-28 17:49:40 +03:00
def select_target():
args['target_path'] = filedialog.askopenfilename(title="Select a target")
threading.Thread(target=preview_video, args=(args['target_path'],)).start()
2023-05-28 17:49:40 +03:00
def toggle_fps_limit():
args['keep_fps'] = limit_fps.get() != True
2023-05-30 01:57:42 +03:00
def toggle_keep_frames():
args['keep_frames'] = keep_frames.get() != True
2023-05-29 11:09:44 +03:00
def save_file():
2023-05-30 01:57:42 +03:00
args['output_file'] = asksaveasfilename(initialfile='output.mp4', defaultextension=".mp4", filetypes=[("All Files","*.*"),("Videos","*.mp4")])
2023-05-29 11:09:44 +03:00
2023-05-29 23:58:31 +03:00
def status(string):
2023-05-30 06:26:50 +03:00
if args['source_img']:
print("Status: " + string)
else:
status_label["text"] = "Status: " + string
window.update()
2023-05-29 23:58:31 +03:00
2023-05-28 17:49:40 +03:00
def start():
2023-05-29 23:58:31 +03:00
print("DON'T WORRY. IT'S NOT STUCK/CRASHED.\n" * 5)
if not args['source_img'] or not os.path.isfile(args['source_img']):
print("\n[WARNING] Please select an image containing a face.")
return
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.")
return
2023-05-28 17:49:40 +03:00
global pool
pool = mp.Pool(psutil.cpu_count()-1)
target_path = args['target_path']
2023-05-29 15:20:42 +03:00
test_face = get_face(cv2.imread(args['source_img']))
if not test_face:
print("\n[WARNING] No face detected in source image. Please try with another one.\n")
return
2023-05-28 17:49:40 +03:00
if is_img(target_path):
process_img(args['source_img'], target_path)
2023-05-30 01:57:42 +03:00
status("swap successful!")
2023-05-28 17:49:40 +03:00
return
2023-05-29 18:54:40 +03:00
video_name = target_path.split("/")[-1].split(".")[0]
2023-05-29 20:25:47 +03:00
output_dir = target_path.replace(target_path.split("/")[-1], "").rstrip("/") + "/" + video_name
2023-05-28 17:49:40 +03:00
Path(output_dir).mkdir(exist_ok=True)
2023-05-30 01:57:42 +03:00
status("detecting video's FPS...")
2023-05-28 17:49:40 +03:00
fps = detect_fps(target_path)
if not args['keep_fps'] and fps > 30:
2023-05-29 18:54:40 +03:00
this_path = output_dir + "/" + video_name + ".mp4"
2023-05-28 17:49:40 +03:00
set_fps(target_path, this_path, 30)
target_path, fps = this_path, 30
else:
shutil.copy(target_path, output_dir)
2023-05-30 01:57:42 +03:00
status("extracting frames...")
2023-05-28 17:49:40 +03:00
extract_frames(target_path, output_dir)
args['frame_paths'] = tuple(sorted(
2023-05-29 18:54:40 +03:00
glob.glob(output_dir + f"/*.png"),
2023-05-29 20:25:47 +03:00
key=lambda x: int(x.split(sep)[-1].replace(".png", ""))
2023-05-28 17:49:40 +03:00
))
2023-05-30 01:57:42 +03:00
status("swapping in progress...")
2023-05-28 17:49:40 +03:00
start_processing()
2023-05-30 01:57:42 +03:00
status("creating video...")
2023-05-28 17:49:40 +03:00
create_video(video_name, fps, output_dir)
2023-05-30 01:57:42 +03:00
status("adding audio...")
2023-05-29 15:12:52 +03:00
add_audio(output_dir, target_path, args['keep_frames'], args['output_file'])
2023-05-29 18:54:40 +03:00
save_path = args['output_file'] if args['output_file'] else output_dir + "/" + video_name + ".mp4"
2023-05-29 12:57:52 +03:00
print("\n\nVideo saved as:", save_path, "\n\n")
2023-05-30 01:57:42 +03:00
status("swap successful!")
2023-05-28 17:49:40 +03:00
if __name__ == "__main__":
2023-05-29 23:58:31 +03:00
global status_label, window
2023-05-28 17:49:40 +03:00
if args['source_img']:
start()
quit()
window = tk.Tk()
2023-05-30 04:10:46 +03:00
window.geometry("600x700")
2023-05-28 17:49:40 +03:00
window.title("roop")
2023-05-30 01:57:42 +03:00
window.configure(bg="#2d3436")
window.resizable(width=False, height=False)
2023-05-28 17:49:40 +03:00
# Contact information
2023-05-30 01:57:42 +03:00
support_link = tk.Label(window, text="Donate to project <3", fg="#fd79a8", bg="#2d3436", cursor="hand2", font=("Arial", 8))
support_link.place(x=180,y=20,width=250,height=30)
2023-05-28 17:49:40 +03:00
support_link.bind("<Button-1>", lambda e: webbrowser.open("https://github.com/sponsors/s0md3v"))
# Select a face button
2023-05-30 01:57:42 +03:00
face_button = tk.Button(window, text="Select a face", command=select_face, bg="#2d3436", fg="#74b9ff", highlightthickness=4, relief="flat", highlightbackground="#74b9ff", activebackground="#74b9ff", borderwidth=4)
2023-05-30 04:10:46 +03:00
face_button.place(x=60,y=320,width=180,height=80)
2023-05-28 17:49:40 +03:00
# Select a target button
2023-05-30 04:10:46 +03:00
target_button = tk.Button(window, text="Select a target", command=select_target, bg="#2d3436", fg="#74b9ff", highlightthickness=4, relief="flat", highlightbackground="#74b9ff", activebackground="#74b9ff", borderwidth=4)
target_button.place(x=360,y=320,width=180,height=80)
2023-05-28 17:49:40 +03:00
# FPS limit checkbox
limit_fps = tk.IntVar()
2023-05-30 01:57:42 +03:00
fps_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Limit FPS to 30", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=limit_fps, command=toggle_fps_limit)
2023-05-30 04:10:46 +03:00
fps_checkbox.place(x=30,y=500,width=240,height=31)
2023-05-28 17:49:40 +03:00
fps_checkbox.select()
2023-05-30 01:57:42 +03:00
# Keep frames checkbox
keep_frames = tk.IntVar()
frames_checkbox = tk.Checkbutton(window, relief="groove", activebackground="#2d3436", activeforeground="#74b9ff", selectcolor="black", text="Keep frames dir", fg="#dfe6e9", borderwidth=0, highlightthickness=0, bg="#2d3436", variable=keep_frames, command=toggle_keep_frames)
2023-05-30 04:10:46 +03:00
frames_checkbox.place(x=37,y=450,width=240,height=31)
2023-05-28 17:49:40 +03:00
# Start button
2023-05-30 04:10:46 +03:00
start_button = tk.Button(window, text="Start", bg="#f1c40f", relief="flat", borderwidth=0, highlightthickness=0, command=lambda: [save_file(), start()])
start_button.place(x=240,y=560,width=120,height=49)
2023-05-29 23:58:31 +03:00
# Status label
2023-05-30 04:10:46 +03:00
status_label = tk.Label(window, width=580, justify="center", text="Status: waiting for input...", fg="#2ecc71", bg="#2d3436")
status_label.place(x=10,y=640,width=580,height=30)
2023-05-28 17:49:40 +03:00
window.mainloop()