justadudewhohacks / face-api.js

JavaScript API for face detection and face recognition in the browser and nodejs with tensorflow.js
MIT License
16.71k stars 3.72k forks source link

Save models into db #501

Open sledgemhammer opened 4 years ago

sledgemhammer commented 4 years ago

Hi,

I would like to save detected faces into a database for later repeat recognition. I keep failing in doing this because of not understanding the data structure. (can be any db but preferable mysql)

Can you provide some clue how to store a detected face landmarks and gender into a db and how to get the data into face-api again after a reboot ? this way the system can be "self learning" recognising recurrent faces

neumartin commented 4 years ago

Hi, You can create a varchar field and save the descriptors. In this code I parse all the float values in a string and encoding in a json, later you can decode it.

            case "/GetTemplate":
                response.writeHead(200, {
                    "Content-Type": "text\plain"
                });

                var form = new formidable.IncomingForm();

                form.parse(request, async function (err, fields, files) {
                    let buff = new Buffer(fields.picture, 'base64');

                    const img = await commons.canvas.loadImage(buff);
                    let detections = await faceapi.detectSingleFace(img, faceDetectionOptions).withFaceLandmarks().withFaceDescriptor();

                    if (detections) {
                        let encoding = {};
                        let vecEnco = [];

                        for (var i = 0; i < detections.descriptor.length; i++)
                            vecEnco.push(detections.descriptor[i]);

                        encoding.Encoding = "[" + vecEnco.toString() + "]";

                        console.log("GetTemplate: template encontrado");
                        response.write(JSON.stringify(encoding));
                    }
                    else { 
                        console.log("GetTemplate: template NO encontrado");
                        response.write(null);
                    }

                    response.end();
                });
                break;
ra669180 commented 1 year ago

@neumartin Do you add this code in face-api.js or create it's own file and call in in face-api.js? Thanks

neumartin commented 1 year ago

@neumartin Do you add this code in face-api.js or create it's own file and call in in face-api.js? Thanks

I created my own js file, using face-api.js