|
| 1 | +import glob |
| 2 | +import argparse |
| 3 | +import multiprocessing as mp |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | +import tkinter as tk |
| 7 | +from tkinter import filedialog |
| 8 | +from core.processor import process_video, process_img |
| 9 | +from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames |
| 10 | +import webbrowser |
| 11 | +import psutil |
| 12 | +import shutil |
| 13 | + |
| 14 | +pool = None |
| 15 | +args = {} |
| 16 | + |
| 17 | +parser = argparse.ArgumentParser() |
| 18 | +parser.add_argument('-f', '--face', help='use this face', dest='source_img') |
| 19 | +parser.add_argument('-t', '--target', help='replace this face', dest='target_path') |
| 20 | +parser.add_argument('--keep-fps', help='maintain original fps', dest='keep_fps', action='store_true', default=False) |
| 21 | +parser.add_argument('--gpu', help='use gpu', dest='gpu', action='store_true', default=False) |
| 22 | +parser.add_argument('--keep-frames', help='keep frames directory', dest='keep_frames', action='store_true', default=False) |
| 23 | + |
| 24 | +for name, value in vars(parser.parse_args()).items(): |
| 25 | + args[name] = value |
| 26 | + |
| 27 | +def start_processing(): |
| 28 | + if args['gpu']: |
| 29 | + process_video(args['source_img'], args["frame_paths"]) |
| 30 | + return |
| 31 | + frame_paths = args["frame_paths"] |
| 32 | + n = len(frame_paths)//(psutil.cpu_count()-1) |
| 33 | + processes = [] |
| 34 | + for i in range(0, len(frame_paths), n): |
| 35 | + p = pool.apply_async(process_video, args=(args['source_img'], frame_paths[i:i+n],)) |
| 36 | + processes.append(p) |
| 37 | + for p in processes: |
| 38 | + p.get() |
| 39 | + pool.close() |
| 40 | + pool.join() |
| 41 | + |
| 42 | + |
| 43 | +def select_face(): |
| 44 | + args['source_img'] = filedialog.askopenfilename(title="Select a face") |
| 45 | + |
| 46 | + |
| 47 | +def select_target(): |
| 48 | + args['target_path'] = filedialog.askopenfilename(title="Select a target") |
| 49 | + |
| 50 | + |
| 51 | +def toggle_fps_limit(): |
| 52 | + args['keep_fps'] = limit_fps.get() != True |
| 53 | + |
| 54 | + |
| 55 | +def start(): |
| 56 | + global pool |
| 57 | + pool = mp.Pool(psutil.cpu_count()-1) |
| 58 | + current_dir = os.getcwd() |
| 59 | + target_path = args['target_path'] |
| 60 | + if is_img(target_path): |
| 61 | + process_img(args['source_img'], target_path) |
| 62 | + return |
| 63 | + video_name = target_path.split("/")[-1].split(".")[0] |
| 64 | + output_dir = current_dir + "/" + video_name |
| 65 | + Path(output_dir).mkdir(exist_ok=True) |
| 66 | + fps = detect_fps(target_path) |
| 67 | + if not args['keep_fps'] and fps > 30: |
| 68 | + this_path = output_dir + "/" + video_name + ".mp4" |
| 69 | + set_fps(target_path, this_path, 30) |
| 70 | + target_path, fps = this_path, 30 |
| 71 | + else: |
| 72 | + shutil.copy(target_path, output_dir) |
| 73 | + extract_frames(target_path, output_dir) |
| 74 | + args['frame_paths'] = tuple(sorted( |
| 75 | + glob.glob(output_dir + "/*.png"), |
| 76 | + key=lambda x: int(x.split("/")[-1].replace(".png", "")) |
| 77 | + )) |
| 78 | + start_processing() |
| 79 | + create_video(video_name, fps, output_dir) |
| 80 | + add_audio(current_dir, output_dir, target_path, args['keep_frames']) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + if args['source_img']: |
| 85 | + start() |
| 86 | + quit() |
| 87 | + window = tk.Tk() |
| 88 | + window.geometry("600x200") |
| 89 | + window.title("roop") |
| 90 | + |
| 91 | + # Contact information |
| 92 | + support_link = tk.Label(window, text="Support the project ^_^", fg="red", cursor="hand2") |
| 93 | + support_link.pack(padx=10, pady=10) |
| 94 | + support_link.bind("<Button-1>", lambda e: webbrowser.open("https://github.com/sponsors/s0md3v")) |
| 95 | + |
| 96 | + # Select a face button |
| 97 | + face_button = tk.Button(window, text="Select a face", command=select_face) |
| 98 | + face_button.pack(side=tk.LEFT, padx=10, pady=10) |
| 99 | + |
| 100 | + # Select a target button |
| 101 | + target_button = tk.Button(window, text="Select a target", command=select_target) |
| 102 | + target_button.pack(side=tk.RIGHT, padx=10, pady=10) |
| 103 | + |
| 104 | + # FPS limit checkbox |
| 105 | + limit_fps = tk.IntVar() |
| 106 | + fps_checkbox = tk.Checkbutton(window, text="Limit FPS to 30", variable=limit_fps, command=toggle_fps_limit, font=("Arial", 8)) |
| 107 | + fps_checkbox.pack(side=tk.BOTTOM) |
| 108 | + fps_checkbox.select() |
| 109 | + |
| 110 | + # Start button |
| 111 | + start_button = tk.Button(window, text="Start", bg="green", command=start) |
| 112 | + start_button.pack(side=tk.BOTTOM, padx=10, pady=10) |
| 113 | + window.mainloop() |
0 commit comments