⭐ Unity + 佐糖抠图

佐糖抠图API

智能检测图像中的前景主体并识别轮廓,实现精细化背景分割抠图,一键抠出人像、商品、动物、图章等。调用佐糖API,将AI抠图功能集成到您的网站、应用程序、软件中,轻松拥有像素级抠图服务!

1.首先看下两张图片的对比

⭐ Unity + 佐糖抠图_第1张图片

⭐ Unity + 佐糖抠图_第2张图片

2.佐糖的网址,里边不止有抠图的API 还有其他

https://picwish.cn/background-removal-api-doc

⭐ Unity + 佐糖抠图_第3张图片

3.废话不多说 直接上抠图代码

MattingKEY 替换成自己的就行

public class Faseapi : MonoBehaviour
{
    public string MattingURL = "https://techsz.aoscdn.com/api/tasks/visual/segmentation";
    public string MattingKEY = "wx1kcjxudkn4918**";
    public string videoFilePath;
    // Start is called before the first frame update
    void Start()
    {
        videoFilePath = Application.streamingAssetsPath + "/savedImage.png";

        StartCoroutine(StartGetMatting());
    }

    // Update is called once per frame
    void Update()
    {

    } /// 
      /// 加载抠图api
      /// 
      /// 
      /// 
    IEnumerator StartGetMatting()
    {
        WWWForm _form = new WWWForm();
        //_form.AddField("X-API-KEY", MattingKEY);
        byte[] videoData = File.ReadAllBytes(videoFilePath);
        _form.AddBinaryData("image_file", videoData);
        _form.AddField("type", "person");
        _form.AddField("return_type", 2);
        _form.AddField("output_type", 2);
        _form.AddField("crop", 0);
        _form.AddField("sync", 1);
        UnityWebRequest webRequest = UnityWebRequest.Post(MattingURL, _form);
        webRequest.SetRequestHeader("X-API-KEY", MattingKEY);
        //string _time = ShowTime();
        yield return webRequest.SendWebRequest();
        if (!string.IsNullOrEmpty(webRequest.error))
        {
            print("服务器获取出错" + webRequest.error.ToString());
            //yield return new WaitForSeconds(4f);  4秒后重新连接
        }
        else
        {
            string data = webRequest.downloadHandler.text;
            Debug.Log(data);
            JsonData Mapper = JsonMapper.ToObject(data);
            if (Mapper["status"].ToString() == "200")
            {
                //JsonData data_ = JsonMapper.ToObject(Mapper["data"].ToString());
                //Debug.Log(int.Parse(Mapper["data"]["state"].ToString()));
                if (int.Parse(Mapper["data"]["state"].ToString()) == 1)
                {
                    Debug.Log(Mapper["data"]["image"].ToString());
                    byte[] buffers = Convert.FromBase64String(Mapper["data"]["image"].ToString());
                    //imagee = Base64ToTexture2D(Mapper["data"]["image"].ToString());
                    File.WriteAllBytes(Application.streamingAssetsPath + "/1.png", buffers);

                }
            }
        }
    }

感谢大家的观看,您的点赞和关注是我最大的动力

不定时更新知识点和干货呦~

你可能感兴趣的:(Unity,unity,java,游戏引擎)