All Products
Search
Document Center

Platform For AI:AI Portrait: Use the Java SDK

Last Updated:Mar 11, 2026

Train custom LoRA models and generate AI portraits using the Java SDK for AI Portrait service.

Prerequisites

  • Java environment is installed.

  • 5 to 20 training images and one template image prepared. Supported formats: .jpg, .jpeg, and .png.

    • For solo portraits, template image must contain a single face. Training images must show the same person.

    • For multi-person portraits, template image must contain multiple faces matching the number of model_id values used for training.

    • Image dimensions must be greater than 512 × 512 pixels.

Setup

  1. Add the aiservice SDK dependency to the <dependencies> section of your pom.xml file:

    <dependency>
     <groupId>com.aliyun.openservices.aiservice</groupId>
     <artifactId>aiservice-sdk</artifactId>
     <version>1.0.1</version>
    </dependency>
  1. Initialize the client.

    import com.aliyun.openservices.aiservice.ApiClient;
    
    public class AIGCImageTest {
     public static ApiClient apiClient;
     static {
     String host = 'HOST';
     String appId = 'YOUR-APPID';
     String token = 'YOUR-TOKEN';
     apiClient = new ApiClient(host, appId, token);
     }
    }

    Replace the following parameters:

    Parameter

    Description

    <HOST>

    Server-side endpoint: http://ai-service.ce8cc13b6421545749e7b4605f3d02607.cn-hangzhou.alicontainer.com.

    <YOUR-APPID>

    Obtain from the AI Portrait page after activation.

    <YOUR-TOKEN>

    Obtain from the AI Portrait page after activation.

For more information, see the open source code on GitHub.

API examples

AI Portrait involves two stages: model training (several minutes) and portrait creation (tens of seconds).

image

Check images (api.aigcImagesCheck)

  • Sample request:

    import com.aliyun.openservices.aiservice.api;
    import com.aliyun.openservices.aiservice.ApiClient;
    import com.aliyun.openservices.aiservice.ApiException;
    import com.aliyun.openservices.aiservice.model.AIGCCreatRequest;
    import com.aliyun.openservices.aiservice.model.AIGCImageCheckResponse;
    import com.aliyun.openservices.aiservice.model.AIGCImageCreateResponse;
    import com.aliyun.openservices.aiservice.model.Response;
    
    import java.util.Arrays;
    import java.util.List;
    
    public class AIGCImageTest {
        public static ApiClient apiClient;
        private  AigcImagesApi api ;
        static {
            String host  = 'HOST';
            String appId = 'YOUR-APPID';
            String token = 'YOUR-TOKEN';
            apiClient = new ApiClient(host, appId, token);
            api = new AigcImagesApi(apiClient);
        }
    
        public void aigcImagesCheckTest() throws Exception {
            // The URLs of the training images.
            List<String> images =Arrays.asList(
                "https://xxx/0.jpg",
                "https://xxx/1.jpg",
                "https://xxx/2.jpg"
            );
    
            AIGCImageCheckResponse response = api.aigcImagesCheck(images);
            // Request ID.
            String request_id = response.getRequestId();
            // Request status.
            String code = response.getCode();
            // Details of the request status.
            String message = response.getMessage();
            // Content of the response.
            AIGCImageCheckData data = response.getData();
    
            // The code of the check result.
            List<AIGCImageCheckResult> CheckResultsList =  response.getCheckResults();
            for(int i=0; i < CheckResultsList.size(); i++ ){
                AIGCImageCheckResult result = CheckResultsList.get(i);
                Integer ResultCode = result.getCode();
                System.out.println(ResultCode);
            }
        }
    
    }

    Parameters

    Parameter

    Description

    images

    List of image URLs (List<String>).

    <HOST>

    Server-side endpoint: http://ai-service.ce8cc13b6421545749e7b4605f3d02607.cn-hangzhou.alicontainer.com.

    <YOUR-APPID>

    Obtain from the AI Portrait page after activation.

    <YOUR-TOKEN>

    Obtain from the AI Portrait page after activation.

  • Sample response:

    Response type: AIGCImageCheckResponse

    {
    	requestId: 3c0eeb6b-1faf-4dc4-8f9a-9a02486051c6
    	code: OK
    	message: success
    	data:
    		AIGCImageCheckData{
    			requestId = '3c0eeb6b-1faf-4dc4-8f9a-9a02486051c6', 
     			images = ["https://xxx/0.jpg",
     								"https://xxx/1.jpg",
     								"https://xxx/2.jpg"], 
    		costTime=0.5460958480834961, 
     		checkResults=[
     			AIGCImageCheckResult{
     				code=1, 
     				frontal=false, 
     				url='https://xxx/0.jpg',
     				message='success'}, 
     			AIGCImageCheckResult{
            code=1, 
              frontal=true, 
              url='https://xxx/1.jpg',
              message='success'}, 
          AIGCImageCheckResult{
            code=4,
            frontal=false, 
            url='https://xxx/2.jpg',
            message='Image detect error.'}
      		]
    		}
    }

    Response parameters

    Parameter

    Description

    Type

    requestId

    Request ID.

    String

    code

    Request status code. Valid values:

    • OK: Request successful.

    • error: Request failed.

    String

    message

    Request status details. "success" indicates successful request.

    String

    data

    Returned data details.

    AIGCImageCheckData

    Data field (AIGCImageCheckData)

    Parameter

    Description

    Type

    checkResults

    Detection result for each image. Contains url (image URL), message (detection details), and frontal (whether frontal view).

    List<AIGCImageCheckResult>

    costTime

    Server-side processing time (seconds).

    float

    images

    URLs of checked images.

    List<String>

    requestId

    Request ID (same as parent request_id).

    String

    Check result messages:

    message

    Status code

    Description

    success

    1

    The requirements are met.

    Image decode error.

    2

    The image cannot be downloaded or decoded.

    Number of face is not 1.

    3

    The number of faces in the image is not 1.

    Image detect error.

    4

    An error occurred during face detection.

    Image encoding error.

    5

    An error occurred when encoding the face into a feature vector. This indicates that no face was detected.

    This photo is not the same person in photos.

    6

    This fault indicates that the faces in multiple images do not belong to the same person.

Train model (api.aigcImagesTrain)

  • Sample request:

    package com.aliyun.aisdk;
    
    import com.aliyun.openservices.aiservice.api.AigcImagesApi;
    import com.aliyun.openservices.aiservice.ApiClient;
    import com.aliyun.openservices.aiservice.ApiException;
    import com.aliyun.openservices.aiservice.model.*;
    
    import java.util.Arrays;
    import java.util.List;
    
    import java.io.IOException;
    
    public class AIGCImageTrain {
      public String host = 'HOST';
      public String appId = 'YOUR-APPID';
      public String token = 'YOUR-TOKEN';
    
      public ApiClient apiClient = new ApiClient(host, appId, token);
      public AigcImagesApi api = new AigcImagesApi(apiClient);
    
      public void aigcImagesTrainTest() throws Exception {
        List<String> images =Arrays.asList(
          "https://xxx/0.jpg",
          "https://xxx/1.jpg",
          "https://xxx/2.jpg"
        );
        AIGCImageTrainResponse response = api.aigcImagesTrain(images);
        int jobId = response.getData().getJobId();
        String modelId = response.getData().getModelId();
        String message = response.getMessage();
        InlineResponse200Data Data = response.getData();
    
        System.out.println(response);
        System.out.println(response.getMessage());
    
        System.out.println("jobId:" + jobId);
        System.out.println("modelId:" + modelId);
        System.out.println("Data:" + Data);
      }
    }

    Parameters

    Parameter Location

    Description

    images

    A list of image URLs. Type: List<String>.

    <HOST>

    The server-side address: http://ai-service.ce8cc13b6421545749e7b4605f3d02607.cn-hangzhou.alicontainer.com.

    <YOUR-APPID>

    Obtain from the AI Portrait page after activation.

    <YOUR-TOKEN>

    Obtain from the AI Portrait page after activation.

  • Sample response:

    Response type: AIGCImageTrainResponse

    {
      requestId: 2bd438df-2358-4852-b6b0-bf7d39b6dde7
      code: OK
      message: success
      data: class InlineResponse200Data {
              	jobId: xxxx
                modelId: xxxx-xxxxxxxx-xxxxxx
              }
    }

  • Response fields:

    Description of the data field

    Parameter Name

    Description

    Type

    jobId

    The task ID.

    int

    modelId

    The ID of the model from this training task. It is a 36-character string.

    String

    • Use the job_id to query the training result.

    • Provide the model_id to call the portrait creation service.

