face-api.js icon indicating copy to clipboard operation
face-api.js copied to clipboard

Save models into db

Open sledgemhammer opened this issue 5 years ago • 3 comments

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

sledgemhammer avatar Dec 10 '19 06:12 sledgemhammer

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;

neumartin avatar Dec 12 '19 01:12 neumartin

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

ra669180 avatar Feb 04 '23 18:02 ra669180

@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

neumartin avatar Feb 05 '23 04:02 neumartin