[英]How do I record AND download / upload the webcam stream on server (javascript) WHILE using the webcam input for facial recognition (opencv4nodejs)?
I had to write a program for facial recognition in JavaScript, for which I used the opencv4nodejs API, since there's NOT many working examples;我不得不在 JavaScript 中编写一个面部识别程序,为此我使用了 opencv4nodejs API,因为没有多少工作示例; Now I somehow want to record and save the stream (for saving on the client-side or uploading on the server) alongwith the audio.
现在我想以某种方式记录并保存 stream(用于保存在客户端或上传到服务器)以及音频。 This is where I am stuck.
这就是我卡住的地方。 Any help is appreciated.
任何帮助表示赞赏。 In simple words I need to use the Webcam input for multiple purposes, one for facial recognition and two to somehow save, latter is what i'm unable to do.
简而言之,我需要将网络摄像头输入用于多种用途,一种用于面部识别,另一种用于以某种方式保存,后者是我无法做到的。 Also in the worst case, If it's not possible Instead of recording and saving the webcam video I could also save the Complete Screen recording, Please Answer if there's a workaround to this.
同样在最坏的情况下,如果无法录制和保存网络摄像头视频,我还可以保存完整的屏幕录制,如果有解决方法,请回答。
Below is what i tried to do, But it doesn't work for obvious reasons.以下是我尝试做的事情,但由于明显的原因它不起作用。
$(document).ready(function () {
run1()
})
let chunks = []
// run1() for uploading model and for facecam
async function run1() {
const MODELS = "/models";
await faceapi.loadSsdMobilenetv1Model(MODELS)
await faceapi.loadFaceLandmarkModel(MODELS)
await faceapi.loadFaceRecognitionModel(MODELS)
var _stream
//Accessing the user webcam
const videoEl = document.getElementById('inputVideo')
navigator.mediaDevices.getUserMedia({
video: true,
audio: true
}).then(
(stream) => {
_stream = stream
recorder = new MediaRecorder(_stream);
recorder.ondataavailable = (e) => {
chunks.push(e.data);
console.log(chunks, i);
if (i == 20) makeLink(); //Trying to make Link from the blob for some i==20
};
videoEl.srcObject = stream
},
(err) => {
console.error(err)
}
)
}
// run2() main recognition code and training
async function run2() {
// wait for the results of mtcnn ,
const input = document.getElementById('inputVideo')
const mtcnnResults = await faceapi.ssdMobilenetv1(input)
// Detect All the faces in the webcam
const fullFaceDescriptions = await faceapi.detectAllFaces(input).withFaceLandmarks().withFaceDescriptors()
// Training the algorithm with given data of the Current Student
const labeledFaceDescriptors = await Promise.all(
CurrentStudent.map(
async function (label) {
// Training the Algorithm with the current students
for (let i = 1; i <= 10; i++) {
// console.log(label);
const imgUrl = `http://localhost:5500/StudentData/${label}/${i}.jpg`
const img = await faceapi.fetchImage(imgUrl)
// detect the face with the highest score in the image and compute it's landmarks and face descriptor
const fullFaceDescription = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor()
if (!fullFaceDescription) {
throw new Error(`no faces detected for ${label}`)
}
const faceDescriptors = [fullFaceDescription.descriptor]
return new faceapi.LabeledFaceDescriptors(label, faceDescriptors)
}
}
)
)
const maxDescriptorDistance = 0.65
const faceMatcher = new faceapi.FaceMatcher(labeledFaceDescriptors, maxDescriptorDistance)
const results = fullFaceDescriptions.map(fd => faceMatcher.findBestMatch(fd.descriptor))
i++;
}
// I somehow want this to work
function makeLink() {
alert("ML")
console.log("IN MAKE LINK");
let blob = new Blob(chunks, {
type: media.type
}),
url = URL.createObjectURL(blob),
li = document.createElement('li'),
mt = document.createElement(media.tag),
hf = document.createElement('a');
mt.controls = true;
mt.src = url;
hf.href = url;
hf.download = `${counter++}${media.ext}`;
hf.innerHTML = `donwload ${hf.download}`;
li.appendChild(mt);
li.appendChild(hf);
ul.appendChild(li);
}
// onPlay(video) function
async function onPlay(videoEl) {
run2()
setTimeout(() => onPlay(videoEl), 50)
}
I'm not familiar with JavaScript.我不熟悉 JavaScript。 But in general only one program may communicate with the camera.
但通常只有一个程序可以与相机通信。 You will probably need to write a server which will read the data from the camera.
您可能需要编写一个从相机读取数据的服务器。 Then the server will send the data to your facial recognition, recording, etc.
然后服务器会将数据发送到您的面部识别,录音等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.