# huaweicloud-metastudio-picturemodeling-java **Repository Path**: livesky/huaweicloud-metastudio-picturemodeling-java ## Basic Information - **Project Name**: huaweicloud-metastudio-picturemodeling-java - **Description**: 通过一张照片生成风格化3D数字人模型的Java示例代码 - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master-dev - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2024-08-16 - **Last Updated**: 2024-08-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 版本说明 本示例基于华为云SDK V3.0版本开发 ## 功能介绍 **什么是华为云MetaStudio服务?** 华为云MetaStudio服务联合伙伴打造数字内容生产线,帮助千行百业实现虚拟世界与现实世界无缝融合。数字人生产线是数字内容生产线的主要组成部分,包含数字人生产、数字人视频制作和数字人直播等场景化的应用。 华为云MetaStudio服务在数字内容生产线中为伙伴提供PaaS原子API能力。 MetaStudio提供的PaaS API能力包括: * 照片建模能力,输入单张照片,输出3D数字人模型。 * 语音驱动能力,输入文本,MetaStudio内部TTS转成语音后,输出驱动数字人的表情基数据和肢体动作数据。 * 视频驱动能力,输入摄像头视频流,输出数字人的表情基数据和肢体动作数据。 **您将学到什么?** 华为云提供了 MetaStudio 服务端 SDK,您可以直接集成服务端 SDK 来调用 MetaStudio 的相关 API,从而实现对 MetaStudio 的快速操作。 该场景示例代码以数字人风格化照片建模为例,介绍如何使用MetaStudio Java SDK创建一个风格化照片建模任务。 建模生成的3D数字人模型可以用于后续视频制作和视频直播。 ## 开发时序图 ![时序图](assets/风格化建模.png) ## 前置条件 - 1、获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。 - 2、您需要拥有华为云租户账号以及该账号对应的 Access Key(AK)和 Secret Access Key(SK)请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 [访问密钥](https://support.huaweicloud.com/usermanual-ca/zh-cn_topic_0046606340.html) 。 - 3、华为云 Java SDK 支持 **Java JDK 1.8** 及其以上版本。 - 4、由于当前MetaStudio服务还处于公测阶段,仅对伙伴开放权限,需要申请才能使用,请提供联系华为销售人员开通MetaStudio服务权限。 具体操作步骤请参考:[华为云MetaStudio开发指南](https://support.huaweicloud.com/api-metastudio/metastudio_02_0000.html) 。 ## 接口参数说明 关于数字人风格化照片建模接口的详细说明可参见: MetaStudio API参考的[“风格化照片建模”](https://support.huaweicloud.com/api-metastudio/CreatePictureModelingJob.html) 章节。 ### SDK获取和安装 开发者可以通过Maven方式获取和安装SDK,包括“huaweicloud-sdk-core”和“huaweicloud-sdk-metastudio”。需要在Java项目的pom.xml文件中加入如下的依赖项。 ```xml com.huaweicloud.sdk huaweicloud-sdk-core 3.1.36 com.huaweicloud.sdk huaweicloud-sdk-metastudio 3.1.36 org.slf4j slf4j-nop 1.7.2 ``` ## 示例代码 ```java public class MetaStudioPictureModeling { public static void main(String[] args) throws InterruptedException { System.out.println("Start HUAWEI CLOUD MetaStudio Picture Modeling Java Demo..."); ICredential auth = getCredential("YOUR AK", "YOUR SK","YOUR PROJECT ID"); // 初始数字人服务的客户端 MetaStudioClient client = getClient(MetaStudioRegion.CN_NORTH_4, auth); // 获取数字人建模任务列表 listPictureModelingJob(client); // 获取数字人建模用的风格 ListStylesResponse styleResponse = listStyles(client); // 风格化数字人建模 CreatePictureModelingJobResponse createPictureModelingJobResponse = createPictureModelingRequestJob(client,styleResponse.getStyles().get(0).getStyleId()); Thread.sleep(5000); // 查询风格化数字人建模详细 showPictureModelingJobRequest(client,createPictureModelingJobResponse.getJobId()); } /** * 查询查询照片建模任务列表 * * @param client MetaStudio client * @return 建模任务查询响应 */ public static ListPictureModelingJobsResponse listPictureModelingJob(MetaStudioClient client) { System.out.println("listPictureModelingJob start"); try { ListPictureModelingJobsRequest listPictureModelingJobsRequest = new ListPictureModelingJobsRequest(); ListPictureModelingJobsResponse response = client.listPictureModelingJobs(listPictureModelingJobsRequest); System.out.println(response); return response; } catch (ConnectionException | RequestTimeoutException e) { System.out.println("there is some error, exception:" + e); } return null; } /** * 查询数字人风格 * * @param client MetaStudio client * @return 数字人风格查询响应 */ public static ListStylesResponse listStyles(MetaStudioClient client) { System.out.println("listStyles start"); try { ListStylesRequest listStylesRequest = new ListStylesRequest(); ListStylesResponse response = client.listStyles(listStylesRequest); System.out.println(response); return response; } catch (ConnectionException | RequestTimeoutException e) { System.out.println("there is some error, exception:" + e); } return null; } /** * 创建风格化照片建模任务 * * @param client MetaStudio client * @return 风格化照片建模任务响应 */ public static CreatePictureModelingJobResponse createPictureModelingRequestJob(MetaStudioClient client, String style_id) { System.out.println("createPictureModelingRequestJob start"); CreatePictureModelingJobRequestBody createPictureModelingJobRequestBody = new CreatePictureModelingJobRequestBody(); createPictureModelingJobRequestBody.setName("my_digital_model"); createPictureModelingJobRequestBody.setStyleId(style_id); try { FileInputStream inputStream = new FileInputStream("./assets/picture.jpg"); FormDataFilePart picFile = new FormDataFilePart(inputStream,"picture.jpg"); createPictureModelingJobRequestBody.setFile(picFile); CreatePictureModelingJobRequest createPictureModelingJobRequest = new CreatePictureModelingJobRequest() .withBody(createPictureModelingJobRequestBody); CreatePictureModelingJobResponse response = client.createPictureModelingJob(createPictureModelingJobRequest); System.out.println(response); return response; } catch (ConnectionException | RequestTimeoutException | FileNotFoundException e) { System.out.println("there is some error, exception:" + e); } return null; } /** * 查询风格化照片建模详情 * * @param client MetaStudio client * @param jobId 风格化照片建模任务ID * @return 风格化照片建模任务详情 */ public static ShowPictureModelingJobResponse showPictureModelingJobRequest(MetaStudioClient client, String jobId) { ShowPictureModelingJobResponse response = null; try { System.out.println("showPictureModelingJobRequest start"); ShowPictureModelingJobRequest showPictureModelingJobRequest = new ShowPictureModelingJobRequest(); showPictureModelingJobRequest.withJobId(jobId); response = client.showPictureModelingJob(showPictureModelingJobRequest); System.out.println("ShowPictureModelingJobResponse:" + response.toString()); return response; } catch (ConnectionException | RequestTimeoutException e) { System.out.println("there is some error, exception:" + e); } return null; } /** * 创建鉴权凭证 * * @param ak 租户帐号对应的 Access Key(AK) * @param sk 租户帐号对应的 Secret Access Key(SK) * @return 鉴权凭证 */ public static ICredential getCredential(String ak, String sk, String projectId) { return new BasicCredentials() .withAk(ak) .withSk(sk) .withProjectId(projectId); } /** * 创建MetaStudio client * * @param region region信息 * @param auth 鉴权凭证 * @return MetaStudio client */ public static MetaStudioClient getClient(Region region, ICredential auth) { // 使用默认配置 HttpConfig config = HttpConfig.getDefaultHttpConfig(); config.withIgnoreSSLVerification(true); // 初始化metaStudio服务的客户端 return MetaStudioClient.newBuilder() .withHttpConfig(config) .withCredential(auth) .withRegion(region) // 选择服务所在区域 .build(); } } ``` ## 参考 更多信息请参考: [API Explorer](https://console.huaweicloud.com/apiexplorer/#/openapi/MetaStudio/doc?api=CreateTtsa) [《华为云MetaStudio开发指南》](https://support.huaweicloud.com/api-metastudio/metastudio_02_0000.html) ## 修订记录 | 发布日期 | 文档版本 | 修订说明 | | :--------: | :------: | :----------: | | 2023-04-12 | 1.0 | 文档首次发布 |