unity接入阿里云人体分割SDK

需求是调用外部相机拍照,上传照片用AI抠出人像,实现替换照片背景。

本来已经用百度云AI实现了,无绿幕背景使用正常,最近有客户使用绿幕背景,发现绿幕背景时抠图边缘绿色没抠干净,可能是光线问题,但客户展厅无法修改光照条件,测试了一下阿里云的,效果不错。

阿里云链接:人体分割SegmentBody的语法及示例_视觉智能开放平台(VIAPI)-阿里云帮助中心本文介绍分割抠图(imageseg)类目下的人体分割SegmentBody的语法及示例。icon-default.png?t=O83Ahttps://help.aliyun.com/zh/viapi/developer-reference/api-q897dp?spm=a2c4g.11186623.0.0.35c3191cBqMqYP官方调试脚本修改了一下

// 安装依赖包
// dotnet add package AlibabaCloud.SDK.Imageseg20191230
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Imageseg20191230.Models;
using LitJson;
using Tea;
using Tea.Utils;
using UnityEngine;
using UnityEngine.Networking;

public class SubmitPhoto : MonoBehaviour
{
    private static Queue<string> downloadQueue = new Queue<string>();
    private static Action<Texture2D,string> onCallback;

    void Update()
    {
        if (downloadQueue.Count > 0)
        {
            string url = downloadQueue.Dequeue();
            Debug.Log("下载处理后的图片");
            StartCoroutine(DownloadImage(url, (texture2d) =>
            {
                onCallback?.Invoke(texture2d, url);
            }));
        }
    }

    public static IEnumerator SubmitImage(string filePath, Action<Texture2D,string> callback)
    {
        // 参数检查
        if (string.IsNullOrEmpty(filePath))
        {
            Debug.LogError("File path cannot be null or empty.");
            callback?.Invoke(null, "File path is invalid.");
            yield break;
        }

        onCallback = callback;

        yield return null;

        AlibabaCloud.SDK.Imageseg20191230.Client client = CreateClient("你的AccessKey ID", "你的AccessKey Secret");
            
        AlibabaCloud.SDK.Imageseg20191230.Models.SegmentBodyAdvanceRequest segmentBodyAdvanceRequest = new AlibabaCloud.SDK.Imageseg20191230.Models.SegmentBodyAdvanceRequest
        ();
        // 场景一,使用本地文件
        StreamReader file = new StreamReader(@filePath);
        segmentBodyAdvanceRequest.ImageURLObject = file.BaseStream;


        AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
        try
        {
            SegmentBodyResponse SegmentBodyResponse = client.SegmentBodyAdvance(segmentBodyAdvanceRequest, runtime);
            // 获取整体结果
            string jsonResponse = AlibabaCloud.TeaUtil.Common.ToJSONString(SegmentBodyResponse.Body);
            Debug.Log("jsonResponse:" + jsonResponse);
            
            downloadQueue.Enqueue(SegmentBodyResponse.Body.Data.ImageURL);

           
        }
        catch (TeaException error)
        {
            // 如有需要,请打印 error
            Debug.Log(AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message));
            callback?.Invoke(null, null);
        }
        catch (Exception _error)
        {
            TeaException error = new TeaException(new Dictionary<string, object>
              {
                { "message", _error.Message }
              });
            
            // 如有需要,请打印 error
            Debug.Log(AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message));
            callback?.Invoke(null, null);
        }

    }

    /**
        * 使用AK&SK初始化账号Client
        * @param accessKeyId
        * @param accessKeySecret
        * @return Client
        * @throws Exception
        */
    public static AlibabaCloud.SDK.Imageseg20191230.Client CreateClient(string accessKeyId, string accessKeySecret)
    {
        AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
        {
            AccessKeyId = accessKeyId,
            AccessKeySecret = accessKeySecret,
        };
        // 访问的域名
        config.Endpoint = "imageseg.cn-shanghai.aliyuncs.com";
        return new AlibabaCloud.SDK.Imageseg20191230.Client(config);
    }

    /// <summary>
    /// 加载图片
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="downloadEnd"></param>
    /// <returns></returns>
    private static IEnumerator DownloadImage(string url, Action<Texture2D> complate)
    {
        using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(url))
        {
            UnityWebRequestAsyncOperation asyncOperation = request.SendWebRequest();
            yield return asyncOperation;
            if (asyncOperation.webRequest.isHttpError || asyncOperation.webRequest.isNetworkError)
            {
                Debug.LogError(asyncOperation.webRequest.error);
            }
            else
            {
                Texture2D texture = DownloadHandlerTexture.GetContent(asyncOperation.webRequest);
                complate?.Invoke(texture);
            }
        }
    }
}

调用方法

 /// <summary>
    /// 应用结果
    /// </summary>
    private void ApplyResult()
    {
        //显示处理中…… 
        //if (processingFx.activeInHierarchy) return;
        //processingFx.SetActive(true);

        //上传到阿里云,等待结果
        Debug.Log("上传图片");
        StartCoroutine(SubmitPhoto.SubmitImage(photoPath, (result, alyurl) =>
        {
            if (result == null)
            {
                // 给出错误提示
                obj提示.SetActive(true);
                //3秒后关闭提示
                Invoke("again", 3);
            }
            else
            {
                //写入本地
                byte[] imageTytes = result.EncodeToPNG();
                File.WriteAllBytes(Application.dataPath + "/Folder02/" + TimeIcon() + ".png", imageTytes);
               //显示在UI上
                img.texture = result;              
                                
#if UNITY_EDITOR
                AssetDatabase.Refresh();
#endif
            }

        }));
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值