头像视频合成支持动态模型

This commit is contained in:
spllzh
2025-10-09 08:38:17 +08:00
parent e5cba62007
commit 6dff49fcc1
30 changed files with 376 additions and 36 deletions

View File

@@ -33,6 +33,17 @@ public interface IFaceDetectLogService extends IService<FaceDetectLog> {
*/
FaceDetectLog detectFaceAndSave(String imageUrl, String ownerName, String ownerPhone, String avatarName);
/**
* 执行人脸检测并保存日志(带扩展信息和模型名)
* @param imageUrl 图片URL
* @param ownerName 所属人姓名
* @param ownerPhone 所属人电话
* @param avatarName 头像名称
* @param model 模型名称
* @return 人脸检测日志对象
*/
FaceDetectLog detectFaceAndSave(String imageUrl, String ownerName, String ownerPhone, String avatarName, String model);
/**
* 分页查询人脸检测日志
* @param current 当前页

View File

@@ -69,6 +69,9 @@ public interface ICustomerProfileAnalysisService extends IService<CustomerProfil

View File

@@ -104,6 +104,9 @@ public class CustomerProfileAnalysisServiceImpl extends ServiceImpl<CustomerProf

View File

@@ -60,6 +60,13 @@ public class FaceDetectLogServiceImpl extends ServiceImpl<FaceDetectLogMapper, F
* 执行人脸检测并保存日志(带扩展信息)
*/
public FaceDetectLog detectFaceAndSave(String imageUrl, String ownerName, String ownerPhone, String avatarName) {
return detectFaceAndSave(imageUrl, ownerName, ownerPhone, avatarName, null);
}
/**
* 执行人脸检测并保存日志(带扩展信息和模型名)
*/
public FaceDetectLog detectFaceAndSave(String imageUrl, String ownerName, String ownerPhone, String avatarName, String model) {
if (apiKey == null || apiKey.trim().isEmpty()) {
log.error("DASHSCOPE_API_KEY未配置");
return createErrorLog(imageUrl, "DASHSCOPE_API_KEY未配置", ownerName, ownerPhone, avatarName);
@@ -71,7 +78,10 @@ public class FaceDetectLogServiceImpl extends ServiceImpl<FaceDetectLogMapper, F
LocalDateTime requestTime = LocalDateTime.now();
faceDetectLog.setRequestId(requestId);
faceDetectLog.setModel("liveportrait-detect"); //TODO 模型名称
// 设置模型名称,如果未提供则使用默认值
String modelName = (model != null && !model.trim().isEmpty()) ? model : "liveportrait-detect";
faceDetectLog.setModel(modelName);
log.info("使用模型: {}", modelName);
faceDetectLog.setImageUrl(imageUrl);
faceDetectLog.setOwnerName(ownerName);
faceDetectLog.setOwnerPhone(ownerPhone);
@@ -83,8 +93,14 @@ public class FaceDetectLogServiceImpl extends ServiceImpl<FaceDetectLogMapper, F
try {
// 创建请求体
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "liveportrait-detect");
requestBody.put("model", modelName);
if (modelName.equals("emo-detect-v1")){
Map<String, Object> ratioo = new HashMap<>();
ratioo.put("ratio", "1:1");
requestBody.put("parameters", ratioo);
}
Map<String, String> input = new HashMap<>();
input.put("image_url", imageUrl);
requestBody.put("input", input);
@@ -93,8 +109,8 @@ public class FaceDetectLogServiceImpl extends ServiceImpl<FaceDetectLogMapper, F
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBearerAuth(apiKey);
// 创建请求实体
log.info("发送人脸检测请求requestBody : {}, 请求参数headers: {}", requestBody , headers);
// 创建请求实体 parameters.ratio 希望检测确认的画幅,可选 "1:1"或"3:4"。默认值为"1:1"。
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
// 发送请求
@@ -131,23 +147,50 @@ public class FaceDetectLogServiceImpl extends ServiceImpl<FaceDetectLogMapper, F
faceDetectLog.setSuccess(isSuccess);
log.info("人脸检测结果 - pass: {}", isSuccess);
} else {
// 如果没有pass字段则根据HTTP状态码判断
isSuccess = response.getStatusCode().is2xxSuccessful();
faceDetectLog.setSuccess(isSuccess);
}
// else {
// // 如果没有pass字段则根据HTTP状态码判断
// isSuccess = response.getStatusCode().is2xxSuccessful();
// faceDetectLog.setSuccess(isSuccess);
// }
// 检查是否有检测到人脸
int faceCount = 0;
if (jsonResponse.has("output") && jsonResponse.get("output").has("faces")) {
JsonNode faces = jsonResponse.get("output").get("faces");
faceCount = faces.size();
faceDetectLog.setFaceCount(faceCount);
if (jsonResponse.has("output")) {
JsonNode output = jsonResponse.get("output");
log.info("检测到人脸数量: {}", faceCount);
for (int i = 0; i < faces.size(); i++) {
JsonNode face = faces.get(i);
log.debug("人脸 {}: {}", i + 1, face.toString());
// 根据pass字段判断是否检测到人脸
if (output.has("pass")) {
boolean pass = output.get("pass").asBoolean();
faceDetectLog.setFaceCount(1);
log.info("人脸检测结果 - pass: {}, 检测到人脸数量: {}", pass, faceDetectLog.getFaceCount());
// 记录message信息
if (output.has("message")) {
String message = output.get("message").asText();
log.info("检测消息: {}", message);
}
}
// 解析人脸区域坐标如果API返回中包含
if (output.has("face_bbox")) {
JsonNode faceBbox = output.get("face_bbox");
if (faceBbox.isArray()) {
String faceBboxStr = faceBbox.toString();
faceDetectLog.setFaceBbox(faceBboxStr);
log.info("人脸区域坐标: {}", faceBboxStr);
}
}
// 解析动态区域坐标如果API返回中包含
if (output.has("ext_bbox")) {
JsonNode extBbox = output.get("ext_bbox");
if (extBbox.isArray()) {
String extBboxStr = extBbox.toString();
faceDetectLog.setExtBbox(extBboxStr);
log.info("动态区域坐标: {}", extBboxStr);
}
}
}
@@ -163,7 +206,12 @@ public class FaceDetectLogServiceImpl extends ServiceImpl<FaceDetectLogMapper, F
if (usage.has("image_count")) {
int imageCount = usage.get("image_count").asInt();
log.info("处理的图片数量: {}", imageCount);
// 可以将image_count保存到数据库字段中如果有的话
faceDetectLog.setFaceCount(imageCount);
}
// 记录其他usage信息
log.info("使用情况: {}", usage.toString());
}
} else {
// HTTP状态码不是200直接标记为失败

View File

@@ -6,6 +6,7 @@ import com.rj.dto.VideoSynthesisRequestDto;
import com.rj.entity.VideoSynthesisLog;
import com.rj.mapper.VideoSynthesisLogMapper;
import com.rj.service.IVideoSynthesisService;
import com.rj.utils.MinIOUrlGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,6 +17,7 @@ import org.springframework.web.client.RestTemplate;
import java.time.LocalDateTime;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* 视频合成服务实现类
@@ -33,6 +35,8 @@ public class VideoSynthesisServiceImpl implements IVideoSynthesisService {
@Autowired
private RestTemplate restTemplate;
@Autowired
MinIOUrlGenerator minIOUrlGenerator ;
@Value("${dashscope.api.key}")
private String apiKey;
@@ -62,6 +66,18 @@ public class VideoSynthesisServiceImpl implements IVideoSynthesisService {
log.info("使用默认视频名称: {}", synthesisLog.getVideoName());
}
// 设置人脸区域坐标(如果请求中有提供)
if (request.getFaceBbox() != null && !request.getFaceBbox().trim().isEmpty()) {
synthesisLog.setFaceBbox(request.getFaceBbox());
log.info("设置人脸区域坐标: {}", request.getFaceBbox());
}
// 设置动态区域坐标(如果请求中有提供)
if (request.getExtBbox() != null && !request.getExtBbox().trim().isEmpty()) {
synthesisLog.setExtBbox(request.getExtBbox());
log.info("设置动态区域坐标: {}", request.getExtBbox());
}
try {
log.info("开始视频合成请求ID: {}, 图片URL: {}, 音频URL: {}",
requestId, request.getImageUrl(), request.getAudioUrl());
@@ -110,6 +126,7 @@ public class VideoSynthesisServiceImpl implements IVideoSynthesisService {
// 生成临时访问URL7天有效期
try {
log.info("即将用音频名称: {} 生成临时访问URL", synthesisLog.getVideoName());
String tempUrl = generateTempVideoUrl(synthesisLog.getVideoName(), requestId);
synthesisLog.setVideoTempUrl(tempUrl);
log.info("生成临时访问URL: {}", tempUrl);
@@ -199,14 +216,8 @@ public class VideoSynthesisServiceImpl implements IVideoSynthesisService {
*/
private String generateTempVideoUrl(String videoName, String requestId) {
try {
// 构建临时访问URL
// 格式: https://your-domain.com/video/temp/{requestId}?name={videoName}
String baseUrl = "https://your-domain.com"; // 这里应该从配置文件读取
String encodedVideoName = java.net.URLEncoder.encode(videoName, "UTF-8");
String tempUrl = String.format("%s/video/temp/%s?name=%s", baseUrl, requestId, encodedVideoName);
log.info("生成临时视频URL: 视频名称={}, 请求ID={}, URL={}", videoName, requestId, tempUrl);
return tempUrl;
return minIOUrlGenerator.generateTempUrl(videoName, 7, TimeUnit.DAYS).getUrl();
} catch (Exception e) {
log.error("生成临时视频URL失败: {}", e.getMessage(), e);

View File

@@ -70,6 +70,10 @@ public interface IMenuService extends IService<Menu> {

View File

@@ -74,6 +74,10 @@ public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IM

View File

@@ -74,6 +74,10 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IR

View File

@@ -74,6 +74,10 @@ public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> i