Development Guide

App Development

Importing Mandatory Classes

Add the classes related to video portrait segmentation to the project.
import com.huawei.hiai.vision.image.segmentation.ImageSegmentation;//Load the image segmentation method class.
import com.huawei.hiai.vision.common.VisionImage;//Load the input data class.
import com.huawei.hiai.vision.visionkit.image.ImageResult;//Load the returned result class.
import com.huawei.hiai.pdk.resultcode.HwHiAIResultCode;//Load the returned result class.
 import com.huawei.hiai.vision.image.segmentation.SegConfiguration;//Load the configuration class.
import com.huawei.hiai.vision.visionkit.common.VisionConfiguration;
import com.huawei.hiai.vision.common.VisionImageMetadata;
import com.huawei.hiai.vision.common.VisionBase;//Load the static class for connecting to the service.
import com.huawei.hiai.vision.common.ConnectionCallback;//Load the callback for connecting to the service.
import com.huawei.hiai.vision.common.VisionCallback;
import com.huawei.hiai.pdk.pluginservice.ILoadPluginCallback;

Developing the Function

  1. Perform initialization by using the VisionBase static class, to obtain a connection to the service.
    VisionBase.init(MainActivity.this, new ConnectionCallback(){
        @Override
        public void onServiceConnect(){
            Log.i(LOG_TAG, "onServiceConnect");
        }
    
        @Override
        public void onServiceDisconnect(){
            Log.i(LOG_TAG, "onServiceDisconnect");
        }
    });
  2. Define the mImageSegmentation instance, and use the context of this app as the input parameter.
    ImageSegmentation mImageSegmentation = new ImageSegmentation(mContext);
  3. Configure the parameters.
    SegConfiguration mSegmentationConfiguration = new SegConfiguration.Builder()
        .setProcessMode(VisionConfiguration.MODE_IN)
        .setSegmentationType(SegConfiguration.TYPE_PORTRAIT_SEGMENTATION_VIDEO)
        .setOutputType(SegConfiguration.OUTPUT_TYPE_BYTEARRAY)
        .build();
    mImageSegmentation.setConfiguration(mSegmentationConfiguration);
  4. Define VisionImageMetadata. Images cannot be input for video portrait segmentation. The default rotation angle is 270°.
    VisionImageMetadata.Builder builder = new VisionImageMetadata.Builder();
    builder.setFormat(17);/NV21 format
    builder.setHeight(mHeight);
    builder.setWidth(mWidth);
    builder.setRotation(mRotation);
    VisionImageMetadata metadata = builder.build();
  5. Set the byte array to be portrait-segmented.
    VisionImage image = VisionImage.fromByteArray(mybytes, metadata);
  6. Download the plug-in in lazyload mode. Call getAvailability() to check whether the plug-in is required. If yes, call loadPlugin() to download the plug-in.
    int availability = imageSegmentation.getAvailability();
    if (availability == HwHiAIResultCode.AIRESULT_PLUGIN_PENDING_UPDATE) {
        Lock lock = new ReentrantLock();
    	Condition condition = lock.newCondition();
    	LoadPluginCallback cb = new LoadPluginCallback(lock, condition);
    	imageSegmentation.loadPlugin(cb);
    	lock.lock();
    	try {
    	    condition.await(90, TimeUnit.SECONDS);
    	} catch (InterruptedException e) {
    	    Log.e(TAG, e.getMessage());
    	} finally {
    	    lock.unlock();     
    	}
    }
  7. Call doSegmentation to perform segmentation.
    ImageResult srt = new ImageResult();
    int rltCode = mImageSegmentation.doSegmentation(image, srt, null);
  8. Call getBytes to obtain the segmentation result.
    byte[] values = srt.getByteArray();
  9. Generate a Bitmap for saving the segmentation result.
    private static final int A_CHANNEL_PIXEL_MASK = 0xFF;
    private static final int A_CHANNEL_RIGHT_SHIFT_INDEX = 24;
    private static final int HUMAN_VALUE_THRESHOLD = 50;
    private static final int HUMAN_PIXEL_VALUE = 0xFF0000FF;
    private static final int BACKGROUND_PIXEL_VALUE = 0xFF000000;
    
    int pixels[] = new int[mWidth * mHeight];
    int out_pixels[] = new int[mWidth * mHeight];
    for (int i = 0; i < pixels.length; ++i) {
        pixels[i] = values[i];
    }
    for (int pixelIdx = 0; pixelIdx < mWidth * mHeight; pixelIdx++) {
        int pixelValue = pixels[pixelIdx];
        if (((pixelValue >> A_CHANNEL_RIGHT_SHIFT_INDEX) & A_CHANNEL_PIXEL_MASK) < HUMAN_VALUE_THRESHOLD ) {
            out_pixels[pixelIdx] = BACKGROUND_PIXEL_VALUE;
        } else {
            out_pixels[pixelIdx] = HUMAN_PIXEL_VALUE;
        }
    }
    Bitmap segmentedBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
    setgmentedBitmap.setPixels(out_pixels, 0, mWidth, 0, 0, mWidth, mHeight);
    saveMyBitmap(filename, segmentedBitmap);
搜索
请输入您想要搜索的关键词