当前位置: 首页 > news >正文

Spring Boot集成腾讯云人脸识别实现智能小区门禁系统

1. 项目概述

本文将介绍如何基于Spring Boot集成腾讯云人员库实现人脸识别门禁系统。系统功能包括:

  • 前端拍照或上传人脸照片
  • 调用腾讯云AI接口进行人脸识别
  • 验证人员身份及小区归属
  • 生成出入记录并保存人脸图片
  • 数据库实时更新出入状态

2. 系统架构设计

前端页面 → Spring Boot后端 → 腾讯云AI接口 → 本地数据库↑        ↑人脸图片    出入记录

3. 前端页面实现

前端页面需要实现以下功能:

  • 摄像头实时拍照功能
  • 人脸图片上传功能
  • 小区选择下拉菜单
  • 识别结果展示区域
  • 出入记录显示面板

4. 后端接口实现

4.1 出入记录控制器(InOurRecordController)

@PostMapping("/add")
public Result add(@RequestBody InOutForm inOutFaceForm) {// 调用腾讯AI接口进行人脸识别FaceApi faceApi = new FaceApi();RootResp resp = faceApi.searchPersonsReturnsByGroup(apiConfiguration, inOutFaceForm.getFileBase64());if (resp.getRet() != 0) {return Result.error("人脸识别失败: " + resp.getMsg());}// 解析腾讯云返回数据JSONObject result = parseTencentResponse(resp);if (result == null) {return Result.error("未识别到有效人脸信息");}// 验证人员身份和小区归属Person person = validatePerson(result, inOutFaceForm.getCommunityId());if (person == null) {return Result.error("身份验证失败");}// 生成出入记录return generateInOutRecord(inOutFaceForm, person);
}// 解析腾讯云返回数据
private JSONObject parseTencentResponse(RootResp resp) {JSONObject object = JSONObject.parseObject(resp.getData().toString());JSONArray resultsReturnsByGroup = object.getJSONArray("ResultsReturnsByGroup");JSONObject returnsByGroup = resultsReturnsByGroup.getJSONObject(0);JSONArray groupCandidates = returnsByGroup.getJSONArray("GroupCandidates");JSONObject groupCandidate = groupCandidates.getJSONObject(0);JSONArray candidates = groupCandidate.getJSONArray("Candidates");// 获取相似度最高的候选人JSONObject candidate = candidates.getJSONObject(0);if (candidate.getInteger("Score") < 80) {return null; // 相似度过低}return candidate;
}// 验证人员信息
private Person validatePerson(JSONObject candidate, Integer communityId) {String personId = candidate.getString("PersonId").substring(4);Person person = personService.getById(Long.parseLong(personId));if (person == null) {return null;}if (!communityId.equals(person.getCommunityId())) {throw new BusinessException("非本小区居民");}return person;
}// 生成出入记录
private Result generateInOutRecord(InOutForm form, Person person) {InOutRecord record = new InOutRecord();record.setCommunityId(person.getCommunityId());record.setPersonId(person.getPersonId());try {// 保存人脸图片String fileName = saveFaceImage(form.getFileBase64(), form.getExtName());String imagePath = urlPrefix + "community/upload/face/" + fileName;// 检查现有记录InOutRecord existingRecord = inOutRecordMapper.getInOutRecord(record);if (existingRecord == null) {// 进入记录record.setInTime(LocalDateTime.now());record.setInPic(imagePath);inOutRecordMapper.insert(record);return Result.ok("【" + person.getUserName() + "】进入小区");} else {// 离开记录existingRecord.setOutTime(LocalDateTime.now());existingRecord.setOutPic(imagePath);inOutRecordMapper.updateById(existingRecord);return Result.ok("【" + person.getUserName() + "】离开小区");}} catch (Exception e) {logger.error("记录生成失败", e);return Result.error("系统异常");}
}// 保存图片到本地
private String saveFaceImage(String base64Data, String extName) {String fileName = UUID.randomUUID() + "." + extName;Base64Util.decoderBase64File(base64Data, faceImagePath + fileName);return fileName;
}

4.2 出入记录表单(InOutForm)

@Data
public class InOutForm {private Integer communityId;  // 小区IDprivate String extName;       // 图片扩展名private String fileBase64;    // Base64编码的图片数据
}

5. 腾讯云接口封装(FaceApi)

5.1 关键方法:人员搜索

public RootResp searchPersonsReturnsByGroup(ApiConfiguration config, String image) {RootResp result = new RootResp();try {Credential cred = new Credential(config.getSecretId(), config.getSecretKey());HttpProfile httpProfile = new HttpProfile();httpProfile.setEndpoint(config.getServerIp());ClientProfile clientProfile = new ClientProfile();clientProfile.setHttpProfile(httpProfile);IaiClient client = new IaiClient(cred, config.getArea(), clientProfile);JSONObject paramObj = new JSONObject();paramObj.put("GroupIds", new String[]{config.getGroupId()});paramObj.put("Image", image);paramObj.put("MaxPersonNumPerGroup", 5); // 返回最相似的5个候选人paramObj.put("NeedPersonInfo", 1);       // 返回人员详细信息paramObj.put("MaxFaceNum", 1);           // 最多识别1张人脸SearchFacesReturnsByGroupRequest req = SearchFacesReturnsByGroupRequest.fromJsonString(paramObj.toJSONString(), SearchFacesReturnsByGroupRequest.class);SearchFacesReturnsByGroupResponse resp = client.SearchFacesReturnsByGroup(req);result.setData(SearchFacesReturnsByGroupResponse.toJsonString(resp));} catch (TencentCloudSDKException e) {result.setRet(-1);result.setMsg(e.toString());logger.error("腾讯云接口调用失败", e);}return result;
}

6. 数据库设计与关键SQL

6.1 出入记录表(in_out_record)

字段类型描述
idBIGINT主键ID
community_idINT小区ID
person_idBIGINT人员ID
in_timeDATETIME进入时间
out_timeDATETIME离开时间
in_picVARCHAR(255)进入时照片路径
out_picVARCHAR(255)离开时照片路径

6.2 关键SQL:判断进出状态

<select id="getInOutRecord" resultType="com.qcby.AICommunity.entity.InOutRecord">SELECT * FROM in_out_record WHERE community_id = #{communityId} AND person_id = #{personId} AND out_time IS NULL
</select>
http://www.dtcms.com/a/343039.html

相关文章:

  • windows无法访问某个服务器共享文件夹
  • linux中ctype.h字符判断函数实现原理
  • linux中的iptables的简介与常用基础用法
  • springboot人事管理系统源码和论文
  • Python编程练习100例(含答案)
  • HTTP 与 HTTPS:网络通信幽径上的安全秘钥
  • Promise详解:Promise解决ajax回调嵌套问题
  • system\core\init\init.cpp----LoadBootScripts()解析init.rc(2)
  • 五大主流ETL数据集成平台推荐
  • 鸿蒙 NEXT开发中轻松实现人脸识别功能
  • hadoop-3.3.6和hbase-2.4.13
  • 日志收集(ELK)
  • k8s--NetworkPolicy资源对象
  • 打工人项目日报计划
  • 搭建FTP文件共享服务器
  • linux 之 virtio 子系统核心的数据结构
  • DeepSeek R2难产:近期DeepSeek-V3.1 发布更新并开源,成功实现迈向 Agent 时代的第一步
  • 信息收集4----(收集网站指纹信息)
  • CSS 3D动画,围绕旋转动画Demo
  • 常见 Linux 网络命令梳理
  • AGV 技术落地场景解析:从制造业到仓储物流,看自动导引车的行业应用
  • 【Ruoyi解密-02.登录流程:】登录-找密码不抓瞎
  • 封装FTPSClient连接ftps服务器
  • 一个成熟的运维及售后岗位应掌握的知识体系详解
  • Linux动态库制作和使用
  • Manus AI 与多语言手写识别:技术、应用与未来
  • Nginx + Vue/React 前端 + API:防止路径混淆漏洞与跨域问题实战分享
  • [Mysql数据库] Mysql安全知识
  • Oracle ADG 切换方式详解:Switchover 与 Failover 操作指南
  • 〖领码方案〗前端 PageData 完整解决方案 第四版