data() { return { showContainer: true, // 显示 tracker: null, tipFlag: false, // 提示用户已经检测到 flag: false, // 判断是否已经拍照 context: null, // canvas上下文 removePhotoID: null, // 停止转换图片 scanTip: '人脸识别中...',// 提示文字 imgUrl: '', // base64格式图片 canvas: null } }, mounted() { this.playVideo() }, methods: { playVideo() { var video = document.getElementById('video'); this.canvas = document.getElementById('canvas'); this.context = this.canvas.getContext('2d'); this.tracker = new tracking.ObjectTracker('face'); this.tracker.setInitialScale(4); this.tracker.setStepSize(2); this.tracker.setEdgesDensity(0.1); tracking.track('#video', this.tracker, {camera: true}); this.tracker.on('track', this.handleTracked); }, handleTracked(event) { this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); if (event.data.length === 0) { this.scanTip = '未识别到人脸' } else { if (!this.tipFlag) { this.scanTip = '识别成功,正在拍照,请勿乱动~' } // 1秒后拍照,仅拍一次 if (!this.flag) { this.scanTip = '拍照中...' this.flag = true this.removePhotoID = setTimeout(() => { this.tackPhoto() this.tipFlag = true }, 2000 ) } event.data.forEach(this.plot); } }, plot(rect){ this.context.strokeStyle = '#eb652e'; this.context.strokeRect(rect.x, rect.y, rect.width, rect.height); this.context.font = '11px Helvetica'; this.context.fillStyle = "#fff"; this.context.fillText('x: ' + rect.x + 'px', rect.x + rect.width + 5, rect.y + 11); this.context.fillText('y: ' + rect.y + 'px', rect.x + rect.width + 5, rect.y + 22); }, // 拍照 tackPhoto() { this.context.drawImage(this.$refs.refVideo, 0, 0, 500, 500) // 保存为base64格式 this.imgUrl = this.saveAsPNG(this.$refs.refCanvas) var formData = new FormData(); formData.append("file", this.imgUrl); this.scanTip = '登录中,请稍等~' axios({ method: 'post', url: '/faceDiscern', data: formData, }).then(function (response) { alert(response.data.data); window.location.href="http://127.0.0.1:8081/home"; }).catch(function (error) { console.log(error); }); this.close() }, // 保存为png,base64格式图片 saveAsPNG(c) { return c.toDataURL('image/png', 0.3) }, // 关闭并清理资源 close() { this.flag = false this.tipFlag = false this.showContainer = false this.tracker && this.tracker.removeListener('track', this.handleTracked) && tracking.track('#video', this.tracker, {camera: false}); this.tracker = null this.context = null this.scanTip = '' clearTimeout(this.removePhotoID) } }人脸识别
在百度云注册一个应用 地址, 得到 API Key和 Secret Key,为了后续获取 token用。
百度云人脸识别的API非常友好,各种操作的 demo都写好了,拿过来简单改改就可以。
https://aip.baidubce.com/oauth/2.0/token? grant_type=client_credentials& client_id=【百度云应用的AK】& client_secret=【百度云应用的SK】接下来我们开始对图片进行比对,百度云提供了一个在线的人脸库,用户登录我们先在人脸库查询人像是否存在,存在则表示登录成功,如果不存在则注册到人脸库。每个图片有一个唯一标识face_token。
@Override public BaiDuFaceSearchResult faceSearch(String file) { try { byte[] decode = Base64.decode(Base64Util.base64Process(file)); String faceFile = Base64Util.encode(decode); Map<String, Object> map = new HashMap<>(); map.put("image", faceFile); map.put("liveness_control", "NORMAL"); map.put("group_id_list", "user"); map.put("image_type", "BASE64"); map.put("quality_control", "LOW"); String param = GsonUtils.toJson(map); String result = HttpUtil.post(faceSearchUrl, this.getAccessToken(), "application/json", param); BaiDuFaceSearchResult searchResult = JSONObject.parseObject(result, BaiDuFaceSearchResult.class); log.info(" faceSearch: {}", JSON.toJSONString(searchResult)); return searchResult; } catch (Exception e) { log.error("get faceSearch error {}", e.getStackTrace()); e.getStackTrace(); } return null; } @Override public BaiDuFaceDetectResult faceDetect(String file) { try { byte[] decode = Base64.decode(Base64Util.base64Process(file)); String faceFile = Base64Util.encode(decode); Map<String, Object> map = new HashMap<>(); map.put("image", faceFile); map.put("face_field", "faceshape,facetype"); map.put("image_type", "BASE64"); String param = GsonUtils.toJson(map); String result = HttpUtil.post(faceDetectUrl, this.getAccessToken(), "application/json", param); BaiDuFaceDetectResult detectResult = JSONObject.parseObject(result, BaiDuFaceDetectResult.class); log.info(" detectResult: {}", JSON.toJSONString(detectResult)); return detectResult; } catch (Exception e) { log.error("get faceDetect error {}", e.getStackTrace()); e.getStackTrace(); } return null; } @Override public BaiDuFaceAddResult addFace(String file, UserFaceInfo userFaceInfo) { try { byte[] decode = Base64.decode(Base64Util.base64Process(file)); String faceFile = Base64Util.encode(decode); Map<String, Object> map = new HashMap<>(); map.put("image", faceFile); map.put("group_id", "user"); map.put("user_id", userFaceInfo.getUserId()); map.put("user_info", JSON.toJSONString(userFaceInfo)); map.put("liveness_control", "NORMAL"); map.put("image_type", "BASE64"); map.put("quality_control", "LOW"); String param = GsonUtils.toJson(map); String result = HttpUtil.post(addfaceUrl, this.getAccessToken(), "application/json", param); BaiDuFaceAddResult addResult = JSONObject.parseObject(result, BaiDuFaceAddResult.class); log.info("addResult: {}", JSON.toJSONString(addResult)); return addResult; } catch (Exception e) { log.error("get addFace error {}", e.getStackTrace()); e.getStackTrace(); } return null; }项目是前后端分离的,但为了大家学习方便,我把人脸识别页面整合到了后端项目。