Merge remote-tracking branch 'origin/main'

# Conflicts:
#	run.py
This commit is contained in:
chris 2023-05-31 13:50:33 -04:00
commit 7bfdd73783
3 changed files with 20 additions and 22 deletions

View File

@ -38,9 +38,9 @@ options:
--keep-fps maintain original fps --keep-fps maintain original fps
--keep-frames keep frames directory --keep-frames keep frames directory
--max-memory MAX_MEMORY --max-memory MAX_MEMORY
set max memory maximum amount of RAM in GB to be used
--max-cores CORES_COUNT --max-cores CORES_COUNT
set max cpu cores number of cores to be use for CPU mode
``` ```
Looking for a CLI mode? Using the -f/--face argument will make the program in cli mode. Looking for a CLI mode? Using the -f/--face argument will make the program in cli mode.

View File

@ -23,7 +23,7 @@ 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() 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:
return int(output.split("/")[0]) // int(output.split("/")[1]), output.removesuffix('\n') return int(output.split(os.sep)[0]) // int(output.split(os.sep)[1]), output.removesuffix('\n')
except: except:
pass pass
return 30, 30 return 30, 30

36
run.py Normal file → Executable file
View File

@ -8,24 +8,24 @@ import glob
import argparse import argparse
import multiprocessing as mp import multiprocessing as mp
import os import os
import random import torch
from pathlib import Path from pathlib import Path
import tkinter as tk import tkinter as tk
from tkinter import filedialog from tkinter import filedialog
from opennsfw2 import predict_image as face_check from opennsfw2 import predict_video_frames, predict_image
from tkinter.filedialog import asksaveasfilename from tkinter.filedialog import asksaveasfilename
import core.globals
from core.processor import process_video, process_img
from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames, rreplace
from core.config import get_face
import webbrowser import webbrowser
import psutil import psutil
import cv2 import cv2
import threading import threading
from PIL import Image, ImageTk from PIL import Image, ImageTk
import core.globals
from core.processor import process_video, process_img
from core.utils import is_img, detect_fps, set_fps, create_video, add_audio, extract_frames, rreplace
from core.config import get_face
if 'ROCMExecutionProvider' not in core.globals.providers: if 'ROCMExecutionProvider' in core.globals.providers:
import torch del torch
pool = None pool = None
args = {} args = {}
@ -37,9 +37,9 @@ parser.add_argument('-o', '--output', help='save output to this file', dest='out
parser.add_argument('--gpu', help='use gpu', dest='gpu', action='store_true', default=False) parser.add_argument('--gpu', help='use gpu', dest='gpu', action='store_true', default=False)
parser.add_argument('--keep-fps', help='maintain original fps', dest='keep_fps', action='store_true', default=False) parser.add_argument('--keep-fps', help='maintain original fps', dest='keep_fps', action='store_true', default=False)
parser.add_argument('--keep-frames', help='keep frames directory', dest='keep_frames', action='store_true', default=False) parser.add_argument('--keep-frames', help='keep frames directory', dest='keep_frames', action='store_true', default=False)
parser.add_argument('--max-memory', help='set max memory', type=int) parser.add_argument('--max-memory', help='maximum amount of RAM in GB to be used', type=int)
parser.add_argument('--max-cores', help='number of cores to use', dest='cores_count', type=int, default=max(psutil.cpu_count() - 2, 2)) parser.add_argument('--max-cores', help='number of cores to be use for CPU mode', dest='cores_count', type=int, default=max(psutil.cpu_count() - 2, 2))
parser.add_argument('-a', '--all-faces', help='swap all faces in frame', dest='all_faces', default=False) parser.add_argument('--all-faces', help='swap all faces in frame', dest='all_faces', action='store_true', default=False)
for name, value in vars(parser.parse_args()).items(): for name, value in vars(parser.parse_args()).items():
args[name] = value args[name] = value
@ -70,8 +70,7 @@ def pre_check():
if not os.path.isfile(model_path): if not os.path.isfile(model_path):
quit('File "inswapper_128.onnx" does not exist!') quit('File "inswapper_128.onnx" does not exist!')
if '--gpu' in sys.argv: if '--gpu' in sys.argv:
NVIDIA_PROVIDERS = ['CUDAExecutionProvider', 'TensorrtExecutionProvider'] if 'ROCMExecutionProvider' not in core.globals.providers:
if len(list(set(core.globals.providers) - set(NVIDIA_PROVIDERS))) == 1:
CUDA_VERSION = torch.version.cuda CUDA_VERSION = torch.version.cuda
CUDNN_VERSION = torch.backends.cudnn.version() CUDNN_VERSION = torch.backends.cudnn.version()
if not torch.cuda.is_available() or not CUDA_VERSION: if not torch.cuda.is_available() or not CUDA_VERSION:
@ -92,10 +91,6 @@ def pre_check():
def start_processing(): def start_processing():
start_time = time.time() start_time = time.time()
threshold = len(['frame_args']) if len(args['frame_paths']) <= 10 else 10
for i in range(threshold):
if face_check(random.choice(args['frame_paths'])) > 0.8:
quit("[WARNING] Unable to determine location of the face in the target. Please make sure the target isn't wearing clothes matching to their skin.")
if args['gpu']: if args['gpu']:
process_video(args['source_img'], args["frame_paths"]) process_video(args['source_img'], args["frame_paths"])
end_time = time.time() end_time = time.time()
@ -200,11 +195,14 @@ def start():
print("\n[WARNING] No face detected in source image. Please try with another one.\n") print("\n[WARNING] No face detected in source image. Please try with another one.\n")
return return
if is_img(target_path): if is_img(target_path):
if face_check(target_path) > 0.7: if predict_image(target_path) > 0.7:
quit("[WARNING] Unable to determine location of the face in the target. Please make sure the target isn't wearing clothes matching to their skin.") quit()
process_img(args['source_img'], target_path, args['output_file']) process_img(args['source_img'], target_path, args['output_file'])
status("swap successful!") status("swap successful!")
return return
seconds, probabilities = predict_video_frames(video_path=args['target_path'], frame_interval=50)
if any(probability > 0.7 for probability in probabilities):
quit()
video_name_full = target_path.split("/")[-1] video_name_full = target_path.split("/")[-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 output_dir = os.path.dirname(target_path) + "/" + video_name