-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKinectSilhouetteVideoOverlay.pde
408 lines (353 loc) · 11.6 KB
/
KinectSilhouetteVideoOverlay.pde
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import javax.swing.JFrame;
import java.util.Map;
import java.util.Map.Entry;
import java.util.LinkedList;
import java.util.Arrays;
import processing.video.*;
import processing.opengl.*;
import SimpleOpenNI.*;
import netP5.*;
/*
* Description:
*
* Concept for an interactive art installation project where people in the scene are used as a canvas
* on which video imagery gets overlayed in realtime.
*
* @Authors: Jean-Claude Batista
*
* Based on Greg's Book Making things see. Also based on the Comperas source tree
* https://github.com/ITPNYU/Comperas/tree/master/KinectBackgroundRemoval
*
* Instructions:
* install this on your machine and also the library in processing
* http://code.google.com/p/simple-openni/wiki/Installation
*
*/
/*
* constants
*/
final int KINECT_WIDTH = 640;
final int KINECT_HEIGHT = 480;
final int WIDTH = 640; // WIDTH = 1280;
final int HEIGHT = 480; // HEIGHT = 720;
final int colorMask = 0xffffff; // skip alpha channel
final int LIVE = 1;
final int CACHED = 0;
void setup() {
application = this;
println("Java version = " + System.getProperty("java.version"));
// set black background for full screen mode
((JFrame) frame).getContentPane().setBackground(java.awt.Color.BLACK);
configMgr = new ConfigManager();
scaledWidth = configMgr.getScaleWidth();
scaledHeight = configMgr.getScaleHeight();
// Note: init order is important!
initConfigSettings();
initComponents();
initKinect();
// init silhouette related videos
LinkedList<SilhouetteClipInfo> clipList = configMgr.getClips();
clipMgr.add(clipList);
clock.setDuration(clipMgr.getTotalDuration());
clock.setGranularity(1000); // 1 second
actionMgr.definePeriod(clock.getDuration());
// display all the clips available for playback
configMgr.listClips();
// misc stuff
font = createFont("Arial", 16, true); // Arial, 16 point, anti-aliasing on
println("crossfade setting = " + configMgr.getCrossfade());
}
void initKinect() {
if(!useKinect) {
return;
}
kinect = new SimpleOpenNI(this, SimpleOpenNI.RUN_MODE_MULTI_THREADED);
if(kinect.isInit() == false) {
println("Can't init SimpleOpenNI, maybe the camera is not connected!");
exit();
}
// enable depthMap generation
kinect.enableDepth();
// enable skeleton generation for all joints
kinect.enableUser();
// enable color image from the Kinect
kinect.enableRGB();
//enable the finding of users but dont' worry about skeletons
// turn on depth/color alignment
kinect.alternativeViewPointDepthToImage();
userManager = new UserManager(kinect);
}
void initComponents() {
clock = new CyclicalClock();
oscManager = new OscManager(configMgr.getOscSettings());
silhouetteCache = new SilhouetteFrameCache(configMgr.getSilhouetteCacheSettings());
clipMgr = new SilhouetteClipManager();
actionMgr = new ActionClipManager(clock, configMgr.getActionClipSettings());
renderer = new RendererFactory(scaledWidth, scaledHeight).create(useGpu);
}
void initConfigSettings() {
useKinect = configMgr.useKinect();
useGpu = configMgr.useGpu();
shouldOverlayVideo = configMgr.overlayVideo();
shouldResizeSilhouette = configMgr.resizeSilhouette();
shouldMirrorSilouette = configMgr.mirrorSilhouette();
silhouettePadding = configMgr.getSilhouettePadding();
smooth = configMgr.getSmoothSilhouette(); // silhouette smoothing ratio
}
void draw() {
if(useKinect) {
kinect.update();
}
if (useKinect? tracking: true) {
if(shouldOverlayVideo && !clipMgr.isStarted()) {
clipMgr.start();
actionMgr.start();
clock.start();
}
// create a buffer image that will contain the rendered content
resultImage = new PImage(WIDTH, HEIGHT, RGB);
if(actionMgr.shouldPlay()) {
actionMgr.next();
Clip actionClip = actionMgr.getCurrent();
renderer.addActionClip(actionClip, resultImage);
} else {
renderer.initImage(resultImage);
}
processSilhouette();
// blur silhouette (including silhouettes in action clips)
resultImage = renderer.smoothEdges(resultImage, smooth);
if(shouldOverlayVideo) {
// don't display an image if video overlay failed
boolean success = renderer.overlayVideo(clipMgr, resultImage);
if(!success) {
return;
}
}
image(resultImage, 0, 0, scaledWidth, scaledHeight);
processCenterOfMass();
drawElapsedTime();
clock.tick();
} else {
// get the Kinect color image
PImage rgbImage = kinect.rgbImage();
image(rgbImage, 0, 0, scaledWidth, scaledHeight);
}
}
void processSilhouette() {
if(!useKinect) {
return;
}
SilhouetteFrame silhouetteFrame = getSilhouetteFrame();
PImage silhouette = renderer.convertSilhouette(silhouetteFrame);
if(silhouette!=null) {
if(shouldResizeSilhouette) {
silhouette = renderer.resizeSilhouette(silhouette);
}
renderer.addSilhouette(silhouette, resultImage);
}
}
void displayCenterOfMass(PVector position) {
if(configMgr.showCenterOfMass()) {
fill(255, 0, 0);
// adjust position in re-scaled image
float posx = position.x * scaledWidth / KINECT_WIDTH;
float posy = position.y * scaledHeight/ KINECT_HEIGHT;
ellipse(position.x, position.y, 25, 25);
}
}
void processLiveUserPositionData() {
IntVector userList = userManager.getUsers();
Map<Integer, PVector> userPositions = userManager.getUserPositions();
int userCount = userPositions.size();
oscManager.send(clipMgr.getCurrentIndex(), userCount, actionMgr.getCurrentIndex(), LIVE);
for(Entry<Integer, PVector> userPosition :userPositions.entrySet()) {
int userId = userPosition.getKey();
PVector position = userPosition.getValue();
displayCenterOfMass(position);
oscManager.sendUserIndex(userId, position);
SilhouetteFrame frame = silhouetteCache.getLast();
if(frame!=null) {
frame.addMetaData(userCount, userId, position);
}
}
}
void handleFocusedUser() {
int userId = userManager.getFocusedUser();
if(userId!=-1 && userId!=focusedUser) {
focusedUser = userId;
oscManager.sendFocusedUserIndex(focusedUser);
println("***** focused user=" + focusedUser);
}
}
void processCenterOfMass()
{
if(!useKinect || clipMgr.getCurrent()==null) {
return;
}
if(silhouetteCache.isStarted()) {
// were using cached frames, send the cached meta data using OSC
SilhouetteFrame frame = silhouetteCache.getCurrent();
if(frame!=null && frame.getMetaDataList().size()>0) {
oscManager.send(clipMgr.getCurrentIndex(), 0, actionMgr.getCurrentIndex(), CACHED);
for(MetaData metaData: frame.getMetaDataList()) {
oscManager.sendUserIndex(metaData.userIndex, metaData.position);
displayCenterOfMass(metaData.position);
}
} else {
if(frame==null) {
println("warning: invalid cached SilhouetteFrame received ...");
}
// cached frame has not metadata
oscManager.send(clipMgr.getCurrentIndex(), 0, actionMgr.getCurrentIndex(), CACHED);
}
} else {
processLiveUserPositionData();
handleFocusedUser();
}
}
/*
* retrieves a silhouette frame ( a bitset where all the pixel that represent the silhouette are set to true )
*/
SilhouetteFrame getSilhouetteFrame() {
SilhouetteFrame frame = null;
userMap = kinect.userMap();
IntVector userList = userManager.getUsers();
long userCount = userList.size();
if(!hasUserMap && userCount > 0) {
println("actually tracking users !!!!!!!!!!!!!!!!!!!!");
hasUserMap = true;
} else if(hasUserMap && userCount == 0) {
println("no longer tracking users ###################");
hasUserMap = false;
}
if(userMap.length > 0 && userCount > 0) {
if(silhouetteCache.isStarted()) {
silhouetteCache.stop();
println("starting using Kinect user map frames ...");
}
// store silhouette frame in cache
frame = new SilhouetteFrame();
for (int i = 0; i < userMap.length; i++) {
if (userMap[i] != 0) {
frame.set(i, true);
} else {
frame.set(i, false);
}
}
silhouetteCache.add(frame);
} else {
// if the cache has enough frames from playback get the current one
if(silhouetteCache.canPlayback()) {
if(!silhouetteCache.isStarted()) {
silhouetteCache.start();
}
silhouetteCache.next();
frame = silhouetteCache.getCurrent();
}
}
return frame;
}
/*
* apply the silhouette on the resultImage
*/
void drawElapsedTime() {
if(!configMgr.showTime()) {
return;
}
textFont(font, 36);
fill(color(255,0,0));
String fps = String.format("%.01f", frameRate);
String focusedUser = "";
if(useKinect)
{
int user = userManager.getFocusedUser();
if(user!=-1)
focusedUser = String.format(" user:%d", user);
}
String output = "Elapsed: " + str(clock.getCurrentTimeInSec()) + focusedUser + " fps: " + fps;
output += " " + userManager.dumpOrderUserList() + " " + userManager.dumpUserMapKeys();
text(output , 10, 35);
}
// Called every time a new frame is available to read
void movieEvent(Movie movie) {
movie.read();
movie.loadPixels();
}
void captureEvent(Capture captureDevice) {
captureDevice.read();
captureDevice.loadPixels();
}
void onNewUser(SimpleOpenNI curContext, int userId) {
userID = userId;
tracking = true;
//curContext.startTrackingSkeleton(userId);
oscManager.sendNewUserIndex(userId);
userManager.add(userId);
int focusedUser = userManager.getFocusedUser();
if(focusedUser!=-1) {
oscManager.sendFocusedUserIndex(focusedUser);
}
println("**************** tracking:" + userId);
}
void onLostUser(SimpleOpenNI curContext, int userId) {
oscManager.sendLostUserIndex(userId);
userManager.remove(userId);
println("**************** onLostUser - userId: " + userId);
}
void onVisibleUser(SimpleOpenNI curContext, int userId) {
//println("onVisibleUser - userId: " + userId);
}
/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
/* print the address pattern and the typetag of the received OscMessage */
/*
print("### received an osc message.");
print(" addrpattern: "+theOscMessage.addrPattern());
println(" typetag: "+theOscMessage.typetag());
*/
}
PApplet getApp() {
return application;
}
Capture getCaptureDevice() {
if(capture!=null) {
return capture;
}
try {
capture = new Capture(application, KINECT_WIDTH, KINECT_HEIGHT);
} catch(Exception ex) {
println("warning: coundn't initialize capture device");
}
return capture;
}
/*
* Members
*/
private PApplet application;
private Renderer renderer = null;
private Capture capture = null;
private int scaledHeight = KINECT_HEIGHT;
private int scaledWidth = KINECT_WIDTH;
private Clock clock;
private SimpleOpenNI kinect; // Kinect API
private boolean useKinect = false;
private boolean useGpu = false;
private boolean hasUserMap = false;
private SilhouetteClipManager clipMgr;
private ConfigManager configMgr;
private ActionClipManager actionMgr;
private OscManager oscManager;
private SilhouetteFrameCache silhouetteCache;
private UserManager userManager;
private boolean shouldResizeSilhouette = false;
private boolean shouldOverlayVideo = false;
private boolean shouldMirrorSilouette = false;
private SilhouettePadding silhouettePadding;
private boolean tracking = false;
private int userID;
private int[] userMap;
private int smooth = 0;
private PImage resultImage;
private PFont font;
private SilhouetteFrame previousFrame = null;
private int focusedUser = -1;