该文档详细描述了通过API创建图片免训数字人的完整流程,包括背景和图片素材上传、数字人创建及对话配置组装。
重要
图片免训数字人可用于实时对话,不可用于播报视频合成和直播。
操作流程
使用获取上传凭证API获取图片素材上传凭证并上传图片素材。
通过创建免训图片数字人API设置变量值,创建图片免训数字人。
1 上传图片素材
使用获取上传凭证API获取图片素材上传凭证。
上传图片素材,图片素材文件要求如下:
格式: jpg、jpeg、png。
分辨率:最小分辨率:400px,最大分辨率:7000px。
文件大小:不超过10M。
宽高比:9:16。
1.1 获取图片素材上传凭证
private static GetUploadPolicyResponseBody.GetUploadPolicyResponseBodyData upload(Client client, String type, File file) throws Exception {
//获取上传凭证
GetUploadPolicyRequest getUploadPolicyRequest = new GetUploadPolicyRequest();
getUploadPolicyRequest.setType("INPUT_INFER_PIC");
GetUploadPolicyResponse uploadPolicy = client.getUploadPolicy(getUploadPolicyRequest);
GetUploadPolicyResponseBody.GetUploadPolicyResponseBodyData data = uploadPolicy.getBody().getData();
System.out.println("获取上传凭证:" + JSON.toJSONString(data));
return data;
}1.2 上传训练图片素材
private static void uploadFile(GetUploadPolicyResponseBody.GetUploadPolicyResponseBodyData data, File file) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost uploadFile = new HttpPost("https://" + data.getOssPolicy().getHost());
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
ContentType contentType = ContentType.create("multipart/form-data", Consts.UTF_8);
// 添加OSS所需的表单字段
builder.addTextBody("key", data.getOssKey() + "/" + file.getName(), contentType);
builder.addTextBody("policy", data.getOssPolicy().getPolicy(), contentType);
builder.addTextBody("OSSAccessKeyId", data.getOssPolicy().getAccessId(), contentType);
builder.addTextBody("signature", data.getOssPolicy().getSignature(), contentType);
// 添加文件
builder.addBinaryBody("file", Files.newInputStream(file.toPath()), ContentType.IMAGE_PNG, file.getName());
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(uploadFile);
try {
HttpEntity responseEntity = response.getEntity();
System.err.println("Upload result: " + response.getStatusLine());
if (responseEntity != null) {
String responseString = EntityUtils.toString(responseEntity);
System.err.println("Response content: " + responseString);
}
EntityUtils.consume(responseEntity);
} finally {
response.close();
}
} finally {
httpClient.close();
}
}1.3 上传示例

