Spaces:
Running
on
Zero
Running
on
Zero
added vid tab and fubncs
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import os
|
|
4 |
os.environ['CUDA_HOME'] = '/usr/local/cuda'
|
5 |
os.environ['PATH'] = os.environ['PATH'] + ':/usr/local/cuda/bin'
|
6 |
from datetime import datetime
|
7 |
-
|
8 |
import gradio as gr
|
9 |
import spaces
|
10 |
import numpy as np
|
@@ -241,48 +241,85 @@ def submit_function(
|
|
241 |
return new_result_image
|
242 |
|
243 |
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
#
|
264 |
-
|
265 |
-
|
266 |
-
#
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
|
287 |
|
288 |
@spaces.GPU(duration=120)
|
@@ -359,7 +396,7 @@ def submit_function_flux(
|
|
359 |
new_result_image = Image.new("RGB", (width + condition_width + 5, height))
|
360 |
new_result_image.paste(conditions, (0, 0))
|
361 |
new_result_image.paste(result_image, (condition_width + 5, 0))
|
362 |
-
return new_result_image,
|
363 |
|
364 |
|
365 |
def person_example_fn(image_path):
|
@@ -654,124 +691,70 @@ def app_gradio():
|
|
654 |
result_image_flux,gal_output
|
655 |
)
|
656 |
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
|
678 |
-
|
679 |
-
|
680 |
-
|
681 |
-
|
682 |
-
|
683 |
-
|
684 |
-
|
685 |
-
|
686 |
-
|
687 |
-
|
688 |
-
|
689 |
-
|
690 |
-
|
691 |
-
|
692 |
-
|
693 |
-
|
694 |
-
|
695 |
-
|
696 |
-
|
697 |
-
|
698 |
-
|
699 |
-
|
700 |
-
|
701 |
-
|
702 |
-
|
703 |
-
|
704 |
-
|
705 |
-
|
706 |
-
|
707 |
-
|
708 |
-
|
709 |
-
|
710 |
-
|
711 |
-
|
712 |
-
|
713 |
-
|
714 |
-
|
715 |
-
|
716 |
-
|
717 |
-
|
718 |
-
|
719 |
-
|
720 |
-
|
721 |
-
# ],
|
722 |
-
# examples_per_page=4,
|
723 |
-
# inputs=image_path_p2p,
|
724 |
-
# label="Person Examples ②",
|
725 |
-
# )
|
726 |
-
# gr.Markdown(
|
727 |
-
# '<span style="color: #808080; font-size: small;">*Person examples come from the demos of <a href="https://huggingface.co/spaces/levihsu/OOTDiffusion">OOTDiffusion</a> and <a href="https://www.outfitanyone.org">OutfitAnyone</a>. </span>'
|
728 |
-
# )
|
729 |
-
# with gr.Column():
|
730 |
-
# gr.Examples(
|
731 |
-
# examples=[
|
732 |
-
# os.path.join(root_path, "condition", "upper", _)
|
733 |
-
# for _ in os.listdir(os.path.join(root_path, "condition", "upper"))
|
734 |
-
# ],
|
735 |
-
# examples_per_page=4,
|
736 |
-
# inputs=cloth_image_p2p,
|
737 |
-
# label="Condition Upper Examples",
|
738 |
-
# )
|
739 |
-
# gr.Examples(
|
740 |
-
# examples=[
|
741 |
-
# os.path.join(root_path, "condition", "overall", _)
|
742 |
-
# for _ in os.listdir(os.path.join(root_path, "condition", "overall"))
|
743 |
-
# ],
|
744 |
-
# examples_per_page=4,
|
745 |
-
# inputs=cloth_image_p2p,
|
746 |
-
# label="Condition Overall Examples",
|
747 |
-
# )
|
748 |
-
# condition_person_exm = gr.Examples(
|
749 |
-
# examples=[
|
750 |
-
# os.path.join(root_path, "condition", "person", _)
|
751 |
-
# for _ in os.listdir(os.path.join(root_path, "condition", "person"))
|
752 |
-
# ],
|
753 |
-
# examples_per_page=4,
|
754 |
-
# inputs=cloth_image_p2p,
|
755 |
-
# label="Condition Reference Person Examples",
|
756 |
-
# )
|
757 |
-
# gr.Markdown(
|
758 |
-
# '<span style="color: #808080; font-size: small;">*Condition examples come from the Internet. </span>'
|
759 |
-
# )
|
760 |
-
#
|
761 |
-
# image_path_p2p.change(
|
762 |
-
# person_example_fn, inputs=image_path_p2p, outputs=person_image_p2p
|
763 |
-
# )
|
764 |
-
#
|
765 |
-
# submit_p2p.click(
|
766 |
-
# submit_function_p2p,
|
767 |
-
# [
|
768 |
-
# person_image_p2p,
|
769 |
-
# cloth_image_p2p,
|
770 |
-
# num_inference_steps_p2p,
|
771 |
-
# guidance_scale_p2p,
|
772 |
-
# seed_p2p],
|
773 |
-
# result_image_p2p,
|
774 |
-
# )
|
775 |
|
776 |
demo.queue().launch(share=True, show_error=True)
|
777 |
|
|
|
4 |
os.environ['CUDA_HOME'] = '/usr/local/cuda'
|
5 |
os.environ['PATH'] = os.environ['PATH'] + ':/usr/local/cuda/bin'
|
6 |
from datetime import datetime
|
7 |
+
import cv2
|
8 |
import gradio as gr
|
9 |
import spaces
|
10 |
import numpy as np
|
|
|
241 |
return new_result_image
|
242 |
|
243 |
|
244 |
+
|
245 |
+
def extract_frames(video_path):
|
246 |
+
"""
|
247 |
+
Extract frames from a video file
|
248 |
+
|
249 |
+
Args:
|
250 |
+
video_path (str): Path to the video file
|
251 |
+
|
252 |
+
Returns:
|
253 |
+
list: List of frames as PIL Image objects
|
254 |
+
"""
|
255 |
+
frames = []
|
256 |
+
cap = cv2.VideoCapture(video_path)
|
257 |
+
|
258 |
+
while True:
|
259 |
+
ret, frame = cap.read()
|
260 |
+
if not ret:
|
261 |
+
break
|
262 |
+
|
263 |
+
# Convert OpenCV BGR to RGB
|
264 |
+
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
265 |
+
|
266 |
+
# Convert to PIL Image
|
267 |
+
pil_image = Image.fromarray(frame_rgb)
|
268 |
+
frames.append(pil_image)
|
269 |
+
|
270 |
+
cap.release()
|
271 |
+
return frames
|
272 |
+
|
273 |
+
@spaces.GPU(duration=120)
|
274 |
+
def process_video_frames(video, cloth_image, cloth_type, num_inference_steps, guidance_scale, seed, show_type):
|
275 |
+
"""
|
276 |
+
Process each frame of the video through the flux pipeline
|
277 |
+
|
278 |
+
Args:
|
279 |
+
video (str): Path to the input video file
|
280 |
+
cloth_image (str): Path to the cloth image
|
281 |
+
... (other parameters from original function)
|
282 |
+
|
283 |
+
Returns:
|
284 |
+
list: Processed frames
|
285 |
+
"""
|
286 |
+
# Extract frames from video
|
287 |
+
frames = extract_frames(video)
|
288 |
+
|
289 |
+
processed_frames = []
|
290 |
+
|
291 |
+
for person_image in frames:
|
292 |
+
# Create a generator if seed is set
|
293 |
+
generator = None
|
294 |
+
if seed != -1:
|
295 |
+
generator = torch.Generator(device='cuda').manual_seed(seed)
|
296 |
+
|
297 |
+
# Resize and process person image
|
298 |
+
person_image = resize_and_crop(person_image, (args.width, args.height))
|
299 |
+
|
300 |
+
# Load and resize cloth image (do this outside the loop if it's constant)
|
301 |
+
cloth_image = Image.open(cloth_image).convert("RGB")
|
302 |
+
cloth_image = resize_and_padding(cloth_image, (args.width, args.height))
|
303 |
+
|
304 |
+
# Generate mask (you might want to optimize this if mask is similar across frames)
|
305 |
+
mask = automasker(person_image, cloth_type)['mask']
|
306 |
+
mask = mask_processor.blur(mask, blur_factor=9)
|
307 |
+
|
308 |
+
# Run inference
|
309 |
+
result_image = pipeline_flux(
|
310 |
+
image=person_image,
|
311 |
+
condition_image=cloth_image,
|
312 |
+
mask_image=mask,
|
313 |
+
width=args.width,
|
314 |
+
height=args.height,
|
315 |
+
num_inference_steps=num_inference_steps,
|
316 |
+
guidance_scale=guidance_scale,
|
317 |
+
generator=generator
|
318 |
+
).images[0]
|
319 |
+
|
320 |
+
processed_frames.append(result_image)
|
321 |
+
|
322 |
+
return processed_frames
|
323 |
|
324 |
|
325 |
@spaces.GPU(duration=120)
|
|
|
396 |
new_result_image = Image.new("RGB", (width + condition_width + 5, height))
|
397 |
new_result_image.paste(conditions, (0, 0))
|
398 |
new_result_image.paste(result_image, (condition_width + 5, 0))
|
399 |
+
return new_result_image, result_image
|
400 |
|
401 |
|
402 |
def person_example_fn(image_path):
|
|
|
691 |
result_image_flux,gal_output
|
692 |
)
|
693 |
|
694 |
+
with gr.Tab("Mask-free & SD1.5"):
|
695 |
+
with gr.Row():
|
696 |
+
with gr.Column(scale=1, min_width=350):
|
697 |
+
with gr.Row():
|
698 |
+
image_path_p2p = gr.Video(
|
699 |
+
type="filepath",
|
700 |
+
visible=True,
|
701 |
+
)
|
702 |
+
person_image_p2p = gr.ImageEditor(
|
703 |
+
interactive=True, label="Person Image", type="filepath"
|
704 |
+
)
|
705 |
+
|
706 |
+
with gr.Row():
|
707 |
+
with gr.Column(scale=1, min_width=230):
|
708 |
+
cloth_image_p2p = gr.Image(
|
709 |
+
interactive=True, label="Condition Image", type="filepath"
|
710 |
+
)
|
711 |
+
|
712 |
+
submit_p2p = gr.Button("Submit")
|
713 |
+
gr.Markdown(
|
714 |
+
'<center><span style="color: #FF0000">!!! Click only Once, Wait for Delay !!!</span></center>'
|
715 |
+
)
|
716 |
+
|
717 |
+
gr.Markdown(
|
718 |
+
'<span style="color: #808080; font-size: small;">Advanced options can adjust details:<br>1. `Inference Step` may enhance details;<br>2. `CFG` is highly correlated with saturation;<br>3. `Random seed` may improve pseudo-shadow.</span>'
|
719 |
+
)
|
720 |
+
with gr.Accordion("Advanced Options", open=False):
|
721 |
+
num_inference_steps_p2p = gr.Slider(
|
722 |
+
label="Inference Step", minimum=10, maximum=100, step=5, value=50
|
723 |
+
)
|
724 |
+
# Guidence Scale
|
725 |
+
guidance_scale_p2p = gr.Slider(
|
726 |
+
label="CFG Strenth", minimum=0.0, maximum=7.5, step=0.5, value=2.5
|
727 |
+
)
|
728 |
+
# Random Seed
|
729 |
+
seed_p2p = gr.Slider(
|
730 |
+
label="Seed", minimum=-1, maximum=10000, step=1, value=42
|
731 |
+
)
|
732 |
+
# show_type = gr.Radio(
|
733 |
+
# label="Show Type",
|
734 |
+
# choices=["result only", "input & result", "input & mask & result"],
|
735 |
+
# value="input & mask & result",
|
736 |
+
# )
|
737 |
+
|
738 |
+
with gr.Column(scale=2, min_width=500):
|
739 |
+
#result_image_p2p = gr.Image(interactive=False, label="Result")
|
740 |
+
gall_output = gr.Gallery(label="Processed Frames")
|
741 |
+
|
742 |
+
|
743 |
+
image_path_p2p.change(
|
744 |
+
person_example_fn, inputs=image_path_p2p, outputs=person_image_p2p
|
745 |
+
)
|
746 |
+
|
747 |
+
submit_p2p.click(
|
748 |
+
submit_function_p2p,
|
749 |
+
[
|
750 |
+
person_image_p2p,
|
751 |
+
cloth_image_p2p,
|
752 |
+
num_inference_steps_p2p,
|
753 |
+
guidance_scale_p2p,
|
754 |
+
seed_p2p],
|
755 |
+
gall_output,
|
756 |
+
)
|
757 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
758 |
|
759 |
demo.queue().launch(share=True, show_error=True)
|
760 |
|