initial build

This commit is contained in:
Somdev Sangwan 2023-05-28 20:19:40 +05:30 committed by GitHub
parent 4bef0d1d7f
commit f5d85163e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 266 additions and 0 deletions

65
README.md Normal file
View File

@ -0,0 +1,65 @@
Take a video and replace the face in it with a face of your choice. You only need one image of the desired face. No dataset, no training.
That's it, that's the software.
![demo-gif](demo.gif)
## Installation
> Do not create any issues regarding installation problems. I am only responsible for issues in this program, use google for help.
1. install `python`, `pip` and `git`
2. install `ffmpeg`
3. run the following commands in terminal:
```
git clone https://github.com/s0md3v/roop
cd roop
pip3 install -r requirements.txt
```
### Do you have a decent GPU?
If you have a good enough GPU, you can use it to speed up the face-swapping process by running `run.py` with `--gpu` flag.
If you plan on doing, you will need to install the appropriate `onnxruntime-*` package as follows:
#### NVIDIA
```
pip3 install onnxruntime-gpu
```
#### AMD
```
git clone https://github.com/microsoft/onnxruntime
cd onnxruntime
./build.sh --config Release --build_wheel --update --build --parallel --cmake_extra_defines CMAKE_PREFIX_PATH=/opt/rocm/lib/cmake ONNXRUNTIME_VERSION=$ONNXRUNTIME_VERSION onnxruntime_BUILD_UNIT_TESTS=off --use_rocm --rocm_home=/opt/rocm
pip install build/Linux/Release/dist/*.whl
```
## Usage
> Note: When you run this program for the first time, it will download some models ~300MB in size.
Executing `python run.py` command will launch this window:
Choose a face (image with desired face) and the target image/video (image/video in which you want to replace the face) and click on `Start`. The output will be saved in `output.mp4` file.
Don't touch the FPS checkbox unless you know what you are doing.
Additional command line arguments are given below:
```
-h, --help show this help message and exit
-f SOURCE_IMG, --face SOURCE_IMG
use this face
-t TARGET_PATH, --target TARGET_PATH
replace this face
--keep-fps keep original fps
--gpu use gpu
--keep-frames don't delete frames directory
```
Looking for a CLI mode? Using the -f/--face argument will make the program in cli mode.
## Future plans
- [ ] Replace a selective face throughout the video
- [ ] Support for replacing multiple faces
## Credits
- [ffmpeg](https://ffmpeg.org/): for making video related operations easy
- [deepinsight](https://github.com/deepinsight): for their [insightface](https://github.com/deepinsight/insightface) project which provided a well-made library and models.
- and all developers behind libraries used in this project.

1
core/__init__.py Normal file
View File

@ -0,0 +1 @@

9
core/config.py Normal file
View File

@ -0,0 +1,9 @@
import insightface
face_analyser = insightface.app.FaceAnalysis(name='buffalo_l', providers=['CUDAExecutionProvider', 'ROCMExecutionProvider', 'CPUExecutionProvider'])
face_analyser.prepare(ctx_id=0, det_size=(640, 640))
def get_face(img_data):
analysed = face_analyser.get(img_data)
return sorted(analysed, key=lambda x: x.bbox[0])[0]

28
core/processor.py Normal file
View File

@ -0,0 +1,28 @@
import cv2
import insightface
from core.config import get_face
from core.utils import rreplace
face_swapper = insightface.model_zoo.get_model('inswapper_128.onnx', providers=['CUDAExecutionProvider', 'ROCMExecutionProvider', 'CPUExecutionProvider'])
def process_video(source_img, frame_paths):
source_face = get_face(cv2.imread(source_img))
for frame_path in frame_paths:
frame = cv2.imread(frame_path)
try:
face = get_face(frame)
result = face_swapper.get(frame, face, source_face, paste_back=True)
cv2.imwrite(frame_path, result)
except Exception as e:
pass
def process_img(source_img, target_path):
frame = cv2.imread(target_path)
face = get_face(frame)
source_face = get_face(cv2.imread(source_img))
result = face_swapper.get(frame, face, source_face, paste_back=True)
target_path = rreplace(target_path, "/", "/swapped-", 1) if "/" in target_path else "swapped-"+target_path
print(target_path)
cv2.imwrite(target_path, result)

46
core/utils.py Normal file
View File

@ -0,0 +1,46 @@
import os
import shutil
def run_command(command, mode="silent"):
if mode == "debug":
return os.system(command)
return os.popen(command).read()
def detect_fps(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()
if "/" in output:
try:
return int(output.split("/")[0]) // int(output.split("/")[1])
except:
pass
return 60
def set_fps(input_path, output_path, fps):
os.system(f"ffmpeg -i {input_path} -filter:v fps=fps={fps} {output_path}")
def create_video(video_name, fps, output_dir):
os.system(f"ffmpeg -framerate {fps} -pattern_type glob -i '{output_dir}/*.png' -c:v libx264 -pix_fmt yuv420p -y {output_dir}/output.mp4")
def extract_frames(input_path, output_dir):
os.system(f"ffmpeg -i {input_path} '{output_dir}/%04d.png'")
def add_audio(current_dir, output_dir, target_path, keep_frames):
video = target_path.split("/")[-1]
video_name = video.split(".")[0]
os.system(f"ffmpeg -i {output_dir}/output.mp4 -i {output_dir}/{video} -c:v copy -map 0:v:0 -map 1:a:0 -y {current_dir}/swapped-{video_name}.mp4")
if not os.path.isfile(current_dir + "/swapped-" + video_name + ".mp4"):
shutil.move(output_dir + "/output.mp4", current_dir + "/swapped-" + video_name + ".mp4")
if not keep_frames:
shutil.rmtree(output_dir)
def is_img(path):
return path.lower().endswith(("png", "jpg", "jpeg", "bmp"))
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)

BIN
demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 MiB

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
numpy
opencv-python
onnx
insightface

113
run.py Normal file
View File

@ -0,0 +1,113 @@
import glob
import argparse
import multiprocessing as mp
import os
from pathlib import Path
import tkinter as tk
from tkinter import filedialog
from core.processor import process_video, process_img
from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames
import webbrowser
import psutil
import shutil
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')
parser.add_argument('--keep-fps', help='maintain original fps', dest='keep_fps', action='store_true', default=False)
parser.add_argument('--gpu', help='use gpu', dest='gpu', action='store_true', default=False)
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
def start_processing():
if args['gpu']:
process_video(args['source_img'], args["frame_paths"])
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()
def select_face():
args['source_img'] = filedialog.askopenfilename(title="Select a face")
def select_target():
args['target_path'] = filedialog.askopenfilename(title="Select a target")
def toggle_fps_limit():
args['keep_fps'] = limit_fps.get() != True
def start():
global pool
pool = mp.Pool(psutil.cpu_count()-1)
current_dir = os.getcwd()
target_path = args['target_path']
if is_img(target_path):
process_img(args['source_img'], target_path)
return
video_name = target_path.split("/")[-1].split(".")[0]
output_dir = current_dir + "/" + video_name
Path(output_dir).mkdir(exist_ok=True)
fps = detect_fps(target_path)
if not args['keep_fps'] and fps > 30:
this_path = output_dir + "/" + video_name + ".mp4"
set_fps(target_path, this_path, 30)
target_path, fps = this_path, 30
else:
shutil.copy(target_path, output_dir)
extract_frames(target_path, output_dir)
args['frame_paths'] = tuple(sorted(
glob.glob(output_dir + "/*.png"),
key=lambda x: int(x.split("/")[-1].replace(".png", ""))
))
start_processing()
create_video(video_name, fps, output_dir)
add_audio(current_dir, output_dir, target_path, args['keep_frames'])
if __name__ == "__main__":
if args['source_img']:
start()
quit()
window = tk.Tk()
window.geometry("600x200")
window.title("roop")
# Contact information
support_link = tk.Label(window, text="Support the project ^_^", fg="red", cursor="hand2")
support_link.pack(padx=10, pady=10)
support_link.bind("<Button-1>", lambda e: webbrowser.open("https://github.com/sponsors/s0md3v"))
# Select a face button
face_button = tk.Button(window, text="Select a face", command=select_face)
face_button.pack(side=tk.LEFT, padx=10, pady=10)
# Select a target button
target_button = tk.Button(window, text="Select a target", command=select_target)
target_button.pack(side=tk.RIGHT, padx=10, pady=10)
# FPS limit checkbox
limit_fps = tk.IntVar()
fps_checkbox = tk.Checkbutton(window, text="Limit FPS to 30", variable=limit_fps, command=toggle_fps_limit, font=("Arial", 8))
fps_checkbox.pack(side=tk.BOTTOM)
fps_checkbox.select()
# Start button
start_button = tk.Button(window, text="Start", bg="green", command=start)
start_button.pack(side=tk.BOTTOM, padx=10, pady=10)
window.mainloop()