0

I am developing an face detection application,for that I need to collect the users image for reference to detect them later.i have successfully uploaded the image in MySQL databse.now I need upload the image in public folder in react to detect the image in camera.i stuck in uploading image in react public folder.help me out get rid of this problem..

This is the React code where image to be detected in the imgUrl variable

detect = async () => {
    const videoTag = document.getElementById("videoTag");
    const canvas = document.getElementById("myCanvas");
    const displaySize = { width: videoTag.width, height: videoTag.height };
    faceapi.matchDimensions(canvas, displaySize);
    //setInterval starts here for continuous detection
    time = setInterval(async () => {
        let fullFaceDescriptions = await faceapi
            .detectAllFaces(videoTag)
            .withFaceLandmarks()
            .withFaceExpressions()
            .withFaceDescriptors();

        const value = fullFaceDescriptions.length;
        this.setState({ detection: value });
        fullFaceDescriptions = faceapi.resizeResults(
            fullFaceDescriptions,
            displaySize
        );
        canvas.getContext("2d").clearRect(0, 0, canvas.width, canvas.height);
        //Label Images
        var dummy = ["praveen", "vikranth", "Gokul", "Rahul"];
        const labels = nameArray1;
        // const labels = ["praveen", "vikranth", "Gokul", "Rahul"];
        if (no_of_times <= 0) {
            if (no_of_times === 0) {
                labeledFaceDescriptors = await Promise.all(
                    labels.map(async (label) => {
                        // fetch image data from urls and convert blob to HTMLImage element
                        const imgUrl = `/img/${label}.png`; // for testing purpose
                        // const imgUrl = testImage;
                        const img = await faceapi.fetchImage(imgUrl);
                        const fullFaceDescription = await faceapi
                            .detectSingleFace(img)
                            .withFaceLandmarks()
                            .withFaceExpressions()
                            .withFaceDescriptor();
                        if (!fullFaceDescription) {
                            throw new Error(`no faces detected for ${label}`);
                        }

                        const faceDescriptors = [fullFaceDescription.descriptor];
                        return new faceapi.LabeledFaceDescriptors(label, faceDescriptors);
                    })
                );
                // console.log(no_of_times);
            }
        }

        const maxDescriptorDistance = 0.7;
        no_of_times++;
        const faceMatcher = new faceapi.FaceMatcher(
            labeledFaceDescriptors,
            maxDescriptorDistance
        );

        const results = fullFaceDescriptions.map((fd) =>
            faceMatcher.findBestMatch(fd.descriptor)
        );
        result = [];
        results.forEach((bestMatch, i) => {
            const box = fullFaceDescriptions[i].detection.box;
            // console.log(box)
            const text = bestMatch.toString(); //this for basMatch name detection
            var str = "";
            //This is for removing names confidence to map value without duplicate
            var val = text.replace(/[0-9]/g, "");
            for (let i of val) {
                if (i !== " ") {
                    str += i;
                } else {
                    break;
                }
            }
            if (result.includes(str) === false) result.push(str);

            const drawBox = new faceapi.draw.DrawBox(box, { label: text });

            drawBox.draw(canvas);
            faceapi.draw.drawFaceExpressions(canvas, fullFaceDescriptions, 0.85);
        });

        for (let i = 0; i < fullFaceDescriptions.length; i++) {
            const result1 = fullFaceDescriptions[i].expressions.asSortedArray()[i];
            // console.log(result[i]);
            // console.log(result1.expression);
            this.test(result[i], result1.expression);
        }
    }, 100);

In the above code i am manually putting image in public folder,this need to be done dynamically when the user uploads image.

this is place i get the images in base64 from nodejs

axios.get("/image").then((res) => {
        testImage = res.data;
        // console.log("from image" + res.data);
        imgback = <img src={`data:image/jpeg;base64,${res.data}`} />;
    });

This is nodejs code for the get request from reactjs

app.get("/image", (req, res) => {

connection.query("SELECT * FROM images", (error, row, fields) => {
    if (!!error) {
        console.log("Error in the query");
    } else {
        console.log("successful query");

        var buffer = new Buffer(row[0].image, "binary");
        var bufferBase64 = buffer.toString("base64");

        res.send(bufferBase64);

    }
});

});

my goal is, in the imgUrl variable in react code i need to specify the image folder for that i need to dynamically add image in folder. Or is there is any other way to directly give image array in the imgUrl variable.please help me to sort out this problem.

0 Answers0