-
Notifications
You must be signed in to change notification settings - Fork 77
/
webcam.py
61 lines (49 loc) · 1.68 KB
/
webcam.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
import cv2
import transformer
import torch
import utils
STYLE_TRANSFORM_PATH = "transforms/mosaic.pth"
PRESERVE_COLOR = False
WIDTH = 1280
HEIGHT = 720
def webcam(style_transform_path, width=1280, height=720):
"""
Captures and saves an image, perform style transfer, and again saves the styled image.
Reads the styled image and show in window.
"""
# Device
device = ("cuda" if torch.cuda.is_available() else "cpu")
# Load Transformer Network
print("Loading Transformer Network")
net = transformer.TransformerNetwork()
net.load_state_dict(torch.load(style_transform_path))
net = net.to(device)
print("Done Loading Transformer Network")
# Set webcam settings
cam = cv2.VideoCapture(0)
cam.set(3, width)
cam.set(4, height)
# Main loop
with torch.no_grad():
while True:
# Get webcam input
ret_val, img = cam.read()
# Mirror
img = cv2.flip(img, 1)
# Free-up unneeded cuda memory
torch.cuda.empty_cache()
# Generate image
content_tensor = utils.itot(img).to(device)
generated_tensor = net(content_tensor)
generated_image = utils.ttoi(generated_tensor.detach())
if (PRESERVE_COLOR):
generated_image = utils.transfer_color(img, generated_image)
generated_image = generated_image / 255
# Show webcam
cv2.imshow('Demo webcam', generated_image)
if cv2.waitKey(1) == 27:
break # esc to quit
# Free-up memories
cam.release()
cv2.destroyAllWindows()
webcam(STYLE_TRANSFORM_PATH, WIDTH, HEIGHT)