Unity3d without green screen keying processing

1. Unity3d can use the somatosensory tool to cut out images, but there will be glitches. Use the third-party SDK to realize the cutout processing without a green screen. The habitual effect is first: (the effect is processed perfectly)

2. This article uses Tencent Cloud Portrait Segmentation to search after entering Tencent Cloud: Portrait Segmentation

 3. For details, you can study and upload the code first

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. The image is converted to base64, and the base64 to image code is as follows:

/// <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. The key can be obtained in Tencent Cloud

 6. Note: The resolution of the picture shall not exceed 2000*2000, and the size shall not exceed 5M. PNG, JPG, JPEG, BMP are supported, and GIF pictures are not supported.

Guess you like

Origin blog.csdn.net/m0_38116456/article/details/130990273