需求是调用外部相机拍照,上传照片用AI抠出人像,实现替换照片背景。
本来已经用百度云AI实现了,无绿幕背景使用正常,最近有客户使用绿幕背景,发现绿幕背景时抠图边缘绿色没抠干净,可能是光线问题,但客户展厅无法修改光照条件,测试了一下阿里云的,效果不错。
// 安装依赖包
// 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
}
}));
}
547

被折叠的 条评论
为什么被折叠?



