亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Unity實(shí)現(xiàn)識(shí)別圖像中主體及其位置

 更新時(shí)間:2022年02月21日 09:32:33   作者:CoderZ1010  
EasyDL基于飛槳開(kāi)源深度學(xué)習(xí)平臺(tái),面向企業(yè)AI應(yīng)用開(kāi)發(fā)者提供零門(mén)檻AI開(kāi)發(fā)平臺(tái),實(shí)現(xiàn)零算法基礎(chǔ)定制高精度AI模型。本文將利用Unity和EasyDL實(shí)現(xiàn)識(shí)別圖像中主體及其位置,感興趣的可以了解一下

EasyDL圖像分割介紹

創(chuàng)建應(yīng)用

1.進(jìn)入百度AI開(kāi)放平臺(tái)打開(kāi)控制臺(tái):

2.在左上角打開(kāi)產(chǎn)品服務(wù)列表,找到EasyDL零門(mén)檻AI開(kāi)放平臺(tái):

3.打開(kāi)EasyDL圖像:

4.在公有云部署-應(yīng)用列表中創(chuàng)建一個(gè)應(yīng)用:

5.創(chuàng)建完成后獲取到AppID、API Key、Secret Key:

創(chuàng)建模型

1.進(jìn)入EasyGL圖像分割:

2.創(chuàng)建模型:

3.創(chuàng)建數(shù)據(jù)集:

4.數(shù)據(jù)導(dǎo)入:

上傳圖片,圖片的數(shù)量盡量多些

導(dǎo)入完成后查看并標(biāo)注:

框選目標(biāo)所在范圍:

添加標(biāo)簽并為框選的目標(biāo)設(shè)置標(biāo)簽:

設(shè)置完成后保存當(dāng)前標(biāo)注:

5.訓(xùn)練模型:(開(kāi)始訓(xùn)練后需要等待一定時(shí)間)

6.發(fā)布模型:

發(fā)布完成后,拿到接口地址,來(lái)到Unity中,根據(jù)接口響應(yīng)字段說(shuō)明定義相應(yīng)數(shù)據(jù)結(jié)構(gòu):

using System;
 
[Serializable]
public class ImageSegmentationResponse
{
    /// <summary>
    /// 唯一的log id 用于問(wèn)題定位
    /// </summary>
    public int log_id;
    /// <summary>
    /// 標(biāo)簽數(shù)組結(jié)果
    /// </summary>
    public ImageSegmentationResult[] results;
}
[Serializable]
public class ImageSegmentationResult
{
    /// <summary>
    /// 標(biāo)簽名稱(chēng)
    /// </summary>
    public string name;
    /// <summary>
    /// 置信度
    /// </summary>
    public string score;
    /// <summary>
    /// 位置
    /// </summary>
    public Location location;
    /// <summary>
    /// 基于游程編碼的字符串,編碼內(nèi)容為和原圖寬高相同的布爾數(shù)組
    /// 若數(shù)組值為0,代表原圖此位置像素點(diǎn)不屬于檢測(cè)目標(biāo),若為1,代表原圖此位置像素點(diǎn)屬于檢測(cè)目標(biāo)
    /// </summary>
    public bool[] mask;
}
[Serializable]
public class Location
{
    /// <summary>
    /// 目標(biāo)定位位置的長(zhǎng)方形左上頂點(diǎn)的水平坐標(biāo)
    /// </summary>
    public int left;
    /// <summary>
    /// 目標(biāo)定位位置的長(zhǎng)方形左上頂點(diǎn)的垂直坐標(biāo)
    /// </summary>
    public int top;
    /// <summary>
    /// 目標(biāo)定位位置的長(zhǎng)方形的寬度
    /// </summary>
    public int width;
    /// <summary>
    /// 目標(biāo)定位位置的長(zhǎng)方形的高度
    /// </summary>
    public int height;
}

在任意一個(gè)模塊下載C#SDK,例如在圖像識(shí)別中下載,它是包含EasyDL的API內(nèi)容的:

