Create ,ass
#132
by
kakaj6354gmailcom
- opened
,ass
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pip install diffusers transformers torch torchvision torchaudio moviepy pillow openai accelerate safetensors
|
| 2 |
+
|
| 3 |
+
# ai_cartoon_animated.py
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
import torch
|
| 7 |
+
from diffusers import StableDiffusionPipeline, StableVideoDiffusionPipeline
|
| 8 |
+
from transformers import pipeline
|
| 9 |
+
from moviepy.editor import concatenate_videoclips, AudioFileClip, VideoFileClip
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
# ========= SETTINGS =========
|
| 13 |
+
huggingface_token = "YOUR_HUGGINGFACE_TOKEN"
|
| 14 |
+
output_dir = "ai_cartoon_movie"
|
| 15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 17 |
+
|
| 18 |
+
# ========= 1. STORY CREATION =========
|
| 19 |
+
story_theme = "A brave robot exploring an alien planet with its cat companion."
|
| 20 |
+
num_scenes = 20 # about 30 seconds per scene
|
| 21 |
+
|
| 22 |
+
story_gen = pipeline("text-generation", model="gpt2")
|
| 23 |
+
story_text = story_gen(
|
| 24 |
+
f"Write a {num_scenes}-scene cartoon story about {story_theme}. "
|
| 25 |
+
"Each scene should include a one-sentence visual description and one sentence of narration.",
|
| 26 |
+
max_length=1200,
|
| 27 |
+
temperature=0.8,
|
| 28 |
+
do_sample=True
|
| 29 |
+
)[0]["generated_text"]
|
| 30 |
+
|
| 31 |
+
scenes = [s.strip() for s in story_text.split(".") if len(s.strip()) > 10][:num_scenes]
|
| 32 |
+
|
| 33 |
+
# ========= 2. IMAGE GENERATION =========
|
| 34 |
+
print("\n🎨 Generating base cartoon frames...")
|
| 35 |
+
sd_pipe = StableDiffusionPipeline.from_pretrained(
|
| 36 |
+
"runwayml/stable-diffusion-v1-5",
|
| 37 |
+
use_auth_token=huggingface_token
|
| 38 |
+
).to(device)
|
| 39 |
+
|
| 40 |
+
image_paths = []
|
| 41 |
+
for i, desc in enumerate(scenes):
|
| 42 |
+
prompt = f"cartoon style, vibrant colors, {desc}"
|
| 43 |
+
img = sd_pipe(prompt).images[0]
|
| 44 |
+
img_path = os.path.join(output_dir, f"scene_{i}.png")
|
| 45 |
+
img.save(img_path)
|
| 46 |
+
image_paths.append(img_path)
|
| 47 |
+
|
| 48 |
+
# ========= 3. VIDEO ANIMATION =========
|
| 49 |
+
print("\n🎞️ Generating motion for each scene (Stable Video Diffusion)...")
|
| 50 |
+
svd_pipe = StableVideoDiffusionPipeline.from_pretrained(
|
| 51 |
+
"stabilityai/stable-video-diffusion-img2vid",
|
| 52 |
+
use_auth_token=huggingface_token
|
| 53 |
+
).to(device)
|
| 54 |
+
|
| 55 |
+
video_paths = []
|
| 56 |
+
for i, img_path in enumerate(image_paths):
|
| 57 |
+
image = Image.open(img_path).convert("RGB")
|
| 58 |
+
print(f"Animating scene {i+1}/{len(image_paths)}...")
|
| 59 |
+
frames = svd_pipe(image, num_frames=16).frames[0] # 16 short frames
|
| 60 |
+
frame_dir = os.path.join(output_dir, f"frames_{i}")
|
| 61 |
+
os.makedirs(frame_dir, exist_ok=True)
|
| 62 |
+
frame_paths = []
|
| 63 |
+
for j, frame in enumerate(frames):
|
| 64 |
+
fpath = os.path.join(frame_dir, f"frame_{j}.png")
|
| 65 |
+
frame.save(fpath)
|
| 66 |
+
frame_paths.append(fpath)
|
| 67 |
+
|
| 68 |
+
# Combine frames into a video
|
| 69 |
+
import cv2
|
| 70 |
+
frame = cv2.imread(frame_paths[0])
|
| 71 |
+
height, width, _ = frame.shape
|
| 72 |
+
out_path = os.path.join(output_dir, f"scene_{i}.mp4")
|
| 73 |
+
out = cv2.VideoWriter(out_path, cv2.VideoWriter_fourcc(*'mp4v'), 8, (width, height))
|
| 74 |
+
for fpath in frame_paths:
|
| 75 |
+
out.write(cv2.imread(fpath))
|
| 76 |
+
out.release()
|
| 77 |
+
video_paths.append(out_path)
|
| 78 |
+
|
| 79 |
+
# ========= 4. VOICE / NARRATION =========
|
| 80 |
+
print("\n🎤 Generating narration...")
|
| 81 |
+
tts = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits")
|
| 82 |
+
audio_paths = []
|
| 83 |
+
for i, desc in enumerate(scenes):
|
| 84 |
+
narration = f"Scene {i+1}. {desc}."
|
| 85 |
+
speech = tts(narration)
|
| 86 |
+
audio_path = os.path.join(output_dir, f"scene_{i}.wav")
|
| 87 |
+
with open(audio_path, "wb") as f:
|
| 88 |
+
f.write(speech["audio"])
|
| 89 |
+
audio_paths.append(audio_path)
|
| 90 |
+
|
| 91 |
+
# ========= 5. ASSEMBLE FINAL MOVIE =========
|
| 92 |
+
print("\n🎬 Combining all animated scenes and voiceovers...")
|
| 93 |
+
final_clips = []
|
| 94 |
+
for i, video_path in enumerate(video_paths):
|
| 95 |
+
clip = VideoFileClip(video_path)
|
| 96 |
+
audio = AudioFileClip(audio_paths[i])
|
| 97 |
+
clip = clip.set_audio(audio).set_duration(audio.duration)
|
| 98 |
+
final_clips.append(clip)
|
| 99 |
+
|
| 100 |
+
final_video = concatenate_videoclips(final_clips, method="compose")
|
| 101 |
+
output_path = os.path.join(output_dir, "ai_cartoon_animated.mp4")
|
| 102 |
+
final_video.write_videofile(output_path, fps=24)
|
| 103 |
+
print(f"\n✅ Finished cartoon: {output_path}")
|