-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple_finger_tracking
286 lines (233 loc) · 7.54 KB
/
multiple_finger_tracking
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
import processing.video.*;
Capture video;
int sizeX = 640;
int sizeY = 480;
int maximumFingerWidth = 100;
int minimumFingerWidth = 5;
int depthScanSpacing = 2;
int fingerColorThreshold = 80;
int maximumNumberOfNotes = 10;
int maxFingerSteer = 30;
int maximumInterFingerDisplacement = 50;
int maxNumberOfFinger = 10;
int waterSurfaceLocation;
int scanWidthFrom = 0;
int scanWidthTo = 640;
int scanDepthFrom = 0;
int scanDepthTo = 480;
Finger[] fingers;
Note[] notes;
class Note
{
int pitch;
int volume;
int noteOn;
int newNote;
// Constructor
Note(int pitchTemp, int volumeTemp, int noteOnTemp, int newNoteTemp)
{
pitch = pitchTemp;
volume = volumeTemp;
noteOn = noteOnTemp;
newNote = newNoteTemp;
}
void updateNoteInfo(int xLocation, int yDepth)
{
}
void setNoteOn(int xLocation, int yDepth)
{
}
void setNoteOff()
{
pitch = 0;
volume = 0;
noteOn = 0;
newNote = 0;
}
}
class Finger
{
int xLocation;
int xLocationPrevious;
int yDepth;
int yDepthPrevious;
int fingerOn;
// Constructor
Finger(int xLTemp, int xLPTemp, int yDTemp, int yDPTemp, int fingerOnTemp)
{
xLocation = xLTemp;
xLocationPrevious = xLPTemp;
yDepth = yDTemp;
yDepthPrevious = yDPTemp;
fingerOn = fingerOnTemp;
}
void updateFingerInfo(int xLocationTemp, int yDepthTemp, int fingerOnTemp)
{
xLocation = xLocationTemp;
yDepth = yDepthTemp;
fingerOn = fingerOnTemp;
}
void initializeFinger()
{
xLocation = 0;
xLocationPrevious = 0;
yDepth = 0;
yDepthPrevious = 0;
fingerOn = 0;
}
int getFingerOn() { return fingerOn; }
int getXLocation() { return xLocation; }
int getXLocationPrev() { return xLocationPrevious; }
int getYDepth() { return yDepth; }
int getYDepthPrev() { return yDepthPrevious; }
}
void setup()
{
size(sizeX, sizeY);
// Uses the default video input, see the reference if this causes an error
video = new Capture(this, width, height);
video.start();
noStroke();
smooth();
fingers = new Finger[maxNumberOfFinger];
notes = new Note[maxNumberOfFinger]; // The maximum number of note is same as one of finger.
for (int i = 0; i < maxNumberOfFinger; i++)
{
fingers[i] = new Finger(0, 0, 0, 0, 0);
notes[i] = new Note(0, 0, 0, 0);
}
}
// Proposition 1 : Finger information is processed within one frame
// Proposition 2 : Note information is processed with multiple frame information within time frame
void draw()
{
int fingerRecognitionCounter = 0;
int tempFingerCenterLocationX = 0;
int tempFingerCenterLocationFrom, tempFingerCenterLocationTo;
int tempFingerCenterLocationY;
if (video.available())
{
video.read();
image(video, 0, 0, width, height); // Draw the webcam video onto the screen
video.loadPixels();
int pixelIndex = 0;
tempFingerCenterLocationX = 0;
tempFingerCenterLocationFrom = 0;
tempFingerCenterLocationTo = 0;
tempFingerCenterLocationY = 0;
int newFingerIndex = 0;
int prevY = 0;
for ( int i = 0; i < maxNumberOfFinger; i++ )
{
fingers[i].initializeFinger();
}
// Main loop for pixel scanning
// Loop for Y-axis depth scan
for ( int y = 0; y < sizeY; y++ )
{
fingerRecognitionCounter = 0;
// Jump the depth scan spacing
if ( (y - prevY) < depthScanSpacing && y != 0 )
{
pixelIndex = pixelIndex + sizeX;
continue;
}
else
{
prevY = y;
}
for ( int x = 0; x < sizeX; x++ )
{
// Loop for X-axis finger recognition, only within the scan depth range
if ( y >= scanDepthFrom && y <= scanDepthTo && x >= scanWidthFrom && x <= scanWidthTo )
{
int pixelValue = video.pixels[pixelIndex];
float pixelBrightness = brightness(pixelValue);
// Scan for darker region for finger recognition
if (pixelBrightness < fingerColorThreshold)
{
if ( fingerRecognitionCounter == 0 )
{
tempFingerCenterLocationFrom = x;
}
fingerRecognitionCounter++;
}
else
{
// Process the finger region
if ( fingerRecognitionCounter >= minimumFingerWidth )
{
int fingerExist = 0;
int xDisplacementTemp = 1000; // Arbitrary big value
int xDisplacement = 0;
int closestIndex = 0;
tempFingerCenterLocationTo = x;
tempFingerCenterLocationX = (tempFingerCenterLocationFrom + tempFingerCenterLocationTo) / 2;
tempFingerCenterLocationY = y;
// Add new finger based on the difference between current depth scan data and previous one
for ( int i = 0; i < maxNumberOfFinger; i++ )
{
if ( 1 == fingers[i].getFingerOn() )
{
fingerExist = 1;
xDisplacement = abs(tempFingerCenterLocationX - fingers[i].getXLocation());
// Find the closest finger note index recognized on previous depth
if ( xDisplacement < xDisplacementTemp )
{
xDisplacementTemp = xDisplacement;
closestIndex = i;
}
}
}
// Update the finger note information which is adjacent to previous depth note
if ( fingerExist == 1 && xDisplacementTemp < maxFingerSteer )
{
fingers[closestIndex].updateFingerInfo(tempFingerCenterLocationX, y, 1);
}
// Create new finger note information which is not adjacent to previous depth note
else if ( fingerExist == 1 && xDisplacementTemp > maxFingerSteer && newFingerIndex < maxNumberOfFinger)
{
fingers[newFingerIndex].updateFingerInfo(tempFingerCenterLocationX, y, 1);
if ( ( y - scanDepthFrom ) < depthScanSpacing )
{
newFingerIndex++;
}
}
// Create new finger note for the first time
else if ( fingerExist != 1 )
{
fingers[newFingerIndex].updateFingerInfo(tempFingerCenterLocationX, y, 1);
if ( ( y - scanDepthFrom ) < depthScanSpacing )
{
newFingerIndex++;
}
}
}
fingerRecognitionCounter = 0;
}
}
pixelIndex++;
// End of single y depth scanning process
}
// End of a frame process
}
// Compare the notes recognized in current frame with the ones in previous frame
// Discriminate new notes, updated notes, and notes disappeared.
// Play the MIDI sound with SoundCipher library
// Draw a yellow circles at recognized fingertips
fill(255, 204, 0, 128);
for ( int i = 0; i < maxNumberOfFinger; i++ )
{
if ( 1 == fingers[i].getFingerOn() )
{
ellipse(fingers[i].getXLocation(), fingers[i].getYDepth(), 50, 50);
}
}
// For frame rate checking
float m = millis();
print("time : ");
print(m);
print(", X : ");
println(tempFingerCenterLocationX);
}
}