Query training status (jobApi.getAsyncJob)

  • Sample request:

    import com.aliyun.openservices.aiservice.api.AiServiceJobApi;
    import com.aliyun.openservices.aiservice.api.AigcImagesApi;
    import com.aliyun.openservices.aiservice.ApiClient;
    import com.aliyun.openservices.aiservice.ApiException;
    import com.aliyun.openservices.aiservice.model.*;
    
    
    import java.util.Arrays;
    import java.util.List;
    
    import java.io.IOException;
    
    public class AIGCJobCheck {
    	public void aigcJobStateGet() throws Exception {
        Integer jobId = new Integer(xxxx); // Asynchronous task ID
    
        AiServiceJobApi jobApi = new AiServiceJobApi(apiClient);
    
        AsyncJobResponse jobResponse = jobApi.getAsyncJob(jobId);
        String request_id = jobResponse.getRequestId();
        String job_code = jobResponse.getCode();
        String job_message = jobResponse.getMessage();
        Map<String, AsyncJobData> job_data = jobResponse.getData();
    
        String Result_string = (String) job_data.get("job").getResult();
        JsonObject jsonObject = new JsonParser().parse(Result_string).getAsJsonObject();
        JsonArray result_states = jsonObject.get("states").getAsJsonArray();
    
        for (int result_idx=0; result_idx < result_states.size(); result_idx++){
          JsonObject result_one = result_states.get(result_idx).getAsJsonObject();
          String result_url = result_one.get("url").getAsString();
        }
      }
    }
  • Parameters:

    Parameter

    Type

    Description

    jobId

    Integer

    The ID of the training task.

  • Sample responses:

    • If the model training is in progress, the following response is returned:

      {
          requestId: 9a76c77d-c241-4691-8c93-fc6953fb668c
          code: OK
          message: success
          data: {
                job=AsyncJobData{
                  id=12746, 
                  appId='xxxxxxxxxx', 
                  state=1, 
                  message='model requesting', 
                  result="", 
                  requestId='111a6503-c2f7-4141-b17b-f8567e6a0a5f'
                  }
          }
      }
    • When the model training is complete, the following response is returned:

      {
            requestId: 0fc513d1-5a9e-48e1-9b6f-2eca7c0b62e9
            code: OK
            message: success
              data: {
                job=AsyncJobData{
                  id=12744, 
                  appId='xxxxxxxxxxx', 
                  state=2, 
                  message='success', 
                  result={
                    "cost_time":232.83351230621338,
                    "model_id":"xxxxxxxxxxxx",
                    "states":[{"code":1,
                               "frontal":true,
                               "message":"success",
                               "url":"https://xxxx/train/1.jpg"}]
                  }, 
                  requestId='83146ee3-68aa-40f7-b523-06f029e1db15'
                }
              }
      }
  • Description of return values

    Parameter name

    Description

    Type

    requestId

    The request ID.

    String

    code

    The request status code. OK indicates success. error indicates failure.

    String

    message

    Details of the request status. A value of success indicates that the request is successful. For other values, see the specific returned content.

    String

    data

    The details of the returned data.

    Map<String, AsyncJobData>

    Description of the job field in data

    Parameter Name

    Description

    Type

    id

    The task ID, which is the job_id.

    int

    appId

    The user's AppId.

    String

    state

    The task status code:

    • 0: Initializing.

    • 1: Running.

    • 2: Succeeded.

    • 3: Failed.

    int

    message

    Information about the task execution.

    String

    Result

    The result returned by the model.

    String

    Description of the model result (type: String)

    Parameter

    Description

    cost_time

    The total time consumed by this training task.

    states

    The check result for each image.

    The detection result for each input image. Each image corresponds to a dictionary that contains three keys: url, message, and frontal. These keys represent the image URL, image detection details, and whether the image is a frontal view, respectively.

    model_id

    The LoRA model name.

    This is the same model_id obtained from the training request and is used as an input for portrait creation.

  • Related error codes

    • The error codes for service requests are as follows:

      HTTP status code

      code

      message

      Description

      400

      PARAMETER_ERROR

      not found appid

      The AppId is incorrect.

      401

      PARAMETER_ERROR

      sign error

      The token is incorrect.

      404

      PARAMETER_ERROR

      model not found

      The corresponding model service is not deployed.

    • Query results can contain the following error codes:

      HTTP status code

      code

      message

      Description

      462

      error

      Invalid input data

      Failed to parse the input data.

      462

      error

      Image not provided

      No training images were provided.

      462

      error

      Make dir in oss Error.

      Failed to create a folder in OSS. Check if OSS is mounted.

      462

      error

      Image process error.

      An error occurred during image pre-processing.

      469

      error

      Training - Not get best template image

      The training task exited unexpectedly and failed to generate a reference image.

      469

      error

      Training - Not get lora weight

      The training task exited unexpectedly and failed to generate the LoRA weight.

