Ii
commited on
Update refacer.py
Browse files- refacer.py +78 -40
refacer.py
CHANGED
|
@@ -1,49 +1,87 @@
|
|
| 1 |
import cv2
|
| 2 |
-
import ffmpeg
|
| 3 |
-
import numpy as np
|
| 4 |
-
import os
|
| 5 |
import onnxruntime as rt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
class Refacer:
|
| 8 |
-
def __init__(self,
|
|
|
|
| 9 |
self.force_cpu = force_cpu
|
| 10 |
self.colab_performance = colab_performance
|
| 11 |
-
self.
|
| 12 |
-
self.
|
| 13 |
-
self.
|
| 14 |
self.__init_apps()
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def __init_apps(self):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
"""
|
| 35 |
-
Refacing the video by swapping faces.
|
| 36 |
-
"""
|
| 37 |
-
output_video_path = "output_video.mp4" # Set the output video path
|
| 38 |
-
# Code to process the faces and perform refacing (image manipulation here)
|
| 39 |
-
# Process faces and apply face swap logic
|
| 40 |
-
print("Refacing video...")
|
| 41 |
-
return self.__convert_video(video_path, output_video_path)
|
| 42 |
-
|
| 43 |
-
# Example of running the app (just for understanding, Gradio app will call this part)
|
| 44 |
-
if __name__ == "__main__":
|
| 45 |
-
refacer = Refacer(force_cpu=True)
|
| 46 |
-
faces = [{'origin': 'origin_image.jpg', 'destination': 'destination_image.jpg', 'threshold': 0.8}]
|
| 47 |
-
video_path = 'input_video.mp4'
|
| 48 |
-
refaced_video_path = refacer.reface(video_path, faces)
|
| 49 |
-
print(f"Refaced video can be found at: {refaced_video_path}")
|
|
|
|
| 1 |
import cv2
|
|
|
|
|
|
|
|
|
|
| 2 |
import onnxruntime as rt
|
| 3 |
+
import sys
|
| 4 |
+
from insightface.app import FaceAnalysis
|
| 5 |
+
sys.path.insert(1, './recognition')
|
| 6 |
+
from scrfd import SCRFD
|
| 7 |
+
from arcface_onnx import ArcFaceONNX
|
| 8 |
+
import os.path as osp
|
| 9 |
+
import os
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from tqdm import tqdm
|
| 12 |
+
import ffmpeg
|
| 13 |
+
import random
|
| 14 |
+
import multiprocessing as mp
|
| 15 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 16 |
+
from insightface.model_zoo.inswapper import INSwapper
|
| 17 |
+
import psutil
|
| 18 |
+
from enum import Enum
|
| 19 |
+
from insightface.app.common import Face
|
| 20 |
+
from insightface.utils.storage import ensure_available
|
| 21 |
+
import re
|
| 22 |
+
import subprocess
|
| 23 |
+
|
| 24 |
+
class RefacerMode(Enum):
|
| 25 |
+
CPU, CUDA, COREML, TENSORRT = range(1, 5)
|
| 26 |
|
| 27 |
class Refacer:
|
| 28 |
+
def __init__(self,force_cpu=False,colab_performance=False):
|
| 29 |
+
self.first_face = False
|
| 30 |
self.force_cpu = force_cpu
|
| 31 |
self.colab_performance = colab_performance
|
| 32 |
+
self.__check_encoders()
|
| 33 |
+
self.__check_providers()
|
| 34 |
+
self.total_mem = psutil.virtual_memory().total
|
| 35 |
self.__init_apps()
|
| 36 |
|
| 37 |
+
def __check_providers(self):
|
| 38 |
+
if self.force_cpu :
|
| 39 |
+
self.providers = ['CPUExecutionProvider']
|
| 40 |
+
else:
|
| 41 |
+
self.providers = rt.get_available_providers()
|
| 42 |
+
rt.set_default_logger_severity(4)
|
| 43 |
+
self.sess_options = rt.SessionOptions()
|
| 44 |
+
self.sess_options.execution_mode = rt.ExecutionMode.ORT_SEQUENTIAL
|
| 45 |
+
self.sess_options.graph_optimization_level = rt.GraphOptimizationLevel.ORT_ENABLE_ALL
|
| 46 |
+
|
| 47 |
+
if len(self.providers) == 1 and 'CPUExecutionProvider' in self.providers:
|
| 48 |
+
self.mode = RefacerMode.CPU
|
| 49 |
+
self.use_num_cpus = mp.cpu_count()-1
|
| 50 |
+
self.sess_options.intra_op_num_threads = int(self.use_num_cpus/3)
|
| 51 |
+
print(f"CPU mode with providers {self.providers}")
|
| 52 |
+
elif self.colab_performance:
|
| 53 |
+
self.mode = RefacerMode.TENSORRT
|
| 54 |
+
self.use_num_cpus = mp.cpu_count()-1
|
| 55 |
+
self.sess_options.intra_op_num_threads = int(self.use_num_cpus/3)
|
| 56 |
+
print(f"TENSORRT mode with providers {self.providers}")
|
| 57 |
+
elif 'CoreMLExecutionProvider' in self.providers:
|
| 58 |
+
self.mode = RefacerMode.COREML
|
| 59 |
+
self.use_num_cpus = mp.cpu_count()-1
|
| 60 |
+
self.sess_options.intra_op_num_threads = int(self.use_num_cpus/3)
|
| 61 |
+
print(f"CoreML mode with providers {self.providers}")
|
| 62 |
+
elif 'CUDAExecutionProvider' in self.providers:
|
| 63 |
+
self.mode = RefacerMode.CUDA
|
| 64 |
+
self.use_num_cpus = 2
|
| 65 |
+
self.sess_options.intra_op_num_threads = 1
|
| 66 |
+
if 'TensorrtExecutionProvider' in self.providers:
|
| 67 |
+
self.providers.remove('TensorrtExecutionProvider')
|
| 68 |
+
print(f"CUDA mode with providers {self.providers}")
|
| 69 |
+
|
| 70 |
def __init_apps(self):
|
| 71 |
+
assets_dir = ensure_available('models', 'buffalo_l', root='~/.insightface')
|
| 72 |
+
|
| 73 |
+
model_path = os.path.join(assets_dir, 'det_10g.onnx')
|
| 74 |
+
sess_face = rt.InferenceSession(model_path, self.sess_options, providers=self.providers)
|
| 75 |
+
self.face_detector = SCRFD(model_path,sess_face)
|
| 76 |
+
self.face_detector.prepare(0,input_size=(640, 640))
|
| 77 |
+
|
| 78 |
+
model_path = os.path.join(assets_dir , 'w600k_r50.onnx')
|
| 79 |
+
sess_rec = rt.InferenceSession(model_path, self.sess_options, providers=self.providers)
|
| 80 |
+
self.rec_app = ArcFaceONNX(model_path,sess_rec)
|
| 81 |
+
self.rec_app.prepare(0)
|
| 82 |
+
|
| 83 |
+
model_path = 'inswapper_128.onnx'
|
| 84 |
+
sess_swap = rt.InferenceSession(model_path, self.sess_options, providers=self.providers)
|
| 85 |
+
self.face_swapper = INSwapper(model_path,sess_swap)
|
| 86 |
+
|
| 87 |
+
# மேலும் தேவையான முறைமைகள்...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|