Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. import argparse
  2. import os
  3. #os.environ["PL_GLOBAL_SEED"]="1"
  4. import string
  5. import json
  6. #import cv2
  7. from datetime import datetime
  8. from tqdm import tqdm
  9. from ldm.generate import Generate
  10. from random import choice
  11. import PIL
  12. from PIL.Image import Resampling
  13. import math
  14. import pdb
  15. import random
  16. from scipy.special import expit
  17.  
  18. import itertools
  19. #os.environ["PL_GLOBAL_SEED"]="1"
  20.  
  21. #def permutation(pop_size, chrom_length):
  22. # return itertools.permutation(pop_size, chrom_length)
  23.  
  24. _t2i = Generate()
  25.  
  26. def get_folder_name(prompt = ""):
  27. now = datetime.now()
  28.  
  29. prompt_parts = prompt.split()
  30. first_word = prompt_parts[0] if len(prompt_parts) > 0 else ""
  31.  
  32. h, m, s = (str(t).ljust(2, "0") for t in [now.hour, now.minute, now.second])
  33.  
  34. return f"{first_word}-{now.year}-{now.month}-{now.day}-{h}{m}{s}"
  35.  
  36. def get_vid_path(prompt = ""):
  37. return os.path.join(".", "outputs", "vid-samples", get_folder_name(prompt))
  38.  
  39. def prompt2vid(**config):
  40. prompt = config["prompt"]
  41. n_frames = config["n_frames"]
  42. #initial_image = config["init_img"]
  43. fps = config["fps"] if "fps" in config else 30.0
  44. cfg_scale = config["cfg_scale"] if "cfg_scale" in config else 7.5
  45. # strength = config["strength"] if "strength" in config else 0.8
  46. zoom_speed = config["zoom_speed"] if "zoom_speed" in config else 2.0
  47. vid_path = get_vid_path(prompt)
  48. frames_path = os.path.join(vid_path, "frames")
  49. os.makedirs(frames_path, exist_ok=True)
  50.  
  51. pixels_sizes = (
  52. # 64,
  53. # 128,
  54.  
  55. # dont go under here becuase it just produces junk
  56. # 256,
  57. 512,
  58. )
  59.  
  60.  
  61. #stepsize = 85
  62. #stepsize = 5
  63. #stepsize = 35
  64. seed = 111
  65.  
  66.  
  67. fixed_seed = seed
  68. # many_iterations64
  69. many_iterations=1
  70. #many_iterations=2
  71. for stepsize in (25,):
  72. for pixel in pixels_sizes:
  73. #seed = seed + 1
  74. # video_writer = cv2.VideoWriter(os.path.join(vid_path, f"videox{pixel}.mp4"), 1, fps, (pixel, pixel))
  75. #logfile = open(os.path.join(vid_path, f"videox{pixel}.log"),"w")
  76. stepno = 0
  77. order = list(range(0,1132))
  78. #order = (1,)
  79. #random.shuffle(order)
  80. #print(order)
  81. for parameter in order:
  82. #state = random.getstate()
  83. #random.seed(parameter)
  84. #rslice = random.sample(order,n_frames) # we want the slice to have a different sequence
  85. #random.setstate(state)
  86.  
  87. for i in tqdm(range(n_frames), desc=f"Creating Video {stepsize} "):
  88. #print(stepsize)
  89. if stepno > 100:
  90. stepno = 1
  91.  
  92. vars = [ ]
  93.  
  94.  
  95. # wei = abs(math.sin(weight * stepno ))
  96. # val = x *math.exp(stepno)
  97. # if val > 4294967295:
  98. # val = val % 4294967295
  99. # vars.append(( val , wei))
  100.  
  101. stepno = stepno + 1
  102. #stepsize =stepsize + 1 # how much do we increase
  103. mutations = []
  104.  
  105.  
  106. #@if i > 0:
  107. mutations.append(parameter)
  108.  
  109.  
  110. co = 0.2
  111. step = (co * 2 )/n_frames
  112. mutation = (-1 * co) + (i * step)
  113.  
  114. data = dict(
  115. # strength=strength,
  116. seed=fixed_seed,
  117. variation_amount=0,
  118. width=pixel,
  119. height=pixel,
  120. cfg_scale=7,
  121. #latent_channels=8,
  122. #downsampling_factor=10,
  123. #strength=0.85,
  124. steps=stepsize,
  125. iterations=many_iterations,
  126. #with_variations=vars,
  127. mutations=mutations,
  128. mutation=mutation,
  129. )
  130. images = _t2i.prompt2image(prompt,**data)
  131.  
  132. for (j,image) in enumerate(images):
  133. next_frame_filename = os.path.join(frames_path, f"p{parameter}s{i}c{j}s{stepsize}x{pixel}.png")
  134. #print(j,image)
  135. image[0].save(next_frame_filename)
  136. #for x in range(4):# write the same frame again
  137. #logfile.write(json.dumps() + "\n")
  138. # video_writer.write(cv2.imread(next_frame_filename))
  139. logfile2 = open(os.path.join(frames_path, f"p{parameter}s{i}c{j}s{stepsize}x{pixel}.json"),"w")
  140.  
  141. logfile2.write(json.dumps(
  142. dict(
  143. seed=image[1],
  144. #mutatations=image[2],
  145. #data=image,
  146. fn=next_frame_filename,
  147. prompt=prompt,
  148. data=data) )+ "\n")
  149. logfile2.close()
  150.  
  151. #logfile.close()
  152.  
  153. #cv2.destroyAllWindows()
  154. #video_writer.release()
  155.  
  156. #pdb.set_trace()
  157. prompt2vid( prompt = "cube. block. wooden. spinning. rotating. bright red. low poly. 3d. letter a. alphabet. simple. focal length. ",
  158. n_frames=4 )
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement