-
Notifications
You must be signed in to change notification settings - Fork 0
/
Postprocess.pde
263 lines (232 loc) · 6.59 KB
/
Postprocess.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
//This is a utility to post-process and check for drop frames in Kinect recordings.
//Copy the data folder recorded by Kinect_just_record before you start.
class MApplet extends PApplet{
PApplet pgx;
void setup(){
size(sW,sH,P3D);
frameRate(fps);
background(0);
}
void draw(){
tester();
if (modeRender) {
if(!loadedForRender){
initPlay();
setupRender();
shotNum = shotNumOrig;
}
drawRender();
}
}
}
void tester(){
println("*******************************");
}
void setupRender() {
reInit();
if(record3D){
cam = new PeasyCam(this, sW);
}
//smooth();
stroke(255);
}
void xmlLoad() {
xmlIO = new XMLInOut(this);
try {
xmlIO.loadElement(xmlFileName); //loads the XML
}
catch(Exception e) {
//if loading failed
println("Loading Failed");
}
}
void reInit() {
//~~~
readFrameNum = readFrameNumOrig;
writeFrameNum = readFrameNum;
addFrameCounter = 0;
subtractFrameCounter = 0;
xmlFileName = readFilePath + "/" + readFileName + shotNum + ".xml";
errorAllow = 0;
loadedForRender = false;
countFolder();
xmlLoad();
}
void xmlEvent(proxml.XMLElement element) {
if(modeRec){
//this function is ccalled by default when an XML object is loadedForRender
xmlFile = element;
//parseXML(); //appelle la fonction qui analyse le fichier XML
loadedForRec = true;
}else if(modeRender){ //this function is ccalled by default when an XML object is loadedForRender
xmlFile = element;
//parseXML(); //appelle la fonction qui analyse le fichier XML
loadedForRender = true;
readTimestamps();
println("average interval: " + getAverageInterval() + " ms | correct interval: " + idealInterval + " ms");
}
}
void drawRender() {
background(0);
if (shotNum<=numberOfFolders) {
if (loadedForRender) {
if (readFrameNum<readFrameNumMax) {
//******
readString = readFilePath + "/" + readFileName + shotNum + folderIndicator+"/" + readFileName + shotNum + "_frame" + readFrameNum + "." + readFileType;
println("-- read: " + readString + " timestamp: " + timestamps[readFrameNum-1] + " ms");
img = loadImage(readString);
if(record3D){
objGenerate();
}else{
image(img, 0, 0);
}
checkTimestamps();
if (!checkTimeAhead()&&checkTimeBehind()) { //behind and not ahead; add a missing frame
//********************
int q = 1;
if (readFrameNum>readFrameNumOrig) {
q = timestamps[readFrameNum-1]-timestamps[readFrameNum-2];
}
writeFile(int(q/int(idealInterval)));
//********************
addFrameCounter+=errorAllow/idealInterval;
diffReport += " ADDED FRAMES";
errorAllow -= idealInterval;
}
else if (checkTimeAhead()&&!checkTimeBehind()) { //ahead and not behind; skip an extra frame
subtractFrameCounter++;
diffReport += " REMOVED FRAMES";
errorAllow += idealInterval;
}
else if (!checkTimeAhead()&&!checkTimeBehind()) { //not ahead and not behind; do nothing
diffReport += " OK";
writeFile(1);
}
println("written: " + writeString + diffReport);
readFrameNum++;
}
else {
renderVerdict();
if (shotNum==numberOfFolders) {
exit();
}
else {
shotNum++;
reInit();
}
}
}
}
else {
exit();
}
}
void countFolder() {
dataFolder = new File(sketchPath, readFilePath + "/" + readFileName + shotNum+folderIndicator+"/");
try{
numFiles = dataFolder.list();
readFrameNumMax = numFiles.length+1;
println("********* " + readFrameNumMax);
}catch(Exception e){
//
}
}
void writeFile(int reps) {
for (int i=0;i<reps;i++) {
writeString = writeFilePath + "/" + writeFileName + shotNum + folderIndicator+"/" + writeFileName + shotNum + "_frame"+writeFrameNum+"."+writeFileType;
saveFrame(writeString);
if(record3D&&reps>1){
objGenerate();
}
//println("written: " + writeString + diffReport);
writeFrameNum++;
}
}
void readTimestamps() {
timestamps = new int[numFiles.length];
for (int i=0;i<numFiles.length;i++) {
timestamps[i] = int(xmlFile.getChild(i).getAttribute("timestamp"));
println(timestamps[i]);
}
}
void checkTimestamps() {
if (readFrameNum>readFrameNumOrig) {
float q = timestamps[readFrameNum-1]-timestamps[readFrameNum-2];
diffReport = " diff: " + int(q) + " ms" + " min: " + int(idealInterval)+ " ms" + " cumulative error: " + int(errorAllow) + " ms";
errorAllow += q-idealInterval;
}
}
boolean checkTimeBehind() {
if (errorAllow>idealInterval) {
return true;
}
else {
return false;
}
}
boolean checkTimeAhead() {
if (errorAllow<-1*idealInterval) {
return true;
}
else {
return false;
}
}
void renderVerdict() {
//int timeDiff = int(30*((timestamps[timestamps.length-1] - timestamps[0])/1000));
println("SHOT" + shotNum + " COMPLETE");
/*
println(int(addFrameCounter) + " dropped frames added");
println(int(subtractFrameCounter) + " extra frames removed");
*/
}
float getAverageInterval() {
float q = ((timestamps[3] - timestamps[2]) + (timestamps[1] - timestamps[0]))/2;
for (int i=4;i<timestamps.length/4;i++) {
float qq = ((timestamps[i+3] - timestamps[i+2]) + (timestamps[i+1] - timestamps[i]))/2;
q = (q+qq)/2;
}
return q;
}
static final int gray(color value) {
return max((value >> 16) & 0xff, (value >> 8 ) & 0xff, value & 0xff);
}
void objGenerate(){
background(0);
if(record3D){
objBegin();
}
buffer = img;
for (int y = 0; y < sH; y++) {
for (int x = 0; x < sW; x++) {
// FIXME: this loses Z-resolution about tenfold ...
// -> should grab the real distance instead...
color argb = buffer.pixels[y*width+x];
gray[y][x] = gray(argb);
}
}
// Kyle McDonald's original source used here
pushMatrix();
translate(-sW / 2, -sH / 2);
int step = 2;
for (int y = step; y < sH; y += step) {
float planephase = 0.5 - (y - (sH / 2)) / zskew;
for (int x = step; x < sW; x += step)
{
stroke(gray[y][x]);
//point(x, y, (gray[y][x] - planephase) * zscale);
line(x, y, (gray[y][x] - planephase) * zscale, x+1, y, (gray[y][x] - planephase) * zscale);
}
}
popMatrix();
if(record3D){
objEnd();
}
}
void objBegin(){
beginRaw("superCAD.ObjFile", writeFilePath + "/" + writeFileName + shotNum + folderIndicator+"/" + writeFileName + shotNum + "_frame"+writeFrameNum+"."+ "obj"); // Start recording to the file
}
void objEnd(){
endRaw();
}
//~~~ END ~~~