11<script setup>
2- import * as faceapi from " face-api.js " ;
2+ import * as faceapi from " @vladmandic/ face-api" ;
33import { onMounted , reactive , watch } from " vue" ;
44
55/** 属性状态 */
@@ -12,7 +12,6 @@ const state = reactive({
1212 netsOptions: {
1313 ssdMobilenetv1: undefined ,
1414 tinyFaceDetector: undefined ,
15- mtcnn: undefined ,
1615 },
1716 /** 目标图片数据匹配对象 */
1817 faceMatcher: {},
@@ -25,49 +24,57 @@ const state = reactive({
2524 {
2625 name: " 张伟" ,
2726 imgs: [
28- " /images/zw/face/zw01.png" ,
29- " /images/zw/face/zw02.png" ,
30- " /images/zw/face/zw03.png" ,
31- " /images/zw/face/zw04.png" ,
27+ ` /images/zw/face/zw01.png` ,
28+ ` /images/zw/face/zw02.png` ,
29+ ` /images/zw/face/zw03.png` ,
30+ ` /images/zw/face/zw04.png` ,
3231 ],
3332 },
3433 {
3534 name: " 曾小贤" ,
3635 imgs: [
37- " /images/zxx/face/zxx01.png" ,
38- " /images/zxx/face/zxx02.png" ,
39- " /images/zxx/face/zxx03.png" ,
40- " /images/zxx/face/zxx04.png" ,
36+ ` /images/zxx/face/zxx01.png` ,
37+ ` /images/zxx/face/zxx02.png` ,
38+ ` /images/zxx/face/zxx03.png` ,
39+ ` /images/zxx/face/zxx04.png` ,
4140 ],
4241 },
4342 ],
4443});
4544
4645/** 初始化模型加载 */
4746async function fnLoadModel () {
47+ // 模型文件访问路径
48+ const modelsPath = ` /models` ;
4849 // 面部轮廓模型
49- await faceapi .loadFaceLandmarkModel ( " /models " );
50+ await faceapi .nets . faceLandmark68Net . load (modelsPath );
5051 // 面部识别模型
51- await faceapi .loadFaceRecognitionModel ( " /models " );
52+ await faceapi .nets . faceRecognitionNet . load (modelsPath );
5253
5354 // 模型参数-ssdMobilenetv1
54- await faceapi .nets .ssdMobilenetv1 .loadFromUri ( " /models " );
55+ await faceapi .nets .ssdMobilenetv1 .load (modelsPath );
5556 state .netsOptions .ssdMobilenetv1 = new faceapi.SsdMobilenetv1Options ({
56- minConfidence: 0.6 , // 0.1 ~ 0.9
57+ minConfidence: 0.5 , // 0 ~ 1
58+ maxResults: 50 , // 0 ~ 100
5759 });
5860 // 模型参数-tinyFaceDetector
59- await faceapi .nets .tinyFaceDetector .loadFromUri ( " /models " );
61+ await faceapi .nets .tinyFaceDetector .load (modelsPath );
6062 state .netsOptions .tinyFaceDetector = new faceapi.TinyFaceDetectorOptions ({
61- inputSize: 512 , // 160 224 320 416 512 608
62- scoreThreshold: 0.5 , // 0.1 ~ 0.9
63- });
64- // 模型参数-mtcnn 已弃用,将很快被删除
65- await faceapi .nets .mtcnn .loadFromUri (" /models" );
66- state .netsOptions .mtcnn = new faceapi.MtcnnOptions ({
67- minFaceSize: 20 , // 1 ~ 50
68- scaleFactor: 0.709 , // 0.1 ~ 0.9
63+ inputSize: 416 , // 160 224 320 416 512 608
64+ scoreThreshold: 0.5 , // 0 ~ 1
6965 });
7066
67+ // 输出库版本
68+ console .log (
69+ ` FaceAPI Version: ${
70+ faceapi? .version || " (not loaded)"
71+ } \nTensorFlow/ JS Version: ${
72+ faceapi .tf ? .version_core || " (not loaded)"
73+ } \nBackend: ${
74+ faceapi .tf ? .getBackend () || " (not loaded)"
75+ } \nModels loaded: ${faceapi .tf .engine ().state .numTensors } tensors`
76+ );
77+
7178 // 节点元素
7279 state.targetImgEl = document.getElementById("page_draw-img-target");
7380 state.targetCanvasEl = document.getElementById("page_draw-canvas-target");
@@ -190,7 +197,6 @@ onMounted(() => {
190197 <select v-model="state.netsType">
191198 <option value="ssdMobilenetv1">SSD Mobilenet V1</option>
192199 <option value="tinyFaceDetector">Tiny Face Detector</option>
193- <option value =" mtcnn" >MTCNN</option >
194200 </select>
195201 </div>
196202 <div style="color: #f44336">
0 commit comments