2023-05-30 22:12:19 +03:00
|
|
|
import os
|
|
|
|
|
2023-05-28 17:49:40 +03:00
|
|
|
import cv2
|
|
|
|
import insightface
|
|
|
|
from core.config import get_face
|
|
|
|
from core.utils import rreplace
|
|
|
|
|
2023-05-30 17:29:51 +03:00
|
|
|
FACE_SWAPPER = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_face_swapper():
|
|
|
|
global FACE_SWAPPER
|
|
|
|
if FACE_SWAPPER is None:
|
2023-05-30 22:52:39 +03:00
|
|
|
model_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), '../inswapper_128.onnx')
|
|
|
|
FACE_SWAPPER = insightface.model_zoo.get_model(model_path)
|
2023-05-30 17:29:51 +03:00
|
|
|
return FACE_SWAPPER
|
2023-05-30 01:40:02 +03:00
|
|
|
|
2023-05-28 17:49:40 +03:00
|
|
|
|
|
|
|
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)
|
2023-05-29 16:47:57 +03:00
|
|
|
if face:
|
2023-05-30 17:29:51 +03:00
|
|
|
result = get_face_swapper().get(frame, face, source_face, paste_back=True)
|
2023-05-29 16:47:57 +03:00
|
|
|
cv2.imwrite(frame_path, result)
|
|
|
|
print('.', end='', flush=True)
|
|
|
|
else:
|
|
|
|
print('S', end='', flush=True)
|
2023-05-28 17:49:40 +03:00
|
|
|
except Exception as e:
|
2023-05-29 16:47:57 +03:00
|
|
|
print('E', end='', flush=True)
|
2023-05-28 17:49:40 +03:00
|
|
|
pass
|
|
|
|
|
2023-05-30 01:40:02 +03:00
|
|
|
|
2023-05-30 16:37:22 +03:00
|
|
|
def process_img(source_img, target_path, output_file):
|
2023-05-28 17:49:40 +03:00
|
|
|
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)
|
2023-05-30 16:37:22 +03:00
|
|
|
cv2.imwrite(output_file, result)
|
2023-05-30 17:33:02 +03:00
|
|
|
print("\n\nImage saved as:", output_file, "\n\n")
|