Delete refacer.py
Browse files- refacer.py +0 -77
refacer.py
DELETED
@@ -1,77 +0,0 @@
|
|
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"Colab Mode (TENSORRT): {self.providers}")
|
57 |
-
else:
|
58 |
-
self.mode = RefacerMode.CUDA
|
59 |
-
self.use_num_cpus = mp.cpu_count()-2
|
60 |
-
self.sess_options.intra_op_num_threads = int(self.use_num_cpus/3)
|
61 |
-
print(f"CUDA Mode with providers {self.providers}")
|
62 |
-
|
63 |
-
def __init_apps(self):
|
64 |
-
self.face_app = FaceAnalysis()
|
65 |
-
self.face_app.prepare(ctx_id=0, det_size=(640, 640))
|
66 |
-
|
67 |
-
self.inswapper = INSwapper()
|
68 |
-
|
69 |
-
def reface(self, video_path, faces):
|
70 |
-
self.first_face = True
|
71 |
-
input_video = cv2.VideoCapture(video_path)
|
72 |
-
video_fps = input_video.get(cv2.CAP_PROP_FPS)
|
73 |
-
width = int(input_video.get(cv2.CAP_PROP_FRAME_WIDTH))
|
74 |
-
height = int(input_video.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
75 |
-
output_video = cv2.VideoWriter('refaced_video.mp4', cv2.VideoWriter_fourcc(*'mp4v'), video_fps, (width, height))
|
76 |
-
|
77 |
-
return output_video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|