头像检测。否符合要求
This commit is contained in:
97
src/test/java/com/rj/audio/FaceDetectTest.java
Normal file
97
src/test/java/com/rj/audio/FaceDetectTest.java
Normal file
@@ -0,0 +1,97 @@
|
||||
package com.rj.audio;
|
||||
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.client.HttpClientErrorException;
|
||||
import org.springframework.web.client.HttpServerErrorException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.rj.entity.FaceDetectLog;
|
||||
import com.rj.service.IFaceDetectLogService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class FaceDetectTest {
|
||||
|
||||
private static final String API_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/face-detect";
|
||||
private static final String API_KEY = System.getenv("DASHSCOPE_API_KEY");
|
||||
|
||||
@Autowired
|
||||
private IFaceDetectLogService faceDetectLogService;
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (API_KEY == null || API_KEY.trim().isEmpty()) {
|
||||
System.err.println("请设置DASHSCOPE_API_KEY环境变量");
|
||||
return;
|
||||
}
|
||||
String imageUrl = "http://101.35.52.237:19006/api/v1/download-shared-object/aHR0cDovLzEyNy4wLjAuMTo5MDA1L3NyYy1iYXNlLWF1ZGlvLXZpZGVvLyVFNSVCRSVBRSVFNCVCRiVBMSVFNSU5QiVCRSVFNyU4OSU4N18yMDI1MTAwNTEyMDIwNV81Nl80OS5qcGc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD0wRUE4RlhHQUlTN1FQSk9VQUZIOSUyRjIwMjUxMDA1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MTAwNVQwNDE3NDVaJlgtQW16LUV4cGlyZXM9NDMyMDAmWC1BbXotU2VjdXJpdHktVG9rZW49ZXlKaGJHY2lPaUpJVXpVeE1pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmhZMk5sYzNOTFpYa2lPaUl3UlVFNFJsaEhRVWxUTjFGUVNrOVZRVVpJT1NJc0ltVjRjQ0k2TVRjMU9UWTNPVGN4TkN3aWNHRnlaVzUwSWpvaWJXbHVhVzloWkcxcGJpSjkuLWhSOFRobUpXUFNQdGVQM2V4TzdITmlkUHQ1RXBDeWRJMVV2cG0yQXJxczJsR0hBcnBTeEZWZEdJY1ZOUHdKNlF2QUZjbUJQOWQtQmtFampyUXpGaGcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JnZlcnNpb25JZD1udWxsJlgtQW16LVNpZ25hdHVyZT1mN2NjODUxNWFiYzgxZDFlNDZmNjg1YzBkMzAxMzM0MjAyZjM5NmIxODMzMzY4MGM0OWE3ZGYxYmQ5ODk5ZWM4";
|
||||
try {
|
||||
// 创建请求体
|
||||
Map<String, Object> requestBody = new HashMap<>();
|
||||
requestBody.put("model", "liveportrait-detect");
|
||||
|
||||
Map<String, String> input = new HashMap<>();
|
||||
input.put("image_url",imageUrl);
|
||||
requestBody.put("input", input);
|
||||
|
||||
// 设置请求头
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.setBearerAuth(API_KEY);
|
||||
|
||||
// 创建请求实体
|
||||
HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
|
||||
|
||||
// 发送请求
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
System.out.println("发送人脸检测请求到: " + API_URL);
|
||||
System.out.println("请求参数: " + new ObjectMapper().writeValueAsString(requestBody));
|
||||
|
||||
ResponseEntity<String> response = restTemplate.exchange(
|
||||
API_URL,
|
||||
HttpMethod.POST,
|
||||
requestEntity,
|
||||
String.class
|
||||
);
|
||||
|
||||
// 处理响应
|
||||
System.out.println("响应状态码: " + response.getStatusCode());
|
||||
System.out.println("响应头: " + response.getHeaders());
|
||||
System.out.println("响应体: " + response.getBody());
|
||||
|
||||
// 解析JSON响应
|
||||
if (response.getStatusCode() == HttpStatus.OK) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
JsonNode jsonResponse = mapper.readTree(response.getBody());
|
||||
System.out.println("解析后的响应: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonResponse));
|
||||
|
||||
// 检查是否有检测到人脸
|
||||
if (jsonResponse.has("output") && jsonResponse.get("output").has("faces")) {
|
||||
JsonNode faces = jsonResponse.get("output").get("faces");
|
||||
System.out.println("检测到人脸数量: " + faces.size());
|
||||
for (int i = 0; i < faces.size(); i++) {
|
||||
JsonNode face = faces.get(i);
|
||||
System.out.println("人脸 " + (i + 1) + ": " + face.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (HttpClientErrorException e) {
|
||||
System.err.println("客户端错误 (4xx): " + e.getStatusCode());
|
||||
System.err.println("错误响应: " + e.getResponseBodyAsString());
|
||||
e.printStackTrace();
|
||||
} catch (HttpServerErrorException e) {
|
||||
System.err.println("服务器错误 (5xx): " + e.getStatusCode());
|
||||
System.err.println("错误响应: " + e.getResponseBodyAsString());
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
System.err.println("人脸检测请求失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
50
src/test/java/com/rj/audio/VoiceEnrollmentSampleCodes.java
Normal file
50
src/test/java/com/rj/audio/VoiceEnrollmentSampleCodes.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.rj.audio;
|
||||
|
||||
import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesisParam;
|
||||
import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesizer;
|
||||
import com.alibaba.dashscope.exception.InputRequiredException;
|
||||
import com.alibaba.dashscope.exception.NoApiKeyException;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import static java.lang.System.exit;
|
||||
|
||||
public class VoiceEnrollmentSampleCodes {
|
||||
public static String apiKey = System.getenv("DASHSCOPE_API_KEY"); // 如果您没有配置环境变量,请在此处用您的API-KEY进行替换
|
||||
private static String targetModel = "cosyvoice-v2";
|
||||
// private static String targetModel = "cosyvoice-v3";
|
||||
|
||||
public static void main(String[] args)
|
||||
throws NoApiKeyException, InputRequiredException {
|
||||
// 复刻声音
|
||||
// VoiceEnrollmentService service = new VoiceEnrollmentService(apiKey);
|
||||
// Voice myVoice = service.createVoice(targetModel, prefix, fileUrl);
|
||||
// System.out.println("RequestId: " + service.getLastRequestId());
|
||||
// System.out.println("your voice id is " + myVoice.getVoiceId()); //cosyvoice-v3-mayun-224491d8b48344a5a483c8980eeb22fc cosyvoice-v2-mayun-5c4ee144ead34ded8b12970101cef484
|
||||
// 使用复刻的声音来合成文本为语音
|
||||
System.out.println("开始TTS合成,使用语音: longanran");
|
||||
SpeechSynthesisParam param = SpeechSynthesisParam.builder()
|
||||
.apiKey(apiKey)
|
||||
.model(targetModel)
|
||||
.voice("longyingling")
|
||||
.voice("longyuan_v2")
|
||||
.build();
|
||||
SpeechSynthesizer synthesizer = new SpeechSynthesizer(param, null);
|
||||
String testText = "您好, 您可以通过以下步骤使用通义万相进行视频生成,如果您在使用过程中遇到权限问题,可以尝试在阿里云官网搜索通义万相视频生成申请体验权限";
|
||||
System.out.println("合成文本: " + testText);
|
||||
ByteBuffer audio = synthesizer.call(testText);
|
||||
// 保存合成的语音到文件
|
||||
System.out.println("TTS RequestId: " + synthesizer.getLastRequestId());
|
||||
System.out.println("音频数据大小: " + audio.array().length + " bytes");
|
||||
File file = new File("output_longyuan_v2.mp3");
|
||||
try (FileOutputStream fos = new FileOutputStream(file)) {
|
||||
fos.write(audio.array());
|
||||
System.out.println("音频文件已保存到: " + file.getAbsolutePath());
|
||||
} catch (IOException e) {
|
||||
System.err.println("保存音频文件失败: " + e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
package com.rj.controller;
|
||||
|
||||
import com.rj.entity.FaceDetectLog;
|
||||
import com.rj.service.IFaceDetectLogService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* 人脸检测控制器集成测试
|
||||
*/
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("test")
|
||||
@Transactional
|
||||
public class FaceDetectControllerIntegrationTest {
|
||||
|
||||
|
||||
@Autowired
|
||||
private IFaceDetectLogService faceDetectLogService;
|
||||
|
||||
@Test
|
||||
public void testDetectFaceWithValidUrl() {
|
||||
// 使用测试图片URL
|
||||
String testImageUrl = "http://101.35.52.237:19006/api/v1/download-shared-object/aHR0cDovLzEyNy4wLjAuMTo5MDA1L3NyYy1iYXNlLWF1ZGlvLXZpZGVvLyVFNSVCRSVBRSVFNCVCRiVBMSVFNSU5QiVCRSVFNyU4OSU4N18yMDI1MTAwNTEyMDIwNV81Nl80OS5qcGc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD0wRUE4RlhHQUlTN1FQSk9VQUZIOSUyRjIwMjUxMDA1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MTAwNVQwNDE3NDVaJlgtQW16LUV4cGlyZXM9NDMyMDAmWC1BbXotU2VjdXJpdHktVG9rZW49ZXlKaGJHY2lPaUpJVXpVeE1pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmhZMk5sYzNOTFpYa2lPaUl3UlVFNFJsaEhRVWxUTjFGUVNrOVZRVVpJT1NJc0ltVjRjQ0k2TVRjMU9UWTNPVGN4TkN3aWNHRnlaVzUwSWpvaWJXbHVhVzloWkcxcGJpSjkuLWhSOFRobUpXUFNQdGVQM2V4TzdITmlkUHQ1RXBDeWRJMVV2cG0yQXJxczJsR0hBcnBTeEZWZEdJY1ZOUHdKNlF2QUZjbUJQOWQtQmtFampyUXpGaGcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JnZlcnNpb25JZD1udWxsJlgtQW16LVNpZ25hdHVyZT1mN2NjODUxNWFiYzgxZDFlNDZmNjg1YzBkMzAxMzM0MjAyZjM5NmIxODMzMzY4MGM0OWE3ZGYxYmQ5ODk5ZWM4";
|
||||
|
||||
try {
|
||||
// 执行人脸检测
|
||||
FaceDetectLog result = faceDetectLogService.detectFaceAndSave(testImageUrl);
|
||||
|
||||
// 验证结果
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getRequestId());
|
||||
assertEquals("liveportrait-detect", result.getModel());
|
||||
assertEquals(testImageUrl, result.getImageUrl());
|
||||
assertNotNull(result.getRequestTime());
|
||||
|
||||
System.out.println("人脸检测测试结果:");
|
||||
System.out.println("请求ID: " + result.getRequestId());
|
||||
System.out.println("成功状态: " + result.getSuccess());
|
||||
System.out.println("人脸数量: " + result.getFaceCount());
|
||||
System.out.println("处理时间: " + result.getProcessingTimeMs() + "ms");
|
||||
System.out.println("HTTP状态码: " + result.getStatusCode());
|
||||
|
||||
if (!result.getSuccess()) {
|
||||
System.out.println("错误信息: " + result.getErrorMessage());
|
||||
}
|
||||
|
||||
// 验证响应数据包含pass字段
|
||||
if (result.getResponseData() != null) {
|
||||
System.out.println("响应数据: " + result.getResponseData());
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("人脸检测测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDetectFaceWithInvalidUrl() {
|
||||
// 使用无效的图片URL
|
||||
String invalidImageUrl = "https://invalid-url.com/nonexistent.jpg";
|
||||
|
||||
try {
|
||||
// 执行人脸检测
|
||||
FaceDetectLog result = faceDetectLogService.detectFaceAndSave(invalidImageUrl);
|
||||
|
||||
// 验证结果
|
||||
assertNotNull(result);
|
||||
assertNotNull(result.getRequestId());
|
||||
assertEquals("liveportrait-detect", result.getModel());
|
||||
assertEquals(invalidImageUrl, result.getImageUrl());
|
||||
|
||||
System.out.println("无效URL测试结果:");
|
||||
System.out.println("请求ID: " + result.getRequestId());
|
||||
System.out.println("成功状态: " + result.getSuccess());
|
||||
System.out.println("错误信息: " + result.getErrorMessage());
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("无效URL测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPassFieldLogic() {
|
||||
// 测试pass字段逻辑
|
||||
System.out.println("=== 测试pass字段逻辑 ===");
|
||||
|
||||
// 模拟响应数据
|
||||
String mockResponseData = "{\"output\":{\"message\":\"\",\"pass\":true},\"usage\":{\"image_count\":1},\"request_id\":\"ee4a54ca-c190-4594-990e-d9546cbbd5ed\"}";
|
||||
|
||||
try {
|
||||
// 创建测试日志
|
||||
FaceDetectLog testLog = new FaceDetectLog();
|
||||
testLog.setRequestId("test-pass-logic-" + System.currentTimeMillis());
|
||||
testLog.setModel("liveportrait-detect");
|
||||
testLog.setImageUrl("https://example.com/test.jpg");
|
||||
testLog.setRequestTime(java.time.LocalDateTime.now());
|
||||
testLog.setResponseTime(java.time.LocalDateTime.now());
|
||||
testLog.setStatusCode(200);
|
||||
testLog.setResponseData(mockResponseData);
|
||||
testLog.setProcessingTimeMs(1500L);
|
||||
|
||||
// 解析响应数据中的pass字段
|
||||
com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
|
||||
com.fasterxml.jackson.databind.JsonNode jsonResponse = mapper.readTree(mockResponseData);
|
||||
|
||||
boolean isSuccess = false;
|
||||
if (jsonResponse.has("output") && jsonResponse.get("output").has("pass")) {
|
||||
isSuccess = jsonResponse.get("output").get("pass").asBoolean();
|
||||
testLog.setSuccess(isSuccess);
|
||||
|
||||
System.out.println("解析到的pass值: " + isSuccess);
|
||||
System.out.println("设置的成功状态: " + testLog.getSuccess());
|
||||
}
|
||||
|
||||
// 验证pass字段解析
|
||||
assertTrue(isSuccess, "pass字段应该为true");
|
||||
assertTrue(testLog.getSuccess(), "成功状态应该为true");
|
||||
|
||||
System.out.println("pass字段逻辑测试通过");
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println("pass字段逻辑测试失败: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveFaceDetectLog() {
|
||||
// 创建测试日志
|
||||
FaceDetectLog testLog = new FaceDetectLog();
|
||||
testLog.setRequestId("test-request-id-" + System.currentTimeMillis());
|
||||
testLog.setModel("liveportrait-detect");
|
||||
testLog.setImageUrl("https://example.com/test.jpg");
|
||||
testLog.setRequestTime(java.time.LocalDateTime.now());
|
||||
testLog.setResponseTime(java.time.LocalDateTime.now());
|
||||
testLog.setStatusCode(200);
|
||||
testLog.setSuccess(true);
|
||||
testLog.setFaceCount(1);
|
||||
testLog.setResponseData("{\"output\":{\"faces\":[{\"bbox\":[100,100,200,200]}]}}");
|
||||
testLog.setProcessingTimeMs(1500L);
|
||||
|
||||
// 保存日志
|
||||
boolean saved = faceDetectLogService.saveFaceDetectLog(testLog);
|
||||
|
||||
// 验证保存结果
|
||||
assertTrue(saved);
|
||||
System.out.println("日志保存测试成功,请求ID: " + testLog.getRequestId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user