-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLJMU_FrameDifferencing_Sound.pde
More file actions
119 lines (97 loc) · 3.58 KB
/
LJMU_FrameDifferencing_Sound.pde
File metadata and controls
119 lines (97 loc) · 3.58 KB
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
import processing.video.*;
import ddf.minim.*;
// this is the number of pixels that have to change for a picture to be taken.
// making this higer makes it less sensitive (fewer pictures).
int MOVEMENT_THRESHOLD = 100000;
// this is the minimum amount of time between each triggering (in milliseconds).
// 1000 = 1 second
int CAPTURE_PERIOD = 1000;
// to avoid taking pictures of empty frames, this is the number of pixels
// that have to differ between the current frame and the calibration background.
int BACKGROUND_THRESHOLD = 200000;
// this is the audio file to play when movement is detected
String AUDIO_FILENAME = "audio0.mp3";
long lastCaptureMillis;
int numPixels;
boolean showBackgroundDiff;
boolean needToCaptureBackground;
Capture video;
PImage mBackground, backDiff, frameDiff, previousFrame;
Minim mMinim;
AudioPlayer mAudioPlayer;
void setup() {
size(640, 480);
frameRate(30);
video = new Capture(this, width, height);
video.start();
numPixels = video.width * video.height;
lastCaptureMillis = millis();
showBackgroundDiff = false;
needToCaptureBackground = true;
mBackground = createImage(width, height, ARGB);
backDiff = createImage(width, height, ARGB);
frameDiff = createImage(width, height, ARGB);
previousFrame = createImage(width, height, ARGB);
mMinim = new Minim(this);
mAudioPlayer = mMinim.loadFile(dataPath(AUDIO_FILENAME));
mAudioPlayer.play(mAudioPlayer.length());
}
void draw() {
if (video.available()) {
video.read();
video.loadPixels();
if (needToCaptureBackground) {
image(video, 0, 0);
textSize(32);
fill(255, 0, 255);
text("Make sure no one is on camera and hit 'c' on the keyboard to calibrate the camera", 50, height/2-100, width-100, height-200);
} else {
backDiff.loadPixels();
frameDiff.loadPixels();
previousFrame.loadPixels();
int movementSum = 0;
int backgroundSum = 0;
for (int i = 0; i < numPixels; i++) {
color currColor = video.pixels[i];
color prevColor = previousFrame.pixels[i];
color backColor = mBackground.pixels[i];
int currB = currColor & 0xFF;
int prevB = prevColor & 0xFF;
int backB = backColor & 0xFF;
int backDiffB = abs(currB - backB) & 0xFF;
int frameDiffB = abs(currB - prevB) & 0xFF;
movementSum += (frameDiffB>128)?frameDiffB:0;
backgroundSum += (backDiffB>128)?backDiffB:0;
frameDiff.pixels[i] = 0xff000000 | (frameDiffB << 16) | (frameDiffB << 8) | frameDiffB;
backDiff.pixels[i] = 0xff000000 | (backDiffB << 16) | (backDiffB << 8) | backDiffB;
previousFrame.pixels[i] = currColor;
}
previousFrame.updatePixels();
if (showBackgroundDiff) {
backDiff.updatePixels();
image(backDiff, 0, 0);
} else {
frameDiff.updatePixels();
image(frameDiff, 0, 0);
}
if ((movementSum > MOVEMENT_THRESHOLD) && (backgroundSum > BACKGROUND_THRESHOLD)) {
println("move Sum: "+movementSum);
println("back Sum: "+backgroundSum);
if ((millis()-lastCaptureMillis > CAPTURE_PERIOD) && (mAudioPlayer.position() >= mAudioPlayer.length()) ) {
mAudioPlayer.rewind();
mAudioPlayer.play();
lastCaptureMillis = millis();
}
}
}
}
}
void keyPressed() {
if (key == ' ' || key == 'd' || key == 'D') {
showBackgroundDiff = !showBackgroundDiff;
} else if (key == 'c' || key == 'C') {
mBackground.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
needToCaptureBackground = false;
image(video, 0, 0);
}
}