简体   繁体   English

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

[英]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.

相关问题 如何使用javascript和python3上传网络摄像头视频流并在它们到达服务器后立即发送字节? - How to upload a webcam stream of video and send bytes as soon as they arrive to the server using javascript and python3? 当我的 python 脚本在服务器上运行时,如何访问我的本地网络摄像头? (实时人脸识别) - How do I access my local webcam while my python script is running on a server? (Face-Recognition in real time) 如何使用javascript通过网络摄像头录制视频 - how to record the video through webcam using javascript 如何使用 JavaScript ajax 将网络摄像头图像上传到 Django 站点? - How do I upload a webcam image using JavaScript ajax to a Django site? 仅使用javascript / html5获取麦克风/网络摄像头记录流 - Get mic/webcam record stream using javascript/html5 only 使用 JavaScript 录制网络摄像头视频 - Record WebCam video using JavaScript 如何将服务器网络摄像头流式传输到servlet - How to stream server webcam to servlet 在javascript和webgl中对摄像头流进行实时运动对象检测/识别 - Real time motion object detection/recognition on webcam stream in javascript and webgl Javascript 网络摄像头捕获并使用 PHP 上传到服务器 - Javascript webcam capture and upload to server with PHP 如何通过 Javascript 在 PythonAnywhere 上访问 OpenCV 中的网络摄像头? - How to access webcam in OpenCV on PythonAnywhere through Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM