Skip to content

Commit

Permalink
faster findFace worker postMessage
Browse files Browse the repository at this point in the history
Use transferList to send imageData ArrayBuffer, which transfers ownership
instead of serializing / deserialzing
  • Loading branch information
yofreke committed Sep 14, 2016
1 parent ba19e81 commit f832136
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
10 changes: 7 additions & 3 deletions js/jsfeat/JsfeatFace.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,15 @@ export default class JsfeatFace extends EventEmitter {
}

// console.time('findFace');
this.worker.postMessage({
// Send the underlying ArrayBuffer to worker
const imageDataBuffer = imageData.data.buffer;
const message = {
w: w,
h: h,
imageData: imageData
});
imageDataBuffer: imageDataBuffer,
imageDataLength: imageData.length
};
this.worker.postMessage(message, [message.imageDataBuffer]);
}

}
27 changes: 14 additions & 13 deletions js/jsfeat/findFace.worker.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import jsfeat from './jsfeatWithFrontalface';

onmessage = function (e) {
const imageData = e.data.imageData;
const buffer = e.data.imageDataBuffer;
const imageData = new Uint8Array(buffer, 0, e.data.imageDataLength);
const w = e.data.w;
const h = e.data.h;

const img_u8 = new jsfeat.matrix_t(w, h, jsfeat.U8_t | jsfeat.C1_t);
const ii_sum = new Int32Array((w + 1) * (h + 1));
const ii_sqsum = new Int32Array((w + 1) * (h + 1));
const ii_tilted = new Int32Array((w + 1) * (h + 1));
const imgU8 = new jsfeat.matrix_t(w, h, jsfeat.U8_t | jsfeat.C1_t);
const iiSum = new Int32Array((w + 1) * (h + 1));
const iiSqsum = new Int32Array((w + 1) * (h + 1));
const iiTilted = new Int32Array((w + 1) * (h + 1));

const classifier = jsfeat.haar.frontalface;

// Old findFace
jsfeat.imgproc.grayscale(imageData.data, img_u8.data);
jsfeat.imgproc.equalize_histogram(img_u8, img_u8);
jsfeat.imgproc.compute_integral_image(img_u8, ii_sum, ii_sqsum, null);
jsfeat.imgproc.grayscale(imageData, imgU8.data);
jsfeat.imgproc.equalize_histogram(imgU8, imgU8);
jsfeat.imgproc.compute_integral_image(imgU8, iiSum, iiSqsum, null);
let rects = jsfeat.haar.detect_multi_scale(
ii_sum,
ii_sqsum,
ii_tilted,
iiSum,
iiSqsum,
iiTilted,
null,
img_u8.cols,
img_u8.rows,
imgU8.cols,
imgU8.rows,
classifier,
1.15,
2
Expand Down

0 comments on commit f832136

Please sign in to comment.