CiaraRowles commited on
Commit
33ee32f
1 Parent(s): 9f2539b

Upload runtemporalnetxl.py

Browse files
Files changed (1) hide show
  1. runtemporalnetxl.py +51 -1
runtemporalnetxl.py CHANGED
@@ -61,5 +61,55 @@ else:
61
  initial_frame_path = os.path.join(frames_dir, "frame0000.png")
62
  last_generated_image = load_image(initial_frame_path)
63
 
64
- # ... (rest of the script remains unchanged)
 
 
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  initial_frame_path = os.path.join(frames_dir, "frame0000.png")
62
  last_generated_image = load_image(initial_frame_path)
63
 
64
+ base_model_path = "stabilityai/stable-diffusion-xl-base-1.0"
65
+ controlnet1_path = "CiaraRowles/controlnet-temporalnet-sdxl-1.0"
66
+ controlnet2_path = "diffusers/controlnet-canny-sdxl-1.0"
67
 
68
+ controlnet = [
69
+ ControlNetModel.from_pretrained(controlnet1_path, torch_dtype=torch.float16),
70
+ ControlNetModel.from_pretrained(controlnet2_path, torch_dtype=torch.float16)
71
+ ]
72
+ #controlnet = ControlNetModel.from_pretrained(controlnet2_path, torch_dtype=torch.float16)
73
+
74
+ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
75
+ base_model_path, controlnet=controlnet, torch_dtype=torch.float16
76
+ )
77
+
78
+ #pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
79
+ #pipe.enable_xformers_memory_efficient_attention()
80
+ pipe.enable_model_cpu_offload()
81
+
82
+ generator = torch.manual_seed(7)
83
+
84
+ # Loop over the saved frames in numerical order
85
+ frame_files = sorted(os.listdir(frames_dir), key=frame_number)
86
+
87
+ for i, frame_file in enumerate(frame_files):
88
+ # Use the original video frame to create Canny edge-detected image as the conditioning image for the first ControlNetModel
89
+ control_image_path = os.path.join(frames_dir, frame_file)
90
+ control_image = load_image(control_image_path)
91
+
92
+ canny_image = np.array(control_image)
93
+ canny_image = cv2.Canny(canny_image, 25, 200)
94
+ canny_image = canny_image[:, :, None]
95
+ canny_image = np.concatenate([canny_image, canny_image, canny_image], axis=2)
96
+ canny_image = Image.fromarray(canny_image)
97
+
98
+ # Generate image
99
+ image = pipe(
100
+ prompt, num_inference_steps=20, generator=generator, image=[last_generated_image, canny_image], controlnet_conditioning_scale=[0.6, 0.7]
101
+ #prompt, num_inference_steps=20, generator=generator, image=canny_image, controlnet_conditioning_scale=0.5
102
+ ).images[0]
103
+
104
+ # Save the generated image to output folder
105
+ output_path = os.path.join(output_frames_dir, f"output{str(i).zfill(4)}.png")
106
+ image.save(output_path)
107
+
108
+ # Save the Canny image for reference
109
+ canny_image_path = os.path.join(output_frames_dir, f"outputcanny{str(i).zfill(4)}.png")
110
+ canny_image.save(canny_image_path)
111
+
112
+ # Update the last_generated_image with the newly generated image for the next iteration
113
+ last_generated_image = image
114
+
115
+ print(f"Saved generated image for frame {i} to {output_path}")