RageshAntony commited on
Commit
4c8a229
·
verified ·
1 Parent(s): 20d1f25

added vid tab and fubncs

Browse files
Files changed (1) hide show
  1. app.py +145 -162
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
- # @spaces.GPU(duration=120)
245
- # def submit_function_p2p(
246
- # person_image,
247
- # cloth_image,
248
- # num_inference_steps,
249
- # guidance_scale,
250
- # seed):
251
- # person_image = person_image["background"]
252
- #
253
- # tmp_folder = args.output_dir
254
- # date_str = datetime.now().strftime("%Y%m%d%H%M%S")
255
- # result_save_path = os.path.join(tmp_folder, date_str[:8], date_str[8:] + ".png")
256
- # if not os.path.exists(os.path.join(tmp_folder, date_str[:8])):
257
- # os.makedirs(os.path.join(tmp_folder, date_str[:8]))
258
- #
259
- # generator = None
260
- # if seed != -1:
261
- # generator = torch.Generator(device='cuda').manual_seed(seed)
262
- #
263
- # person_image = Image.open(person_image).convert("RGB")
264
- # cloth_image = Image.open(cloth_image).convert("RGB")
265
- # person_image = resize_and_crop(person_image, (args.width, args.height))
266
- # cloth_image = resize_and_padding(cloth_image, (args.width, args.height))
267
- #
268
- # # Inference
269
- # try:
270
- # result_image = pipeline_p2p(
271
- # image=person_image,
272
- # condition_image=cloth_image,
273
- # num_inference_steps=num_inference_steps,
274
- # guidance_scale=guidance_scale,
275
- # generator=generator
276
- # )[0]
277
- # except Exception as e:
278
- # raise gr.Error(
279
- # "An error occurred. Please try again later: {}".format(e)
280
- # )
281
- #
282
- # # Post-process
283
- # save_result_image = image_grid([person_image, cloth_image, result_image], 1, 3)
284
- # save_result_image.save(result_save_path)
285
- # return result_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, 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
- # with gr.Tab("Mask-free & SD1.5"):
658
- # with gr.Row():
659
- # with gr.Column(scale=1, min_width=350):
660
- # with gr.Row():
661
- # image_path_p2p = gr.Image(
662
- # type="filepath",
663
- # interactive=True,
664
- # visible=False,
665
- # )
666
- # person_image_p2p = gr.ImageEditor(
667
- # interactive=True, label="Person Image", type="filepath"
668
- # )
669
- #
670
- # with gr.Row():
671
- # with gr.Column(scale=1, min_width=230):
672
- # cloth_image_p2p = gr.Image(
673
- # interactive=True, label="Condition Image", type="filepath"
674
- # )
675
- #
676
- # submit_p2p = gr.Button("Submit")
677
- # gr.Markdown(
678
- # '<center><span style="color: #FF0000">!!! Click only Once, Wait for Delay !!!</span></center>'
679
- # )
680
- #
681
- # gr.Markdown(
682
- # '<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>'
683
- # )
684
- # with gr.Accordion("Advanced Options", open=False):
685
- # num_inference_steps_p2p = gr.Slider(
686
- # label="Inference Step", minimum=10, maximum=100, step=5, value=50
687
- # )
688
- # # Guidence Scale
689
- # guidance_scale_p2p = gr.Slider(
690
- # label="CFG Strenth", minimum=0.0, maximum=7.5, step=0.5, value=2.5
691
- # )
692
- # # Random Seed
693
- # seed_p2p = gr.Slider(
694
- # label="Seed", minimum=-1, maximum=10000, step=1, value=42
695
- # )
696
- # # show_type = gr.Radio(
697
- # # label="Show Type",
698
- # # choices=["result only", "input & result", "input & mask & result"],
699
- # # value="input & mask & result",
700
- # # )
701
- #
702
- # with gr.Column(scale=2, min_width=500):
703
- # result_image_p2p = gr.Image(interactive=False, label="Result")
704
- # with gr.Row():
705
- # # Photo Examples
706
- # root_path = "resource/demo/example"
707
- # with gr.Column():
708
- # gr.Examples(
709
- # examples=[
710
- # os.path.join(root_path, "person", "men", _)
711
- # for _ in os.listdir(os.path.join(root_path, "person", "men"))
712
- # ],
713
- # examples_per_page=4,
714
- # inputs=image_path_p2p,
715
- # label="Person Examples ①",
716
- # )
717
- # gr.Examples(
718
- # examples=[
719
- # os.path.join(root_path, "person", "women", _)
720
- # for _ in os.listdir(os.path.join(root_path, "person", "women"))
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