/
LectureCapture.pde
214 lines (185 loc) · 6.78 KB
/
LectureCapture.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
import SimpleOpenNI.*;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.*;
//install this on your machine and also the library in processing
//http://code.google.com/p/simple-openni/wiki/Installation
SimpleOpenNI kinect;
boolean handsTrackFlag = false;
PVector handVec = new PVector();
//based on Greg's Book Making things see.
boolean userPresent = false;
int userID;
int[] userMap;
// declare our images
PImage backgroundImage;
PImage resultImage;
BufferedImage desktop;
Rectangle bounds;
Robot myRobot;
int drawingThreshold = 800;
float lastX = -1;
float lastY = -1;
PGraphics whiteBoard;
DisplayMode mode;
void setup() {
size(640, 480);
// load the background image
// backgroundImage = loadImage("http://www.eheart.com/SHASTA/wallpaper/alpenglow-640x480.jpg");
kinect = new SimpleOpenNI(this);
kinect.enableDepth();
// enable color image from the Kinect
kinect.enableRGB();
//unfortunately have to track guestures to get hands
kinect.enableGesture();
kinect.enableHands();
kinect.addGesture("RaiseHand");
fill(255, 0, 0);
kinect.setMirror(true);
//enable the finding of users but dont' worry about skeletons
kinect.enableUser(SimpleOpenNI.SKEL_PROFILE_NONE);
// turn on depth-color alignment
kinect.alternativeViewPointDepthToImage();
//create a buffer image to work with instead of using sketch pixels
resultImage = new PImage(640, 480, RGB);
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices();
// int whichScreen = gs.length-1;
int whichScreen = 0;
mode = gs[whichScreen].getDisplayMode();
bounds = new Rectangle(0, 0, mode.getWidth(), mode.getHeight());
desktop = new BufferedImage(mode.getWidth(), mode.getHeight(), BufferedImage.TYPE_INT_RGB);
try {
myRobot = new Robot(gs[whichScreen]);
}
catch(AWTException e) {
System.err.println("Screen capture failed.");
}
whiteBoard = createGraphics(bounds.width, bounds.height);
whiteBoard.endDraw();
backgroundImage =getScreen();
}
void draw() {
kinect.update();
// get the Kinect color image
PImage rgbImage = kinect.rgbImage();
//backgroundImage =getScreen();
//image(backgroundImage,0,0);
if (userPresent) {
//ask kinect for bitmap of user pixels
userMap = kinect.getUsersPixels(SimpleOpenNI.USERS_ALL);
// for (int i =0; i < userMap.length; i++) {
for (int row =0; row < rgbImage.height; row++) {
for (int col =0; col < rgbImage.width; col++) {
int videoOffset = row*rgbImage.width + col;
int screenGrabOffset = row*backgroundImage.width + col;
// if the pixel is part of the user
if (userMap[videoOffset] != 0) {
// set the pixel to the color pixel
resultImage.pixels[videoOffset] = rgbImage.pixels[videoOffset];
}
else {
//set it to the background
//resultImage.pixels[videoOffset] = 0x00000000;
resultImage.pixels[videoOffset] = backgroundImage.pixels[screenGrabOffset];
}
}
}
//update the pixel from the inner array to image
resultImage.updatePixels();
image(resultImage, 0, 0);
}else{
backgroundImage =getScreen(); //only bother slowing thing down grabing screen if no one is there
}
if (handsTrackFlag)
{
PVector myPositionScreenCoords = new PVector(); //storage device
//convert the weird kinect coordinates to screen coordinates.
kinect.convertRealWorldToProjective(handVec, myPositionScreenCoords);
//move the screen around the desktop
int moveDesktopRectX = 0;
int moveDesktopRectY = 0;
if (myPositionScreenCoords.x > width-50) moveDesktopRectX = 1;
if (myPositionScreenCoords.x < 50) moveDesktopRectX = -1;
if (myPositionScreenCoords.y > height-50) moveDesktopRectY = 1;
if (myPositionScreenCoords.y < 50) moveDesktopRectY = -1;
if (moveDesktopRectX != 0 || moveDesktopRectY != 0){
bounds.translate(moveDesktopRectX, moveDesktopRectY);
backgroundImage =getScreen();
}
float redNess = max(255,255- (myPositionScreenCoords.z-drawingThreshold));
if (myPositionScreenCoords.z < drawingThreshold) {
whiteBoard.beginDraw();
whiteBoard.stroke(255, 0, 0);
whiteBoard.strokeWeight(4);
if (lastX == -1) {
lastX = myPositionScreenCoords.x;
lastY = myPositionScreenCoords.y;
}
whiteBoard.line(lastX, lastY, myPositionScreenCoords.x, myPositionScreenCoords.y);
lastX = myPositionScreenCoords.x;
lastY = myPositionScreenCoords.y;
// whiteBoard.ellipse(myPositionScreenCoords.x, myPositionScreenCoords.y, 2, 2);
whiteBoard.endDraw();
}
fill(redNess,0,0);
ellipse(myPositionScreenCoords.x, myPositionScreenCoords.y, 10,10);
}
image(whiteBoard, -bounds.x,-bounds.y);
//bounds.translate(1,0);
}
PImage getScreen() {
desktop = myRobot.createScreenCapture(bounds);
return (new PImage(desktop));
}
void keyPressed() {
switch(key)
{
case ' ':
kinect.setMirror(!kinect.mirror());
break;
case 'd':
drawingThreshold--;
println("Drawing Threshold " + drawingThreshold);
break;
case 'D':
drawingThreshold++;
println("Drawing Threshold " + drawingThreshold);
break;
}
}
void onNewUser(int uID) {
userID = uID;
userPresent = true;
println("userPresent");
}
// ----------------------------------------------------------------- // hand events
void onCreateHands(int handId, PVector pos, float time) {
println("onCreateHands - handId: " + handId + ", pos: " + pos + ", time:" + time);
handsTrackFlag = true;
handVec = pos;
}
void onUpdateHands(int handId, PVector pos, float time) {
//println("onUpdateHandsCb - handId: " + handId + ", pos: " + pos + ", time:" + time);
//store the location of the hand in a vector object
handVec = pos;
}
void onDestroyHands(int handId, float time) {
println("onDestroyHandsCb - handId: " + handId + ", time:" + time);
handsTrackFlag = false;
lastX = -1;
//go back to looking for the guesture that gave you hand.
kinect.addGesture("RaiseHand");
}
// ----------------------------------------------------------------- // gesture events
void onRecognizeGesture(String strGesture, PVector idPosition, PVector endPosition) {
println("onRecognizeGesture - strGesture: " + strGesture + ", idPosition: " + idPosition + ", endPosition:" + endPosition);
//stop looking for the gesture
kinect.removeGesture(strGesture);
//use the location of this guesture tell you where to start userPresent the hand
kinect.startTrackingHands(endPosition);
}
void onProgressGesture(String strGesture, PVector position, float progress) {
//println("onProgressGesture - strGesture: " + strGesture + ", position: " + position + ", progress:" + progress);
}
// ----------------------------------------------------------------- // Keyboard event