0

我不得不用 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)
}
4

1 回答 1

0

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

于 2020-08-18T10:15:11.937 回答