File size: 5,708 Bytes
cfeca7d
2bfb87a
7514f76
2bfb87a
 
 
 
 
 
 
 
 
21e90d0
 
2bfb87a
 
 
 
 
 
 
9466bd8
09b2cc2
2bfb87a
 
5a2cb46
69e4532
2bfb87a
 
 
 
21e90d0
 
9555d2f
 
21e90d0
 
 
 
 
0a865f1
21e90d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e6d3064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f86d98c
9555d2f
 
 
 
 
402a7e4
9555d2f
 
 
 
 
dd28c7b
091b875
9555d2f
 
 
 
661cf3a
21e90d0
9555d2f
a36bce1
cfeca7d
9555d2f
e6d3064
a408f1e
e6d3064
9ccf14e
 
e6d3064
 
9555d2f
a408f1e
9555d2f
9ccf14e
 
9555d2f
eddd278
9555d2f
a408f1e
9555d2f
9ccf14e
 
9555d2f
 
 
e66144f
cfeca7d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import gradio as gr
import datetime
import shutil
import numpy as np
import os, subprocess, re, glob, cv2
import os.path as osp
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image
import matplotlib.pyplot as plt
import gdown
from PIL import Image
from rich.progress import Progress, track

#/.insightface/models/buffalo_l.zip
app = FaceAnalysis(name='buffalo_l',providers=['CUDAExecutionProvider']) # , root=str('models/insightface')
app.prepare(ctx_id=0, det_size=(640, 640))
# Import the library
import gdown

# Google Drive file ID
file_id = '1MLAMn5pdHsx7lafeA3KjPechXiqYiYEM'

# Construct the download URL
url = f'https://drive.google.com/uc?id={file_id}'
#url = 'https://drive.google.com/file/d/1MLAMn5pdHsx7lafeA3KjPechXiqYiYEM/view?usp=drive_link'

# Download the file
gdown.download(url, 'inswapper_128.onnx', quiet=False)

swapper = insightface.model_zoo.get_model('inswapper_128.onnx',download=False,download_zip=False)
os.system("mkdir output")
os.system("mkdir frame")
os.system("mkdir allpic")


def facecompare(face1,face2):
  feat1 = face1.normed_embedding
  feat2 = face2.normed_embedding
  similarity = np.dot(feat1, feat2)
  threshold = 0.3
  if similarity > threshold:
      return True
  else:
      return False

def faceswapdir(face_image,path=".",face_id=0,fcfrmid=0):
  img1 = cv2.imread(face_image)
  lst = os.listdir(path)
  lst.sort()
  img_lst = [cv2.imread(f"{path}/{img}") for img in track(lst,description='Read frames')]
  base_face = app.get(img1)[0]
  face_lst =[app.get(face)[face_id] if app.get(face) else None  for face in track(img_lst,description='Extract faces ')]
  out_img = [swapper.get(swap.copy(), face_lst[i], base_face, paste_back=True)  if face_lst[i] is not None  and  facecompare(face_lst[i],face_lst[fcfrmid]) else img_lst[i] for i,swap in enumerate(track(img_lst, description='Swap frames '))]
  swapped_img1_rgb = [cv2.cvtColor(swapped, cv2.COLOR_BGR2RGB) for swapped in track(out_img,description='Refine swapped faces ')]

  for i,swapped2 in enumerate(swapped_img1_rgb):
    Image.fromarray(np.uint8(swapped2)).save(f"output/{i+1:06}.jpg")

def faceswapdirold(face_image,path=".",face_id=0):
  img1 = cv2.imread(face_image)
  lst = os.listdir(path)
  lst.sort()
  img_lst = [cv2.imread(f"{path}/{img}") for img in lst]
  base_face = app.get(img1)[0]
  face_lst =[app.get(face)[0] if app.get(face) else None  for face in img_lst]
  out_img = [swapper.get(swap.copy(), face_lst[i], base_face, paste_back=True)  if face_lst[i] is not None  else img_lst[i] for i,swap in enumerate(track(img_lst, description="Swap frames "))]
  swapped_img1_rgb = [cv2.cvtColor(swapped, cv2.COLOR_BGR2RGB) for swapped in out_img]

  for i,swapped2 in enumerate(swapped_img1_rgb):
    Image.fromarray(np.uint8(swapped2)).save(f"output/{i+1:06}.jpg")

