diff --git a/src/main/java/com/ycwl/basic/mapper/pc/DeviceMapper.java b/src/main/java/com/ycwl/basic/mapper/pc/DeviceMapper.java index 569a22e..00e2aaa 100644 --- a/src/main/java/com/ycwl/basic/mapper/pc/DeviceMapper.java +++ b/src/main/java/com/ycwl/basic/mapper/pc/DeviceMapper.java @@ -21,4 +21,6 @@ public interface DeviceMapper { int deleteById(Long id); int update(DeviceAddOrUpdateReq deviceReqQuery); int updateStatus(Long id); + + List listByScenicId(Long scenicId); } diff --git a/src/main/java/com/ycwl/basic/model/pc/face/entity/FaceEntity.java b/src/main/java/com/ycwl/basic/model/pc/face/entity/FaceEntity.java index f428caa..9db8b04 100644 --- a/src/main/java/com/ycwl/basic/model/pc/face/entity/FaceEntity.java +++ b/src/main/java/com/ycwl/basic/model/pc/face/entity/FaceEntity.java @@ -16,6 +16,10 @@ import java.util.Date; public class FaceEntity { @TableId private Long id; + /** + * 人脸得分 + */ + private float score; /** * 会员id */ diff --git a/src/main/java/com/ycwl/basic/model/pc/faceSample/entity/FaceSampleEntity.java b/src/main/java/com/ycwl/basic/model/pc/faceSample/entity/FaceSampleEntity.java index 7f8dc23..25ac0b9 100644 --- a/src/main/java/com/ycwl/basic/model/pc/faceSample/entity/FaceSampleEntity.java +++ b/src/main/java/com/ycwl/basic/model/pc/faceSample/entity/FaceSampleEntity.java @@ -15,10 +15,19 @@ import java.util.Date; public class FaceSampleEntity { @TableId private Long id; + /** + * 景区ID + */ + private Long scenicId; /** * 来源设备 */ private Long deviceId; + /** + * 样本ID + */ + private Long sourceId; + private float score; /** * 人脸照片 */ diff --git a/src/main/java/com/ycwl/basic/model/pc/faceSample/req/FaceSampleReqQuery.java b/src/main/java/com/ycwl/basic/model/pc/faceSample/req/FaceSampleReqQuery.java index 6ab0884..0dd67c9 100644 --- a/src/main/java/com/ycwl/basic/model/pc/faceSample/req/FaceSampleReqQuery.java +++ b/src/main/java/com/ycwl/basic/model/pc/faceSample/req/FaceSampleReqQuery.java @@ -16,6 +16,11 @@ import java.util.Date; @Data @ApiModel("人脸样本查询参数") public class FaceSampleReqQuery extends BaseQueryParameterReq { + /** + * 景区ID + */ + @ApiModelProperty("景区ID") + private Long scenicId; /** * 来源设备 */ diff --git a/src/main/java/com/ycwl/basic/model/pc/faceSample/resp/FaceSampleRespVO.java b/src/main/java/com/ycwl/basic/model/pc/faceSample/resp/FaceSampleRespVO.java index 69f48d8..b3d7ea1 100644 --- a/src/main/java/com/ycwl/basic/model/pc/faceSample/resp/FaceSampleRespVO.java +++ b/src/main/java/com/ycwl/basic/model/pc/faceSample/resp/FaceSampleRespVO.java @@ -16,6 +16,11 @@ import java.util.Date; @ApiModel("人脸样本响应参数") public class FaceSampleRespVO { private Long id; + /** + * 景区ID + */ + @ApiModelProperty("景区ID") + private Long scenicId; /** * 来源设备 */ diff --git a/src/main/java/com/ycwl/basic/model/task/resp/AddFaceRespVo.java b/src/main/java/com/ycwl/basic/model/task/resp/AddFaceRespVo.java new file mode 100644 index 0000000..6afc14e --- /dev/null +++ b/src/main/java/com/ycwl/basic/model/task/resp/AddFaceRespVo.java @@ -0,0 +1,8 @@ +package com.ycwl.basic.model.task.resp; + +import lombok.Data; + +@Data +public class AddFaceRespVo { + private float score; +} diff --git a/src/main/java/com/ycwl/basic/model/task/resp/SearchFaceRespVo.java b/src/main/java/com/ycwl/basic/model/task/resp/SearchFaceRespVo.java new file mode 100644 index 0000000..e9302ed --- /dev/null +++ b/src/main/java/com/ycwl/basic/model/task/resp/SearchFaceRespVo.java @@ -0,0 +1,8 @@ +package com.ycwl.basic.model.task.resp; + +import lombok.Data; + +@Data +public class SearchFaceRespVo { + private float score; +} diff --git a/src/main/java/com/ycwl/basic/service/impl/task/TaskFaceServiceImpl.java b/src/main/java/com/ycwl/basic/service/impl/task/TaskFaceServiceImpl.java new file mode 100644 index 0000000..d665c4a --- /dev/null +++ b/src/main/java/com/ycwl/basic/service/impl/task/TaskFaceServiceImpl.java @@ -0,0 +1,146 @@ +package com.ycwl.basic.service.impl.task; + +import cn.hutool.core.date.DateUtil; +import com.alibaba.fastjson.JSON; +import com.aliyuncs.exceptions.ClientException; +import com.aliyuncs.facebody.model.v20191230.AddFaceEntityRequest; +import com.aliyuncs.facebody.model.v20191230.AddFaceRequest; +import com.aliyuncs.facebody.model.v20191230.AddFaceResponse; +import com.aliyuncs.facebody.model.v20191230.DeleteFaceEntityRequest; +import com.aliyuncs.facebody.model.v20191230.SearchFaceRequest; +import com.aliyuncs.facebody.model.v20191230.SearchFaceResponse; +import com.ycwl.basic.mapper.pc.DeviceMapper; +import com.ycwl.basic.mapper.pc.FaceMapper; +import com.ycwl.basic.mapper.pc.FaceSampleMapper; +import com.ycwl.basic.mapper.pc.ScenicMapper; +import com.ycwl.basic.model.pc.face.entity.FaceEntity; +import com.ycwl.basic.model.pc.face.resp.FaceRespVO; +import com.ycwl.basic.model.pc.faceSample.entity.FaceSampleEntity; +import com.ycwl.basic.model.pc.faceSample.req.FaceSampleReqQuery; +import com.ycwl.basic.model.pc.faceSample.resp.FaceSampleRespVO; +import com.ycwl.basic.model.task.resp.AddFaceRespVo; +import com.ycwl.basic.model.task.resp.SearchFaceRespVo; +import com.ycwl.basic.service.task.TaskFaceService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import com.aliyuncs.DefaultAcsClient; +import com.aliyuncs.IAcsClient; +import com.aliyuncs.profile.DefaultProfile; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +@Service +public class TaskFaceServiceImpl implements TaskFaceService { + @Autowired + private FaceMapper faceMapper; + @Autowired + private FaceSampleMapper faceSampleMapper; + @Autowired + private ScenicMapper scenicMapper; + private static final String DATE_FORMAT="yyyyMMddHHmmssSSS"; + @Autowired + private DeviceMapper deviceMapper; + + private IAcsClient getClient() { + DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai","LTAIDySvOV8yC7VZ","RgO5bwRnHrsyDak0IaLEF6iTRo7469"); + IAcsClient client = new DefaultAcsClient(profile); + return client; + } + @Override + public SearchFaceRespVo searchFace(Long scenicId, Long faceId) { + FaceRespVO faceRespVO = faceMapper.getById(faceId); + IAcsClient client = getClient(); + SearchFaceRequest request = new SearchFaceRequest(); + request.setDbName(scenicId.toString()); + request.setImageUrl(faceRespVO.getFaceUrl()); + request.setLimit(100); + request.setQualityScoreThreshold(80F); + try { + SearchFaceResponse response = client.getAcsResponse(request); + List matchList = response.getData().getMatchList(); + if (matchList.isEmpty()) { + return null; + } + SearchFaceRespVo respVo = new SearchFaceRespVo(); + FaceEntity faceEntity = new FaceEntity(); + faceEntity.setId(faceId); + faceEntity.setMatchResult(JSON.toJSONString(matchList)); + faceEntity.setScore(matchList.get(0).getQualitieScore()); + List faceItems = matchList.get(0).getFaceItems().stream() + .filter(faceItemsItem -> faceItemsItem.getConfidence() > 50).collect(Collectors.toList()); + faceEntity.setMatchSampleIds( + faceItems.stream() + .map(SearchFaceResponse.Data.MatchListItem.FaceItemsItem::getExtraData) + .collect(Collectors.joining(",")) + ); + faceMapper.update(faceEntity); + respVo.setScore(matchList.get(0).getQualitieScore()); + return respVo; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public AddFaceRespVo addFaceSample(Long faceSampleId) { + FaceSampleRespVO faceSampleRespVO = faceSampleMapper.getById(faceSampleId); + AddFaceEntityRequest request = new AddFaceEntityRequest(); + request.setDbName(faceSampleRespVO.getScenicId().toString()); + SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); + String entityId = faceSampleRespVO.getDeviceId().toString() + "_" + sdf.format(faceSampleRespVO.getCreateAt()); + request.setEntityId(entityId); + IAcsClient client = getClient(); + try { + client.getAcsResponse(request); + } catch (ClientException e) { + return null; + } + AddFaceRequest addFaceRequest = new AddFaceRequest(); + addFaceRequest.setDbName(faceSampleRespVO.getScenicId().toString()); + addFaceRequest.setEntityId(entityId); + addFaceRequest.setImageUrl(faceSampleRespVO.getFaceUrl()); + addFaceRequest.setExtraData(faceSampleId.toString()); + AddFaceRespVo respVo = new AddFaceRespVo(); + try { + AddFaceResponse acsResponse = client.getAcsResponse(addFaceRequest); + FaceSampleEntity faceSampleEntity = new FaceSampleEntity(); + faceSampleEntity.setId(faceSampleId); + faceSampleEntity.setScore(acsResponse.getData().getQualitieScore()); + faceSampleEntity.setUpdateAt(new Date()); + faceSampleMapper.update(faceSampleEntity); + respVo.setScore(acsResponse.getData().getQualitieScore()); + } catch (ClientException e) { + return null; + } + return respVo; + } + + @Override + public void batchDeleteFace(Long scenicId) { + FaceSampleReqQuery query = new FaceSampleReqQuery(); + query.setDeviceId(scenicId); + faceSampleMapper.list(query); + Date thatDay = DateUtil.offsetDay(new Date(), -3); + Date dayStart = DateUtil.beginOfDay(thatDay); + Date dayEnd = DateUtil.endOfDay(thatDay); + query.setStartTime(dayStart); + query.setEndTime(dayEnd); + IAcsClient client = getClient(); + SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); + faceSampleMapper.list(query).forEach(faceSampleEntity -> { + String entityId = faceSampleEntity.getDeviceId().toString() + "_" + sdf.format(faceSampleEntity.getCreateAt()); + DeleteFaceEntityRequest request = new DeleteFaceEntityRequest(); + request.setDbName(scenicId.toString()); + request.setEntityId(entityId); + try { + client.getAcsResponse(request); + } catch (ClientException e) { + return; + } + }); + } +} diff --git a/src/main/java/com/ycwl/basic/service/task/TaskFaceService.java b/src/main/java/com/ycwl/basic/service/task/TaskFaceService.java new file mode 100644 index 0000000..76a2eaf --- /dev/null +++ b/src/main/java/com/ycwl/basic/service/task/TaskFaceService.java @@ -0,0 +1,13 @@ +package com.ycwl.basic.service.task; + +import com.ycwl.basic.model.task.resp.AddFaceRespVo; +import com.ycwl.basic.model.task.resp.SearchFaceRespVo; + +public interface TaskFaceService { + + SearchFaceRespVo searchFace(Long scenicId, Long faceId); + + AddFaceRespVo addFaceSample(Long faceSampleId); + + void batchDeleteFace(Long scenicId); +} diff --git a/src/main/resources/mapper/pc/DeviceMapper.xml b/src/main/resources/mapper/pc/DeviceMapper.xml index f0ffd5d..9be8f4e 100644 --- a/src/main/resources/mapper/pc/DeviceMapper.xml +++ b/src/main/resources/mapper/pc/DeviceMapper.xml @@ -53,4 +53,9 @@ left join scenic s on d.scenic_id = s.id where d.id = #{id} + \ No newline at end of file diff --git a/src/main/resources/mapper/pc/FaceMapper.xml b/src/main/resources/mapper/pc/FaceMapper.xml index 3e273d4..9fc6cb3 100644 --- a/src/main/resources/mapper/pc/FaceMapper.xml +++ b/src/main/resources/mapper/pc/FaceMapper.xml @@ -2,8 +2,8 @@ - insert into face(id, member_id, face_url, match_sample_ids, first_match_rate, match_result) - values (#{id}, #{memberId}, #{faceUrl}, #{matchSampleIds}, #{firstMatchRate}, #{matchResult}) + insert into face(id, score, member_id, face_url, match_sample_ids, first_match_rate, match_result) + values (#{id}, #{score}, #{memberId}, #{faceUrl}, #{matchSampleIds}, #{firstMatchRate}, #{matchResult}) update face @@ -11,6 +11,9 @@ member_id = #{memberId}, + + score = #{score}, + face_url = #{faceUrl}, diff --git a/src/main/resources/mapper/pc/FaceSampleMapper.xml b/src/main/resources/mapper/pc/FaceSampleMapper.xml index 3ad9cb7..7315c6a 100644 --- a/src/main/resources/mapper/pc/FaceSampleMapper.xml +++ b/src/main/resources/mapper/pc/FaceSampleMapper.xml @@ -42,9 +42,12 @@