Merge pull request #196 from Moeblack/CLI-processerbar

Add a processer bar with remain time for CLI
This commit is contained in:
Henry Ruhs
2023-05-31 20:54:28 +02:00
committed by GitHub
2 changed files with 45 additions and 42 deletions

View File

@@ -1,42 +1,44 @@
import os import os
from tqdm import tqdm
import cv2 import cv2
import insightface import insightface
import core.globals import core.globals
from core.config import get_face from core.config import get_face
FACE_SWAPPER = None FACE_SWAPPER = None
def get_face_swapper(): def get_face_swapper():
global FACE_SWAPPER global FACE_SWAPPER
if FACE_SWAPPER is None: if FACE_SWAPPER is None:
model_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../inswapper_128.onnx') model_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../inswapper_128.onnx')
FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=core.globals.providers) FACE_SWAPPER = insightface.model_zoo.get_model(model_path, providers=core.globals.providers)
return FACE_SWAPPER return FACE_SWAPPER
def process_video(source_img, frame_paths): def process_video(source_img, frame_paths):
source_face = get_face(cv2.imread(source_img)) source_face = get_face(cv2.imread(source_img))
for frame_path in frame_paths: with tqdm(total=len(frame_paths), desc="Processing", unit="frame", dynamic_ncols=True, bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]') as pbar:
frame = cv2.imread(frame_path) for frame_path in frame_paths:
try: frame = cv2.imread(frame_path)
face = get_face(frame) try:
if face: face = get_face(frame)
result = get_face_swapper().get(frame, face, source_face, paste_back=True) if face:
cv2.imwrite(frame_path, result) result = get_face_swapper().get(frame, face, source_face, paste_back=True)
print('.', end='', flush=True) cv2.imwrite(frame_path, result)
else: pbar.set_postfix(status='.', refresh=True)
print('S', end='', flush=True) else:
except Exception: pbar.set_postfix(status='S', refresh=True)
print('E', end='', flush=True) except Exception:
pass pbar.set_postfix(status='E', refresh=True)
pass
pbar.update(1)
def process_img(source_img, target_path, output_file):
frame = cv2.imread(target_path)
face = get_face(frame) def process_img(source_img, target_path, output_file):
source_face = get_face(cv2.imread(source_img)) frame = cv2.imread(target_path)
result = get_face_swapper().get(frame, face, source_face, paste_back=True) face = get_face(frame)
cv2.imwrite(output_file, result) source_face = get_face(cv2.imread(source_img))
print("\n\nImage saved as:", output_file, "\n\n") result = get_face_swapper().get(frame, face, source_face, paste_back=True)
cv2.imwrite(output_file, result)
print("\n\nImage saved as:", output_file, "\n\n")

View File

@@ -9,3 +9,4 @@ torch==2.0.1
onnxruntime-gpu==1.15.0 onnxruntime-gpu==1.15.0
opennsfw2==0.10.2 opennsfw2==0.10.2
protobuf==3.20.2 protobuf==3.20.2
tqdm==4.65.0