def faceswapfnvidold(path,path2,num1=0):         
    shutil.copyfile(path.name, 'video.mp4')
    shutil.copyfile(path2.name, 'baseimg.jpg')  
    
    os.system('ffmpeg -i video.mp4 -q:a 0 -map a ia.mp3')  
    os.system('ffmpeg -hwaccel auto -i video.mp4 -q:v 18 -pix_fmt rgb24 -vf fps=25  frame/%06d.jpg')
    faceswapdirold('baseimg.jpg','frame',num1)
    os.system('ffmpeg -hwaccel auto -r 25 -i output/%06d.jpg -vcodec libx264 -crf 18  -pix_fmt yuv420p   ov.mp4')
    os.system('ffmpeg -i ov.mp4 -i ia.mp3 -c:v copy -c:a libmp3lame  out_video.mp4')    
    
    return ['out_video.mp4']
    
def faceswapfnvid(path,num1,path2):         
    shutil.copyfile(path.name, 'video.mp4')
    shutil.copyfile(path2.name, 'baseimg.jpg')  
    
    os.system('ffmpeg -i video.mp4 -q:a 0 -map a ia.mp3')  
    os.system('ffmpeg -hwaccel auto -i video.mp4 -q:v 18 -pix_fmt rgb24 -vf fps=25  frame/%06d.jpg')
    faceswapdir('baseimg.jpg','frame',0,num1)
    os.system('ffmpeg -hwaccel auto -r 25 -i output/%06d.jpg -vcodec libx264 -crf 18  -pix_fmt yuv420p   ov.mp4')
    os.system('ffmpeg -i ov.mp4 -i ia.mp3 -c:v copy -c:a libmp3lame  out_video.mp4')    
    
    return ['out_video.mp4']

def faceswapfn(path,path2):         
    shutil.copyfile(path.name, 'allpic/targ.jpg')
    shutil.copyfile(path2.name, 'baseimg.jpg')  
    
    faceswapdir('baseimg.jpg','allpic',0,0)
    
    return ['output/000001.jpg']

#demo = gr.Interface(fn=faceswapfn, title="Subtitle the Video" ,inputs="file", outputs="file")
gr.set_static_paths(paths=["/home/user/app"])

with gr.Blocks() as demo:
    with gr.Tab('Video Faceswap old'):
        gr.Markdown("Upload Video old")
        with gr.Row():
            inp = [gr.File(file_types=['.mp4'],label="Target Video"),gr.File(file_types=['.jpg','.jpeg','.png'],label="Base Face Image"),gr.Number(value=0)]
            out = gr.File(label="Swapped Video")
        btn = gr.Button("Face Swap")
        btn.click(fn=faceswapfnvidold, inputs=inp, outputs=out)
    with gr.Tab('Video Faceswap'):
        gr.Markdown("Upload Video")
        with gr.Row():
            inp = [gr.File(file_types=['.mp4'],label="Target Video"),gr.Number(value=0),gr.File(file_types=['.jpg','.jpeg','.png'],label="Base Face Image")]
            out = gr.File(label="Swapped Video")
        btn = gr.Button("Face Swap")
        btn.click(fn=faceswapfnvid, inputs=inp, outputs=out)
    with gr.Tab('Image Faceswap'):
        gr.Markdown("Upload Image")
        with gr.Row():
            inp = [gr.File(file_types=['.jpg','.jpeg','.png'], label="Target Image"),gr.File(file_types=['.jpg','.jpeg','.png'], label="Base Face Image")]
            out = gr.File(label="Swapped Image")
        btn = gr.Button("Face Swap")
        btn.click(fn=faceswapfn, inputs=inp, outputs=out)
    
demo.launch(share=True,allowed_paths=['out_video.mp4','output/000001.jpg'])