felixrosberg commited on
Commit
eccdcbe
β€’
1 Parent(s): 12bb2e4

webcam, anonymization, compare setting/support

Browse files
.idea/vcs.xml CHANGED
@@ -2,5 +2,8 @@
2
  <project version="4">
3
  <component name="VcsDirectoryMappings">
4
  <mapping directory="$PROJECT_DIR$" vcs="Git" />
 
 
 
5
  </component>
6
  </project>
 
2
  <project version="4">
3
  <component name="VcsDirectoryMappings">
4
  <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ <mapping directory="$PROJECT_DIR$/arcface_model" vcs="Git" />
6
+ <mapping directory="$PROJECT_DIR$/g_model" vcs="Git" />
7
+ <mapping directory="$PROJECT_DIR$/retina_model" vcs="Git" />
8
  </component>
9
  </project>
app.py CHANGED
@@ -13,7 +13,7 @@ from tensorflow.keras.models import load_model
13
  from options.swap_options import SwapOptions
14
 
15
  # Invalidated!
16
- token = "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"
17
 
18
  opt = SwapOptions().parse()
19
 
@@ -44,17 +44,20 @@ blend_mask_base[80:250, 32:224] = 1
44
  blend_mask_base = gaussian_filter(blend_mask_base, sigma=7)
45
 
46
 
47
- def run_inference(target, source):
48
  try:
49
  source = np.array(source)
50
  target = np.array(target)
51
 
52
  # Prepare to load video
53
- source_a = RetinaFace(np.expand_dims(source, axis=0)).numpy()[0]
54
- source_h, source_w, _ = source.shape
55
- source_lm = get_lm(source_a, source_w, source_h)
56
- source_aligned = norm_crop(source, source_lm, image_size=256)
57
- source_z = ArcFace.predict(np.expand_dims(tf.image.resize(source_aligned, [112, 112]) / 255.0, axis=0))
 
 
 
58
 
59
  # read frame
60
  im = target
@@ -79,6 +82,14 @@ def run_inference(target, source):
79
  # align the detected face
80
  M, pose_index = estimate_norm(lm_align, 256, "arcface", shrink_factor=1.0)
81
  im_aligned = cv2.warpAffine(im, M, (256, 256), borderValue=0.0)
 
 
 
 
 
 
 
 
82
 
83
  # face swap
84
  changed_face_cage = G.predict([np.expand_dims((im_aligned - 127.5) / 127.5, axis=0),
@@ -97,7 +108,7 @@ def run_inference(target, source):
97
  blend_mask = np.expand_dims(blend_mask, axis=-1)
98
  total_img = (iim_aligned * blend_mask + total_img * (1 - blend_mask))
99
 
100
- if opt.compare:
101
  total_img = np.concatenate((im / 255.0, total_img), axis=1)
102
 
103
  total_img = np.clip(total_img, 0, 1)
@@ -109,15 +120,25 @@ def run_inference(target, source):
109
  print(e)
110
  return None
111
 
112
- description = "Performs subject agnostic identity transfer from a source face to all target faces."
113
- examples = [["rick_astely_example.jpg", "elon_musk_example.jpg"], ["10017.png", "9538.png"]]
 
 
 
 
 
 
 
 
114
  article="""
115
  Demo is based of recent research from my Ph.D work. Results expects to be published in the coming months.
116
  """
117
 
118
  iface = gradio.Interface(run_inference,
119
- [gradio.inputs.Image(shape=None),
120
- gradio.inputs.Image(shape=None)],
 
 
121
  gradio.outputs.Image(),
122
  title="Face Swap",
123
  description=description,
 
13
  from options.swap_options import SwapOptions
14
 
15
  # Invalidated!
16
+ token = os.environ['model_fetch']
17
 
18
  opt = SwapOptions().parse()
19
 
 
44
  blend_mask_base = gaussian_filter(blend_mask_base, sigma=7)
45
 
46
 
47
+ def run_inference(target, source, slider, settings):
48
  try:
49
  source = np.array(source)
50
  target = np.array(target)
51
 
52
  # Prepare to load video
53
+ if "anonymize" not in settings:
54
+ source_a = RetinaFace(np.expand_dims(source, axis=0)).numpy()[0]
55
+ source_h, source_w, _ = source.shape
56
+ source_lm = get_lm(source_a, source_w, source_h)
57
+ source_aligned = norm_crop(source, source_lm, image_size=256)
58
+ source_z = ArcFace.predict(np.expand_dims(tf.image.resize(source_aligned, [112, 112]) / 255.0, axis=0))
59
+ else:
60
+ source_z = None
61
 
62
  # read frame
63
  im = target
 
82
  # align the detected face
83
  M, pose_index = estimate_norm(lm_align, 256, "arcface", shrink_factor=1.0)
84
  im_aligned = cv2.warpAffine(im, M, (256, 256), borderValue=0.0)
85
+
86
+ if "anonymize" in settings:
87
+ source_z = ArcFace.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) / 255.0, axis=0))
88
+ anon_ratio = int(512 * (slider / 100))
89
+ anon_vector = np.ones(shape=(1, 512))
90
+ anon_vector[:, :anon_ratio] = -1
91
+ np.random.shuffle(anon_vector)
92
+ source_z *= anon_vector
93
 
94
  # face swap
95
  changed_face_cage = G.predict([np.expand_dims((im_aligned - 127.5) / 127.5, axis=0),
 
108
  blend_mask = np.expand_dims(blend_mask, axis=-1)
109
  total_img = (iim_aligned * blend_mask + total_img * (1 - blend_mask))
110
 
111
+ if "compare" in settings:
112
  total_img = np.concatenate((im / 255.0, total_img), axis=1)
113
 
114
  total_img = np.clip(total_img, 0, 1)
 
120
  print(e)
121
  return None
122
 
123
+
124
+ description = "Performs subject agnostic identity transfer from a source face to all target faces. \n\n" \
125
+ "Options:\n" \
126
+ "compare returns the target image concatenated with the results.\n" \
127
+ "anonymize will ignore the source image and perform an identity permutation of target faces.\n" \
128
+ "\n" \
129
+ "Note, source image with too high resolution may not work properly!"
130
+ examples = [["assets/rick.jpg", "assets/musk.jpg", 80, ["compare"]],
131
+ ["assets/girl_1.png", "assets/girl_0.png", 80, []],
132
+ ["assets/musk.jpg", "assets/musk.jpg", 30, ["anonymize"]]]
133
  article="""
134
  Demo is based of recent research from my Ph.D work. Results expects to be published in the coming months.
135
  """
136
 
137
  iface = gradio.Interface(run_inference,
138
+ [gradio.inputs.Image(shape=None, label='Target', source='webcam'),
139
+ gradio.inputs.Image(shape=None, label='Source'),
140
+ gradio.inputs.Slider(0, 100, default=80, label="Anonymization ratio (%)"),
141
+ gradio.inputs.CheckboxGroup(["compare", "anonymize"], label='Options')],
142
  gradio.outputs.Image(),
143
  title="Face Swap",
144
  description=description,
9538.png β†’ assets/girl_0.png RENAMED
File without changes
10017.png β†’ assets/girl_1.png RENAMED
File without changes
elon_musk_example.jpg β†’ assets/musk.jpg RENAMED
File without changes
rick_astely_example.jpg β†’ assets/rick.jpg RENAMED
File without changes