You've already forked FrameTour-BE
人脸检测逻辑
This commit is contained in:
@ -21,4 +21,6 @@ public interface DeviceMapper {
|
||||
int deleteById(Long id);
|
||||
int update(DeviceAddOrUpdateReq deviceReqQuery);
|
||||
int updateStatus(Long id);
|
||||
|
||||
List<DeviceRespVO> listByScenicId(Long scenicId);
|
||||
}
|
||||
|
@ -16,6 +16,10 @@ import java.util.Date;
|
||||
public class FaceEntity {
|
||||
@TableId
|
||||
private Long id;
|
||||
/**
|
||||
* 人脸得分
|
||||
*/
|
||||
private float score;
|
||||
/**
|
||||
* 会员id
|
||||
*/
|
||||
|
@ -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;
|
||||
/**
|
||||
* 人脸照片
|
||||
*/
|
||||
|
@ -16,6 +16,11 @@ import java.util.Date;
|
||||
@Data
|
||||
@ApiModel("人脸样本查询参数")
|
||||
public class FaceSampleReqQuery extends BaseQueryParameterReq {
|
||||
/**
|
||||
* 景区ID
|
||||
*/
|
||||
@ApiModelProperty("景区ID")
|
||||
private Long scenicId;
|
||||
/**
|
||||
* 来源设备
|
||||
*/
|
||||
|
@ -16,6 +16,11 @@ import java.util.Date;
|
||||
@ApiModel("人脸样本响应参数")
|
||||
public class FaceSampleRespVO {
|
||||
private Long id;
|
||||
/**
|
||||
* 景区ID
|
||||
*/
|
||||
@ApiModelProperty("景区ID")
|
||||
private Long scenicId;
|
||||
/**
|
||||
* 来源设备
|
||||
*/
|
||||
|
@ -0,0 +1,8 @@
|
||||
package com.ycwl.basic.model.task.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AddFaceRespVo {
|
||||
private float score;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.ycwl.basic.model.task.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SearchFaceRespVo {
|
||||
private float score;
|
||||
}
|
@ -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<SearchFaceResponse.Data.MatchListItem> 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<SearchFaceResponse.Data.MatchListItem.FaceItemsItem> 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
@ -53,4 +53,9 @@
|
||||
left join scenic s on d.scenic_id = s.id
|
||||
where d.id = #{id}
|
||||
</select>
|
||||
<select id="listByScenicId" resultType="com.ycwl.basic.model.pc.device.resp.DeviceRespVO">
|
||||
select d.id, d.name, no, d.status, create_at, d.update_at
|
||||
from device d
|
||||
where d.scenic_id = #{scenicId}
|
||||
</select>
|
||||
</mapper>
|
@ -2,8 +2,8 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.ycwl.basic.mapper.pc.FaceMapper">
|
||||
<insert id="add">
|
||||
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})
|
||||
</insert>
|
||||
<update id="update">
|
||||
update face
|
||||
@ -11,6 +11,9 @@
|
||||
<if test="memberId!= null ">
|
||||
member_id = #{memberId},
|
||||
</if>
|
||||
<if test="score!= null ">
|
||||
score = #{score},
|
||||
</if>
|
||||
<if test="faceUrl!= null and faceUrl!= ''">
|
||||
face_url = #{faceUrl},
|
||||
</if>
|
||||
|
@ -42,9 +42,12 @@
|
||||
</if>
|
||||
</delete>
|
||||
<select id="list" resultType="com.ycwl.basic.model.pc.faceSample.resp.FaceSampleRespVO">
|
||||
select id, device_id, face_url, match_sample_ids, first_match_rate, match_result,`status`
|
||||
select id, scenic_id, device_id, face_url, match_sample_ids, first_match_rate, match_result,`status`
|
||||
from face_sample
|
||||
<where>
|
||||
<if test="scenicId!= null and scenicId!= ''">
|
||||
and device_id = #{deviceId}
|
||||
</if>
|
||||
<if test="deviceId!= null and deviceId!= ''">
|
||||
and device_id = #{deviceId}
|
||||
</if>
|
||||
@ -69,7 +72,7 @@
|
||||
</where>
|
||||
</select>
|
||||
<select id="getById" resultType="com.ycwl.basic.model.pc.faceSample.resp.FaceSampleRespVO">
|
||||
select id, device_id, face_url, match_sample_ids, first_match_rate, match_result,`status`
|
||||
select id, scenic_id, device_id, face_url, match_sample_ids, first_match_rate, match_result,`status`
|
||||
from face_sample
|
||||
where id = #{id}
|
||||
</select>
|
||||
|
Reference in New Issue
Block a user