Advertisement
Guest User

stable diffusion video script

a guest
Sep 11th, 2022
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. import argparse
  2. import os
  3. import string
  4. import cv2
  5. from datetime import datetime
  6. from tqdm import tqdm
  7. from ldm.simplet2i import T2I
  8. from random import choice
  9. import PIL
  10. from PIL.Image import Resampling
  11.  
  12. _t2i = T2I()
  13.  
  14. def get_folder_name(prompt = ""):
  15. now = datetime.now()
  16.  
  17. prompt_parts = prompt.split()
  18. first_word = prompt_parts[0] if len(prompt_parts) > 0 else ""
  19.  
  20. h, m, s = (str(t).ljust(2, "0") for t in [now.hour, now.minute, now.second])
  21.  
  22. return f"{first_word}-{now.year}-{now.month}-{now.day}-{h}{m}{s}"
  23.  
  24. def get_vid_path(prompt = ""):
  25. return os.path.join(".", "outputs", "vid-samples", get_folder_name(prompt))
  26.  
  27. def prompt2vid(**config):
  28. prompt = config["prompt"]
  29. n_frames = config["n_frames"]
  30. #initial_image = config["init_img"]
  31. fps = config["fps"] if "fps" in config else 30.0
  32. cfg_scale = config["cfg_scale"] if "cfg_scale" in config else 7.5
  33. strength = config["strength"] if "strength" in config else 0.8
  34. zoom_speed = config["zoom_speed"] if "zoom_speed" in config else 2.0
  35. vid_path = get_vid_path(prompt)
  36. frames_path = os.path.join(vid_path, "frames")
  37. os.makedirs(frames_path, exist_ok=True)
  38. stepsize = 1
  39. pixels_sizes = (
  40. #64,128,
  41. 256,512
  42. )
  43.  
  44. for pixel in pixels_sizes:
  45. video_writer = cv2.VideoWriter(os.path.join(vid_path, f"videox{pixel}.mp4"), 1, fps, (pixel, pixel))
  46. for i in tqdm(range(n_frames), desc="Creating Video"):
  47. print(stepsize)
  48. stepsize =stepsize + 1 # how much do we increase
  49. weight = 0.01
  50. images = _t2i.prompt2image(prompt,
  51. strength=strength,
  52. seed=2,
  53. variation_amount=weight,
  54. width=pixel,
  55. height=pixel,
  56. steps=stepsize,
  57. iterations=32,
  58. with_variations=[ (x,weight) for x in [
  59. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
  60. ]]
  61. #cfg_scale=cfg_scale
  62. )
  63. print(images)
  64. for (j,image) in enumerate(images):
  65. next_frame_filename = os.path.join(frames_path, f"s{i}c{j}x{pixel}.png")
  66. print(j,image)
  67. image[0].save(next_frame_filename)
  68. video_writer.write(cv2.imread(next_frame_filename))
  69.  
  70.  
  71. #cv2.destroyAllWindows()
  72. video_writer.release()
  73.  
  74.  
  75.  
  76.  
  77. prompt2vid(
  78. #strength=1,
  79. prompt="(lizards|Aphids|Aphididae|Carpet Beetle|Warehouse Beetle|Dermestidae|Armyworms|Cutworms|Earworms|Noctuidae|Longhorned Wood Boring Beetles|Roundheaded Wood Boring Beetles|Cerambycidae|Weevils|Billbugs|Bark Beetles|Curculionidae). highly detailed digital 3d printer sculpture",
  80. n_frames=3
  81. # cfg_scale=7.5,
  82. # zoom_speed=2,
  83.  
  84. # fps=30,
  85. )
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement