diff options
| author | jules <jules@asdf.us> | 2018-05-01 20:20:27 +0200 |
|---|---|---|
| committer | jules <jules@asdf.us> | 2018-05-01 20:20:27 +0200 |
| commit | 20662ee305cd21838078eab97568bbbbb9acffd0 (patch) | |
| tree | 16b58c750be5e054c7d9e266c8e2481f359b400c /canny.py | |
| parent | 5979723a35c577ffe70c1107415199444fb5c7fe (diff) | |
new scripts using canny
Diffstat (limited to 'canny.py')
| -rw-r--r-- | canny.py | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/canny.py b/canny.py new file mode 100644 index 0000000..b8dc6e8 --- /dev/null +++ b/canny.py @@ -0,0 +1,111 @@ +import os +from options.test_options import TestOptions +from data import CreateRecursiveDataLoader +from models import create_model +from util.visualizer import Visualizer +from util.util import mkdirs +from util import html +from shutil import move, copyfile +from PIL import Image, ImageOps +from skimage.transform import resize +from scipy.misc import imresize +from shutil import copyfile, rmtree +import time as time +import numpy as np +import cv2 + +import subprocess +from time import sleep + +if __name__ == '__main__': + opt = TestOptions().parse() + opt.nThreads = 1 # test code only supports nThreads = 1 + opt.batchSize = 1 # test code only supports batchSize = 1 + opt.serial_batches = True # no shuffle + opt.no_flip = True # no flip + opt.experiment = opt.start_img.split("/")[-1].split(".")[0] + + render_dir = opt.results_dir + opt.name + "/exp:" + opt.experiment + "/" + + if os.path.exists(render_dir): + rmtree(render_dir) + mkdirs(render_dir) + + cmd = ("convert", opt.start_img, '-canny', '0x1+10%+30%', render_dir + "frame_00000.png") + process = subprocess.Popen(cmd, stdout=subprocess.PIPE) + output, error = process.communicate() + + data_loader = CreateRecursiveDataLoader(opt) + dataset = data_loader.load_data() + ds = dataset.dataset + model = create_model(opt) + visualizer = Visualizer(opt) + # create website + web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.which_epoch)) + webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.which_epoch)) + # test + last_im = None + for i, data in enumerate(data_loader): + if i >= opt.how_many: + break + model.set_input(data) + model.test() + visuals = model.get_current_visuals() + img_path = model.get_image_paths() + print('%04d: process image... %s' % (i, img_path)) + ims = visualizer.save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio) + if dataset.name() == 'RecursiveDatasetDataLoader': + # print(visuals.keys()) + im = visuals['fake_B'] + tmp_path = render_dir + "frame_{:05d}_tmp.png".format(i+1) + edges_path = render_dir + "frame_{:05d}.png".format(i+1) + render_path = render_dir + "ren_{:05d}.png".format(i+1) + # s = 256 + # p = 8 + # im = imresize(im, (s-p, s-p), interp='bicubic') + # image_pil = Image.fromarray(im) + # image_pil = ImageOps.expand(image_pil, p) + # image_pil.save(save_path) + # copyfile(save_path, final_path) + #if last_im is not None: + # frac_a = 999/1000 + # frac_b = 1/1000 + # tmp_im = im.copy() + # array_a = np.multiply(im, frac_a) + # array_b = np.multiply(last_im, frac_b) + # # im = np.add(array_a, array_b).astype('int8') + # # print(im.shape, im.dtype) + # last_im = np.roll(tmp_im, 1, axis=1) + #else: + # last_im = im.copy() + # print(im.shape, im.dtype) + image_pil = Image.fromarray(im, mode='RGB') + image_pil.save(tmp_path) + os.rename(tmp_path, render_path) + + cmd = ("convert", render_path, '-canny', '0x1+10%+30%', tmp_path) + process = subprocess.Popen(cmd, stdout=subprocess.PIPE) + output, error = process.communicate() + + os.rename(tmp_path, edges_path) + + webpage.save() + + os.remove(render_dir + "frame_00000.png") + + t = time.time() + t /= 60 + t %= 525600 + video_fn = opt.name + "_" + opt.experiment + "_canny_" + str(opt.how_many) + "_" + str(int(t)) + ".mp4" + + cmd = ("/usr/bin/ffmpeg", "-i", render_dir + "ren_%05d.png", "-y", "-c:v", "libx264", "-vf", "fps=30", "-pix_fmt", "yuv420p", render_dir + video_fn) + process = subprocess.Popen(cmd, stdout=subprocess.PIPE) + output, error = process.communicate() + + print("________") + + cmd = ("scp", render_dir + video_fn, "jules@asdf.us:asdf/neural/") + process = subprocess.Popen(cmd, stdout=subprocess.PIPE) + output, error = process.communicate() + + print("https://asdf.us/neural/" + video_fn) |
