felixrosberg commited on
Commit
87d3df0
β€’
1 Parent(s): 1b7cc25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -59
app.py CHANGED
@@ -43,65 +43,69 @@ blend_mask_base = gaussian_filter(blend_mask_base, sigma=7)
43
 
44
 
45
  def run_inference(target, source):
46
- source = np.array(source)
47
- target = np.array(target)
48
-
49
- # Prepare to load video
50
- source_a = RetinaFace(np.expand_dims(source, axis=0)).numpy()[0]
51
- source_h, source_w, _ = source.shape
52
- source_lm = get_lm(source_a, source_w, source_h)
53
- source_aligned = norm_crop(source, source_lm, image_size=256)
54
- source_z = ArcFace.predict(np.expand_dims(tf.image.resize(source_aligned, [112, 112]) / 255.0, axis=0))
55
-
56
- # read frame
57
- im = target
58
- im_h, im_w, _ = im.shape
59
- im_shape = (im_w, im_h)
60
-
61
- detection_scale = im_w // 640 if im_w > 640 else 1
62
-
63
- faces = RetinaFace(np.expand_dims(cv2.resize(im,
64
- (im_w // detection_scale,
65
- im_h // detection_scale)), axis=0)).numpy()
66
-
67
- total_img = im / 255.0
68
- for annotation in faces:
69
- lm_align = np.array([[annotation[4] * im_w, annotation[5] * im_h],
70
- [annotation[6] * im_w, annotation[7] * im_h],
71
- [annotation[8] * im_w, annotation[9] * im_h],
72
- [annotation[10] * im_w, annotation[11] * im_h],
73
- [annotation[12] * im_w, annotation[13] * im_h]],
74
- dtype=np.float32)
75
-
76
- # align the detected face
77
- M, pose_index = estimate_norm(lm_align, 256, "arcface", shrink_factor=1.0)
78
- im_aligned = cv2.warpAffine(im, M, (256, 256), borderValue=0.0)
79
-
80
- # face swap
81
- changed_face_cage = G.predict([np.expand_dims((im_aligned - 127.5) / 127.5, axis=0),
82
- source_z])
83
- changed_face = (changed_face_cage[0] + 1) / 2
84
-
85
- # get inverse transformation landmarks
86
- transformed_lmk = transform_landmark_points(M, lm_align)
87
-
88
- # warp image back
89
- iM, _ = inverse_estimate_norm(lm_align, transformed_lmk, 256, "arcface", shrink_factor=1.0)
90
- iim_aligned = cv2.warpAffine(changed_face, iM, im_shape, borderValue=0.0)
91
-
92
- # blend swapped face with target image
93
- blend_mask = cv2.warpAffine(blend_mask_base, iM, im_shape, borderValue=0.0)
94
- blend_mask = np.expand_dims(blend_mask, axis=-1)
95
- total_img = (iim_aligned * blend_mask + total_img * (1 - blend_mask))
96
-
97
- if opt.compare:
98
- total_img = np.concatenate((im / 255.0, total_img), axis=1)
99
-
100
- total_img = np.clip(total_img, 0, 1)
101
- total_img *= 255.0
102
- total_img = total_img.astype('uint8')
103
-
104
- return total_img
 
 
 
 
105
 
106
  description = "Performs subject agnostic identity transfer from a source face to all target faces."
107
  examples = [["elon_musk_example.jpg", "rick_astely_example.jpg"], ["10017.png", "9538.png"]]
 
43
 
44
 
45
  def run_inference(target, source):
46
+ try:
47
+ source = np.array(source)
48
+ target = np.array(target)
49
+
50
+ # Prepare to load video
51
+ source_a = RetinaFace(np.expand_dims(source, axis=0)).numpy()[0]
52
+ source_h, source_w, _ = source.shape
53
+ source_lm = get_lm(source_a, source_w, source_h)
54
+ source_aligned = norm_crop(source, source_lm, image_size=256)
55
+ source_z = ArcFace.predict(np.expand_dims(tf.image.resize(source_aligned, [112, 112]) / 255.0, axis=0))
56
+
57
+ # read frame
58
+ im = target
59
+ im_h, im_w, _ = im.shape
60
+ im_shape = (im_w, im_h)
61
+
62
+ detection_scale = im_w // 640 if im_w > 640 else 1
63
+
64
+ faces = RetinaFace(np.expand_dims(cv2.resize(im,
65
+ (im_w // detection_scale,
66
+ im_h // detection_scale)), axis=0)).numpy()
67
+
68
+ total_img = im / 255.0
69
+ for annotation in faces:
70
+ lm_align = np.array([[annotation[4] * im_w, annotation[5] * im_h],
71
+ [annotation[6] * im_w, annotation[7] * im_h],
72
+ [annotation[8] * im_w, annotation[9] * im_h],
73
+ [annotation[10] * im_w, annotation[11] * im_h],
74
+ [annotation[12] * im_w, annotation[13] * im_h]],
75
+ dtype=np.float32)
76
+
77
+ # align the detected face
78
+ M, pose_index = estimate_norm(lm_align, 256, "arcface", shrink_factor=1.0)
79
+ im_aligned = cv2.warpAffine(im, M, (256, 256), borderValue=0.0)
80
+
81
+ # face swap
82
+ changed_face_cage = G.predict([np.expand_dims((im_aligned - 127.5) / 127.5, axis=0),
83
+ source_z])
84
+ changed_face = (changed_face_cage[0] + 1) / 2
85
+
86
+ # get inverse transformation landmarks
87
+ transformed_lmk = transform_landmark_points(M, lm_align)
88
+
89
+ # warp image back
90
+ iM, _ = inverse_estimate_norm(lm_align, transformed_lmk, 256, "arcface", shrink_factor=1.0)
91
+ iim_aligned = cv2.warpAffine(changed_face, iM, im_shape, borderValue=0.0)
92
+
93
+ # blend swapped face with target image
94
+ blend_mask = cv2.warpAffine(blend_mask_base, iM, im_shape, borderValue=0.0)
95
+ blend_mask = np.expand_dims(blend_mask, axis=-1)
96
+ total_img = (iim_aligned * blend_mask + total_img * (1 - blend_mask))
97
+
98
+ if opt.compare:
99
+ total_img = np.concatenate((im / 255.0, total_img), axis=1)
100
+
101
+ total_img = np.clip(total_img, 0, 1)
102
+ total_img *= 255.0
103
+ total_img = total_img.astype('uint8')
104
+
105
+ return total_img
106
+ except Exception as e:
107
+ print(e)
108
+ return None
109
 
110
  description = "Performs subject agnostic identity transfer from a source face to all target faces."
111
  examples = [["elon_musk_example.jpg", "rick_astely_example.jpg"], ["10017.png", "9538.png"]]