diff --git a/src/pages/detect.vue b/src/pages/detect.vue index a0f7adb..5bce9ad 100644 --- a/src/pages/detect.vue +++ b/src/pages/detect.vue @@ -7,7 +7,11 @@
- + +
+ + Capture +
- + @@ -86,7 +90,7 @@ - + @@ -321,7 +325,10 @@ uploadUid: null, uploadDirty: false, modelLocation: '', - modelLoading: false + modelLoading: false, + videoDeviceAvailable: false, + videoAvailable: false, + cameraStream: null } }, setup() { @@ -480,18 +487,36 @@ this.detectorLabels.forEach( s => s.detect = false ) } }, - selectImage (mode) { + async selectImage (mode) { this.imageLoadMode = mode - if (mode == "camera") { - this.$refs.image_chooser.setAttribute("capture","environment") - } else { - this.$refs.image_chooser.removeAttribute("capture") - } if (this.isCordova && mode == "camera") { navigator.camera.getPicture(this.getImage, this.onFail, { quality: 50, destinationType: Camera.DestinationType.DATA_URL, correctOrientation: true }); - } else { - var loadResult = this.$refs.image_chooser.click() + return } + if (mode == "camera") { + const devicesList = await navigator.mediaDevices.enumerateDevices() + this.videoDeviceAvailable = devicesList.some( d => d.kind == "videoinput") + if (this.videoDeviceAvailable) { + navigator.mediaDevices.getUserMedia({video: true}) + var vidConstraint = { + video: { + width: { + ideal: 1920 + }, + height: { + ideal: 1080 + }, + facingMode: 'environment' + }, + audio: false + } + const stream = await navigator.mediaDevices.getUserMedia(vidConstraint); + this.videoAvailable = true + this.cameraStream = stream + return + } + } + var loadResult = this.$refs.image_chooser.click() }, onFail (message) { alert(`Camera fail: ${message}`) @@ -539,7 +564,11 @@ }, getImage (searchImage) { let loadImage = new Promise(resolve => { - if (this.isCordova && this.imageLoadMode == "camera") { + if (this.videoAvailable) { + this.cameraStream.getTracks().forEach( t => t.stop()) + this.videoAvailable = false + resolve(searchImage) + } else if (this.isCordova && this.imageLoadMode == "camera") { resolve('data:image/jpg;base64,' + searchImage) } else { const searchImage = this.$refs.image_chooser.files[0] @@ -573,9 +602,18 @@ f7.dialog.alert(`Error loading image: ${e.message}`) }) }, - videoStream() { + async videoStream() { //TODO - return null + }, + captureVidFrame() { + const vidViewer = this.$refs.vid_viewer + vidViewer.pause() + let tempCVS = document.createElement('canvas') + tempCVS.height = vidViewer.videoHeight || parseInt(vidViewer.style.height) + tempCVS.width = vidViewer.videoWidth || parseInt(vidViewer.style.width) + const tempCtx = tempCVS.getContext('2d') + tempCtx.drawImage(vidViewer, 0, 0) + this.getImage(tempCVS.toDataURL()) }, async submitData () { var uploadData = this.showResults