-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo_nips.py
155 lines (117 loc) · 3.78 KB
/
demo_nips.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
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 31 08:24:30 2016
@author: rflamary
"""
import numpy as np
import cv2
import pylab as pl
import sys
import PIL.Image
cap = cv2.VideoCapture(0)
from poissonblending import blend
fname_mask='./data/joconde_0_mask.jpg'
fname_img='./data/joconde_0.jpg'
mask_list=['./data/joconde_0_mask.jpg',
'./data/monet_portrait_mask.png',
# './data/estree_mask.png',
'./data/firefly_mask.png',
'./data/vapnik_mask.png',
'./data/dorothy_mask.png']
img_list=['./data/joconde_0.jpg',
'./data/monet_portrait.png',
# './data/estree.png',
'./data/firefly.png',
'./data/vapnik.png',
'./data/dorothy.png']
adapt_list=['kernel',
'kernel',
'kernel',
'kernel',
'kernel',
'kernel']
nimg=len(img_list)
idimg=0
def load_images(fname_mask,fname_img):
img_mask = np.asarray(PIL.Image.open(fname_mask)).copy()
if len(img_mask.shape)<3:
img_mask = np.tile(img_mask[:,:,None],(1,1,3)) # remove alpha
img_mask = img_mask[:,:,:3]
img_mask.setflags(write=1)
#img_source = np.asarray(PIL.Image.open('./testimages/me_flipped.png'))
#img_source = img_source[:,:,:3]
#img_source.flags.writeable = True
img_target = np.asarray(PIL.Image.open(fname_img)).copy()
img_target = img_target[:,:,:3]
img_target.setflags(write=1)
#img_mask2=cv2.imread('testimages/mask_video.png')
#img_mask=img_mask2>0
mask2= np.maximum(cv2.Laplacian(img_mask,cv2.CV_64F),0)>20
return img_mask,img_target,mask2
img_mask,img_target,mask2=load_images(fname_mask,fname_img)
# nico : change the resolution of the video to match mask resolution
#cap.set(3,img_mask.shape[1])
#cap.set(4,img_mask.shape[0])
etape=0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
#print(frame)
frame = cv2.resize(frame, (img_mask.shape[1],img_mask.shape[0]))
frame=frame[:,::-1,:]
# Our operations on the frame come here
if etape==0:
frame_mask=cv2.cvtColor(img_target, cv2.COLOR_BGR2RGB)
elif etape==1:
frame_mask=(img_mask>0)*frame+(img_mask==0)*cv2.cvtColor(img_target, cv2.COLOR_BGR2RGB)
k=cv2.waitKey(1) & 0xFF
if k in [ ord(' ')]:
if etape==1:
doit=True
break
else:
etape=1
if k in [ ord('q')]:
doit=False
break
if k in [ord('v')]:
etape=1
if k in [ord('i'),ord('m')]:
idimg=(idimg+1) % nimg
fname_mask=mask_list[idimg]
fname_img=img_list[idimg]
img_mask,img_target,mask2=load_images(fname_mask,fname_img)
# Display the resultinfname_maskg frame
cv2.imshow('frame',frame_mask)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
if doit:
nbsample=500
off = (0,0)
img_ret4 = blend(img_target, I, img_mask, reg=1, nbsubsample=nbsample,offset=off,adapt=adapt_list[idimg],verbose=True)
#%%
fs=30
f, axarr = pl.subplots(1, 3,figsize=(30,10))
newax = f.add_axes([0.15, 0, 0.32, 0.32], anchor='NW', zorder=1)
newax.imshow(img_mask)
newax.axis('off')
newax.set_title('mask')
axarr[0].imshow(I)
axarr[0].set_title('Source', fontsize=fs)
axarr[0].axis('off')
axarr[1].imshow(img_target)
axarr[1].set_title('target', fontsize=fs)
axarr[1].axis('off')
axarr[2].imshow(img_ret4)
axarr[2].set_title('Adapted', fontsize=fs)
axarr[2].axis('off')
pl.subplots_adjust(wspace=0.1)
pl.tight_layout()
if len(sys.argv)>1:
savename='./output/{}_{}.png'.format(sys.argv[1],idimg)
else:
savename='./output/{}_{}.png'.format('output',idimg)
pl.savefig(savename)
pl.show()