如何使用网络摄像头输入进行面部识别opencv4nodejs,如何在服务器javascript上记录和下载/上传网络摄像头流?

问题描述

我必须编写一个用JavaScript进行面部识别的程序,为此我使用了opencv4nodejs API,因为这里没有很多可用的示例;现在,我想以某种方式记录和保存流(用于保存在客户端或在服务器上上传)以及音频。这就是我被困住的地方。任何帮助表示赞赏。 简而言之,我需要将网络摄像头输入用于多种用途,一种用于面部识别,另一种用于某种程度上的保存,这是我无法做到的。同样在最坏的情况下,如果不可能,除了录制和保存网络摄像头视频外,我还可以保存“完整屏幕”录制,如果有解决办法,请回答。

下面是我尝试做的事情,但是由于明显的原因,它不起作用。


$(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)
}

解决方法

我对JavaScript不熟悉。但是通常,只有一个程序可以与相机通信。您可能需要编写一个服务器,该服务器将从摄像机读取数据。然后服务器会将数据发送到您的面部识别,记录等。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...