face.service.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const psqlDB = require("./psql.service");
  2. class FaceService{
  3. async getSameReportList(feat68,limit){
  4. limit = limit || 10;
  5. // cosine_similarity
  6. let sql = `
  7. select "objectId" as id,"photo","createdAt", array_agg(item)::vector(128) <=> $1::vector(128) as cos
  8. from (
  9. SELECT *, jsonb_array_elements("feat68")::float AS item from (
  10. SELECT "objectId","photo","feat68","createdAt"
  11. FROM "FaceReport"
  12. WHERE "feat68" is not null
  13. ORDER BY "createdAt" DESC
  14. limit ${limit}
  15. ) as rt1
  16. ) as rt
  17. GROUP BY "objectId",rt."photo","createdAt"
  18. `
  19. let params = [feat68]
  20. let result = await psqlDB.any(sql,params)
  21. // 人物不同 0.08 0.09 0.06 0.059
  22. // 人物不同 长相像 0.046 0.045 0.043 0.039
  23. // 人物相同 仰角/光照有变化 0.03846
  24. // 人物相同 > 0.0214 0.0188 0.00218 0.038 0.023
  25. result = result.filter(item=>item.cos<0.0385 && item.id!=report.id)
  26. result.sort((a,b)=>a.createdAt-b.createdAt)
  27. if(result?.length>0){
  28. console.log(result)
  29. }
  30. return result
  31. }
  32. async matchDistance(img1,img2){
  33. let referenceImage
  34. let queryImage
  35. try{
  36. referenceImage = await canvas.loadImage(img1)
  37. queryImage = await canvas.loadImage(img2)
  38. }catch(err){
  39. console.error(err)
  40. response.status(500);
  41. response.json({ code: 500, mess: "图片地址有误无法加载" });
  42. }
  43. if(!(referenceImage&&queryImage)){
  44. response.status(500);
  45. response.json({ code: 500, mess: "图片不够清晰请重新上传" });
  46. }
  47. let resultsRef = await faceapi.detectAllFaces(referenceImage, faceDetectionOptions)
  48. .withFaceLandmarks()
  49. .withFaceDescriptors()
  50. let resultsQuery = await faceapi.detectAllFaces(queryImage, faceDetectionOptions)
  51. .withFaceLandmarks()
  52. .withFaceDescriptors()
  53. if(!resultsRef || !resultsQuery){
  54. response.json({
  55. code:200,
  56. data:{
  57. label:`照片未上传或不清晰,请重新上传`,
  58. distance:1,
  59. score:0
  60. }
  61. })
  62. return
  63. }
  64. if(resultsRef.length==0 || resultsQuery.length==0){
  65. // openapi.goWrong(response,"照片未上传或不清晰,请重新上传");
  66. let imgstr = ""
  67. if(resultsRef.length==0){
  68. imgstr += "<1>"
  69. }
  70. if(resultsQuery.length==0){
  71. imgstr += "<2>"
  72. }
  73. response.json({
  74. code:200,
  75. data:{
  76. label:`图${imgstr}无法识别人脸`,
  77. distance:1,
  78. score:0
  79. }
  80. })
  81. return
  82. }
  83. let faceMatcher = new faceapi.FaceMatcher(resultsRef)
  84. let labels = faceMatcher.labeledDescriptors
  85. .map(ld => ld.label)
  86. let bestMatch;
  87. let label="",distance=1,score=0;
  88. resultsQuery.forEach(res => {
  89. bestMatch = faceMatcher.findBestMatch(res.descriptor)
  90. if(bestMatch._label.startsWith("person")){
  91. label = bestMatch._label;
  92. distance = bestMatch._distance;
  93. score = 1-distance;
  94. };
  95. // if(bestMatch._distance > score){
  96. // score=bestMatch._distance
  97. // };
  98. })
  99. }
  100. }
  101. module.exports.FaceService = FaceService