forked from wmylxmj/Pix2Pix-Keras
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
52 lines (45 loc) · 1.9 KB
/
Copy pathdemo.py
File metadata and controls
52 lines (45 loc) · 1.9 KB
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
# -*- coding: utf-8 -*-
"""
Created on Sun Feb 24 22:32:37 2019
@author: wmy
"""
import scipy
from keras.datasets import mnist
from keras.layers import Input, Dense, Reshape, Flatten, Dropout, Concatenate
from keras.layers import BatchNormalization, Activation, ZeroPadding2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import Sequential, Model
from keras.optimizers import Adam
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
from model import Pix2Pix
from PIL import Image
def imread(path):
return scipy.misc.imread(path, mode='RGB').astype(np.float)
def predict_single_image(pix2pix, image_path, save_path):
pix2pix.generator.load_weights('./weights/generator_weights.h5')
image_B = imread(image_path)
image_B = scipy.misc.imresize(image_B, (pix2pix.nW, pix2pix.nH))
images_B = []
images_B.append(image_B)
images_B = np.array(images_B)/127.5 - 1.
generates_A = pix2pix.generator.predict(images_B)
generate_A = generates_A[0]
generate_A = np.uint8((np.array(generate_A) * 0.5 + 0.5) * 255)
generate_A = Image.fromarray(generate_A)
generated_image = Image.new('RGB', (pix2pix.nW, pix2pix.nH))
generated_image.paste(generate_A, (0, 0, pix2pix.nW, pix2pix.nH))
generated_image.save(save_path, quality=95)
pass
def convert_to_gray_single_image(image_path, save_path, resize_height=256, resize_weidth=256):
img = Image.open(image_path)
img_color = img.resize((resize_weidth, resize_height), Image.ANTIALIAS)
img_gray = img_color.convert('L')
img_gray = img_gray.convert('RGB')
img_gray.save(save_path, quality=95)
gan = Pix2Pix()
#gan.train(epochs=1200, batch_size=4, sample_interval=10, load_pretrained=True)
predict_single_image(gan, './images/test_1.jpg', './images/generate_test_1.jpg')