-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstyle.py
82 lines (69 loc) · 2.57 KB
/
style.py
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
import argparse
import cv2
import matplotlib.pyplot as plt
import numpy as np
import os
import pathlib
import tensorflow as tf
from model import create_resnet
import utils
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--content', type=str, help='Path to content image')
parser.add_argument('--gen', type=str, help='Path where to save the generated image')
parser.add_argument('--weights', type=str, help='Path to model\'s weights', required=True)
parser.add_argument('--webcam', action='store_true', help='Generate images from the webcam')
return parser.parse_args()
def check_args(args):
if args.gen is not None:
if args.webcam:
print('WARN: Ignoring webcam argument since you specified a content image')
if not utils.path_exists(args.content):
print('Content image not found in', args.content)
exit(-1)
elif not args.webcam:
print('Please specify a content image with --content, or use --webcam to generate from your webcam')
if not utils.path_exists(args.weights):
print('Weights not found in', args.weights)
exit(-1)
if args.gen is not None:
pathlib.Path(args.gen).parent.mkdir(parents=True, exist_ok=True)
args = parse_args()
check_args(args)
tf.reset_default_graph()
transformation_model = create_resnet(input_shape=(None, None, 3))
transformation_model.load_weights(args.weights)
if args.gen is not None:
content_image = utils.preprocess_image_from_path(args.content)
gen = transformation_model.predict(content_image)
gen = np.squeeze(gen)
gen = gen.astype(np.uint8)
if args.gen is None:
plt.figure()
plt.imshow(gen)
plt.axis('off')
plt.show()
else:
gen = cv2.cvtColor(gen, cv2.COLOR_RGB2BGR)
cv2.imwrite(args.gen, gen)
elif args.webcam:
print('No content image specified, using webcam')
cam_capture = cv2.VideoCapture(0)
if not cam_capture.isOpened():
print('No webcam found')
exit(-1)
print('Press q to quit')
while True:
_, frame = cam_capture.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB).astype(np.float32)
frame = np.expand_dims(frame, axis=0)
gen = transformation_model.predict(frame)
gen = np.squeeze(gen)
gen = gen.astype(np.uint8)
gen = cv2.cvtColor(gen, cv2.COLOR_RGB2BGR)
cv2.imshow('Generated image', gen)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cam_capture.release()
cv2.destroyAllWindows()