uhdessai commited on
Commit
a4c9991
·
verified ·
1 Parent(s): ba33c62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -5
app.py CHANGED
@@ -198,13 +198,49 @@ model_path = 'dress_model.pkl'
198
  with open(model_path, 'rb') as f:
199
  G = legacy.load_network_pkl(f)['G_ema'].to(device)
200
 
201
- # Style mixing between two projected images
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  def mix_styles(image1_path, image2_path, styles_to_mix):
203
- image1_name = os.path.splitext(os.path.basename(image1_path))[0]
204
- image2_name = os.path.splitext(os.path.basename(image2_path))[0]
205
 
206
- latent_vector_1 = np.load(os.path.join("projection_results", image1_name, "projected_w.npz"))['w']
207
- latent_vector_2 = np.load(os.path.join("projection_results", image2_name, "projected_w.npz"))['w']
208
 
209
  latent_1_tensor = torch.from_numpy(latent_vector_1).to(device)
210
  latent_2_tensor = torch.from_numpy(latent_vector_2).to(device)
@@ -219,6 +255,7 @@ def mix_styles(image1_path, image2_path, styles_to_mix):
219
  mixed_image = Image.fromarray(image[0], 'RGB')
220
  return mixed_image
221
 
 
222
  # Handles style mixing + output buffer
223
  def style_mixing_interface(image1, image2, mix_value):
224
  if image1 is None or image2 is None:
 
198
  with open(model_path, 'rb') as f:
199
  G = legacy.load_network_pkl(f)['G_ema'].to(device)
200
 
201
+ # # Style mixing between two projected images
202
+ # def mix_styles(image1_path, image2_path, styles_to_mix):
203
+ # image1_name = os.path.splitext(os.path.basename(image1_path))[0]
204
+ # image2_name = os.path.splitext(os.path.basename(image2_path))[0]
205
+
206
+ # latent_vector_1 = np.load(os.path.join("projection_results", image1_name, "projected_w.npz"))['w']
207
+ # latent_vector_2 = np.load(os.path.join("projection_results", image2_name, "projected_w.npz"))['w']
208
+
209
+ # latent_1_tensor = torch.from_numpy(latent_vector_1).to(device)
210
+ # latent_2_tensor = torch.from_numpy(latent_vector_2).to(device)
211
+
212
+ # mixed_latent = latent_1_tensor.clone()
213
+ # mixed_latent[:, styles_to_mix] = latent_2_tensor[:, styles_to_mix]
214
+
215
+ # with torch.no_grad():
216
+ # image = G.synthesis(mixed_latent, noise_mode='const')
217
+
218
+ # image = (image.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8).cpu().numpy()
219
+ # mixed_image = Image.fromarray(image[0], 'RGB')
220
+ # return mixed_image
221
+ # ----------- UTIL: ENSURE PROJECTION ----------
222
+ def ensure_projection(image_path):
223
+ image_name = os.path.splitext(os.path.basename(image_path))[0]
224
+ proj_dir = os.path.join("projection_results", image_name)
225
+ proj_file = os.path.join(proj_dir, "projected_w.npz")
226
+
227
+ if not os.path.exists(proj_file):
228
+ print(f"Projection for {image_name} not found. Running projector.py...")
229
+ os.makedirs(proj_dir, exist_ok=True)
230
+ subprocess.run([
231
+ "python", "projector.py",
232
+ f"--network={NETWORK_PKL}",
233
+ f"--target={image_path}",
234
+ f"--outdir={proj_dir}"
235
+ ], check=True)
236
+ return proj_file
237
+
238
  def mix_styles(image1_path, image2_path, styles_to_mix):
239
+ proj_file1 = ensure_projection(image1_path)
240
+ proj_file2 = ensure_projection(image2_path)
241
 
242
+ latent_vector_1 = np.load(proj_file1)['w']
243
+ latent_vector_2 = np.load(proj_file2)['w']
244
 
245
  latent_1_tensor = torch.from_numpy(latent_vector_1).to(device)
246
  latent_2_tensor = torch.from_numpy(latent_vector_2).to(device)
 
255
  mixed_image = Image.fromarray(image[0], 'RGB')
256
  return mixed_image
257
 
258
+
259
  # Handles style mixing + output buffer
260
  def style_mixing_interface(image1, image2, mix_value):
261
  if image1 is None or image2 is None: