roop/core/processor.py

41 lines
1.3 KiB
Python
Raw Normal View History

import os
2023-05-28 17:49:40 +03:00
import cv2
import insightface
2023-05-29 15:09:52 +03:00
import onnxruntime
2023-05-28 17:49:40 +03:00
from core.config import get_face
from core.utils import rreplace
if os.path.isfile('inswapper_128.onnx'):
face_swapper = insightface.model_zoo.get_model('inswapper_128.onnx', providers=onnxruntime.get_available_providers())
else:
quit('File "inswapper_128.onnx" does not exist!')
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)
if face:
result = face_swapper.get(frame, face, source_face, paste_back=True)
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:
print('E', end='', flush=True)
2023-05-28 17:49:40 +03:00
pass
2023-05-29 17:00:57 +03:00
print(flush=True)
2023-05-28 17:49:40 +03:00
2023-05-28 17:49:40 +03:00
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)