-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangeface.py
75 lines (63 loc) · 2.49 KB
/
changeface.py
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
import datetime
import numpy as np
import os
import glob
import cv2
import insightface
from insightface.app import FaceAnalysis
from insightface.data import get_image as ins_get_image
import time
def generate_image_file_path(time_stamp):
filename = f"images/image_{time_stamp}.png"
file_path = os.path.join(os.getcwd(), filename)
return file_path
def change_face(source_image_file, result_image_file, timestamp):
app = FaceAnalysis(name='buffalo_l')
app.prepare(ctx_id=0, det_size=(640, 640))
swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)
# source_img
source_img = ins_get_image(source_image_file)
source_faces = app.get(source_img)
source_faces = sorted(source_faces, key=lambda x: x.bbox[0])
source_face = source_faces[0]
# res_img
res_img = ins_get_image(result_image_file)
res_faces = app.get(res_img)
res_faces = sorted(res_faces, key=lambda x: x.bbox[0])
# change face
res_img = swapper.get(res_img, res_faces[0], source_face, paste_back=True)
image_file = os.getcwd()
image_name = f"images/image_{timestamp}.png"
image_file_path = os.path.join(image_file, image_name)
cv2.imwrite(image_file_path, res_img)
if __name__ == '__main__':
# app = FaceAnalysis(name='buffalo_l')
# app.prepare(ctx_id=0, det_size=(640, 640))
# swapper = insightface.model_zoo.get_model('inswapper_128.onnx', download=True, download_zip=True)
#
# #source_img
# source_image = f"long"
# source_file = os.getcwd()
# source_img_file = os.path.join(source_file, source_image)
# source_img = ins_get_image(source_img_file)
# source_faces = app.get(source_img)
# source_faces = sorted(source_faces, key=lambda x: x.bbox[0])
# source_face = source_faces[0]
# #res_img
# res_image = f"jirounan"
# res_file = os.getcwd()
# res_img_file = os.path.join(res_file, res_image)
# res_img = ins_get_image(res_img_file)
# res_faces = app.get(res_img)
# res_faces = sorted(res_faces, key=lambda x: x.bbox[0])
# #change face
# res_img = swapper.get(res_img, res_faces[0], source_face, paste_back=True)
# cv2.imwrite("output.jpg", res_img)
source_image = f"yl2"
source_file = os.getcwd()
source_img_file = os.path.join(source_file, source_image)
res_image = f"res"
res_file = os.getcwd()
res_img_file = os.path.join(res_file, res_image)
time_stamp = int(time.time())
change_face(source_img_file, res_img_file, time_stamp)