Skip to content

Commit

Permalink
Check that the number of threads is between the minimum and maximum v…
Browse files Browse the repository at this point in the history
…alues
  • Loading branch information
aleksazh committed Nov 8, 2019
1 parent 736c55c commit cc00fc4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 58 deletions.
16 changes: 1 addition & 15 deletions samples/emotionRecognizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,21 +223,7 @@ function initUI() {
utils.startCamera(videoConstraint, 'videoInput', startVideoProcessing);
});

if (!isMobileDevice()) {
// Init threads number.
let threadsControl = document.getElementsByClassName('threads-control')[0];
threadsControl.classList.remove('hidden');
let threadsNumLabel = document.getElementById('threadsNumLabel');
let threadsNum = document.getElementById('threadsNum');
threadsNum.max = navigator.hardwareConcurrency;
threadsNumLabel.innerHTML = `Number of threads (1 - ${threadsNum.max}): `;
if (3 <= threadsNum.max) threadsNum.value = 3;
else threadsNum.value = 1;
cv.parallel_pthreads_set_threads_num(parseInt(threadsNum.value));
threadsNum.addEventListener('change', () => {
cv.parallel_pthreads_set_threads_num(parseInt(threadsNum.value));
});
}
enableThreads();
}

utils.loadOpenCv(() => {
Expand Down
15 changes: 1 addition & 14 deletions samples/faceDetection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,20 +172,7 @@ function initUI() {
utils.startCamera(videoConstraint, 'videoInput', startVideoProcessing);
});

if (!isMobileDevice()) {
// Init threads number.
let threadsControl = document.getElementsByClassName('threads-control')[0];
threadsControl.classList.remove('hidden');
let threadsNumLabel = document.getElementById('threadsNumLabel');
let threadsNum = document.getElementById('threadsNum');
threadsNum.max = navigator.hardwareConcurrency;
threadsNumLabel.innerHTML = `Number of threads (1 - ${threadsNum.max}):&nbsp;`;
if (3 <= threadsNum.max) threadsNum.value = 3;
else threadsNum.value = 1;
cv.parallel_pthreads_set_threads_num(parseInt(threadsNum.value));
threadsNum.addEventListener('change', () => {
cv.parallel_pthreads_set_threads_num(parseInt(threadsNum.value));
});
enableThreads();
}

// Event listener for dowscale parameter.
Expand Down
15 changes: 1 addition & 14 deletions samples/funnyHats/js/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,7 @@ function initUI() {
utils.startCamera(videoConstraint, 'videoInput', startVideoProcessing);
});

if (!isMobileDevice()) {
// Init threads number.
let threadsControl = document.getElementsByClassName('threads-control')[0];
threadsControl.classList.remove('hidden');
let threadsNumLabel = document.getElementById('threadsNumLabel');
let threadsNum = document.getElementById('threadsNum');
threadsNum.max = navigator.hardwareConcurrency;
threadsNumLabel.innerHTML = `Number of threads (1 - ${threadsNum.max}):&nbsp;`;
if (3 <= threadsNum.max) threadsNum.value = 3;
else threadsNum.value = 1;
cv.parallel_pthreads_set_threads_num(parseInt(threadsNum.value));
threadsNum.addEventListener('change', () => {
cv.parallel_pthreads_set_threads_num(parseInt(parseInt(threadsNum.value)));
});
enableThreads();
}

// Event listener for dowscale parameter.
Expand Down
16 changes: 1 addition & 15 deletions samples/invisibilityCloak/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,7 @@ function initUI() {
utils.startCamera(videoConstraint, 'videoInput', startVideoProcessing);
});

if (!isMobileDevice()) {
// Init threads number.
let threadsControl = document.getElementsByClassName('threads-control')[0];
threadsControl.classList.remove('hidden');
let threadsNumLabel = document.getElementById('threadsNumLabel');
let threadsNum = document.getElementById('threadsNum');
threadsNum.max = navigator.hardwareConcurrency;
threadsNumLabel.innerHTML = `Number of threads (1 - ${threadsNum.max}):&nbsp;`;
if (3 <= threadsNum.max) threadsNum.value = 3;
else threadsNum.value = 1;
cv.parallel_pthreads_set_threads_num(parseInt(threadsNum.value));
threadsNum.addEventListener('change', () => {
cv.parallel_pthreads_set_threads_num(parseInt(threadsNum.value));
});
}
enableThreads();
}

utils.loadOpenCv(() => {
Expand Down
23 changes: 23 additions & 0 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const resolutions = {
'vga': { width: { exact: 640 }, height: { exact: 480 } }
};

const DEFAULT_THREADS_NUM = 3;

function Utils(errorOutputId) { // eslint-disable-line no-unused-vars
let self = this;
this.errorOutput = document.getElementById(errorOutputId);
Expand Down Expand Up @@ -375,3 +377,24 @@ function addButtonToCameraBar(id, text, maxItems) {
liElement.appendChild(divElement);
cameraBar.appendChild(liElement);
}

function enableThreads() {
if (!isMobileDevice()) {
// Init threads number.
let threadsControl = document.getElementsByClassName('threads-control')[0];
threadsControl.classList.remove('hidden');
let threadsNumLabel = document.getElementById('threadsNumLabel');
let threadsNum = document.getElementById('threadsNum');
threadsNum.max = navigator.hardwareConcurrency;
threadsNumLabel.innerHTML = `Number of threads (1 - ${threadsNum.max}):&nbsp;`;
if (DEFAULT_THREADS_NUM <= threadsNum.max) threadsNum.value = DEFAULT_THREADS_NUM;
else threadsNum.value = 1;
cv.parallel_pthreads_set_threads_num(parseInt(threadsNum.value));
threadsNum.addEventListener('change', () => {
if (Number(threadsNum.value) <= Number(threadsNum.max) &&
Number(threadsNum.value) >= Number(threadsNum.min)) {
cv.parallel_pthreads_set_threads_num(parseInt(threadsNum.value));
}
});
}
}

0 comments on commit cc00fc4

Please sign in to comment.