text: #!/usr/bin/env python
  # coding: utf-8
  
  import re
  import random
  import os
  import ffmpeg
  import base64
  import asyncio
  import numpy as np
  import json
  import model
  import PIL.Image
  
  
  class Generator:
      def __init__(self, truncation_psi, tmp_dir, size, framerate):
          self.__model = model.Model("gdrive:networks/stylegan2-ffhq-config-f.pkl", truncation_psi)
          self.__tmp_dir = tmp_dir
          self.__size = size
          self.__framerate = framerate
  
      def __get_next_dir(self, label):
          i = -1
          for name in os.listdir(self.__tmp_dir):
              match = re.match(r"tmp_([0-9]+)", name)
              if match:
                  i = max(i, int(match.group(1)))
          dir = self.__tmp_dir + "/tmp_%04d_%s" % (i + 1, label)
          os.mkdir(dir)
          return dir
  
      def __get_subdir(self, parent, name):
          dir = parent + "/" + name
          os.mkdir(dir)
          return dir
  
      def __make_images(self, dir, weights, filenames, format):
          images = self.__model.weights_to_images(np.array(weights))
          for i in range(images.shape[0]):
              PIL.Image.fromarray(images[i]).save(dir + "/" + filenames[i] + "." + format)
  
      def __make_video(self, dir, label=None):
          if label:
              name = "%s/video_%s.mp4" % (dir, label)
          else:
              name = "%s/video.mp4" % dir
          ffmpeg.input("%s/*.jpg" % dir, pattern_type="glob", framerate=self.__framerate).output(name).run(overwrite_output=True)
  
      def __video_beetwen_weights(self, weights, iteration, dir, copy_layers=[], label=None):
          filenames = []
          for i in range(len(weights)):
              filenames.append("pattern%02d" % i)
          self.__make_images(dir, weights, filenames, "png")
  
          images = self.__model.images_beetwen_weights(weights, iteration, copy_layers)
          for w in range(len(images)):
              for i in range(len(images[w])):
                  PIL.Image.fromarray(images[w][i]).save("%s/%02d_%04d.jpg" % (dir, w, i))
          self.__make_video(dir, label)
  
      def present_seeds(self, seeds, n=250):
          dir = self.__get_next_dir("seeds")
          for _seeds in [seeds[i : i + n] for i in range(0, len(seeds), n)]:
              images_weights = []
              images_filenames = []
              for seed in _seeds:
                  images_weights.append(np.random.RandomState(seed).randn(self.__size))
                  images_filenames.append("s%04d" % seed)
              self.__make_images(dir, images_weights, images_filenames, "jpg")
  
      def video_per_feature(self, seed, iteration):
          weights_org = np.random.RandomState(seed).randn(self.__size)
          dir = self.__get_next_dir("features")
          self.__make_images(dir, [weights_org], ["pattern"], "png")
          for w in range(self.__size):
              weights1 = weights_org.copy()
              weights2 = weights_org.copy()
              weights1[w] = -2.0
              weights2[w] = 2.0
              subdir = self.__get_subdir(dir, "f%03d" % w)
              self.__video_beetwen_weights([weights1, weights2], iteration, subdir)
  
      def video_reverse_image(self, seed, iteration):
          weights1 = np.random.RandomState(seed).randn(self.__size)
          weights2 = -(weights1.copy())
          dir = self.__get_next_dir("reverse_image")
          self.__video_beetwen_weights([weights1, weights2], iteration, dir)
  
      def video_beetwen_images(self, seeds, iteration, copy_layers=[], label=None):
          weights = []
          for seed in seeds:
              weights.append(np.random.RandomState(seed).randn(self.__size))
          if label:
              dir = self.__get_next_dir(label)
          else:
              dir = self.__get_next_dir("beetwen_images")
          self.__video_beetwen_weights(weights, iteration, dir, copy_layers, label)
  
  
  if __name__ == "__main__":
      generator = Generator(1.0, "..", 512, 30)
  
      features_keep_age = [0, 1, 2, 3, 4, 5, 6]
      features_keep_complextion = [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
  
      seeds_age = [16, 9, 5736, 2681, 1789, 153, 2386, 431]
      seeds_complexion = [458, 1296, 230, 100, 3652, 5097, 3370, 75]
      seed = 662
  
      generator.present_seeds(seeds_age + seeds_complexion + [seed])
      generator.video_per_feature(seeds_age[0], 50)
      generator.video_reverse_image(seeds_age[0], 50)
  
      generator.video_beetwen_images(seeds_age, 180)
      generator.video_beetwen_images(seeds_complexion, 180)
  
      features = [features_keep_age] * (len(seeds_age) + 1) + [features_keep_complextion] * len(seeds_age)
      seeds = [0] + seeds_complexion + [0] + seeds_age
      seeds[len(seeds) // 2 + 1] = seed
      for i in range(len(seeds_age)):
          seeds[0] = seeds_age[i]
          seeds[len(seeds) // 2] = seeds_complexion[i]
          generator.video_beetwen_images(seeds, 180, features, "animation")
  
      for feature in range(18):
          features = set(range(18))
          features.remove(feature)
          generator.video_beetwen_images(seeds_age, 180, list(features), "f%02d" % feature)
  