2023-06-03 08:04:06 +02:00
|
|
|
import cv2
|
|
|
|
|
import insightface
|
|
|
|
|
import onnxruntime
|
|
|
|
|
import sys
|
|
|
|
|
from insightface.app import FaceAnalysis
|
|
|
|
|
sys.path.insert(1, './recognition')
|
|
|
|
|
from scrfd import SCRFD
|
|
|
|
|
from arcface_onnx import ArcFaceONNX
|
|
|
|
|
import os.path as osp
|
|
|
|
|
import os
|
|
|
|
|
from pathlib import Path
|
2023-06-05 09:04:02 +02:00
|
|
|
from tqdm import tqdm
|
2023-06-03 08:04:06 +02:00
|
|
|
import ffmpeg
|
2023-06-05 07:09:17 +02:00
|
|
|
import random
|
2023-06-05 23:18:25 +02:00
|
|
|
import multiprocessing as mp
|
|
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
2023-06-03 08:04:06 +02:00
|
|
|
|
|
|
|
|
class Refacer:
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
2023-06-05 23:18:25 +02:00
|
|
|
onnxruntime.set_default_logger_severity(4)
|
2023-06-03 08:04:06 +02:00
|
|
|
|
|
|
|
|
self.face_app = FaceAnalysis(name='buffalo_l')
|
|
|
|
|
self.face_app.prepare(ctx_id=0, det_size=(640, 640))
|
|
|
|
|
|
|
|
|
|
assets_dir = osp.expanduser('~/.insightface/models/buffalo_l')
|
|
|
|
|
|
|
|
|
|
self.face_detector = SCRFD(os.path.join(assets_dir, 'det_10g.onnx'))
|
|
|
|
|
self.face_detector.prepare(0)
|
|
|
|
|
|
|
|
|
|
model_path = os.path.join(assets_dir, 'w600k_r50.onnx')
|
|
|
|
|
self.rec_app = ArcFaceONNX(model_path)
|
|
|
|
|
self.rec_app.prepare(0)
|
|
|
|
|
|
|
|
|
|
self.face_swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True, providers=['CoreMLExecutionProvider','CUDAExecutionProvider'])
|
|
|
|
|
|
|
|
|
|
def __prepare_faces(self, faces):
|
|
|
|
|
replacements=[]
|
|
|
|
|
for face in faces:
|
|
|
|
|
#image1 = cv2.imread(face.origin)
|
2023-06-05 23:18:25 +02:00
|
|
|
bboxes1, kpss1 = self.face_detector.autodetect(face['origin'], max_num=1)
|
|
|
|
|
if len(kpss1)<1:
|
|
|
|
|
raise Exception('No face detected on "Face to replace" image')
|
2023-06-03 08:04:06 +02:00
|
|
|
feat_original = self.rec_app.get(face['origin'], kpss1[0])
|
|
|
|
|
#image2 = cv2.imread(face.destination)
|
|
|
|
|
_faces = self.face_app.get(face['destination'],max_num=1)
|
2023-06-05 23:18:25 +02:00
|
|
|
if len(_faces)<1:
|
|
|
|
|
raise Exception('No face detected on "Destination face" image')
|
2023-06-03 08:04:06 +02:00
|
|
|
replacements.append((feat_original,_faces[0],face['threshold']))
|
|
|
|
|
|
|
|
|
|
return replacements
|
|
|
|
|
def __convert_video(self,video_path,output_video_path):
|
2023-06-05 08:29:47 +02:00
|
|
|
new_path = output_video_path + str(random.randint(0,999)) + "_c.mp4"
|
2023-06-05 07:09:17 +02:00
|
|
|
#stream = ffmpeg.input(output_video_path)
|
2023-06-03 08:04:06 +02:00
|
|
|
in1 = ffmpeg.input(output_video_path)
|
|
|
|
|
in2 = ffmpeg.input(video_path)
|
|
|
|
|
out = ffmpeg.output(in1.video, in2.audio, new_path,vcodec="libx264")
|
|
|
|
|
out.run()
|
|
|
|
|
return new_path
|
2023-06-05 23:18:25 +02:00
|
|
|
|
|
|
|
|
def __process_faces(self,frame):
|
|
|
|
|
faces = self.face_app.get(frame)
|
|
|
|
|
for face in faces:
|
|
|
|
|
for rep_face in self.replacement_faces:
|
|
|
|
|
sim = self.rec_app.compute_sim(rep_face[0], face.embedding)
|
|
|
|
|
if sim>=rep_face[2]:
|
|
|
|
|
frame = self.face_swapper.get(frame, face, rep_face[1], paste_back=True)
|
|
|
|
|
return frame
|
2023-06-03 08:04:06 +02:00
|
|
|
|
2023-06-05 09:04:02 +02:00
|
|
|
def reface(self, video_path, faces):
|
2023-06-03 08:04:06 +02:00
|
|
|
output_video_path = os.path.join('out',Path(video_path).name)
|
2023-06-05 23:18:25 +02:00
|
|
|
self.replacement_faces=self.__prepare_faces(faces)
|
2023-06-03 08:04:06 +02:00
|
|
|
|
|
|
|
|
cap = cv2.VideoCapture(video_path)
|
2023-06-05 09:04:02 +02:00
|
|
|
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
2023-06-05 09:18:23 +02:00
|
|
|
print(f"Total frames: {total_frames}")
|
2023-06-05 09:31:55 +02:00
|
|
|
|
|
|
|
|
#probe = ffmpeg.probe(video_path)
|
|
|
|
|
#video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
|
|
|
|
|
#print(video_stream)
|
2023-06-05 07:09:17 +02:00
|
|
|
|
2023-06-03 08:04:06 +02:00
|
|
|
fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
|
|
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
|
|
|
|
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
|
|
|
|
|
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
|
|
|
|
output = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height))
|
2023-06-05 23:18:25 +02:00
|
|
|
|
|
|
|
|
frames=[]
|
|
|
|
|
with tqdm(total=total_frames,desc="Extracting frames") as pbar:
|
2023-06-05 09:31:55 +02:00
|
|
|
while cap.isOpened():
|
|
|
|
|
flag, frame = cap.read()
|
|
|
|
|
if flag and len(frame)>0:
|
2023-06-05 23:18:25 +02:00
|
|
|
frames.append(frame.copy())
|
|
|
|
|
pbar.update()
|
2023-06-05 09:31:55 +02:00
|
|
|
else:
|
|
|
|
|
break
|
2023-06-05 23:18:25 +02:00
|
|
|
cap.release()
|
|
|
|
|
pbar.close()
|
2023-06-05 09:50:57 +02:00
|
|
|
|
2023-06-05 23:18:25 +02:00
|
|
|
with ThreadPoolExecutor(max_workers = mp.cpu_count()-1) as executor:
|
|
|
|
|
results = list(tqdm(executor.map(self.__process_faces, frames), total=len(frames),desc="Processing frames"))
|
|
|
|
|
for result in results:
|
|
|
|
|
output.write(result)
|
|
|
|
|
output.release()
|
2023-06-03 08:04:06 +02:00
|
|
|
|
|
|
|
|
return self.__convert_video(video_path,output_video_path)
|
|
|
|
|
|
|
|
|
|
|