Advertisement
Guest User

teh codz

a guest
Sep 11th, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. import argparse
  2. import os
  3. import string
  4. import json
  5. import cv2
  6. from datetime import datetime
  7. from tqdm import tqdm
  8. from ldm.simplet2i import T2I
  9. from random import choice
  10. import PIL
  11. from PIL.Image import Resampling
  12. import math
  13. _t2i = T2I()
  14.  
  15. def get_folder_name(prompt = ""):
  16. now = datetime.now()
  17.  
  18. prompt_parts = prompt.split()
  19. first_word = prompt_parts[0] if len(prompt_parts) > 0 else ""
  20.  
  21. h, m, s = (str(t).ljust(2, "0") for t in [now.hour, now.minute, now.second])
  22.  
  23. return f"{first_word}-{now.year}-{now.month}-{now.day}-{h}{m}{s}"
  24.  
  25. def get_vid_path(prompt = ""):
  26. return os.path.join(".", "outputs", "vid-samples", get_folder_name(prompt))
  27.  
  28. def prompt2vid(**config):
  29. prompt = config["prompt"]
  30. n_frames = config["n_frames"]
  31. #initial_image = config["init_img"]
  32. fps = config["fps"] if "fps" in config else 30.0
  33. cfg_scale = config["cfg_scale"] if "cfg_scale" in config else 7.5
  34. strength = config["strength"] if "strength" in config else 0.8
  35. zoom_speed = config["zoom_speed"] if "zoom_speed" in config else 2.0
  36. vid_path = get_vid_path(prompt)
  37. frames_path = os.path.join(vid_path, "frames")
  38. os.makedirs(frames_path, exist_ok=True)
  39.  
  40. pixels_sizes = (
  41. #64,128,
  42. #256,
  43. 512,
  44. )
  45.  
  46.  
  47. stepsize = 55
  48. #stepsize = 35
  49. seed = 666
  50. fixed_seed = 666
  51. many_iterations=1
  52.  
  53. for pixel in pixels_sizes:
  54. seed = seed + 1
  55. video_writer = cv2.VideoWriter(os.path.join(vid_path, f"videox{pixel}.mp4"), 1, fps, (pixel, pixel))
  56. #logfile = open(os.path.join(vid_path, f"videox{pixel}.log"),"w")
  57. for i in tqdm(range(n_frames), desc="Creating Video"):
  58. #print(stepsize)
  59. #stepsize =stepsize + 1 # how much do we increase
  60. weight =0.5
  61. weight2 =0.3
  62. images = _t2i.prompt2image(prompt,
  63. strength=strength,
  64. seed=fixed_seed,
  65. variation_amount=weight2,
  66. width=pixel,
  67. height=pixel,
  68. steps=stepsize,
  69. iterations=many_iterations,
  70. with_variations=[ ( math.pi * x * seed ,weight) for x in [
  71. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89,
  72. 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191,
  73. 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293,
  74. 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421,
  75. 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541
  76.  
  77. ]]
  78. #cfg_scale=cfg_scale
  79. )
  80.  
  81. for (j,image) in enumerate(images):
  82. next_frame_filename = os.path.join(frames_path, f"s{i}c{j}x{pixel}.png")
  83. print(j,image)
  84. image[0].save(next_frame_filename)
  85. #for x in range(4):# write the same frame again
  86. #logfile.write(json.dumps() + "\n")
  87. video_writer.write(cv2.imread(next_frame_filename))
  88. logfile2 = open(os.path.join(frames_path, f"s{i}c{j}x{pixel}.json"),"w")
  89. logfile2.write(json.dumps(
  90. dict(
  91. fn=next_frame_filename,
  92. prompt=prompt,
  93. strength=strength,
  94. seed=seed,
  95. variation_amount=weight,
  96. width=pixel,
  97. height=pixel,
  98. steps=stepsize,
  99. iterations=many_iterations,
  100. with_variations=[ (x * seed ,weight) for x in [
  101. 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 ]]
  102. ) )+ "\n")
  103. logfile2.close()
  104.  
  105. #logfile.close()
  106.  
  107. #cv2.destroyAllWindows()
  108. video_writer.release()
  109.  
  110. prompt2vid(
  111.  
  112. prompt="the royal english prince charles as shapeshifting reptilian lizard demon in hyper realistic and detailed stone statue",
  113. n_frames=60
  114. # cfg_scale=7.5,
  115. # zoom_speed=2,
  116.  
  117. # fps=30,
  118. )
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement