faceReco.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <template>
  2. <div class="container">
  3. <div v-if="step === 1">
  4. <div class="faceWindowWrapper">
  5. <div class="faceWindow" :style="{ borderColor: tagColor }">
  6. <video id="page_draw-video" muted playsinline></video>
  7. </div>
  8. <div class="changeButton">
  9. <a-button type="primary" shape="circle"
  10. @click="state.constraints.video.facingMode === 'user' ? state.constraints.video.facingMode = 'environment' : state.constraints.video.facingMode = 'user'">
  11. <icon-sync />
  12. </a-button>
  13. </div>
  14. </div>
  15. <div class="faceInfo">
  16. <a-tag size="large" :color="tagColor">
  17. {{ tagInfo }}
  18. </a-tag>
  19. </div>
  20. <div class="countTime" v-if="state.isRecording">
  21. <span>还需等待:</span>
  22. <span class="time">{{ recTime }}</span>
  23. <span>s</span>
  24. </div>
  25. </div>
  26. <div v-if="step === 2" class="spin">
  27. <a-spin dot tip="人脸信息上传中,请稍候..." />
  28. </div>
  29. <div v-if="step === 3">
  30. <a-result status="success" title="采集完成">
  31. <template #extra>
  32. <a-space>
  33. <a-button type='primary' @click="$router.go(0)">返回</a-button>
  34. </a-space>
  35. </template>
  36. </a-result>
  37. </div>
  38. <div v-if="step === 4">
  39. <a-result status="error" title="采集失败">
  40. <template #extra>
  41. <a-space>
  42. <a-button type='primary' @click="$router.go(0)">返回重试</a-button>
  43. </a-space>
  44. </template>
  45. </a-result>
  46. </div>
  47. <div class="userInfo">
  48. <div class="left">
  49. <a-avatar>
  50. <img :alt="props.userInfo.name ?? props.userInfo.student_num"
  51. :src="props.userInfo.user_avatar ?? 'https://lepao-cloud.xxoo365.top/view.php/25aa126dc406974ff3579a99a2c6501a.png'" />
  52. </a-avatar>
  53. </div>
  54. <div class="right">
  55. <div class="name">{{ props.userInfo.name }}</div>
  56. <div class="sub">{{ props.userInfo.academy_name }}</div>
  57. </div>
  58. </div>
  59. </div>
  60. <div class="page" style="display: none">
  61. <div class="page_draw" v-show="!state.netsLoadModel">
  62. <img id="page_draw-img-target" :src="props.userInfo.face_img" />
  63. <canvas id="page_draw-canvas-target"></canvas>
  64. <canvas id="page_draw-video-canvas"></canvas>
  65. </div>
  66. </div>
  67. <div class="loading" v-if="state.netsLoadModel">
  68. <div class="loadingTip">
  69. <icon-loading :size="32" />
  70. <div class="text">努力加载中</div>
  71. </div>
  72. </div>
  73. </template>
  74. <script setup>
  75. import { Message } from "@arco-design/web-vue"
  76. import * as faceapi from "@vladmandic/face-api"
  77. import axios from 'axios'
  78. import { ref, onMounted, onUnmounted, reactive, watch } from "vue"
  79. const tagInfo = ref('请将面部完全放置在取景框中央')
  80. const tagColor = ref('blue')
  81. const step = ref(1)
  82. const recTime = ref(10)
  83. let isSuccessRecording = false
  84. let countdownTimer = null
  85. const props = defineProps({
  86. userInfo: {
  87. type: Object,
  88. required: true
  89. }
  90. })
  91. /**属性状态 */
  92. const state = reactive({
  93. netsLoadModel: true,
  94. netsType: "tinyFaceDetector",
  95. netsOptions: {
  96. ssdMobilenetv1: undefined,
  97. tinyFaceDetector: undefined,
  98. },
  99. faceMatcher: null,
  100. targetImgEl: null,
  101. targetCanvasEl: null,
  102. discernVideoEl: null,
  103. discernCanvasEl: null,
  104. timer: 0,
  105. constraints: {
  106. audio: false,
  107. video: {
  108. width: { min: 320, ideal: 720, max: 1280 },
  109. height: { min: 200, ideal: 480, max: 720 },
  110. frameRate: { min: 7, ideal: 15, max: 30 },
  111. facingMode: "user",
  112. },
  113. },
  114. stream: null,
  115. /** 录制相关 */
  116. recorder: null,
  117. recordingChunks: [],
  118. recordingTimer: null,
  119. isRecording: false,
  120. lastBlob: null,
  121. failCount: 0,
  122. uploading: false, // 上传状态
  123. })
  124. /**启动录制 */
  125. function startRecording() {
  126. if (state.isRecording || !state.stream) return
  127. state.recordingChunks = []
  128. state.recorder = new MediaRecorder(state.stream, { mimeType: "video/webm" })
  129. state.recorder.ondataavailable = (e) => {
  130. if (e.data.size > 0) {
  131. state.recordingChunks.push(e.data)
  132. }
  133. }
  134. // 停止录制时
  135. state.recorder.onstop = () => {
  136. const blob = new Blob(state.recordingChunks, { type: "video/webm" })
  137. if (isSuccessRecording && blob) {
  138. uploadVideo(blob)
  139. }
  140. }
  141. state.recorder.start()
  142. state.isRecording = true
  143. recTime.value = 10 // ✅ 开始时重置倒计时
  144. // 倒计时逻辑
  145. countdownTimer = setInterval(() => {
  146. if (recTime.value > 0) {
  147. recTime.value -= 1
  148. }
  149. }, 1000)
  150. // 10秒后自动停止并标记为成功
  151. state.recordingTimer = setTimeout(() => {
  152. isSuccessRecording = true
  153. stopRecording() // ✅ 成功录制
  154. fnClose()
  155. step.value = 3
  156. }, recTime.value * 1000)
  157. }
  158. /**结束录制 */
  159. function stopRecording() {
  160. if (state.recorder && state.isRecording) {
  161. state.recorder.stop()
  162. state.isRecording = false
  163. clearTimeout(state.recordingTimer)
  164. state.recordingTimer = null
  165. clearInterval(countdownTimer)
  166. countdownTimer = null
  167. recTime.value = 10
  168. }
  169. }
  170. /**上传视频到服务器 */
  171. async function uploadVideo(blob) {
  172. try {
  173. step.value = 2
  174. const formData = new FormData()
  175. formData.append("student_num", props.userInfo.student_num)
  176. formData.append("key", props.userInfo.key)
  177. formData.append("upload", blob, "face_record.webm")
  178. const url = import.meta.env.VITE_APP_API_BASE_URL + '/UploadFaceVideo'
  179. const res = await axios.post(url, formData, {
  180. headers: { "Content-Type": "multipart/form-data" }
  181. })
  182. if (!res || !res.data || res.data.code !== 0) {
  183. step.value = 4
  184. throw new Error(res?.data?.msg ?? '上传失败,请稍后再试')
  185. }
  186. Message.success("人脸数据上传成功")
  187. step.value = 3
  188. } catch (err) {
  189. step.value = 4
  190. Message.error(err.message ?? "人脸数据上传失败")
  191. console.error(err)
  192. }
  193. }
  194. /**重新开始录制 */
  195. function restartRecording() {
  196. stopRecording()
  197. // 小延迟确保上一次 recorder 彻底结束
  198. setTimeout(() => {
  199. startRecording()
  200. }, 200)
  201. }
  202. /**初始化模型加载 */
  203. async function fnLoadModel() {
  204. const modelsPath = `/models`
  205. await faceapi.nets.faceLandmark68Net.load(modelsPath)
  206. await faceapi.nets.faceRecognitionNet.load(modelsPath)
  207. await faceapi.nets.tinyFaceDetector.load(modelsPath)
  208. state.netsOptions.tinyFaceDetector = new faceapi.TinyFaceDetectorOptions({
  209. inputSize: 416,
  210. scoreThreshold: 0.5,
  211. })
  212. state.targetImgEl = document.getElementById("page_draw-img-target")
  213. state.discernVideoEl = document.getElementById("page_draw-video")
  214. state.discernCanvasEl = document.getElementById("page_draw-video-canvas")
  215. state.netsLoadModel = false
  216. }
  217. /**根据模型参数识别绘制--目标图 */
  218. async function fnRedrawTarget() {
  219. const detect = await faceapi
  220. .detectAllFaces(state.targetImgEl, state.netsOptions[state.netsType])
  221. .withFaceLandmarks()
  222. .withFaceDescriptors()
  223. if (!detect || detect.length === 0) {
  224. state.faceMatcher = null
  225. return
  226. }
  227. state.faceMatcher = new faceapi.FaceMatcher(detect)
  228. }
  229. /**根据模型参数识别绘制 */
  230. async function fnRedrawDiscern() {
  231. if (!state.faceMatcher) return
  232. if (state.discernVideoEl.paused) {
  233. clearTimeout(state.timer)
  234. state.timer = 0
  235. return
  236. }
  237. const detect = await faceapi
  238. .detectAllFaces(state.discernVideoEl, state.netsOptions[state.netsType])
  239. .withFaceLandmarks()
  240. .withFaceDescriptors()
  241. if (!detect || detect.length === 0) {
  242. state.failCount++
  243. if (state.failCount >= 3) {
  244. tagColor.value = 'blue'
  245. tagInfo.value = '请将面部完全放置在取景框中央'
  246. if (state.isRecording) stopRecording()
  247. }
  248. // 延迟下一次识别,避免直接递归造成栈/CPU 问题
  249. clearTimeout(state.timer)
  250. state.timer = setTimeout(() => fnRedrawDiscern(), 300)
  251. return
  252. }
  253. const dims = faceapi.matchDimensions(
  254. state.discernCanvasEl,
  255. state.discernVideoEl,
  256. true
  257. )
  258. const result = faceapi.resizeResults(detect, dims)
  259. // 若检测到多张脸,逐一判断(若任一匹配成功则开始录制)
  260. for (const item of result) {
  261. const descriptor = item.descriptor
  262. const best = state.faceMatcher.findBestMatch(descriptor)
  263. if (best) {
  264. if (best._distance <= 0.6) {
  265. state.failCount = 0
  266. tagColor.value = 'green'
  267. tagInfo.value = '录制中,请确保面部不要离开取景框'
  268. if (!state.isRecording) startRecording()
  269. // 找到一个合格的人脸就可以停止遍历
  270. break
  271. } else {
  272. state.failCount = 3
  273. tagColor.value = 'red'
  274. tagInfo.value = `人脸匹配失败,请确保为 ${props?.userInfo?.name} 本人操作`
  275. if (state.isRecording) restartRecording()
  276. }
  277. }
  278. }
  279. // 继续调度下一次识别(0ms 相当于下一事件循环)
  280. clearTimeout(state.timer)
  281. state.timer = setTimeout(() => fnRedrawDiscern(), 0)
  282. }
  283. /**启动摄像头视频媒体 */
  284. async function fnOpen() {
  285. if (state.stream !== null) return
  286. try {
  287. state.stream = {}
  288. const stream = await navigator.mediaDevices.getUserMedia(state.constraints)
  289. state.stream = stream
  290. state.discernVideoEl.srcObject = stream
  291. await state.discernVideoEl.play()
  292. state.discernCanvasEl.width = state.discernVideoEl.videoWidth
  293. state.discernCanvasEl.height = state.discernVideoEl.videoHeight
  294. // 稍作延迟再开始识别
  295. setTimeout(() => fnRedrawDiscern(), 300)
  296. } catch (error) {
  297. state.stream = null
  298. Message.error("视频媒体流获取错误: " + error)
  299. }
  300. }
  301. /**结束摄像头视频媒体 */
  302. function fnClose() {
  303. if (state.stream === null) return
  304. try {
  305. state.discernVideoEl.pause()
  306. state.discernVideoEl.srcObject = null
  307. state.stream.getTracks().forEach((track) => track.stop())
  308. } catch (err) {
  309. console.warn("关闭流时出错:", err)
  310. } finally {
  311. state.stream = null
  312. clearTimeout(state.timer)
  313. state.timer = 0
  314. stopRecording()
  315. }
  316. }
  317. watch(
  318. () => state.constraints.video.facingMode,
  319. () => {
  320. if (state.stream !== null) {
  321. fnClose()
  322. fnOpen()
  323. } else {
  324. fnClose()
  325. }
  326. }
  327. )
  328. onMounted(() => {
  329. fnLoadModel().then(() => {
  330. fnRedrawTarget()
  331. fnOpen()
  332. })
  333. })
  334. onUnmounted(() => {
  335. fnClose()
  336. })
  337. </script>
  338. <style lang="less" scoped>
  339. .container {
  340. display: flex;
  341. flex-direction: column;
  342. justify-content: center;
  343. align-items: center;
  344. }
  345. .faceWindowWrapper {
  346. position: relative;
  347. width: 270px;
  348. height: 270px;
  349. }
  350. /* 圆形视频框 */
  351. .faceWindow {
  352. width: 100%;
  353. height: 100%;
  354. border-radius: 50%;
  355. border: 5px solid;
  356. overflow: hidden;
  357. background: #fff;
  358. position: relative;
  359. }
  360. /* 按钮 */
  361. .changeButton {
  362. position: absolute;
  363. right: -10px;
  364. /* 超出圆形框右边 */
  365. bottom: -10px;
  366. /* 超出圆形框底边 */
  367. }
  368. .faceInfo {
  369. margin-top: 20px;
  370. }
  371. .countTime {
  372. margin-top: 10px;
  373. font-size: 1.1em;
  374. font-family: AlibabaSans, -apple-system, BlinkMacSystemFont;
  375. .time {
  376. font-size: 1.3em;
  377. }
  378. }
  379. .faceWindow video {
  380. width: 100%;
  381. height: 100%;
  382. object-fit: cover;
  383. /* 保持视频比例并填满圆形 */
  384. transform: scaleX(-1);
  385. }
  386. .spin {
  387. display: flex;
  388. align-items: center;
  389. min-height: 270px;
  390. }
  391. .userInfo {
  392. display: flex;
  393. align-items: center;
  394. text-align: left;
  395. height: 60px;
  396. min-width: 300px;
  397. margin-top: 20px;
  398. border-radius: 10px;
  399. background-color: rgba(190, 218, 255, 0.5);
  400. padding: 10px;
  401. .right {
  402. margin-left: 10px;
  403. .name {
  404. font-size: 1.2em;
  405. font-weight: bold;
  406. }
  407. }
  408. }
  409. .loading {
  410. position: fixed;
  411. top: 0;
  412. left: 0;
  413. height: 100%;
  414. width: 100%;
  415. background-color: rgba(0, 0, 0, 0.2);
  416. z-index: 9999;
  417. border-radius: 10px;
  418. .loadingTip {
  419. position: absolute;
  420. top: 50%;
  421. left: 50%;
  422. transform: translate(-50%, -50%);
  423. color: #3370FF;
  424. .text {
  425. font-family: 'Alimama ShuHeiTi';
  426. font-size: 1.2em;
  427. }
  428. }
  429. }
  430. </style>