fffiloni commited on
Commit
6a7976f
1 Parent(s): 2dc0a74

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py CHANGED
@@ -17,6 +17,9 @@ using our implementation of the RAFT model. We will also see how to convert the
17
  predicted flows to RGB images for visualization.
18
  """
19
 
 
 
 
20
  import cv2
21
  import numpy as np
22
  import os
@@ -39,6 +42,53 @@ from scipy.interpolate import LinearNDInterpolator
39
  from imageio import imread, imwrite
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def write_flo(flow, filename):
43
  """
44
  Write optical flow in Middlebury .flo format
@@ -74,6 +124,9 @@ def infer():
74
  #frames, _, _ = read_video(str("./spacex.mp4"), output_format="TCHW")
75
  #print(f"FRAME BEFORE stack: {frames[100]}")
76
 
 
 
 
77
 
78
  input_frame_1 = read_image(str("./frame1.jpg"), ImageReadMode.UNCHANGED)
79
  print(f"FRAME 1: {input_frame_1}")
 
17
  predicted flows to RGB images for visualization.
18
  """
19
 
20
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
21
+ from diffusers import UniPCMultistepScheduler
22
+
23
  import cv2
24
  import numpy as np
25
  import os
 
42
  from imageio import imread, imwrite
43
 
44
 
45
+ # Constants
46
+ low_threshold = 100
47
+ high_threshold = 200
48
+
49
+ # Models
50
+ controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
51
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
52
+ "runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None, torch_dtype=torch.float16
53
+ )
54
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
55
+
56
+ # This command loads the individual model components on GPU on-demand. So, we don't
57
+ # need to explicitly call pipe.to("cuda").
58
+ pipe.enable_model_cpu_offload()
59
+
60
+ pipe.enable_xformers_memory_efficient_attention()
61
+
62
+ # Generator seed,
63
+ generator = torch.manual_seed(0)
64
+
65
+ def get_canny_filter(image):
66
+ if not isinstance(image, np.ndarray):
67
+ image = np.array(image)
68
+
69
+ image = cv2.Canny(image, low_threshold, high_threshold)
70
+ image = image[:, :, None]
71
+ image = np.concatenate([image, image, image], axis=2)
72
+ canny_image = Image.fromarray(image)
73
+ return canny_image
74
+
75
+
76
+ def generate_images(image, prompt):
77
+ canny_image = get_canny_filter(image)
78
+ output = pipe(
79
+ prompt,
80
+ canny_image,
81
+ generator=generator,
82
+ num_images_per_prompt=1,
83
+ num_inference_steps=20,
84
+ )
85
+ all_outputs = []
86
+ all_outputs.append(canny_image)
87
+ for image in output.images:
88
+ all_outputs.append(image)
89
+ return all_outputs
90
+
91
+
92
  def write_flo(flow, filename):
93
  """
94
  Write optical flow in Middlebury .flo format
 
124
  #frames, _, _ = read_video(str("./spacex.mp4"), output_format="TCHW")
125
  #print(f"FRAME BEFORE stack: {frames[100]}")
126
 
127
+ pil2diff_img = Image.open("./frame1.jpg")
128
+ diffused_img = generate_images(pil2diff_img)
129
+ print(f"DIFFUSED IMG: {diffused_img[1]}")
130
 
131
  input_frame_1 = read_image(str("./frame1.jpg"), ImageReadMode.UNCHANGED)
132
  print(f"FRAME 1: {input_frame_1}")