[英]How to implement face-api.js in expess REST API
I found face-api.js is useful in recognizing faces.我发现 face-api.js 在识别人脸方面很有用。 I need to compare two images on the server-side.
我需要在服务器端比较两个图像。 I am uploading one image and link to the original image or two images, query image, and original image.
我正在上传一张图片并链接到原始图片或两张图片、查询图片和原始图片。
I wrote a code which I extracted from browser side face recognition.我写了一个从浏览器端人脸识别中提取的代码。 While sending a POST I'm getting numerous errors like.
在发送 POST 时,我遇到了很多错误,例如。
This is my code app.js这是我的代码app.js
const express = require('express');
const faceapi = require('face-api.js');
const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
const multer = require('multer');
faceapi.env.monkeyPatch({ fetch: fetch });
// SET STORAGE
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/uploads/');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
const upload = multer({ storage: storage });
const app = express();
app.post('/', upload.single('image'), async (req, res) => {
console.log(req.file);
try {
const labeledFaceDescriptors = await loadLabeledImages();
console.log('labeledFaceDescriptors: ', labeledFaceDescriptors);
const faceMatcher = new faceapi.FaceMatcher(
labeledFaceDescriptors,
0.6
);
let image;
image = await faceapi.bufferToImage(req.file);
console.log(image);
const displaySize = { width: image.width, height: image.height };
const detections = await faceapi
.detectAllFaces(image)
.withFaceLandmarks()
.withFaceDescriptors();
const resizedDetections = faceapi.resizeResults(
detections,
displaySize
);
const results = resizedDetections.map((d) =>
faceMatcher.findBestMatch(d.descriptor)
);
results.forEach((result, i) => {
const box = resizedDetections[i].detection.box;
res.send(result.toString());
});
} catch (error) {
console.log(error);
res.send(error.message);
}
});
function loadLabeledImages() {
const labels = [
'Black Widow',
'Captain America',
'Captain Marvel',
'Hawkeye',
'Jim Rhodes',
'Thor',
'Tony Stark'
];
return Promise.all(
labels.map(async (label) => {
const descriptions = [];
for (let i = 1; i <= 2; i++) {
const img = await faceapi.fetchImage(
`https://raw.githubusercontent.com/WebDevSimplified/Face-Recognition-JavaScript/master/labeled_images/${label}/${i}.jpg`
);
const detections = await faceapi
.detectSingleFace(img)
.withFaceLandmarks()
.withFaceDescriptor();
descriptions.push(detections.descriptor);
}
return new faceapi.LabeledFaceDescriptors(label, descriptions);
})
);
}
app.listen(3000, () => {
console.log('app running on port: 3000');
});
How do I achieve this recognition in the server-side and pass the result to the client???我如何在服务器端实现这种识别并将结果传递给客户端???
I had the same problem and this worked with me我有同样的问题,这对我有用
const canvas = require("canvas");
const { Canvas, Image, ImageData } = canvas;
replace代替
const img = await faceapi.fetchImage(
`https://raw.githubusercontent.com/WebDevSimplified/Face-Recognition-JavaScript/master/labeled_images/${label}/${i}.jpg`
);
to至
const img = await canvas.loadImage(`http://localhost:3001/labeled_images/${label}/${i}.jpg`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.