-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from zaru/feat
Added the background transparency quality menu
- Loading branch information
Showing
6 changed files
with
231 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
const bodyPix = require('@tensorflow-models/body-pix'); | ||
const VideoManager = require('./video-manager'); | ||
const TrayMenu = require('./tray-menu'); | ||
const Settings = require('./settings'); | ||
const settings = new Settings; | ||
|
||
|
@@ -13,6 +14,9 @@ const state = { | |
return this.videoHeight / this.videoWidth; | ||
}, | ||
tray: null, | ||
trayMenu: null, | ||
net: null, | ||
changingQuality: false, | ||
}; | ||
|
||
/** | ||
|
@@ -26,15 +30,15 @@ async function workload(deviceId) { | |
|
||
_resizeElement(window.innerWidth, window.innerHeight); | ||
|
||
const net = await bodyPix.load(settings.getBodyPixModel()); | ||
state.net = await bodyPix.load(settings.getBodyPixModelParam()); | ||
|
||
const video = document.getElementById('video'); | ||
const canvas = document.getElementById('canvas'); | ||
const originalCanvas = document.getElementById('original-canvas'); | ||
|
||
async function segmentationFrame() { | ||
if (!state.changingVideo) { | ||
const segmentation = await net.segmentPerson(state.video); | ||
if (!state.changingVideo || !state.changingQuality) { | ||
const segmentation = await state.net.segmentPerson(state.video); | ||
|
||
const originalCtx = originalCanvas.getContext('2d'); | ||
const scale = originalCanvas.width / video.videoWidth; | ||
|
@@ -65,6 +69,18 @@ function switchVideo(deviceId) { | |
settings.setDeviceId(deviceId); | ||
} | ||
|
||
/** | ||
* Switch BodyPix quality | ||
* @param {string} quality | ||
* @return {Promise<void>} | ||
*/ | ||
async function switchQuality(quality) { | ||
state.changingQuality = true; | ||
settings.setBodyPixModel(quality); | ||
state.net = await bodyPix.load(settings.getBodyPixModelParam()); | ||
state.changingQuality = false; | ||
} | ||
|
||
function stopExistingVideoCapture() { | ||
if (state.video && state.video.srcObject) { | ||
state.video.srcObject.getTracks().forEach((track) => { | ||
|
@@ -204,40 +220,15 @@ window.addEventListener('resize', () => { | |
}, 500); | ||
}); | ||
|
||
/** | ||
* Build tray menu | ||
* @param {MediaDeviceInfo[]} videoList | ||
*/ | ||
function buildTrayMenu(videoList) { | ||
const remote = window.remote; | ||
const {Tray, Menu} = remote; | ||
const icon = window.os.platform() === 'darwin' ? 'TrayIconTemplate.png' : '[email protected]'; | ||
state.tray = new Tray(window.__dirname + `/assets/${icon}`); | ||
|
||
const videoMenu = []; | ||
videoList.forEach((device) => { | ||
videoMenu.push({ | ||
label: device.label, | ||
click() { | ||
switchVideo(device.deviceId); | ||
}, | ||
}); | ||
}); | ||
|
||
const menu = Menu.buildFromTemplate([ | ||
{ | ||
label: 'Select Video', | ||
submenu: videoMenu, | ||
}, | ||
]); | ||
|
||
state.tray.setContextMenu(menu); | ||
} | ||
|
||
const videoManager = new VideoManager; | ||
const trayMenu = new TrayMenu(window); | ||
videoManager.getVideoList().then((list) => { | ||
buildTrayMenu(list); | ||
|
||
const deviceId = settings.getDeviceId() || list[0].deviceId; | ||
workload(deviceId); | ||
state.deviceId = settings.getDeviceId() || list[0].deviceId; | ||
trayMenu.deviceId = state.deviceId; | ||
trayMenu.quality = settings.getBodyPixModel(); | ||
trayMenu.videoList = list; | ||
trayMenu.addEventListenerToVideoMenu(switchVideo); | ||
trayMenu.addEventListenerToQualityMenu(switchQuality); | ||
trayMenu.launch(); | ||
workload(state.deviceId); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
window.remote = require('electron').remote; | ||
window.ipcRenderer = require('electron').ipcRenderer; | ||
window.__dirname = __dirname; | ||
window.os = require('os'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/** | ||
* TrayMenu class | ||
*/ | ||
module.exports = class TrayMenu { | ||
/** | ||
* Constractor | ||
* @param {Window} window | ||
*/ | ||
constructor(window) { | ||
this.window = window; | ||
|
||
this._deviceId = ''; | ||
this._videoList = []; | ||
this._quality = ''; | ||
|
||
this.tray = null; | ||
this.trayMenu = null; | ||
this.callbackVideoMenu = null; | ||
this.callbackQualityMenu = null; | ||
} | ||
|
||
/** | ||
* Set deviceId | ||
* @param {string} deviceId | ||
*/ | ||
set deviceId(deviceId) { | ||
this._deviceId = deviceId; | ||
} | ||
|
||
/** | ||
* Set video list | ||
* @param {MediaDeviceInfo[]} videoList | ||
*/ | ||
set videoList(videoList) { | ||
this._videoList = videoList; | ||
} | ||
|
||
/** | ||
* Set quality | ||
* @param {string} quality | ||
*/ | ||
set quality(quality) { | ||
this._quality = quality; | ||
} | ||
|
||
/** | ||
* Add event listener to video menu | ||
* @param {function} callback | ||
*/ | ||
addEventListenerToVideoMenu(callback) { | ||
this.callbackVideoMenu = callback; | ||
} | ||
|
||
/** | ||
* Add event listener to quality menu | ||
* @param {function} callback | ||
*/ | ||
addEventListenerToQualityMenu(callback) { | ||
this.callbackQualityMenu = callback; | ||
} | ||
|
||
/** | ||
* Launch tray menu | ||
*/ | ||
launch() { | ||
this._buildTrayMenu(); | ||
} | ||
|
||
/** | ||
* Build video sub menu | ||
* @return {[]} | ||
* @private | ||
*/ | ||
_buildVideoSubMenu() { | ||
const videoMenu = []; | ||
this._videoList.forEach((device) => { | ||
videoMenu.push({ | ||
label: device.label, | ||
click: () => { | ||
if (this.callbackVideoMenu) { | ||
this.callbackVideoMenu(device.deviceId); | ||
} | ||
}, | ||
type: 'radio', | ||
checked: this._deviceId === device.deviceId, | ||
}); | ||
}); | ||
return videoMenu; | ||
} | ||
|
||
/** | ||
* Build window size sub menu | ||
* @return {{checked: boolean, label: string, type: string, click(): void}[]} | ||
* @private | ||
*/ | ||
_buildSizeSubMenu() { | ||
const menu = []; | ||
['Big', 'Middle', 'Small'].forEach((size) => { | ||
menu.push({ | ||
label: size, | ||
click() { | ||
this.window.ipcRenderer.send('windowResize', size); | ||
}, | ||
type: 'radio', | ||
checked: size === 'Middle', | ||
}); | ||
}); | ||
return menu; | ||
} | ||
|
||
/** | ||
* Build quality sub menu | ||
* @return {{checked: boolean, label: string, type: string, click(): void}[]} | ||
* @private | ||
*/ | ||
_buildQualitySubMenu() { | ||
const menu = []; | ||
['High', 'Middle', 'Low'].forEach((size) => { | ||
menu.push({ | ||
label: size, | ||
click: () => { | ||
if (this.callbackQualityMenu) { | ||
this.callbackQualityMenu(size); | ||
} | ||
}, | ||
type: 'radio', | ||
checked: this._quality === size, | ||
}); | ||
}); | ||
return menu; | ||
} | ||
|
||
/** | ||
* Build video sub menu | ||
*/ | ||
_buildTrayMenu() { | ||
const remote = this.window.remote; | ||
const {Tray, Menu} = remote; | ||
const icon = this.window.os.platform() === 'darwin' ? | ||
'TrayIconTemplate.png' : '[email protected]'; | ||
this.tray = new Tray(this.window.__dirname + `/assets/${icon}`); | ||
|
||
const menu = Menu.buildFromTemplate([ | ||
{ | ||
label: 'Select video', | ||
submenu: this._buildVideoSubMenu(), | ||
}, | ||
{ | ||
label: 'Select size', | ||
submenu: this._buildSizeSubMenu(), | ||
}, | ||
{ | ||
label: 'Select quality', | ||
submenu: this._buildQualitySubMenu(), | ||
}, | ||
]); | ||
|
||
this.tray.setContextMenu(menu); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters