summaryrefslogtreecommitdiff
path: root/canny-cv.py
blob: df4526ad03b07c6dfb370292e886526bb8b9e86b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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, crop_image
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 numpy as np
import cv2
import time

import subprocess
from time import sleep

blur = 3
sigma = 0
canny_lo = 10
canny_hi = 220
frac_a = 0.99
frac_b = 1 - frac_a

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()

    #copyfile(opt.start_img, render_dir + "frame_00000.png")

    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:
                tmp_im = im.copy()
                #array_a = np.multiply(im.astype('float64'), frac_a)
                #array_b = np.multiply(last_im.astype('float64'), frac_b)
                #im = np.add(array_a, array_b).astype('uint8')
                # print(im.shape, im.dtype)
                last_im = np.roll(tmp_im, 1, axis=1)
            else:
                last_im = im.copy().astype('uint8')
                tmp_im = im.copy().astype('uint8')
                #print(im.shape, im.dtype)

            image_pil = Image.fromarray(tmp_im, mode='RGB')
            image_pil.save(tmp_path)
            os.rename(tmp_path, render_path)

            image_pil = Image.fromarray(im, mode='RGB')
            image_pil = crop_image(image_pil, (0.50, 0.50), 0.5)
            im = np.asarray(image_pil).astype('uint8')
            #print(im.shape, im.dtype)
            opencv_image = im[:, :, ::-1].copy()
            opencv_image = cv2.GaussianBlur(opencv_image, (blur,blur), sigma)
            opencv_image = cv2.Canny(opencv_image, canny_lo, canny_hi)
            cv2.imwrite(tmp_path, opencv_image)
            os.rename(tmp_path, edges_path)

    webpage.save()

    os.remove(render_dir + "frame_00000.png")

    t = time.time()
    t /= 60
    t %= 525600
    video_fn = "{}_{}_canmix_{}frame_{}mix_{}blur_{}sigma_{}lo_{}hi_{}.mp4".format(
        opt.name, opt.experiment,
        opt.how_many, frac_a,
        blur, sigma, canny_lo, canny_hi,
        int(t))

    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)