Spaces:
Running
on
Zero
Running
on
Zero
guangkaixu
commited on
Commit
•
90347e4
1
Parent(s):
ff72a0b
upload
Browse files- app.py +87 -15
- genpercept/genpercept_pipeline.py +1 -1
app.py
CHANGED
@@ -119,11 +119,11 @@ def process_normal(
|
|
119 |
mode='normal',
|
120 |
)
|
121 |
|
122 |
-
|
123 |
-
|
124 |
|
125 |
-
np.save(path_out_fp32,
|
126 |
-
|
127 |
|
128 |
return (
|
129 |
[path_out_vis],
|
@@ -149,14 +149,14 @@ def process_dis(
|
|
149 |
processing_res=processing_res,
|
150 |
batch_size=1 if processing_res == 0 else 0,
|
151 |
show_progress_bar=False,
|
152 |
-
mode='
|
153 |
)
|
154 |
|
155 |
-
|
156 |
-
|
157 |
|
158 |
-
np.save(path_out_fp32,
|
159 |
-
|
160 |
|
161 |
return (
|
162 |
[path_out_vis],
|
@@ -177,6 +177,39 @@ def process_matting(
|
|
177 |
|
178 |
input_image = Image.open(path_input)
|
179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
pipe_out = pipe(
|
181 |
input_image,
|
182 |
processing_res=processing_res,
|
@@ -185,25 +218,64 @@ def process_matting(
|
|
185 |
mode='seg',
|
186 |
)
|
187 |
|
188 |
-
|
189 |
-
|
190 |
|
191 |
-
np.save(path_out_fp32,
|
192 |
-
|
193 |
|
194 |
return (
|
195 |
[path_out_vis],
|
196 |
[path_out_fp32, path_out_vis],
|
197 |
)
|
198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
199 |
|
200 |
def run_demo_server(pipe_depth, pipe_normal, pipe_dis, pipe_matting, pipe_seg, pipe_disparity):
|
201 |
process_pipe_depth = spaces.GPU(functools.partial(process_depth, pipe_depth))
|
202 |
process_pipe_normal = spaces.GPU(functools.partial(process_normal, pipe_normal))
|
203 |
process_pipe_dis = spaces.GPU(functools.partial(process_dis, pipe_dis))
|
204 |
process_pipe_matting = spaces.GPU(functools.partial(process_matting, pipe_matting))
|
205 |
-
process_pipe_seg = spaces.GPU(functools.partial(
|
206 |
-
process_pipe_disparity = spaces.GPU(functools.partial(
|
207 |
gradio_theme = gr.themes.Default()
|
208 |
|
209 |
with gr.Blocks(
|
|
|
119 |
mode='normal',
|
120 |
)
|
121 |
|
122 |
+
normal_pred = pipe_out.pred_np
|
123 |
+
normal_colored = pipe_out.pred_colored
|
124 |
|
125 |
+
np.save(path_out_fp32, normal_pred)
|
126 |
+
normal_colored.save(path_out_vis)
|
127 |
|
128 |
return (
|
129 |
[path_out_vis],
|
|
|
149 |
processing_res=processing_res,
|
150 |
batch_size=1 if processing_res == 0 else 0,
|
151 |
show_progress_bar=False,
|
152 |
+
mode='dis',
|
153 |
)
|
154 |
|
155 |
+
dis_pred = pipe_out.pred_np
|
156 |
+
dis_colored = pipe_out.pred_colored
|
157 |
|
158 |
+
np.save(path_out_fp32, dis_pred)
|
159 |
+
dis_colored.save(path_out_vis)
|
160 |
|
161 |
return (
|
162 |
[path_out_vis],
|
|
|
177 |
|
178 |
input_image = Image.open(path_input)
|
179 |
|
180 |
+
pipe_out = pipe(
|
181 |
+
input_image,
|
182 |
+
processing_res=processing_res,
|
183 |
+
batch_size=1 if processing_res == 0 else 0,
|
184 |
+
show_progress_bar=False,
|
185 |
+
mode='matting',
|
186 |
+
)
|
187 |
+
|
188 |
+
matting_pred = pipe_out.pred_np
|
189 |
+
matting_colored = pipe_out.pred_colored
|
190 |
+
|
191 |
+
np.save(path_out_fp32, matting_pred)
|
192 |
+
matting_colored.save(path_out_vis)
|
193 |
+
|
194 |
+
return (
|
195 |
+
[path_out_vis],
|
196 |
+
[path_out_fp32, path_out_vis],
|
197 |
+
)
|
198 |
+
|
199 |
+
def process_seg(
|
200 |
+
pipe,
|
201 |
+
path_input,
|
202 |
+
processing_res=default_image_processing_res,
|
203 |
+
):
|
204 |
+
name_base, name_ext = os.path.splitext(os.path.basename(path_input))
|
205 |
+
print(f"Processing image {name_base}{name_ext}")
|
206 |
+
|
207 |
+
path_output_dir = tempfile.mkdtemp()
|
208 |
+
path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_seg_fp32.npy")
|
209 |
+
path_out_vis = os.path.join(path_output_dir, f"{name_base}_seg_colored.png")
|
210 |
+
|
211 |
+
input_image = Image.open(path_input)
|
212 |
+
|
213 |
pipe_out = pipe(
|
214 |
input_image,
|
215 |
processing_res=processing_res,
|
|
|
218 |
mode='seg',
|
219 |
)
|
220 |
|
221 |
+
seg_pred = pipe_out.pred_np
|
222 |
+
seg_colored = pipe_out.pred_colored
|
223 |
|
224 |
+
np.save(path_out_fp32, seg_pred)
|
225 |
+
seg_colored.save(path_out_vis)
|
226 |
|
227 |
return (
|
228 |
[path_out_vis],
|
229 |
[path_out_fp32, path_out_vis],
|
230 |
)
|
231 |
|
232 |
+
def process_disparity(
|
233 |
+
pipe,
|
234 |
+
path_input,
|
235 |
+
processing_res=default_image_processing_res,
|
236 |
+
):
|
237 |
+
print('line 65', path_input)
|
238 |
+
|
239 |
+
name_base, name_ext = os.path.splitext(os.path.basename(path_input))
|
240 |
+
print(f"Processing image {name_base}{name_ext}")
|
241 |
+
|
242 |
+
path_output_dir = tempfile.mkdtemp()
|
243 |
+
path_out_fp32 = os.path.join(path_output_dir, f"{name_base}_disparity_fp32.npy")
|
244 |
+
path_out_vis = os.path.join(path_output_dir, f"{name_base}_disparity_colored.png")
|
245 |
+
|
246 |
+
input_image = Image.open(path_input)
|
247 |
+
|
248 |
+
pipe_out = pipe(
|
249 |
+
input_image,
|
250 |
+
processing_res=processing_res,
|
251 |
+
batch_size=1 if processing_res == 0 else 0,
|
252 |
+
show_progress_bar=False,
|
253 |
+
mode='disparity',
|
254 |
+
)
|
255 |
+
|
256 |
+
disparity_pred = pipe_out.pred_np
|
257 |
+
disparity_colored = pipe_out.pred_colored
|
258 |
+
|
259 |
+
np.save(path_out_fp32, disparity_pred)
|
260 |
+
disparity_colored.save(path_out_vis)
|
261 |
+
|
262 |
+
path_out_16bit = os.path.join(path_output_dir, f"{name_base}_disparity_16bit.png")
|
263 |
+
disparity_16bit = (disparity_pred * 65535.0).astype(np.uint16)
|
264 |
+
Image.fromarray(disparity_16bit).save(path_out_16bit, mode="I;16")
|
265 |
+
|
266 |
+
return (
|
267 |
+
[path_out_16bit, path_out_vis],
|
268 |
+
[path_out_16bit, path_out_fp32, path_out_vis],
|
269 |
+
)
|
270 |
+
|
271 |
|
272 |
def run_demo_server(pipe_depth, pipe_normal, pipe_dis, pipe_matting, pipe_seg, pipe_disparity):
|
273 |
process_pipe_depth = spaces.GPU(functools.partial(process_depth, pipe_depth))
|
274 |
process_pipe_normal = spaces.GPU(functools.partial(process_normal, pipe_normal))
|
275 |
process_pipe_dis = spaces.GPU(functools.partial(process_dis, pipe_dis))
|
276 |
process_pipe_matting = spaces.GPU(functools.partial(process_matting, pipe_matting))
|
277 |
+
process_pipe_seg = spaces.GPU(functools.partial(process_seg, pipe_seg))
|
278 |
+
process_pipe_disparity = spaces.GPU(functools.partial(process_disparity, pipe_disparity))
|
279 |
gradio_theme = gr.themes.Default()
|
280 |
|
281 |
with gr.Blocks(
|
genpercept/genpercept_pipeline.py
CHANGED
@@ -319,7 +319,7 @@ class GenPerceptPipeline(DiffusionPipeline):
|
|
319 |
pred_colored_hwc = chw2hwc(pred_colored)
|
320 |
pred_colored_img = Image.fromarray(pred_colored_hwc)
|
321 |
else:
|
322 |
-
pred_colored_img =
|
323 |
|
324 |
if len(pipe_pred.shape) == 3 and pipe_pred.shape[0] == 3:
|
325 |
pipe_pred = np.transpose(pipe_pred, (1, 2, 0))
|
|
|
319 |
pred_colored_hwc = chw2hwc(pred_colored)
|
320 |
pred_colored_img = Image.fromarray(pred_colored_hwc)
|
321 |
else:
|
322 |
+
pred_colored_img = Image.fromarray((pipe_pred * 255.0).astype(np.uint8))
|
323 |
|
324 |
if len(pipe_pred.shape) == 3 and pipe_pred.shape[0] == 3:
|
325 |
pipe_pred = np.transpose(pipe_pred, (1, 2, 0))
|