グリーン スクリーン キーイング処理なしの Unity3d

1. Unity3d は体性感覚ツールを使用して画像を切り出すことができますが、不具合が発生します。グリーン スクリーンなしで切り抜き処理を実現するには、サードパーティの SDK を使用してください。習慣的な効果が最初です: (効果が完全に処理されます)

2. この記事では、Tencent Cloud ポートレート セグメンテーションを使用して、Tencent Cloud に入った後の検索を行います: ポートレート セグメンテーション

 3. 詳細については、まずコードを調べてアップロードしてください。

void CutoutPicture()
    {
        try
        {
            Credential cred = new Credential
            {
                SecretId = TencentKey.SecretId,
                SecretKey = TencentKey.SecretKey,
            };
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.Endpoint = ("bda.tencentcloudapi.com");
            clientProfile.HttpProfile = httpProfile;

            // 实例化要请求产品的client对象,clientProfile是可选的
            BdaClient client = new BdaClient(cred, "ap-shanghai", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SegmentPortraitPicRequest req = new SegmentPortraitPicRequest();

            //req.Image=""接受图片两种格式:
            //1.一种是服务地址:https://
            //2.base64格式:/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJ...

            //本文采用将本地图片转化为base64
            req.Image = ImageOrBase.Instance.ImgToBase64String(imgPath);
            // 返回的resp是一个SegmentPortraitPicResponse的实例,与请求对象对应
            SegmentPortraitPicResponse resp = client.SegmentPortraitPicSync(req);

            //将resp.ResultImage返回的base64转化为图片
            ImageOrBase.Instance.Base64ToImg(resp.ResultImage);
        }
        catch (Exception e)
        {
            Debug.Log(e.ToString());
        }

4. 画像は Base64 に変換されます。Base64 から画像へのコードは次のとおりです。

/// <summary>
    /// 图片转换成base64编码
    /// </summary>
    public string ImgToBase64String(string path)
    {
        try
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, (int)fs.Length);
            string base64String = Convert.ToBase64String(buffer);
            return base64String;
        }
        catch (Exception e)
        {
            Debug.Log("ImgToBase64String 转换失败:" + e.Message);
        }
        return null;
    }

    /// <summary>
    /// base64编码文本转换成图片
    /// </summary>
    public void Base64ToImg(string recordBase64String)
    {
        string base64 = recordBase64String;
        byte[] bytes = Convert.FromBase64String(base64);
        Texture2D tex2D = new Texture2D(100, 100);
        tex2D.LoadImage(bytes);
        Sprite s = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f));
        imagePic.sprite = s;
        imagePic.SetNativeSize();
        Resources.UnloadUnusedAssets();
    }

5. キーは Tencent Cloud で取得できます

 6. 注: 画像の解像度は 2000*2000 を超えてはならず、サイズは 5M を超えてはなりません。PNG、JPG、JPEG、BMP はサポートされていますが、GIF 画像はサポートされていません。

Supongo que te gusta

Origin blog.csdn.net/m0_38116456/article/details/130990273
Recomendado
Clasificación