qqwjq1981 commited on
Commit
8b4daa9
·
verified ·
1 Parent(s): c989ab3

Upload keyframe_utils.py

Browse files
Files changed (1) hide show
  1. utils/keyframe_utils.py +78 -0
utils/keyframe_utils.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import random
3
+ import os
4
+ from diffusers import StableDiffusionPipeline
5
+ import torch
6
+
7
+ # Load and cache the diffusion pipeline (only once)
8
+ pipe = StableDiffusionPipeline.from_pretrained(
9
+ "CompVis/stable-diffusion-v1-4",
10
+ torch_dtype=torch.float16
11
+ )
12
+ pipe = pipe.to("cuda")
13
+
14
+
15
+ def generate_keyframe_prompt(segment):
16
+ """
17
+ Generates a detailed prompt optimized for Stable Diffusion (low-resolution, preview style)
18
+ based on the segment description.
19
+ """
20
+ description = segment.get("description", "")
21
+ speaker = segment.get("speaker", "")
22
+ narration = segment.get("narration", "")
23
+ segment_id = segment.get("segment_id")
24
+
25
+ prompt_parts = []
26
+
27
+ if description:
28
+ prompt_parts.append(f"Scene: {description}.")
29
+
30
+ if speaker and narration:
31
+ prompt_parts.append(f"Character '{speaker}' speaking: \"{narration}\".")
32
+ elif narration:
33
+ prompt_parts.append(f"Narration: \"{narration}\".")
34
+
35
+ prompt_parts.append("Style: Simple, cartoonish, line art, sketch, low detail, illustrative, minimal background, focus on main subject.")
36
+ prompt_parts.append("Resolution: lowres, 256x256.")
37
+ prompt_parts.append("Lighting: Nighttime museum, dim lighting.")
38
+ prompt_parts.append("Setting: Museum interior, exhibits.")
39
+
40
+ negative_prompt = "blurry, distorted, ugly, tiling, poorly drawn, out of frame, disfigured, deformed, bad anatomy, watermark, text, signature, high detail, realistic, photorealistic, complex"
41
+
42
+ return {
43
+ "prompt": " ".join(prompt_parts).strip(),
44
+ "negative_prompt": negative_prompt
45
+ }
46
+
47
+
48
+ def generate_all_keyframe_images(script_data, output_dir="keyframes"):
49
+ """
50
+ Generates 3 keyframe images per segment using Stable Diffusion,
51
+ stores them in the given output directory.
52
+ """
53
+ os.makedirs(output_dir, exist_ok=True)
54
+ keyframe_outputs = []
55
+
56
+ for segment in script_data:
57
+ sd_prompts = generate_keyframe_prompt(segment)
58
+ prompt = sd_prompts["prompt"]
59
+ negative_prompt = sd_prompts["negative_prompt"]
60
+ segment_id = segment.get("segment_id")
61
+
62
+ frame_images = []
63
+ for i in range(3):
64
+ image = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=20, guidance_scale=7.5, height=256, width=256).images[0]
65
+ image_path = os.path.join(output_dir, f"segment_{segment_id}_v{i+1}.png")
66
+ image.save(image_path)
67
+ frame_images.append(image_path)
68
+
69
+ keyframe_outputs.append({
70
+ "segment_id": segment_id,
71
+ "prompt": prompt,
72
+ "negative_prompt": negative_prompt,
73
+ "frame_images": frame_images
74
+ })
75
+
76
+ print(f"✓ Generated 3 images for Segment {segment_id}")
77
+
78
+ return keyframe_outputs