本文例子主要寫的是如何獲取兩張臉的類似度, 其他例子參考官網, 博主才疏學淺, 若是有錯誤, 麻煩大佬們多多指點.javascript
face-api.js 傳送門: github.com/justadudewh…java
代碼傳送門, 安裝即用, 順手點star, 一天好心情, 例子基於face-api.js
github.com/TheKiteRunn…node
本人csdn地址: blog.csdn.net/c_kite/arti…linux
能夠clone下個人例子代碼, 比對參考圖片是images文件夾下的reference.jpggit
獲取face-api.js: npm i face-api.js
github
若是想要在node端運行, 我做爲一個windows用戶遇到了n多坑, 下面貼上一些解決連接:npm
- node-gyp: windows用戶安裝辦法 github.com/nodejs/node…
- TensorFlow.js Node.js windows故障排查 github.com/tensorflow/…
- 若是遇到 Downloading libtensorflow events.js:173 throw er; // Unhandled 'error' event ^ Error: connect ETIMEDOUT 172.217.160.80:443 網絡問題, 須要合理的工具來解決這個問題, 代碼裏的libtensorflow地址: const BASE_URI = 'storage.googleapis.com/tensorflow/…'; const CPU_DARWIN = 'cpu-darwin-x86_64-1.12.0.tar.gz'; const CPU_LINUX = 'cpu-linux-x86_64-1.12.0.tar.gz'; const GPU_LINUX = 'gpu-linux-x86_64-1.12.0.tar.gz'; const CPU_WINDOWS = 'cpu-windows-x86_64-1.12.0.zip'; const GPU_WINDOWS = 'gpu-windows-x86_64-1.12.0.zip';
- 安裝過程若是遇到"node-pre-gyp install --fallback-to-build", 那是canvas包執行的, 會下載一個文件: github.com/node-gfx/no… 長時間沒反應的話估計是這個包卡主了, 須要合理上網
安裝完包以後, 須要根據你所須要的功能加載適當的model, 人臉檢測一個model, 表情識別一個model, 人臉識別一個model, 若是你沒有提早加載model直接使用api的話會有下面示例代碼相似提示canvas
Uncaught (in promise) Error: FaceLandmark68Net - load model before inference
// 這就意味着沒有加載FaceLandmark model
複製代碼
那麼如何加載model呢, 以下代碼windows
await faceapi.loadTinyFaceDetectorModel('model地址')
// 等價於
await faceapi.nets.tinyFaceDetector.load('model地址')
複製代碼
所有的model能夠在倉庫找到: github.com/justadudewh… 你能夠把他們放到本身的靜態服務器裏api
檢測視頻或者圖片中所有臉
const detections = await faceapi.detectAllFaces(input)
複製代碼
檢測圖像中具備最高置信度分數的面部
const detection = await faceapi.detectSingleFace(input)
複製代碼
默認狀況下,detectAllFaces
和detectSingleFace
使用SSD Mobilenet V1
人臉檢測器。您能夠經過傳遞相應的選項對象來指定面部檢測器
// 我測試的時候使用的是`TinyFaceDetector`, 所以能夠
const detections = await faceapi.detectAllFaces(input, new faceapi.TinyFaceDetectorOptions())
複製代碼
在面部檢測以後,咱們還能夠預測每一個檢測到的面部的面部標誌,以下所示:
const detectionsWithLandmarks = await faceapi.detectAllFaces(input).withFaceLandmarks()
// 或者
const detectionWithLandmarks = await faceapi.detectSingleFace(input).withFaceLandmarks()
複製代碼
Note: 必定要按照博文所寫的順序來調用函數
在面部檢測和麪部標誌預測以後,能夠計算每一個面部的面部描述符:
const results = await faceapi.detectAllFaces(input).withFaceLandmarks().withFaceDescriptors()
// 或者
const result = await faceapi.detectSingleFace(input).withFaceLandmarks().withFaceDescriptor()
複製代碼
要執行面部識別,可使用faceapi.FaceMatcher
將參考面部描述符與查詢面部描述符進行比較
const imgEle = document.createElement('img');
imgEle.src = '/reference.jpg'
const reference = await faceapi.detectSingleFace(imgEle, options).withFaceLandmarks().withFaceDescriptor()
const result = await faceapi.detectSingleFace(videoEl, options).withFaceLandmarks().withFaceDescriptor()
if (result) {
const faceMatcher = new faceapi.FaceMatcher(result)
drawLandmarks(videoEl, $('#overlay').get(0), [result], withBoxes)
if (reference) {
const bestMatch = faceMatcher.findBestMatch(reference.descriptor)
console.log(bestMatch)
}
}
複製代碼
此處主要經過臉部特徵向量來計算euclidean distance(歐氏距離), 所以如預覽圖所示_distance越小, 說明兩張臉越匹配, 這個閾值能夠設置爲0.4, 0.4如下爲匹配成功, 以上則失敗.
所以若是你有臉部特層向量, 你也能夠經過這個face-api.js
api來計算歐式距離
const dist = faceapi.euclideanDistance([0, 0], [0, 10])
console.log(dist) // 10
複製代碼
tips: euclidean distance(歐氏距離)定義: 是一個一般採用的距離定義,它是在m維空間中兩個點之間的真實距離.在二維空間中的歐氏距離就是兩點之間的直線段距離. 二維空間的歐氏距離公式 d = sqrt(( x1-x2)^2 + (y1-y2)^2 )
三維空間的歐氏距離公式d = sqrt( (x1-x2)^2+(y1-y2)^2+(z1-z2)^2 )