有了SDK后,放入U(xiǎn)nity中的Plugins文件夾中,封裝調(diào)用函數(shù),只需要將檢測(cè)圖片的字節(jié)數(shù)據(jù)作為參數(shù),其中appID、apiKey、secretKey是在上面創(chuàng)建應(yīng)用時(shí)獲取到的,url是發(fā)布模型時(shí)獲取到的:

using System;
using UnityEngine;
 
/// <summary>
/// 圖像分割
/// </summary>
public class ImageSegmentation
{
    private const string appID = "";
    private const string apiKey = "";
    private const string secretKey = "";
    private const string url = "";
 
    public static ImageSegmentationResult[] SendRequest(byte[] bytes)
    {
        var client = new Baidu.Aip.EasyDL.EasyDL(appID, apiKey, secretKey);
        try
        {
            var response = client.requestImage(url, bytes);
            Debug.Log(response.ToString());
            ImageSegmentationResponse r = JsonUtility.FromJson<ImageSegmentationResponse>(response.ToString());
            return r.results;
 
        }
        catch (Exception error)
        {
            Debug.LogError(error);
        }
        return null;
    }
}

測(cè)試圖片:

測(cè)試代碼:

using System.IO;
using UnityEngine;
 
public class Example : MonoBehaviour
{
    private void Start()
    {
        ImageSegmentation.SendRequest(File.ReadAllBytes(Application.dataPath + "/1.jpg"));
    }
}

返回結(jié)果:

拿到了定位數(shù)據(jù)后,接下來(lái)將其區(qū)域繪制出來(lái), 響應(yīng)說(shuō)明中解釋?zhuān)╨eft,top)構(gòu)成左上頂點(diǎn),但是從返回值來(lái)看top為16,減去一個(gè)高度312的話(huà),左下頂點(diǎn)的坐標(biāo)已經(jīng)是負(fù)數(shù),這里姑且猜想它構(gòu)成的是左下頂點(diǎn):

首先創(chuàng)建一個(gè)Image來(lái)放置我們的測(cè)試圖片,Canvas、Image大小也設(shè)為測(cè)試圖片的大小640 * 359:

以下是測(cè)試腳本,將其掛載于Image測(cè)試:

using System.IO;
using UnityEngine;
 
public class Example : MonoBehaviour
{
    private void Start()
    {
        var results = ImageSegmentation.SendRequest(File.ReadAllBytes(Application.dataPath + "/測(cè)試.jpg"));
 
        for (int i = 0; i < results.Length; i++)
        {
            var location = results[i].location;
 
            LineRenderer line = new GameObject("LineRenderer").AddComponent<LineRenderer>();
            line.positionCount = 4;
            line.loop = true;
 
            Vector2 leftTop = new Vector2(location.left, location.top);
            Vector2 rightTop = new Vector2(location.left + location.width, location.top);
            Vector2 leftBottom = new Vector2(location.left, location.top + location.height);
            Vector2 rightBottom = new Vector2(location.left + location.width, location.top + location.height);
 
            RectTransformUtility.ScreenPointToWorldPointInRectangle(transform as RectTransform, leftTop, Camera.main, out Vector3 point1);
            RectTransformUtility.ScreenPointToWorldPointInRectangle(transform as RectTransform, rightTop, Camera.main, out Vector3 point2);
            RectTransformUtility.ScreenPointToWorldPointInRectangle(transform as RectTransform, rightBottom, Camera.main, out Vector3 point3);
            RectTransformUtility.ScreenPointToWorldPointInRectangle(transform as RectTransform, leftBottom, Camera.main, out Vector3 point4);
 
            line.SetPosition(0, point1);
            line.SetPosition(1, point2);
            line.SetPosition(2, point3);
            line.SetPosition(3, point4);
        }
    }
}

emmm... 區(qū)域大概準(zhǔn)確吧,可能測(cè)試的模型數(shù)據(jù)集足夠豐富的話(huà)檢測(cè)會(huì)更精確。

以上就是Unity實(shí)現(xiàn)識(shí)別圖像中主體及其位置的詳細(xì)內(nèi)容,更多關(guān)于Unity的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論