-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_gen.py
executable file
·209 lines (151 loc) · 7.26 KB
/
camera_gen.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import cv2
import sys
import logging as log
import datetime as dt
from time import sleep
import sys
import matplotlib.pyplot as plt
import numpy as np
import os
import torch
from PIL import Image
from options.test_options import TestOptions
from data import create_dataset
from models import create_model
from data.base_dataset import BaseDataset, get_transform
from models import networks
import cv2
import numpy as np
from PIL import ImageFilter, ImageEnhance
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
log.basicConfig(filename='webcam.log',level=log.INFO)
video_capture = cv2.VideoCapture(0)
anterior = 0
#############
opt = TestOptions().parse() # get test options
# hard-code some parameters for test
opt.num_threads = 0 # test code only supports num_threads = 1
opt.batch_size = 1 # test code only supports batch_size = 1
opt.serial_batches = True # disable data shuffling; comment this line if results on randomly chosen images are needed.
opt.no_flip = True # no flip; comment this line if results on flipped images are needed.
opt.display_id = -1 # no visdom display; the test code saves the results to a HTML file.
#dataset = create_dataset(opt) # create a dataset given opt.dataset_mode and other options
#model = create_model(opt) # create a model given opt.model and other options
#print(model.model_names)
#model.setup(opt) # regular setup: load and print networks; create schedulers
# create a website
#web_dir = os.path.join(opt.results_dir, opt.name, '%s_%s' % (opt.phase, opt.epoch)) # define the website directory
#webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.epoch))
# test with eval mode. This only affects layers like batchnorm and dropout.
# For [pix2pix]: we use batchnorm and dropout in the original pix2pix. You can experiment it with and without eval() mode.
# For [CycleGAN]: It should not affect CycleGAN as CycleGAN uses instancenorm without dropout.
transform = get_transform(opt, grayscale=False)
netG_B = networks.define_G(opt.output_nc, opt.input_nc, opt.ngf, opt.netG, opt.norm,
not opt.no_dropout, opt.init_type, opt.init_gain, opt.gpu_ids)
#netG_B = netG_B.module
style="Simpsons"
netG_B.load_state_dict(torch.load('checkpoints/1_simpsons2photo/latest_net_G_B.pth'))
netG_B = netG_B.to('cpu')
##########
plt.figure()
font = cv2.FONT_HERSHEY_SIMPLEX
print("\n\n\n\n\n\n############################################\n")
print("Usage: \n")
print("Press 'a' to switch to anime filter")
print("Press 's' to switch to The Simpsons filter")
print("Press 'h' to switch to hand drawn filter")
print("Press 'c' to switch to cartoon filter")
sys.stdout.flush()
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
extra = 10
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
#print("x = %d, y = %d, w = %d, h = %d" % (x,y,w,h))
#print(frame.shape)
#print("[%d : %d][%d : %d]" % (y-extra,y+h+extra,x-extra,x+w+extra))
x_start = x-extra if x-extra >= 0 else 0
y_start = y-extra if y-extra >= 0 else 0
x_end = x+w+extra if x+w+extra <= frame.shape[1] else frame.shape[1]
y_end = y+h+extra if y+h+extra <= frame.shape[0] else frame.shape[0]
just_face = np.array(frame[y_start:y_end,x_start:x_end,:])
face_pil = Image.fromarray(just_face)
face_pil = face_pil.filter(ImageFilter.SHARPEN)
#bright = ImageEnhance.Brightness(face_pil)
#face_pil = bright.enhance(1)
B = transform(face_pil).unsqueeze(0)
#B_img = Image.open('Chuck_Norris_test.jpg').convert('RGB')
#B = transform(B_img).unsqueeze(0)
with torch.no_grad():
fake_A = netG_B(B)
new_img = fake_A[0].detach().numpy()
new_img = np.moveaxis(new_img, 0, -1)
new_img = (255 * (new_img+1)/2).astype(np.uint8)
new_img = Image.fromarray(new_img).resize((x_end-x_start, y_end-y_start))
#print(just_face.shape)
#sys.stdout.flush()
#plt.imshow(new_img)
#plt.show()
#new_img = cv2.cvtColor(np.array(new_img),cv2.COLOR_BGR2RGB)
frame[y_start:y_end,x_start:x_end,:] = new_img
#cv2.rectangle(frame, (x-extra, y-extra), (x+w+extra, y+h+extra), (0, 255, 0), 2)
if anterior != len(faces):
anterior = len(faces)
log.info("faces: "+str(len(faces))+" at "+str(dt.datetime.now()))
# Display the resulting frame
cv2.putText(frame,style,(10,470), font, 2,(255,255,255),2,cv2.LINE_AA)
cv2.imshow('Video', frame)
for i in range(4):
out.write(frame)
keypress = cv2.waitKey(5) & 0xFF
if keypress == ord('a'):
netG_B = networks.define_G(opt.output_nc, opt.input_nc, opt.ngf, opt.netG, opt.norm,
not opt.no_dropout, opt.init_type, opt.init_gain, opt.gpu_ids)
#netG_B = netG_B.module
netG_B.load_state_dict(torch.load('checkpoints/1_anime2photo/latest_net_G_B.pth'))
netG_B = netG_B.to('cpu')
style="Anime"
if keypress == ord('s'):
netG_B = networks.define_G(opt.output_nc, opt.input_nc, opt.ngf, opt.netG, opt.norm,
not opt.no_dropout, opt.init_type, opt.init_gain, opt.gpu_ids)
#netG_B = netG_B.module
netG_B.load_state_dict(torch.load('checkpoints/1_simpsons2photo/latest_net_G_B.pth'))
netG_B = netG_B.to('cpu')
style= "Simpsons"
if keypress == ord('h'):
netG_B = networks.define_G(opt.output_nc, opt.input_nc, opt.ngf, opt.netG, opt.norm,
not opt.no_dropout, opt.init_type, opt.init_gain, opt.gpu_ids)
#netG_B = netG_B.module
netG_B.load_state_dict(torch.load('checkpoints/1_caricature2photo/latest_net_G_B.pth'))
netG_B = netG_B.to('cpu')
style= "Hand drawn"
if keypress == ord('c'):
netG_B = networks.define_G(opt.output_nc, opt.input_nc, opt.ngf, opt.netG, opt.norm,
not opt.no_dropout, opt.init_type, opt.init_gain, opt.gpu_ids)
#netG_B = netG_B.module
netG_B.load_state_dict(torch.load('checkpoints/2_cartoon2photo/latest_net_G_B.pth'))
netG_B = netG_B.to('cpu')
style= "Cartoon"
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Display the resulting frame
#cv2.imshow('Video', frame)
# When everything is done, release the capture
video_capture.release()
out.release()
cv2.destroyAllWindows()