File size: 2,746 Bytes
dd1d21e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import numpy as np
import cv2
import torch
import moviepy.editor as mp
from transformers import AutoModelForCausalLM, AutoTokenizer
# Load AI Pose Model
tokenizer = AutoTokenizer.from_pretrained("CompVis/posegpt")
model = AutoModelForCausalLM.from_pretrained("CompVis/posegpt")
# Memory Storage for Characters
character_memory = {}
def generate_stick_animation(story, character_name):
global character_memory
# Assign unique ID to character
if character_name not in character_memory:
character_memory[character_name] = {"pose": (250, 200), "size": 20}
char_pose = character_memory[character_name]["pose"]
# Convert story to keyframes using AI
inputs = tokenizer(story, return_tensors="pt")
output = model.generate(**inputs, max_length=50)
# Generate Stick Figure Animation
frames = []
for i in range(20): # More frames for smoother motion
img = np.ones((500, 500, 3), dtype=np.uint8) * 255 # White background
# Stick Figure Body
cv2.line(img, (char_pose[0], char_pose[1] + i * 3), (char_pose[0], char_pose[1] + 100 + i * 3), (0, 0, 0), 5)
cv2.circle(img, (char_pose[0], char_pose[1] - 20 + i * 3), 20, (0, 0, 0), -1) # Head
cv2.line(img, (char_pose[0] - 50, char_pose[1] + 50 + i * 3), (char_pose[0] + 50, char_pose[1] + 50 + i * 3), (0, 0, 0), 5) # Arms
# Black & White Filter
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
frames.append(gray)
return frames
# Extra Features
def add_camera_effects(frames, effect):
if effect == "Shake":
return [cv2.warpAffine(frame, np.float32([[1, 0, np.random.randint(-5, 5)], [0, 1, np.random.randint(-5, 5)]]), (500, 500)) for frame in frames]
elif effect == "Zoom":
return [cv2.resize(frame, (600, 600))[50:550, 50:550] for frame in frames]
elif effect == "Slow Motion":
return frames * 2 # Duplicate frames for slow-motion effect
return frames
def add_background(frames, background):
if background == "Dark Forest":
return [cv2.addWeighted(frame, 0.8, cv2.imread("dark_forest.jpg", 0), 0.2, 0) for frame in frames]
elif background == "Haunted House":
return [cv2.addWeighted(frame, 0.8, cv2.imread("haunted_house.jpg", 0), 0.2, 0) for frame in frames]
return frames
def add_sound(video_path, sound_type):
if sound_type == "Horror":
sound = mp.AudioFileClip("horror_music.mp3")
elif sound_type == "Action":
sound = mp.AudioFileClip("action_music.mp3")
else:
return video_path
video = mp.VideoFileClip(video_path)
final = video.set_audio(sound)
final_path = video_path.replace(".mp4", "_sound.mp4")
final.write_videofile(final_path)
return final_path |