Create portrait

  • Sample requests:

    • API for solo portrait creation (api.aigcImagesCreate)

      • Prediction with Stable Diffusion 1.5

        import com.aliyun.openservices.aiservice.api.AiServiceJobApi;
        import com.aliyun.openservices.aiservice.api.AigcImagesApi;
        import com.aliyun.openservices.aiservice.ApiClient;
        import com.aliyun.openservices.aiservice.ApiException;
        import com.aliyun.openservices.aiservice.model.*;
        
        
        import java.io.FileOutputStream;
        import java.io.OutputStream;
        import java.util.Arrays;
        import java.util.List;
        
        import java.io.IOException;
        
        import sun.misc.BASE64Decoder;
        import sun.misc.BASE64Encoder;
        
        public class AIGCImageService {
          public String host = 'HOST';
          public String appId = 'YOUR-APPID';
          public String token = 'YOUR-TOKEN';
        
          public ApiClient apiClient = new ApiClient(host, appId, token);
          public AigcImagesApi api = new AigcImagesApi(apiClient);
        
          public void aigcImageCreateGet() throws Exception {
            String modelId = "xxx-xxxx";
            String templateImage = "https://xxxx.jpg";
            String model_name = "";
            Map<String, Object> configure = new TreeMap<String, Object>();
        
            AIGCImageCreateResponse createResponse = api.aigcImagesCreate(modelId, templateImage, model_name, configure);
        
            // Request ID.
            String request_id = createResponse.getRequestId();
            // Request status.
            String code = createResponse.getCode();
            // Details of the request status.
            String message = createResponse.getMessage();
            // Content of the response.
            AIGCImageCreateData data = createResponse.getData();
        
            // The Base64-encoded generated image.
            String imgStr = createResponse.getData().getImage();
        
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] imgBtyes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < imgBtyes.length; ++i) {
              // Adjust abnormal data.
              if (imgBtyes[i] < 0) {
                imgBtyes[i] += 256;
              }
            }
        
            String imgFilePath = "test_single.jpg";
            OutputStream out = new FileOutputStream(imgFilePath);
        
            out.write(imgBtyes);
            out.flush();
            out.close();
          }
        }

        Parameters:

        Parameter

        Type

        Description

        modelId

        String

        The name of the LoRA model. Enter the model-id obtained from training.

        Set this to "" when using the ipa_control_only mode.

        templateImage

        String

        The URL path of the template.

        model_name

        String

        The model name. The default is an empty string.

        configure

        Map<String, Object>

        The model return configuration. The default is None.

        <HOST>

        String

        The server-side address: http://ai-service.ce8cc13b6421545749e7b4605f3d02607.cn-hangzhou.alicontainer.com.

        <YOUR-APPID>

        String

        After you activate AI Portrait, you can view the AppId on the AI Portrait page.

        <YOUR-TOKEN>

        String

        After you activate AI Portrait, you can view the token on the AI Portrait page.

        Parameters in configure

        Configuring internal parameter names

        Description

        Type

        Required

        Default value

        Value range

        lora_weights

        The LoRA weight.

        float

        No

        0.90

        0.5 to 1.0

        first_denoising_strength

        The intensity of the first image reconstruction.

        float

        No

        0.45

        0.0 to 1.0

        second_denoising_strength

        The intensity of the second image reconstruction.

        float

        No

        0.30

        0.0 to 1.0

        more_like_me

        This is more like an individual's own strength.

        float

        No

        0.50

        0.0 to 1.0

        crop_face_preprocess

        Specifies whether to crop the image before reconstruction. We recommend that you enable this for large images.

        bool

        No

        True

        True, False

        apply_face_fusion_before

        Specifies whether to perform the first portrait merging.

        bool

        No

        True

        True, False

        apply_face_fusion_after

        Specifies whether to perform the second portrait merging.

        bool

        No

        True

        True, False

        color_shift_middle

        Specifies whether to perform the first color correction.

        bool

        No

        True

        True, False

        color_shift_last

        Specifies whether to perform the second color correction.

        bool

        No

        True

        True, False

        background_restore

        Specifies whether to reconstruct the background.

        bool

        No

        False

        True, False

        skin_retouching_bool

        Specifies whether to perform skin smoothing.

        If enabled, the skin is smoothed and brightened, which generally makes the image more appealing but may result in overly white skin. If disabled, the skin texture is enhanced.

        bool

        No

        False

        True, False

        photo_enhancement_bool

        Specifies whether to perform portrait enhancement.

        If enabled, portrait restoration or super resolution is performed to improve the quality of the generated image.

        bool

        No

        True

        True, False

        photo_enhancement_method

        The portrait enhancement method.

        photo_fix performs image restoration. This may cause some distortion but fixes unreasonable parts and may reduce skin texture.

        super_resolution only performs image super resolution, which better preserves the original image.

        String

        No

        photo_fix

        photo_fix, super_resolution

        makeup_transfer

        Specifies whether to perform makeup transfer.

        If enabled, makeup is transferred to prevent the image from looking too plain. However, this may cause the image to look less like the user.

        bool

        No

        False

        True, False

        makeup_transfer_ratio

        The intensity of the makeup transfer.

        A higher value results in a stronger makeup transfer effect, making the generated makeup look more like the template. However, this may also cause the image to look less like the user.

        float

        No

        0.5

        0.0 to 1.0

        face_shape_match

        Specifies whether to adapt the face shape.

        If enabled, the control intensity is reduced, and the face shape and skin texture will more closely match the user.

        bool

        No

        False

        True, False

        ipa_control

        Specifies whether to enable IPA control.

        If enabled, the portrait similarity is increased, but the result is more susceptible to the influence of the reference image.

        bool

        No

        False

        True, False

        ipa_control_only

        If enabled, you can perform prediction without training.

        If enabled, specify ipa_image_path.

        bool

        No

        False

        True, False

        ipa_image_path

        The URL of the reference portrait.

        This is required if ipa_control_only is enabled.

        String

        No

        None

        A downloadable URL

        ipa_weight

        The IPA control intensity.

        A higher value makes the output image look more like the user, but an excessively high value can cause image distortion.

        float

        No

        0.5

        0.0 to 1.0

        style_name

        Specifies the style for the generated output.

        Available in realistic or cartoon styles.

        str

        No

        Realistic

        Realistic,

        Anime

        lcm_accelerate

        Specifies whether to use LCM acceleration for the generation style.

        bool

        No

        False

        True, False

        sharp_ratio

        The sharpening degree. An appropriate value can improve clarity.

        An excessively high value can cause image distortion.

        float

        No

        0.15

        0.0 to 1.0

        t2i_prompt

        If you want to use the text-to-image feature, set the prompt here.

        If you set t2i_prompt, you do not need to pass the URL path of the template.

        String

        No

        None

        N/A

      • Inference with Stable Diffusion XL

        import com.aliyun.openservices.aiservice.api.AiServiceJobApi;
        import com.aliyun.openservices.aiservice.api.AigcImagesApi;
        import com.aliyun.openservices.aiservice.ApiClient;
        import com.aliyun.openservices.aiservice.ApiException;
        import com.aliyun.openservices.aiservice.model.*;
        
        
        import java.io.FileOutputStream;
        import java.io.OutputStream;
        import java.util.Arrays;
        import java.util.List;
        
        import java.io.IOException;
        
        import sun.misc.BASE64Decoder;
        import sun.misc.BASE64Encoder;
        
        public class AIGCImageService {
          public String host = 'HOST';
          public String appId = 'YOUR-APPID';
          public String token = 'YOUR-TOKEN';
        
          public ApiClient apiClient = new ApiClient(host, appId, token);
          public AigcImagesApi api = new AigcImagesApi(apiClient);
        
          public void aigcImageCreateGet() throws Exception {
            String modelId = "xxx-xxxx";
            String templateImage = "https://xxxx.jpg";
            String model_name = "create_xl";
            Map<String, Object> configure = new TreeMap<String, Object>();
        
            AIGCImageCreateResponse createResponse = api.aigcImagesCreate(modelId, templateImage, model_name, configure);
        
            // Request ID.
            String request_id = createResponse.getRequestId();
            // Request status.
            String code = createResponse.getCode();
            // Details of the request status.
            String message = createResponse.getMessage();
            // Content of the response.
            AIGCImageCreateData data = createResponse.getData();
        
            // The Base64-encoded generated image.
            String imgStr = createResponse.getData().getImage();
        
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] imgBtyes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < imgBtyes.length; ++i) {
              // Adjust abnormal data.
              if (imgBtyes[i] < 0) {
                imgBtyes[i] += 256;
              }
            }
        
            String imgFilePath = "test_single.jpg";
            OutputStream out = new FileOutputStream(imgFilePath);
        
            out.write(imgBtyes);
            out.flush();
            out.close();
          }
        }

        Parameters:

        Parameter Location

        Type

        Description

        modelId

        string

        The name of the LoRA model. Enter the model-id obtained from training. Set this to "" when using the ipa_control_only mode.

        templateImage

        string

        The URL path of the template.

        When generating with scene_lora or a prompt, set this to "t2i_generate".

        model_name

        string

        The model name. To use Stable Diffusion XL, set this to create_xl.

        configure

        Map<String, Object>

        The model returns the `configure` configuration, which is `None` by default.

        Parameters in configure

        Configuring internal parameter names

        Description

        Type

        Required

        Default value

        Value range

        lora_weights

        The application intensity of LoRA.

        A higher value makes the output image look more like the user, but an excessively high value can cause image distortion.

        float

        No

        0.90

        0.5 to 1.0

        first_diffusion_steps

        The number of steps for the first diffusion.

        We do not recommend that you modify this parameter. A value that is too low can cause image distortion.

        int

        No

        50

        20-50

        first_denoising_strength

        The intensity of the first image reconstruction.

        The reconstruction intensity for the face. A higher value results in more reconstruction. In theory, a higher value makes the output image look more like the user, but an excessively high value can make the image look unnatural.

        float

        No

        0.45

        0.0 to 1.0

        second_diffusion_steps

        The number of steps for the second diffusion.

        We do not recommend that you modify this parameter. A value that is too low can cause image distortion.

        int

        No

        30

        20-50

        second_denoising_strength

        The intensity of the second image reconstruction.

        The reconstruction intensity for the edges of the face. An excessively high value can make the image look unnatural.

        float

        No

        0.30

        0.0 to 1.0

        more_like_me

        This is more my strong suit.

        The portrait merging ratio. A higher value makes the image look more like the user, but an excessively high value reduces the realism of the image.

        float

        No

        0.60

        0.0 to 1.0

        crop_face_preprocess

        Specifies whether to crop the image before reconstruction. We recommend that you enable this for large images.

        We do not recommend that you adjust this parameter.

        bool

        No

        True

        True, False

        apply_face_fusion_before

        Specifies whether to perform the first portrait merging.

        If enabled, portrait merging is performed. If disabled, the similarity is reduced.

        bool

        No

        True

        True, False

        apply_face_fusion_after

        Specifies whether to perform the second portrait merging.

        If enabled, portrait merging is performed. If disabled, the similarity is reduced.

        bool

        No

        True

        True, False

        color_shift_middle

        Specifies whether to perform the first color correction.

        If enabled, color correction is performed to make the skin tone of the output image more like the template. If disabled, color correction is not performed, and color deviation may occur.

        bool

        No

        True

        True, False

        color_shift_last

        Specifies whether to perform the second color correction.

        If enabled, color correction is performed to make the skin tone of the output image more like the template. If disabled, color correction is not performed, and color deviation may occur.

        bool

        No

        True

        True, False

        background_restore

        Specifies whether to reconstruct the background.

        If enabled, the background is reconstructed. This can make the image look more natural but changes the background and increases the processing time.

        bool

        No

        False

        True, False

        skin_retouching_bool

        Specifies whether to perform skin smoothing.

        If enabled, the skin is smoothed and brightened, which generally makes the image more appealing but may result in overly white skin. If disabled, the skin texture is enhanced.

        bool

        No

        False

        True, False

        photo_enhancement_bool

        Specifies whether to perform portrait enhancement.

        If enabled, portrait restoration or super resolution is performed to improve the quality of the generated image.

        bool

        No

        True

        True, False

        photo_enhancement_method

        The portrait enhancement method:

        photo_fix performs image restoration. This may cause some distortion but fixes unreasonable parts and may reduce skin texture.

        super_resolution only performs image super resolution, which better preserves the original image.

        String

        No

        photo_fix

        photo_fix, super_resolution

        makeup_transfer

        Specifies whether to perform makeup transfer.

        If enabled, makeup is transferred to prevent the image from looking too plain. However, this may cause the image to look less like the user.

        bool

        No

        False

        True, False

        makeup_transfer_ratio

        The intensity of the makeup transfer.

        A higher value results in a stronger makeup transfer effect, making the generated makeup look more like the template. However, this may also cause the image to look less like the user.

        float

        No

        0.5

        0.0 to 1.0

        ipa_control

        Specifies whether to enable IPA control.

        If enabled, the portrait similarity is increased, but the result is more susceptible to the influence of the reference image.

        bool

        No

        False

        True, False

        ipa_control_only

        If enabled, you can perform prediction without training.

        If enabled, you must specify ipa_image_path.

        bool

        No

        False

        True, False

        ipa_image_path

        The URL of the reference portrait.

        This is required if ipa_control_only is enabled.

        String

        No

        None

        A downloadable URL

        ipa_weight

        The IPA control intensity.

        A higher value makes the output image look more like the user, but an excessively high value can cause image distortion.

        float

        No

        0.5

        0.0 to 1.0

        lcm_accelerate

        Specifies whether to use LCM acceleration for the generation style.

        bool

        No

        False

        True, False

        sharp_ratio

        The sharpening degree. An appropriate value can improve clarity.

        An excessively high value can cause image distortion.

        float

        No

        0.15

        0.0 to 1.0

        scene_id

        If you want to use Scene LoRA, set the scene ID here.

        The scene ID is obtained from training. The model ID returned when training a Stable Diffusion XL Scene LoRA is the scene ID.

        String

        No

        None

        N/A

        t2i_prompt

        If you want to use the text-to-image feature, set the prompt here.

        If you set t2i_prompt, you do not need to pass the URL path of the template.

        String

        No

        None

        N/A

    • API for multi-person portrait creation (api.aigcImagesCreateByMultiModelIds)

      import com.aliyun.openservices.aiservice.api.AiServiceJobApi;
      import com.aliyun.openservices.aiservice.api.AigcImagesApi;
      import com.aliyun.openservices.aiservice.ApiClient;
      import com.aliyun.openservices.aiservice.ApiException;
      import com.aliyun.openservices.aiservice.model.*;
      
      
      import java.io.FileOutputStream;
      import java.io.OutputStream;
      import java.util.Arrays;
      import java.util.List;
      
      import java.io.IOException;
      
      import sun.misc.BASE64Decoder;
      import sun.misc.BASE64Encoder;
      
      public class AIGCImageService {
        public String host = 'HOST';
        public String appId = 'YOUR-APPID';
        public String token = 'YOUR-TOKEN';
      
        public ApiClient apiClient = new ApiClient(host, appId, token);
        public AigcImagesApi api = new AigcImagesApi(apiClient);
      
        public void aigcImageCreateMulti() throws Exception {
          String[] modelIds = new String[]{"model-id1","model-id2"};
          String templateImage = "https://xxxx.jpg";
          String model_name = "";
          Map<String, Object> configure = new TreeMap<String, Object>();
      
          AIGCImageCreateResponse createResponse = api.aigcImagesCreateByMultiModelIds(model_id, template_image, model_name, config);
      
          // Request ID.
          String request_id = createResponse.getRequestId();
          // Request status.
          String code = createResponse.getCode();
          // Details of the request status.
          String message = createResponse.getMessage();
          // Content of the response.
          AIGCImageCreateData data = createResponse.getData();
      
          // The Base64-encoded generated image.
          String imgStr = createResponse.getData().getImage();
      
          BASE64Decoder decoder = new BASE64Decoder();
          byte[] imgBtyes = decoder.decodeBuffer(imgStr);
          for (int i = 0; i < imgBtyes.length; ++i) {
            // Adjust abnormal data.
            if (imgBtyes[i] < 0) {
              imgBtyes[i] += 256;
            }
          }
      
          String imgFilePath = "test_multi.jpg";
          OutputStream out = new FileOutputStream(imgFilePath);
      
          out.write(imgBtyes);
          out.flush();
          out.close();
        }
      }

      Parameter

      Type

      Description

      modelId

      String

      The model ID of the trained model.

      templateImage

      String

      The URL path of the template.

      model_name

      String

      The model name. The default is an empty string.

      configure

      Map<String, Object>

      The model return configuration. The default is None.

      <HOST>

      String

      The server-side address: http://ai-service.ce8cc13b6421545749e7b4605f3d02607.cn-hangzhou.alicontainer.com.

      <YOUR-APPID>

      String

      After you activate AI Portrait, you can view the AppId on the AI Portrait page.

      <YOUR-TOKEN>

      String

      After you activate AI Portrait, you can view the token on the AI Portrait page.

  • Sample response:

    {
     requestId: 5eb7741b-540b-4a5c-9c98-fdd1d714e51f
     code: OK
     message: success
     data: com.aliyun.openservices.aiservice.model.AIGCImageCreateData@358c99f5
    }

    Description of the data field (type: AIGCImageCreateData)

    Parameter

    Description

    Type

    costTime

    The time taken for generation.

    Float

    image

    The Base64-encoded image.

    String

  • Error codes for portrait creation

    • Service request errors

      HTTP status code

      code

      message

      Description

      400

      PARAMETER_ERROR

      not found appid

      The AppId is incorrect.

      401

      PARAMETER_ERROR

      sign error

      The token is incorrect.

      404

      PARAMETER_ERROR

      model not found

      The corresponding model service is not deployed.

    • Result query errors

      HTTP status code

      code

      message

      Description

      462

      error

      Invalid input data. Please check the input dict.

      Failed to parse the input data.

      462

      error

      Image not provided. Please check the template_image.

      The template image for portrait creation was not provided.

      462

      error

      Prompts get error. Please check the model_id.

      Check the format of the provided model_id.

      462

      error

      Face id image decord error. Pleace check the user's lora is trained or not.

      An error occurred when decoding the user-uploaded image. Check whether the model is trained.

      462

      error

      Roop image decord error. Pleace check the user's lora is trained or not.

      The Roop image does not exist. Check whether the model is trained.

      462

      error

      Template image decode error. Please Give a new template.

      An error occurred while decoding the template image. Provide a new template.

      462

      error

      There is not face in template. Please Give a new template.

      No face was found in the template image. Provide a new template.

      462

      error

      Template image process error. Please Give a new template.

      An error occurred during template image pre-processing. Provide a new template image.

      469

      error

      First Face Fusion Error, Can't get face in template image.

      An error occurred during the first portrait merging.

      469

      error

      First Stable Diffusion Process error. Check the webui status.

      An error occurred during the first Stable Diffusion process.

      469

      error

      Second Face Fusion Error, Can't get face in template image.

      An error occurred during the second portrait merging.

      469

      error

      Second Stable Diffusion Process error. Check the webui status.

      An error occurred during the second Stable Diffusion process.

      469

      error

      Please confirm if the number of faces in the template corresponds to the user ID.

      Check if the number of user IDs provided matches the number of faces.

      469

      error

      Third Stable Diffusion Process error. Check the webui status.

      An error occurred during background processing. Replace the template.

