Stable-X commited on
Commit
d12ce0d
1 Parent(s): 5634e46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -18
app.py CHANGED
@@ -33,7 +33,7 @@ from gradio_imageslider import ImageSlider
33
  from tqdm import tqdm
34
 
35
  from pathlib import Path
36
-
37
  import gradio
38
  from gradio.utils import get_cache_folder
39
  from stablenormal.pipeline_yoso_normal import YOSONormalsPipeline
@@ -64,6 +64,19 @@ def process_image_check(path_input):
64
  "Missing image in the first pane: upload a file or use one from the gallery below."
65
  )
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  def process_image(
68
  pipe,
69
  path_input,
@@ -72,27 +85,20 @@ def process_image(
72
  print(f"Processing image {name_base}{name_ext}")
73
 
74
  path_output_dir = tempfile.mkdtemp()
75
- # path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_normal_fp32.npy")
76
  path_out_png = os.path.join(path_output_dir, f"{name_base}_normal_colored.png")
77
 
78
  input_image = Image.open(path_input)
79
- # input_image = center_crop(input_image)
80
-
81
  pipe_out = pipe(
82
  input_image,
83
  match_input_resolution=False,
84
- return_intermediate_result=False
85
  )
86
 
87
  normal_pred = pipe_out.prediction[0, :, :]
88
  normal_colored = pipe.image_processor.visualize_normals(pipe_out.prediction)
89
  normal_colored[-1].save(path_out_png)
90
- print(path_out_png)
91
- # np.save(path_out_fp32, normal_pred)
92
- # path_out_vis = os.path.join(path_output_dir, f"{name_base}_normal_refinement_process.gif")
93
- # normal_colored[0].save(path_out_vis, save_all=True,
94
- # append_images=normal_colored[1:],
95
- # duration=400, loop=0)
96
  return [input_image, path_out_png]
97
 
98
  def center_crop(img):
@@ -155,7 +161,6 @@ def process_video(
155
  pipe_out = pipe(
156
  frame_pil,
157
  match_input_resolution=False,
158
- return_intermediate_result=False
159
  )
160
 
161
  processed_frame = pipe.image_processor.visualize_normals( # noqa
@@ -275,7 +280,7 @@ def run_demo_server(pipe):
275
  ]),
276
  inputs=[image_input],
277
  outputs=[image_output_slider],
278
- cache_examples=True,
279
  directory_name="examples_image",
280
  )
281
 
@@ -315,7 +320,7 @@ def run_demo_server(pipe):
315
  inputs=[video_input],
316
  outputs=[processed_frames, video_output_files],
317
  directory_name="examples_video",
318
- cache_examples=True,
319
  )
320
 
321
  with gr.Tab("Panorama"):
@@ -477,18 +482,17 @@ def main():
477
  os.system("pip freeze")
478
 
479
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
480
-
481
  x_start_pipeline = YOSONormalsPipeline.from_pretrained(
482
- 'Stable-X/yoso-normal-v0-1', trust_remote_code=True,
483
  t_start=300).to(device)
484
  dinov2_prior = DINOv2_Encoder(size=672)
485
  dinov2_prior.to(device)
486
 
487
- pipe = StableNormalPipeline.from_pretrained('Stable-X/stable-normal-v0-1', t_start=300, trust_remote_code=True,
488
  scheduler=HEURI_DDIMScheduler(prediction_type='sample',
489
  beta_start=0.00085, beta_end=0.0120,
490
  beta_schedule = "scaled_linear"))
491
- # two stage concat
492
  pipe.x_start_pipeline = x_start_pipeline
493
  pipe.prior = dinov2_prior
494
  pipe.to(device)
 
33
  from tqdm import tqdm
34
 
35
  from pathlib import Path
36
+ import cv2
37
  import gradio
38
  from gradio.utils import get_cache_folder
39
  from stablenormal.pipeline_yoso_normal import YOSONormalsPipeline
 
64
  "Missing image in the first pane: upload a file or use one from the gallery below."
65
  )
66
 
67
+ def resize_image(input_image, resolution):
68
+ input_image = np.asarray(input_image)
69
+ H, W, C = input_image.shape
70
+ H = float(H)
71
+ W = float(W)
72
+ k = float(resolution) / min(H, W)
73
+ H *= k
74
+ W *= k
75
+ H = int(np.round(H / 64.0)) * 64
76
+ W = int(np.round(W / 64.0)) * 64
77
+ img = cv2.resize(input_image, (W, H), interpolation=cv2.INTER_LANCZOS4 if k > 1 else cv2.INTER_AREA)
78
+ return Image.fromarray(img)
79
+
80
  def process_image(
81
  pipe,
82
  path_input,
 
85
  print(f"Processing image {name_base}{name_ext}")
86
 
87
  path_output_dir = tempfile.mkdtemp()
 
88
  path_out_png = os.path.join(path_output_dir, f"{name_base}_normal_colored.png")
89
 
90
  input_image = Image.open(path_input)
91
+ input_image = resize_image(input_image, default_image_processing_resolution)
92
+
93
  pipe_out = pipe(
94
  input_image,
95
  match_input_resolution=False,
96
+ processing_resolution=max(input_image.size)
97
  )
98
 
99
  normal_pred = pipe_out.prediction[0, :, :]
100
  normal_colored = pipe.image_processor.visualize_normals(pipe_out.prediction)
101
  normal_colored[-1].save(path_out_png)
 
 
 
 
 
 
102
  return [input_image, path_out_png]
103
 
104
  def center_crop(img):
 
161
  pipe_out = pipe(
162
  frame_pil,
163
  match_input_resolution=False,
 
164
  )
165
 
166
  processed_frame = pipe.image_processor.visualize_normals( # noqa
 
280
  ]),
281
  inputs=[image_input],
282
  outputs=[image_output_slider],
283
+ cache_examples=False,
284
  directory_name="examples_image",
285
  )
286
 
 
320
  inputs=[video_input],
321
  outputs=[processed_frames, video_output_files],
322
  directory_name="examples_video",
323
+ cache_examples=False,
324
  )
325
 
326
  with gr.Tab("Panorama"):
 
482
  os.system("pip freeze")
483
 
484
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
485
+
486
  x_start_pipeline = YOSONormalsPipeline.from_pretrained(
487
+ 'weights/yoso-normal-v0-1', trust_remote_code=True,
488
  t_start=300).to(device)
489
  dinov2_prior = DINOv2_Encoder(size=672)
490
  dinov2_prior.to(device)
491
 
492
+ pipe = StableNormalPipeline.from_pretrained('weights/stable-normal-v0-1', t_start=300, trust_remote_code=True,
493
  scheduler=HEURI_DDIMScheduler(prediction_type='sample',
494
  beta_start=0.00085, beta_end=0.0120,
495
  beta_schedule = "scaled_linear"))
 
496
  pipe.x_start_pipeline = x_start_pipeline
497
  pipe.prior = dinov2_prior
498
  pipe.to(device)