Add real-time detection to camera stream #143

Merged
jgeorgi merged 5 commits from xps-video-stream into main 2024-03-24 15:51:10 +00:00
19 changed files with 372 additions and 14 deletions
Showing only changes of commit b09cff2a2b - Show all commits

View File

@@ -1,5 +1,3 @@
import * as tf from '@tensorflow/tfjs'
export default { export default {
methods: { methods: {
async openCamera(imContain) { async openCamera(imContain) {
@@ -38,24 +36,6 @@ export default {
const tempCtx = tempCVS.getContext('2d') const tempCtx = tempCVS.getContext('2d')
tempCtx.drawImage(vidViewer, 0, 0) tempCtx.drawImage(vidViewer, 0, 0)
this.getImage(tempCVS.toDataURL()) this.getImage(tempCVS.toDataURL())
},
async videoStream () {
const vidElement = this.$refs.vid_viewer
this.videoAvailable = false
const devicesList = await navigator.mediaDevices.enumerateDevices()
this.videoDeviceAvailable = devicesList.some( d => d.kind == "videoinput")
if (this.videoDeviceAvailable) {
var vidConstraint = {
//resizeWidth: 640,
//resizeHeight: 640,
facingMode: 'environment'
}
tf.data.webcam(vidElement, vidConstraint).then( webData => {
this.videoAvailable = true
this.cameraStream = vidElement.captureStream()
this.videoFrameDetect(webData)
})
}
} }
} }
} }

View File

@@ -1,7 +1,5 @@
import * as tf from '@tensorflow/tfjs' import * as tf from '@tensorflow/tfjs'
import { image } from '@tensorflow/tfjs'
import { f7 } from 'framework7-vue' import { f7 } from 'framework7-vue'
import { nextTick } from 'vue'
var model = null var model = null
@@ -11,7 +9,10 @@ export default {
model = await tf.loadGraphModel(weights) model = await tf.loadGraphModel(weights)
const [modelWidth, modelHeight] = model.inputs[0].shape.slice(1, 3) const [modelWidth, modelHeight] = model.inputs[0].shape.slice(1, 3)
const dummyT = tf.ones([1,modelWidth,modelHeight,3]) const dummyT = tf.ones([1,modelWidth,modelHeight,3])
model.predict(dummyT) //Run model once to preload weights for better response time /*****************
* Run model once to preload weights for better response time
*****************/
model.predict(dummyT)
return model return model
}, },
async localDetect(imageData) { async localDetect(imageData) {