Complete workflow example

Complete example that generates an AI portrait image in the current directory:

  • Standard flow (Stable Diffusion 1.5)

    package com.aliyun.aisdk;
    
    import com.aliyun.openservices.aiservice.api.AiServiceJobApi;
    import com.aliyun.openservices.aiservice.api.AigcImagesApi;
    import com.aliyun.openservices.aiservice.ApiClient;
    import com.aliyun.openservices.aiservice.ApiException;
    import com.aliyun.openservices.aiservice.model.*;
    
    
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.util.Arrays;
    import java.util.List;
    
    import java.io.IOException;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    public class AIGCImageRunner {
    
      public String host = 'HOST';
      public String appId = 'YOUR-APPID';
      public String token = 'YOUR-TOKEN';
    
      public ApiClient apiClient = new ApiClient(host, appId, token);
      public AigcImagesApi api = new AigcImagesApi(apiClient);
    
      public byte[] base64ToBytes(String imgStr) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] imgBtyes = decoder.decodeBuffer(imgStr);
        for (int i = 0; i < imgBtyes.length; ++i) {
          // Adjust abnormal data.
          if (imgBtyes[i] < 0) {
            imgBtyes[i] += 256;
          }
        }
    
        return imgBtyes;
      }
    
      public void aigcImagesCheck(List<String> images) throws Exception{
        AIGCImageCheckResponse response = api.aigcImagesCheck(images);
      }
    
      public Object[] aigcImagesTrainRun(List<String> images) throws Exception {
    
        AIGCImageTrainResponse response = api.aigcImagesTrain(images);
        int jobId = response.getData().getJobId();
    
        Object[] trainOut = new Object[2];
    
        System.out.println(response);
        System.out.println(response.getMessage());
    
        System.out.println("jobId:" + jobId);
        System.out.println("modelId:" + response.getData().getModelId());
        System.out.println(response.getData());
    
        trainOut[0]=jobId;
        trainOut[1] = response.getData().getModelId();
        return trainOut;
      }
    
      public Integer aigcJobStateGet(AiServiceJobApi jobApi, int jobId_int) throws Exception {
        Integer jobId = new Integer(jobId_int); // Asynchronous task ID.
    
        AsyncJobResponse jobResponse = jobApi.getAsyncJob(jobId);
    
        System.out.println(jobResponse.getData().get("job").getResult());
    
        return jobResponse.getData().get("job").getState();
      }
    
      public void CreateSingle(String modelId, String templateImage) throws Exception {
    
        AIGCImageCreateResponse createResponse = api.aigcImagesCreate(modelId, templateImage);
    
        // The Base64-encoded generated image.
        String imgStr = createResponse.getData().getImage();
        System.out.println(createResponse.getData());
    
        byte[] imgBtyes = base64ToBytes(imgStr);
    
        String imgFilePath = "test_single.jpg";
        OutputStream out = new FileOutputStream(imgFilePath);
    
        out.write(imgBtyes);
        out.flush();
        out.close();
      }
      
      public void CreateMulti(String[] model_ids, String template_image)throws ApiException, IOException {
            
        		String imgFilePath = "test_multi.jpg";
        		AIGCImageCreateResponse createResponse = api.aigcImagesCreateByMultiModelIds(model_ids, template_image, model_name, config);
    
            // Request ID.
            String request_id = createResponse.getRequestId();
            // Request status.
            String code = createResponse.getCode();
            // Details of the request status.
            String message = createResponse.getMessage();
            // Content of the response.
            AIGCImageCreateData data = createResponse.getData();
    
            if (!code.equals("OK")){
                System.out.printf("aigc_images_create failed, model_id is %s, request_id is %s\n",model_ids,request_id);
            }else {
                String imgStr = createResponse.getData().getImage();
                byte[] image = base64ToBytes(imgStr);
    
              	
                OutputStream out = new FileOutputStream(output_image);
    
                out.write(image);
                out.flush();
                out.close();
    
            }
        }
    
    
      public void aigcEndtoEndCreate() throws Exception {
        List<String> images =Arrays.asList(
          "https://xxx/0.jpg",
          "https://xxx/1.jpg",
          "https://xxx/2.jpg"
        );
        String templateImage = "https://xxx.jpg";
        String multiTemplateImage = "https://xxx.jpg";
    
        Object[] o = aigcImagesTrainRun(images);
        int jobId = (int)o[0];
        String modelId = (String)o[1];
        
        AiServiceJobApi jobApi = new AiServiceJobApi(apiClient);
    
        while(true){
          Integer jobState = aigcJobStateGet(jobApi, jobId);
    
          if (jobState == AsyncJobState.JOB_STATE_WAIT) { // Job is running.
            System.out.println("job running");
          } else if (jobState == AsyncJobState.JOB_STATE_SUCCESS) {
            System.out.println("job success");
            break;
          } else {
            System.out.println("job fail");
            throw new Exception("job fail");
          }
    
          try {
            Thread.sleep(30000);
          } catch (InterruptedException e) {
            throw new RuntimeException(e);
          }
        }
        
        
    
        CreateSingle(modelId,templateImage);
    
    
        String[] modelIds = new String[]{modelId,modelId};
        CreateMulti(model_ids, template_image)
    
      }
    
    }

    Parameters:

    Parameter

    Description

    <HOST>

    The server-side address: http://ai-service.ce8cc13b6421545749e7b4605f3d02607.cn-hangzhou.alicontainer.com.

    <YOUR-APPID>

    After you activate AI Portrait, you can view the AppId on the AI Portrait page.

    <YOUR-TOKEN>

    After you activate AI Portrait, you can view the token on the AI Portrait page.

    images

    The URLs of the images used to train the model. Separate multiple URLs with commas (,).

    templateImage

    The URL of the template image, which contains a single face. This is used for solo portrait creation.

    multiTemplateImage

    The URL of the template image, which contains multiple faces. The number of faces must match the number of model_id values provided. This is used for multi-person portrait creation.

  • Standard flow (Stable Diffusion XL)

    To use Stable Diffusion XL model, contact PAI team to activate the service first. After activation, specify the model name.

    package com.aliyun.aiservice.demo;
    
    import com.aliyun.openservices.aiservice.ApiClient;
    import com.aliyun.openservices.aiservice.ApiException;
    import com.aliyun.openservices.aiservice.api.AiServiceJobApi;
    import com.aliyun.openservices.aiservice.api.AigcImagesApi;
    import com.aliyun.openservices.aiservice.model.*;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    import org.junit.Test;
    import sun.misc.BASE64Decoder;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.*;
    
    
    public class PhotoEndtoEndTest {
    
        public String host = 'HOST';
      	public String appId = 'YOUR-APPID';
      	public String token = 'YOUR-TOKEN';
      
        public ApiClient apiClient = new ApiClient(host, appId, token);
        public AigcImagesApi api = new AigcImagesApi(apiClient);
        public AiServiceJobApi jobApi = new AiServiceJobApi(apiClient);
    
        public byte[] base64ToBytes(String imgStr) throws IOException {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] imgBtyes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < imgBtyes.length; ++i) {
                // Adjust abnormal data.
                if (imgBtyes[i] < 0) {
                    imgBtyes[i] += 256;
                }
            }
    
            return imgBtyes;
        }
    
        public boolean Check(List<String> images) throws ApiException {
            AIGCImageCheckResponse response = this.api.aigcImagesCheck(images);
            // Request ID.
            String request_id = response.getRequestId();
            // Request status.
            String code = response.getCode();
            // Details of the request status.
            String message = response.getMessage();
            // Content of the response.
            AIGCImageCheckData data = response.getData();
    
            // Print the returned result.
            boolean is_ok = false;
            if (!code.equals("OK")){
                System.out.printf("aigc_images_check failed,request id is %\n", request_id);
            }else{
                is_ok = true;
                System.out.printf("check images done, input %d images, return %d images, %d filtered by lvwang\n",
                        images.size(),(data.getCheckResults().size()),(images.size()-data.getCheckResults().size()));
                for (int check_result_idx=0; check_result_idx < data.getCheckResults().size();check_result_idx++ ){
                    AIGCImageCheckResult checkResult = data.getCheckResults().get(check_result_idx);
                    Integer checkResultCode = checkResult.getCode();
                    if (checkResultCode.equals(1)){
                        System.out.printf("check %s success\n", checkResult.getUrl());
                    }else {
                        is_ok = false;
                        System.out.printf("check %s failed, message is %s , request_id is %s\n",
                                checkResult.getUrl(),checkResult.getMessage(),request_id);
                    }
                }
            }
            return is_ok;
        }
    
        public Object[] Train(List<String> images, String model_name, Map<String, Object> config) throws ApiException {
            Integer job_id = -1;
            String model_id = "";
    
            AIGCImageTrainResponse response = this.api.aigcImagesTrain(images,model_name,config);
            // Request ID.
            String request_id = response.getRequestId();
            // Request status.
            String code = response.getCode();
            // Details of the request status.
            String message = response.getMessage();
            // Content of the response.
            InlineResponse200Data Data = response.getData();
    
            // Print the returned result.
            if (!code.equals("OK")){
                System.out.printf("aigc_images_train failed, request id is %s\n", request_id);
    
            }else{
                job_id = response.getData().getJobId();
                model_id = response.getData().getModelId();
    
                System.out.printf("train job_id is %d, model id %s\n",job_id.intValue(),model_id);
    
            }
            Integer state = -1;
            while(true){
                AsyncJobResponse jobResponse = this.jobApi.getAsyncJob(job_id);
                String job_code = jobResponse.getCode();
                String job_message = jobResponse.getMessage();
                Map<String, AsyncJobData> job_data = jobResponse.getData();
    
                if (!job_code.equals("OK")){
                    System.out.printf("get_async_job failed, request id is %s, message is %s\n",
                            request_id, job_message);
                    job_id = new Integer(-1);
                    model_id = "";
    
                }else{
                    state = job_data.get("job").getState();
                    if (state.equals(2)){
                        System.out.printf("model %s trained successfully\n",model_id);
                        break;
                    }else if(!state.equals(3)){
                        System.out.printf("training model %s\n",model_id);
    
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                    }else{
                        System.out.printf("model %s trained failed, message: %s\n",model_id,job_message);
                        break;
                    }
                }
    
            }
    
            if (!state.equals(2)){
                model_id = "";
            }
    
            Object[] out = new Object[2];
            out[0] = job_id;
            out[1] = model_id;
    
            return out;
    
        }
    
        public String[] QueryValidImageUrlsByJob(Integer job_id) throws ApiException {
            String[] image_urls = null;
    
            AsyncJobResponse jobResponse = this.jobApi.getAsyncJob(job_id);
            String request_id = jobResponse.getRequestId();
            String job_code = jobResponse.getCode();
            String job_message = jobResponse.getMessage();
            Map<String, AsyncJobData> job_data = jobResponse.getData();
    
            if (!job_code.equals("OK")) {
                System.out.printf("get_async_job failed, request id is %s, message is %s\n",
                        request_id, job_message);
            }else {
                Integer state = job_data.get("job").getState();
                if (state == 2){
                    System.out.printf("Job %s trained successfully\n", job_id);
                    String Result_string = (String) job_data.get("job").getResult();
                    JsonObject jsonObject = new JsonParser().parse(Result_string).getAsJsonObject();
                    JsonArray result_states = jsonObject.get("states").getAsJsonArray();
                    image_urls = new String[result_states.size()];
                    for (int result_idx=0; result_idx < result_states.size(); result_idx++){
                        JsonObject result_one = result_states.get(result_idx).getAsJsonObject();
                        String result_url = result_one.get("url").getAsString();
                        image_urls[result_idx] = result_url;
                    }
    
                }else{
                    System.out.printf("job %s not ready\n",job_id);
                }
            }
    
            return image_urls;
        }
    
        public boolean Create(String model_id, String template_image, String output_image, String model_name, Map<String, Object> config) throws IOException {
            System.out.println("Create");
            AIGCImageCreateResponse createResponse = null;
            try{
                createResponse = api.aigcImagesCreate(model_id, template_image, model_name, config);
            }catch (ApiException e){
                System.out.println();
                System.out.println(e.getResponseBody());
            }
            System.out.println(createResponse);
            // Request ID.
            String request_id = createResponse.getRequestId();
            // Request status.
            String code = createResponse.getCode();
            // Details of the request status.
            String message = createResponse.getMessage();
            // Content of the response.
            AIGCImageCreateData data = createResponse.getData();
    
            if (!code.equals("OK")){
                System.out.printf("aigc_images_create failed, model_id is %s, request_id is %s\n",model_id,request_id);
            }else {
                String imgStr = createResponse.getData().getImage();
                byte[] image = base64ToBytes(imgStr);
    
                OutputStream out = new FileOutputStream(output_image);
    
                out.write(image);
                out.flush();
                out.close();
    
            }
            return true;
        }
    
        public boolean CreateMulti(String[] model_ids, String template_image, String output_image, String model_name, Map<String, Object> config) throws ApiException, IOException {
            AIGCImageCreateResponse createResponse = api.aigcImagesCreateByMultiModelIds(model_ids, template_image, model_name, config);
    
            // Request ID.
            String request_id = createResponse.getRequestId();
            // Request status.
            String code = createResponse.getCode();
            // Details of the request status.
            String message = createResponse.getMessage();
            // Content of the response.
            AIGCImageCreateData data = createResponse.getData();
    
            if (!code.equals("OK")){
                System.out.printf("aigc_images_create failed, model_id is %s, request_id is %s\n",model_ids,request_id);
            }else {
                String imgStr = createResponse.getData().getImage();
                byte[] image = base64ToBytes(imgStr);
    
                OutputStream out = new FileOutputStream(output_image);
    
                out.write(image);
                out.flush();
                out.close();
    
            }
            return true;
        }
    
        @Test
        public void aigcEndtoEndCreate() throws Exception {
            List<String> images = Arrays.asList(
                    "https://xxx/0.jpg",
                    "https://xxx/1.jpg",
                    "https://xxx/2.jpg"
            );
    
            
    
            String template_image = "https://xxx.jpg";
            String multi_template_image = "https://xxx.jpg";
          
            String model_name = "train_xl";
            Map<String, Object> config = new HashMap<String, Object>();
    
            Object[] train_out = Train(images, model_name, config);
    
            Integer job_id= (Integer) train_out[0];
            String model_id =  (String) train_out[1];
    
    
    
            String[] model_ids = {model_id,model_id};
            model_name = "create_xl"; //""
            Map<String, Object> configure = new TreeMap<String, Object>();
    
            Create(model_id, template_image,"single_out.jpg", model_name,configure);
    
            CreateMulti(model_ids, multi_template_image,"multi_out.jpg", model_name,configure);
    
    
        }
    
    }

  • Create an AI portrait using a single reference image (no model training required)

    package com.aliyun.aiservice.demo;
    
    import com.aliyun.openservices.aiservice.ApiClient;
    import com.aliyun.openservices.aiservice.ApiException;
    import com.aliyun.openservices.aiservice.api.AiServiceJobApi;
    import com.aliyun.openservices.aiservice.api.AigcImagesApi;
    import com.aliyun.openservices.aiservice.model.*;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    import org.junit.Test;
    import sun.misc.BASE64Decoder;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.*;
    
    public class PhotoReferCreateTest {
        public String host = 'HOST';
      	public String appId = 'YOUR-APPID';
      	public String token = 'YOUR-TOKEN';
      
        public ApiClient apiClient = new ApiClient(host, appId, token);
        public AigcImagesApi api = new AigcImagesApi(apiClient);
        public AiServiceJobApi jobApi = new AiServiceJobApi(apiClient);
    
        public byte[] base64ToBytes(String imgStr) throws IOException {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] imgBtyes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < imgBtyes.length; ++i) {
                // Adjust abnormal data.
                if (imgBtyes[i] < 0) {
                    imgBtyes[i] += 256;
                }
            }
    
            return imgBtyes;
        }
    
        public boolean Check(List<String> images) throws ApiException {
            AIGCImageCheckResponse response = this.api.aigcImagesCheck(images);
            // Request ID.
            String request_id = response.getRequestId();
            // Request status.
            String code = response.getCode();
            // Details of the request status.
            String message = response.getMessage();
            // Content of the response.
            AIGCImageCheckData data = response.getData();
    
            // Print the returned result.
            boolean is_ok = false;
            if (!code.equals("OK")){
                System.out.printf("aigc_images_check failed,request id is %\n", request_id);
            }else{
                is_ok = true;
                System.out.printf("check images done, input %d images, return %d images, %d filtered by lvwang\n",
                        images.size(),(data.getCheckResults().size()),(images.size()-data.getCheckResults().size()));
                for (int check_result_idx=0; check_result_idx < data.getCheckResults().size();check_result_idx++ ){
                    AIGCImageCheckResult checkResult = data.getCheckResults().get(check_result_idx);
                    Integer checkResultCode = checkResult.getCode();
                    if (checkResultCode.equals(1)){
                        System.out.printf("check %s success\n", checkResult.getUrl());
                    }else {
                        is_ok = false;
                        System.out.printf("check %s failed, message is %s , request_id is %s\n",
                                checkResult.getUrl(),checkResult.getMessage(),request_id);
                    }
                }
            }
            return is_ok;
        }
    
    
    
        public boolean Create(String template_image, String output_image, String ref_image) throws IOException {
            System.out.println("Create");
            AIGCImageCreateResponse createResponse = null;
    
            Map<String, Object> config = new TreeMap<String, Object>();
    
            config.put("ipa_control_only",true);
            config.put("ipa_weight",0.6);
            config.put("ipa_image_path",ref_image);
            try{
                createResponse = api.aigcImagesCreate("", template_image, "", config);
            }catch (ApiException e){
                System.out.println();
                System.out.println(e.getResponseBody());
            }
            System.out.println(createResponse);
            // Request ID.
            String request_id = createResponse.getRequestId();
            // Request status.
            String code = createResponse.getCode();
            // Details of the request status.
            String message = createResponse.getMessage();
            // Content of the response.
            AIGCImageCreateData data = createResponse.getData();
    
            if (!code.equals("OK")){
                System.out.printf("aigc_images_create failed, model_id is %s, request_id is %s\n",model_id,request_id);
            }else {
                String imgStr = createResponse.getData().getImage();
                byte[] image = base64ToBytes(imgStr);
    
                OutputStream out = new FileOutputStream(output_image);
    
                out.write(image);
                out.flush();
                out.close();
    
            }
            return true;
        }
    
        @Test
        public void aigcEndtoEndCreate() throws Exception {
    
            String template_image = "https://demo.jpg";
            String ref_image = "https://reference.jpg";
          
            Create(template_image,"ref_out.jpg", ref_image);
    
    
        }
    }
    

  • Create an AI portrait by generating a template image from a prompt and a single reference image (no template image required)

    package com.aliyun.aiservice.demo;
    
    import com.aliyun.openservices.aiservice.ApiClient;
    import com.aliyun.openservices.aiservice.ApiException;
    import com.aliyun.openservices.aiservice.api.AiServiceJobApi;
    import com.aliyun.openservices.aiservice.api.AigcImagesApi;
    import com.aliyun.openservices.aiservice.model.*;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    import org.junit.Ignore;
    import org.junit.Test;
    import sun.misc.BASE64Decoder;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.*;
    
    public class PhotoPtomptCreateTest {
    
        public String host = 'HOST';
      	public String appId = 'YOUR-APPID';
      	public String token = 'YOUR-TOKEN';
      
        public ApiClient apiClient = new ApiClient(host, appId, token);
        public AigcImagesApi api = new AigcImagesApi(apiClient);
        public AiServiceJobApi jobApi = new AiServiceJobApi(apiClient);
    
        public byte[] base64ToBytes(String imgStr) throws IOException {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] imgBtyes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < imgBtyes.length; ++i) {
                // Adjust abnormal data.
                if (imgBtyes[i] < 0) {
                    imgBtyes[i] += 256;
                }
            }
    
            return imgBtyes;
        }
    
        public boolean Check(List<String> images) throws ApiException {
            AIGCImageCheckResponse response = this.api.aigcImagesCheck(images);
            // Request ID.
            String request_id = response.getRequestId();
            // Request status.
            String code = response.getCode();
            // Details of the request status.
            String message = response.getMessage();
            // Content of the response.
            AIGCImageCheckData data = response.getData();
    
            // Print the returned result.
            boolean is_ok = false;
            if (!code.equals("OK")){
                System.out.printf("aigc_images_check failed,request id is %\n", request_id);
            }else{
                is_ok = true;
                System.out.printf("check images done, input %d images, return %d images, %d filtered by lvwang\n",
                        images.size(),(data.getCheckResults().size()),(images.size()-data.getCheckResults().size()));
                for (int check_result_idx=0; check_result_idx < data.getCheckResults().size();check_result_idx++ ){
                    AIGCImageCheckResult checkResult = data.getCheckResults().get(check_result_idx);
                    Integer checkResultCode = checkResult.getCode();
                    if (checkResultCode.equals(1)){
                        System.out.printf("check %s success\n", checkResult.getUrl());
                    }else {
                        is_ok = false;
                        System.out.printf("check %s failed, message is %s , request_id is %s\n",
                                checkResult.getUrl(),checkResult.getMessage(),request_id);
                    }
                }
            }
            return is_ok;
        }
    
        public Object[] Train(List<String> images, String model_name, Map<String, Object> config) throws ApiException {
            Integer job_id = -1;
            String model_id = "";
    
            AIGCImageTrainResponse response = this.api.aigcImagesTrain(images,model_name,config);
            // Request ID.
            String request_id = response.getRequestId();
            // Request status.
            String code = response.getCode();
            // Details of the request status.
            String message = response.getMessage();
            // Content of the response.
            InlineResponse200Data Data = response.getData();
    
            // Print the returned result.
            if (!code.equals("OK")){
                System.out.printf("aigc_images_train failed, request id is %s\n", request_id);
    
            }else{
                job_id = response.getData().getJobId();
                model_id = response.getData().getModelId();
    
                System.out.printf("train job_id is %d, model id %s\n",job_id.intValue(),model_id);
    
            }
            Integer state = -1;
            while(true){
                AsyncJobResponse jobResponse = this.jobApi.getAsyncJob(job_id);
                String job_code = jobResponse.getCode();
                String job_message = jobResponse.getMessage();
                Map<String, AsyncJobData> job_data = jobResponse.getData();
    
                if (!job_code.equals("OK")){
                    System.out.printf("get_async_job failed, request id is %s, message is %s\n",
                            request_id, job_message);
                    job_id = new Integer(-1);
                    model_id = "";
    
                }else{
                    state = job_data.get("job").getState();
                    if (state.equals(2)){
                        System.out.printf("model %s trained successfully\n",model_id);
                        break;
                    }else if(!state.equals(3)){
                        System.out.printf("training model %s\n",model_id);
    
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                    }else{
                        System.out.printf("model %s trained failed, message: %s\n",model_id,job_message);
                        break;
                    }
                }
    
            }
    
            if (!state.equals(2)){
                model_id = "";
            }
    
            Object[] out = new Object[2];
            out[0] = job_id;
            out[1] = model_id;
    
            return out;
    
        }
    
        public String[] QueryValidImageUrlsByJob(Integer job_id) throws ApiException {
            String[] image_urls = null;
    
            AsyncJobResponse jobResponse = this.jobApi.getAsyncJob(job_id);
            String request_id = jobResponse.getRequestId();
            String job_code = jobResponse.getCode();
            String job_message = jobResponse.getMessage();
            Map<String, AsyncJobData> job_data = jobResponse.getData();
    
            if (!job_code.equals("OK")) {
                System.out.printf("get_async_job failed, request id is %s, message is %s\n",
                        request_id, job_message);
            }else {
                Integer state = job_data.get("job").getState();
                if (state == 2){
                    System.out.printf("Job %s trained successfully\n", job_id);
                    String Result_string = (String) job_data.get("job").getResult();
                    JsonObject jsonObject = new JsonParser().parse(Result_string).getAsJsonObject();
                    JsonArray result_states = jsonObject.get("states").getAsJsonArray();
                    image_urls = new String[result_states.size()];
                    for (int result_idx=0; result_idx < result_states.size(); result_idx++){
                        JsonObject result_one = result_states.get(result_idx).getAsJsonObject();
                        String result_url = result_one.get("url").getAsString();
                        image_urls[result_idx] = result_url;
                    }
    
                }else{
                    System.out.printf("job %s not ready\n",job_id);
                }
            }
    
            return image_urls;
        }
    
        public boolean Create(String model_id, String t2i_prompt, String template_image) throws IOException {
            System.out.println("Create");
            AIGCImageCreateResponse createResponse = null;
            HashMap<String,Object> config = new HashMap<String, Object>();
            config.put("t2i_prompt", t2i_prompt);
            try{
                createResponse = api.aigcImagesCreate(model_id, template_image, "", config);
            }catch (ApiException e){
                System.out.println();
                System.out.println(e.getResponseBody());
            }
            System.out.println(createResponse);
            // Request ID.
            String request_id = createResponse.getRequestId();
            // Request status.
            String code = createResponse.getCode();
            // Details of the request status.
            String message = createResponse.getMessage();
            // Content of the response.
            AIGCImageCreateData data = createResponse.getData();
    
            if (!code.equals("OK")){
                System.out.printf("aigc_images_create failed, model_id is %s, request_id is %s\n",model_id,request_id);
            }else {
                String imgStr = createResponse.getData().getImage();
                byte[] image = base64ToBytes(imgStr);
    
                OutputStream out = new FileOutputStream("prompt_out.jpg");
    
                out.write(image);
                out.flush();
                out.close();
    
            }
            return true;
        }
    
    
    
        @Test
        public void aigcEndtoEndCreate() throws Exception {
    
    
            List<String> images =Arrays.asList(
          			"https://xxx/0.jpg",
          			"https://xxx/1.jpg",
          			"https://xxx/2.jpg"
        		);
    
            String template_image = "https://demo.jpg";
    
            String model_name = "";
            Map<String, Object> config = new HashMap<String, Object>();
    
            Object[] train_out = Train(images, model_name, config);
    
            Integer job_id= (Integer) train_out[0];
            String model_id =  (String) train_out[1];
    
            String t2i_prompt = "(portrait:1.5), 1girl, bokeh, bouquet, brown_hair, cloud, flower, hairband, hydrangea, lips, long_hair, outdoors, sunlight, white_flower, white_rose, green sweater, sweater, (cloth:1.0), (best quality), (realistic, photo-realistic:1.3), film photography, minor acne, (portrait:1.1), (indirect lighting), extremely detailed CG unity 8k wallpaper, enormous filesize, best quality, realistic, photo-realistic, ultra high res, raw photo, put on makeup";
            Create(model_id, t2i_prompt, template_image);
    
        }
    
    }
    

  • Create an AI portrait by generating a template image from a prompt and a single reference image (no template image or model training required)

    package com.aliyun.aiservice.demo;
    
    import com.aliyun.openservices.aiservice.ApiClient;
    import com.aliyun.openservices.aiservice.ApiException;
    import com.aliyun.openservices.aiservice.api.AiServiceJobApi;
    import com.aliyun.openservices.aiservice.api.AigcImagesApi;
    import com.aliyun.openservices.aiservice.model.*;
    import com.google.gson.JsonArray;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    import org.junit.Ignore;
    import org.junit.Test;
    import sun.misc.BASE64Decoder;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.*;
    
    public class PhotoPtomptCreateTest {
    
        public String host = 'HOST';
      	public String appId = 'YOUR-APPID';
      	public String token = 'YOUR-TOKEN';
      
        public ApiClient apiClient = new ApiClient(host, appId, token);
        public AigcImagesApi api = new AigcImagesApi(apiClient);
        public AiServiceJobApi jobApi = new AiServiceJobApi(apiClient);
    
        public byte[] base64ToBytes(String imgStr) throws IOException {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] imgBtyes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < imgBtyes.length; ++i) {
                // Adjust abnormal data.
                if (imgBtyes[i] < 0) {
                    imgBtyes[i] += 256;
                }
            }
    
            return imgBtyes;
        }
    
        public boolean Check(List<String> images) throws ApiException {
            AIGCImageCheckResponse response = this.api.aigcImagesCheck(images);
            // Request ID.
            String request_id = response.getRequestId();
            // Request status.
            String code = response.getCode();
            // Details of the request status.
            String message = response.getMessage();
            // Content of the response.
            AIGCImageCheckData data = response.getData();
    
            // Print the returned result.
            boolean is_ok = false;
            if (!code.equals("OK")){
                System.out.printf("aigc_images_check failed,request id is %\n", request_id);
            }else{
                is_ok = true;
                System.out.printf("check images done, input %d images, return %d images, %d filtered by lvwang\n",
                        images.size(),(data.getCheckResults().size()),(images.size()-data.getCheckResults().size()));
                for (int check_result_idx=0; check_result_idx < data.getCheckResults().size();check_result_idx++ ){
                    AIGCImageCheckResult checkResult = data.getCheckResults().get(check_result_idx);
                    Integer checkResultCode = checkResult.getCode();
                    if (checkResultCode.equals(1)){
                        System.out.printf("check %s success\n", checkResult.getUrl());
                    }else {
                        is_ok = false;
                        System.out.printf("check %s failed, message is %s , request_id is %s\n",
                                checkResult.getUrl(),checkResult.getMessage(),request_id);
                    }
                }
            }
            return is_ok;
        }
    
        public Object[] Train(List<String> images, String model_name, Map<String, Object> config) throws ApiException {
            Integer job_id = -1;
            String model_id = "";
    
            AIGCImageTrainResponse response = this.api.aigcImagesTrain(images,model_name,config);
            // Request ID.
            String request_id = response.getRequestId();
            // Request status.
            String code = response.getCode();
            // Details of the request status.
            String message = response.getMessage();
            // Content of the response.
            InlineResponse200Data Data = response.getData();
    
            // Print the returned result.
            if (!code.equals("OK")){
                System.out.printf("aigc_images_train failed, request id is %s\n", request_id);
    
            }else{
                job_id = response.getData().getJobId();
                model_id = response.getData().getModelId();
    
                System.out.printf("train job_id is %d, model id %s\n",job_id.intValue(),model_id);
    
            }
            Integer state = -1;
            while(true){
                AsyncJobResponse jobResponse = this.jobApi.getAsyncJob(job_id);
                String job_code = jobResponse.getCode();
                String job_message = jobResponse.getMessage();
                Map<String, AsyncJobData> job_data = jobResponse.getData();
    
                if (!job_code.equals("OK")){
                    System.out.printf("get_async_job failed, request id is %s, message is %s\n",
                            request_id, job_message);
                    job_id = new Integer(-1);
                    model_id = "";
    
                }else{
                    state = job_data.get("job").getState();
                    if (state.equals(2)){
                        System.out.printf("model %s trained successfully\n",model_id);
                        break;
                    }else if(!state.equals(3)){
                        System.out.printf("training model %s\n",model_id);
    
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
    
                    }else{
                        System.out.printf("model %s trained failed, message: %s\n",model_id,job_message);
                        break;
                    }
                }
    
            }
    
            if (!state.equals(2)){
                model_id = "";
            }
    
            Object[] out = new Object[2];
            out[0] = job_id;
            out[1] = model_id;
    
            return out;
    
        }
    
        public String[] QueryValidImageUrlsByJob(Integer job_id) throws ApiException {
            String[] image_urls = null;
    
            AsyncJobResponse jobResponse = this.jobApi.getAsyncJob(job_id);
            String request_id = jobResponse.getRequestId();
            String job_code = jobResponse.getCode();
            String job_message = jobResponse.getMessage();
            Map<String, AsyncJobData> job_data = jobResponse.getData();
    
            if (!job_code.equals("OK")) {
                System.out.printf("get_async_job failed, request id is %s, message is %s\n",
                        request_id, job_message);
            }else {
                Integer state = job_data.get("job").getState();
                if (state == 2){
                    System.out.printf("Job %s trained successfully\n", job_id);
                    String Result_string = (String) job_data.get("job").getResult();
                    JsonObject jsonObject = new JsonParser().parse(Result_string).getAsJsonObject();
                    JsonArray result_states = jsonObject.get("states").getAsJsonArray();
                    image_urls = new String[result_states.size()];
                    for (int result_idx=0; result_idx < result_states.size(); result_idx++){
                        JsonObject result_one = result_states.get(result_idx).getAsJsonObject();
                        String result_url = result_one.get("url").getAsString();
                        image_urls[result_idx] = result_url;
                    }
    
                }else{
                    System.out.printf("job %s not ready\n",job_id);
                }
            }
    
            return image_urls;
        }
    
        public boolean Create(String t2i_prompt, String template_image, String ref_image) throws IOException {
            System.out.println("Create");
            AIGCImageCreateResponse createResponse = null;
            HashMap<String,Object> config = new HashMap<String, Object>();
    
            config.put("t2i_prompt", t2i_prompt);
            config.put("ipa_control_only", true);
            config.put("ipa_weight", 0.6);
            config.put("ipa_image_path", ref_image);
    
            try{
                createResponse = api.aigcImagesCreate("", template_image, "", config);
            }catch (ApiException e){
                System.out.println();
                System.out.println(e.getResponseBody());
            }
            System.out.println(createResponse);
            // Request ID.
            String request_id = createResponse.getRequestId();
            // Request status.
            String code = createResponse.getCode();
            // Details of the request status.
            String message = createResponse.getMessage();
            // Content of the response.
            AIGCImageCreateData data = createResponse.getData();
    
            if (!code.equals("OK")){
                System.out.printf("aigc_images_create failed, request_id is %s\n",request_id);
            }else {
                String imgStr = createResponse.getData().getImage();
                byte[] image = base64ToBytes(imgStr);
    
                OutputStream out = new FileOutputStream("ref_prompt_out.jpg");
    
                out.write(image);
                out.flush();
                out.close();
    
            }
            return true;
        }
    
    
    
        @Test
        public void aigcEndtoEndCreate() throws Exception {
    
            String template_image = "https://demo.jpg";
            String ref_image = "https://reference.jpg";
    
    
            String t2i_prompt = "(portrait:1.5), 1girl, bokeh, bouquet, brown_hair, cloud, flower, hairband, hydrangea, lips, long_hair, outdoors, sunlight, white_flower, white_rose, green sweater, sweater, (cloth:1.0), (best quality), (realistic, photo-realistic:1.3), film photography, minor acne, (portrait:1.1), (indirect lighting), extremely detailed CG unity 8k wallpaper, enormous filesize, best quality, realistic, photo-realistic, ultra high res, raw photo, put on makeup";
            Create(t2i_prompt, template_image, ref_image);
    
        }
    
    }