2 创建图片免训数字人
说明
如果图片存在内容安全问题或人物不符合形象要求,创建免训图片数字人会失败。
private static CreateNoTrainPicAvatarResponse createNoTrainPicAvatar(Client client,
File imageFile,
GetUploadPolicyResponseBody.GetUploadPolicyResponseBodyData imageData) throws Exception {
CreateNoTrainPicAvatarRequest chatSessionConfigRequest = new CreateNoTrainPicAvatarRequest();
//设置形象表情表现力为普通或热情
chatSessionConfigRequest.setExpressiveness("NORMAL");//"ENTHUSIASTIC"
chatSessionConfigRequest.setName("数字人");
//设置性别
chatSessionConfigRequest.setGender("MALE");//"FEMALE"
chatSessionConfigRequest.setImageOssPath(imageData.getOssKey() + "/" + imageFile.getName());
//设置形象背景为透明或不透明
chatSessionConfigRequest.setTransparent(true);
CreateNoTrainPicAvatarResponse chatAvatar = client.createNoTrainPicAvatar(chatSessionConfigRequest);
System.out.println("创建免训图片数字人:" + JSON.toJSONString(chatAvatar.body.getData()));
return chatAvatar;
}图片免训数字人完整调用示例代码
引入SDK
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>lingmou20250527</artifactId>
<version>1.5.3</version>
</dependency>示例代码
import com.alibaba.fastjson.JSON;
import com.aliyun.lingmou20250527.Client;
import com.aliyun.lingmou20250527.models.*;
import com.aliyun.teaopenapi.models.Config;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class Demo {
public static void main(String[] args) throws Exception {
Client client = Demo.getInstance();
//1.上传形象素材
File imageFile = new File("D:\\images\\criticize_counselor_clear.webp");
GetUploadPolicyResponseBody.GetUploadPolicyResponseBodyData imageData = upload(client, "INPUT_INFER_PIC", imageFile);
//2.创建免训图片数字人形象,如图片存在内容安全问题或人不符合形象要求会失败
CreateNoTrainPicAvatarRequest chatSessionConfigRequest = new CreateNoTrainPicAvatarRequest();
//设置形象表情表现力为普通或热情
chatSessionConfigRequest.setExpressiveness("NORMAL");//"ENTHUSIASTIC"
chatSessionConfigRequest.setName("数字人");
//设置性别
chatSessionConfigRequest.setGender("MALE");//"FEMALE"
chatSessionConfigRequest.setImageOssPath(imageData.getOssKey() + "/" + imageFile.getName());
//设置形象背景为透明或不透明
chatSessionConfigRequest.setTransparent(true);
CreateNoTrainPicAvatarResponse chatAvatar = client.createNoTrainPicAvatar(chatSessionConfigRequest);
System.out.println("创建对话免训图片数字人:" + JSON.toJSONString(chatAvatar.body.getData()));
}
private static GetUploadPolicyResponseBody.GetUploadPolicyResponseBodyData upload(Client client, String type, File file) throws Exception {
//获取上传凭证
GetUploadPolicyRequest getUploadPolicyRequest = new GetUploadPolicyRequest();
getUploadPolicyRequest.setType(type);
GetUploadPolicyResponse uploadPolicy = client.getUploadPolicy(getUploadPolicyRequest);
GetUploadPolicyResponseBody.GetUploadPolicyResponseBodyData data = uploadPolicy.getBody().getData();
System.out.println("获取上传凭证:" + JSON.toJSONString(data));
// HttpClient上传文件
uploadFile(data, file);
return data;
}
private static void uploadFile(GetUploadPolicyResponseBody.GetUploadPolicyResponseBodyData data, File file) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
HttpPost uploadFile = new HttpPost("https://" + data.getOssPolicy().getHost());
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
ContentType contentType = ContentType.create("multipart/form-data", Consts.UTF_8);
// 添加OSS所需的表单字段
builder.addTextBody("key", data.getOssKey() + "/" + file.getName(), contentType);
builder.addTextBody("policy", data.getOssPolicy().getPolicy(), contentType);
builder.addTextBody("OSSAccessKeyId", data.getOssPolicy().getAccessId(), contentType);
builder.addTextBody("signature", data.getOssPolicy().getSignature(), contentType);
// 添加文件
builder.addBinaryBody("file", Files.newInputStream(file.toPath()), ContentType.IMAGE_PNG, file.getName());
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
CloseableHttpResponse response = httpClient.execute(uploadFile);
try {
HttpEntity responseEntity = response.getEntity();
System.err.println("Upload result: " + response.getStatusLine());
if (responseEntity != null) {
String responseString = EntityUtils.toString(responseEntity);
System.err.println("Response content: " + responseString);
}
EntityUtils.consume(responseEntity);
} finally {
response.close();
}
} finally {
httpClient.close();
}
}
public static Client getInstance() throws Exception {
String accessKeyId = "xxx";
String accessKeySecret = "yyy";
String endpoint = "lingmou.cn-beijing.aliyuncs.com";
Config config = new Config();
// noinspection AklessInspection
config.setAccessKeyId(accessKeyId);
config.setAccessKeySecret(accessKeySecret);
config.setEndpoint(endpoint);
return new Client(config);
}
}形象创建错误码
DETECT_FAILED("图像检测失败"),
DETECT_NOT_PASS("图像检测不通过"),
CONTENT_RISK("您的图片涉及平台限制内容,请重新上传"),
NO_HUMAN_BODY("未检测到人脸"),
MULTI_HUMAN_BODY("请上传单个形象图片"),
INVALID_HUMAN_PROPORTION("形象在照片中比例过大/过小"),
INVALID_IMAGE_SIZE("图片大小不符合要求"),
INVALID_PERSON_VAGUE("请上传更清晰的照片"),
INVALID_BODY_ORIENTATION("请确保身体朝向正面"),
FACE_INCOMPLETE("请确保脸部完整"),
INVALID_FACE_ORIENTATION("请确保脸部朝向正面"),该文章对您有帮助吗?