|
import numpy as np |
|
import cv2 |
|
import torch |
|
import moviepy.editor as mp |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("CompVis/posegpt") |
|
model = AutoModelForCausalLM.from_pretrained("CompVis/posegpt") |
|
|
|
|
|
character_memory = {} |
|
|
|
def generate_stick_animation(story, character_name): |
|
global character_memory |
|
|
|
|
|
if character_name not in character_memory: |
|
character_memory[character_name] = {"pose": (250, 200), "size": 20} |
|
|
|
char_pose = character_memory[character_name]["pose"] |
|
|
|
|
|
inputs = tokenizer(story, return_tensors="pt") |
|
output = model.generate(**inputs, max_length=50) |
|
|
|
|
|
frames = [] |
|
for i in range(20): |
|
img = np.ones((500, 500, 3), dtype=np.uint8) * 255 |
|
|
|
|
|
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) |
|
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) |
|
|
|
|
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
|
|
|
frames.append(gray) |
|
|
|
return frames |
|
|
|
|
|
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 |
|
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 |