qqwjq1981 commited on
Commit
af5ead2
·
verified ·
1 Parent(s): 2123fc1

Update utils/keyframe_utils.py

Browse files
Files changed (1) hide show
  1. utils/keyframe_utils.py +42 -0
utils/keyframe_utils.py CHANGED
@@ -72,3 +72,45 @@ def generate_keyframe_prompt(segment):
72
  "prompt": description,
73
  "negative_prompt": ""
74
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  "prompt": description,
73
  "negative_prompt": ""
74
  }
75
+
76
+ def generate_all_keyframe_images(script_data, output_dir="keyframes"):
77
+ os.makedirs(output_dir, exist_ok=True)
78
+ keyframe_outputs = []
79
+
80
+ for segment in script_data:
81
+ sd_prompts = generate_keyframe_prompt(segment)
82
+ prompt = sd_prompts["prompt"]
83
+ negative_prompt = sd_prompts["negative_prompt"]
84
+ segment_id = segment.get("segment_id")
85
+
86
+ description = segment.get("description", "")
87
+ use_reference = any(name in description for name in ASSET_IMAGES)
88
+
89
+ if use_reference:
90
+ ref_key = next(k for k in ASSET_IMAGES if k in description)
91
+ init_image = Image.open(ASSET_IMAGES[ref_key]).convert("RGB").resize((512, 512))
92
+
93
+ frame_images = []
94
+ for i in range(3):
95
+ if use_reference:
96
+ image = pipe_img2img(prompt=prompt, image=init_image, negative_prompt=negative_prompt, strength=0.6, guidance_scale=7.5).images[0]
97
+ else:
98
+ image = pipe_txt2img(prompt, negative_prompt=negative_prompt, num_inference_steps=20, guidance_scale=7.5, height=256, width=256).images[0]
99
+
100
+ image_path = os.path.join(output_dir, f"segment_{segment_id}_v{i+1}.png")
101
+ image.save(image_path)
102
+ frame_images.append(image_path)
103
+
104
+ keyframe_outputs.append({
105
+ "segment_id": segment_id,
106
+ "prompt": prompt,
107
+ "negative_prompt": negative_prompt,
108
+ "frame_images": frame_images
109
+ })
110
+
111
+ print(f"✓ Generated 3 images for Segment {segment_id} ({'img2img' if use_reference else 'txt2img'})")
112
+
113
+ with open("all_prompts_output.json", "w", encoding="utf-8") as f:
114
+ json.dump(keyframe_outputs, f, ensure_ascii=False, indent=2)
115
+
116
+ return keyframe_outputs