Unity实现识别图像中主体及其位置

作者:CoderZ1010 时间:2022-03-04 09:48:56 

EasyDL图像分割介绍

Unity实现识别图像中主体及其位置

创建应用

1.进入百度AI开放平台打开控制台:

Unity实现识别图像中主体及其位置

2.在左上角打开产品服务列表,找到EasyDL零门槛AI开放平台:

Unity实现识别图像中主体及其位置

3.打开EasyDL图像:

Unity实现识别图像中主体及其位置

4.在公有云部署-应用列表中创建一个应用:

Unity实现识别图像中主体及其位置

5.创建完成后获取到AppID、API Key、Secret Key:

Unity实现识别图像中主体及其位置

创建模型

1.进入EasyGL图像分割:

Unity实现识别图像中主体及其位置

2.创建模型:

Unity实现识别图像中主体及其位置

Unity实现识别图像中主体及其位置

3.创建数据集:

Unity实现识别图像中主体及其位置

Unity实现识别图像中主体及其位置

4.数据导入:

Unity实现识别图像中主体及其位置

上传图片,图片的数量尽量多些

Unity实现识别图像中主体及其位置

导入完成后查看并标注:

Unity实现识别图像中主体及其位置

框选目标所在范围:

Unity实现识别图像中主体及其位置

添加标签并为框选的目标设置标签:

Unity实现识别图像中主体及其位置

设置完成后保存当前标注:

Unity实现识别图像中主体及其位置

5.训练模型:(开始训练后需要等待一定时间)

Unity实现识别图像中主体及其位置

6.发布模型:

Unity实现识别图像中主体及其位置

发布完成后,拿到接口地址,来到Unity中,根据接口响应字段说明定义相应数据结构:

Unity实现识别图像中主体及其位置

using System;

[Serializable]
public class ImageSegmentationResponse
{
   /// <summary>
   /// 唯一的log id 用于问题定位
   /// </summary>
   public int log_id;
   /// <summary>
   /// 标签数组结果
   /// </summary>
   public ImageSegmentationResult[] results;
}
[Serializable]
public class ImageSegmentationResult
{
   /// <summary>
   /// 标签名称
   /// </summary>
   public string name;
   /// <summary>
   /// 置信度
   /// </summary>
   public string score;
   /// <summary>
   /// 位置
   /// </summary>
   public Location location;
   /// <summary>
   /// 基于游程编码的字符串,编码内容为和原图宽高相同的布尔数组
   /// 若数组值为0,代表原图此位置像素点不属于检测目标,若为1,代表原图此位置像素点属于检测目标
   /// </summary>
   public bool[] mask;
}
[Serializable]
public class Location
{
   /// <summary>
   /// 目标定位位置的长方形左上顶点的水平坐标
   /// </summary>
   public int left;
   /// <summary>
   /// 目标定位位置的长方形左上顶点的垂直坐标
   /// </summary>
   public int top;
   /// <summary>
   /// 目标定位位置的长方形的宽度
   /// </summary>
   public int width;
   /// <summary>
   /// 目标定位位置的长方形的高度
   /// </summary>
   public int height;
}

在任意一个模块下载C#SDK,例如在图像识别中下载,它是包含EasyDL的API内容的:

Unity实现识别图像中主体及其位置

有了SDK后,放入Unity中的Plugins文件夹中,封装调用函数,只需要将检测图片的字节数据作为参数,其中appID、apiKey、secretKey是在上面创建应用时获取到的,url是发布模型时获取到的:

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;
   }
}

测试图片:

Unity实现识别图像中主体及其位置

测试代码:

using System.IO;
using UnityEngine;

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

返回结果:

Unity实现识别图像中主体及其位置

拿到了定位数据后,接下来将其区域绘制出来, 响应说明中解释(left,top)构成左上顶点,但是从返回值来看top为16,减去一个高度312的话,左下顶点的坐标已经是负数,这里姑且猜想它构成的是左下顶点:

Unity实现识别图像中主体及其位置

首先创建一个Image来放置我们的测试图片,Canvas、Image大小也设为测试图片的大小640 * 359:

Unity实现识别图像中主体及其位置

Unity实现识别图像中主体及其位置

以下是测试脚本,将其挂载于Image测试:

using System.IO;
using UnityEngine;

public class Example : MonoBehaviour
{
   private void Start()
   {
       var results = ImageSegmentation.SendRequest(File.ReadAllBytes(Application.dataPath + "/测试.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);
       }
   }
}

Unity实现识别图像中主体及其位置

emmm... 区域大概准确吧,可能测试的模型数据集足够丰富的话检测会更精确。

来源:https://blog.csdn.net/qq_42139931/article/details/123029331

标签:Unity,识别,图像,主体,位置
0
投稿

猜你喜欢

  • 如何使用Java redis实现发送手机验证码功能

    2023-11-26 17:25:00
  • Java实现简单的日历界面

    2021-10-08 03:13:01
  • C#操作session的类实例

    2023-07-17 13:57:19
  • Java截取字符串的几种方法示例

    2023-11-29 12:36:32
  • Java异常处理之try...catch...finally详解

    2023-09-17 05:38:24
  • Spring Security 构建rest服务实现rememberme 记住我功能

    2023-03-29 14:42:03
  • MyBatis-Plus联表查询(Mybatis-Plus-Join)的功能实现

    2023-11-25 03:24:33
  • SpringBoot整合Mybatis实现多数据源配置与跨数据源事务实例

    2023-06-29 23:47:34
  • Java中十进制和十六进制的相互转换方法

    2022-04-21 11:54:06
  • Spring Security OAuth2实现使用JWT的示例代码

    2022-11-02 22:46:25
  • JAVA递归生成树形菜单的实现过程

    2023-07-15 08:57:22
  • Java中Steam流的用法详解

    2021-12-16 14:18:50
  • 初步了解javafx

    2021-09-30 16:20:11
  • Mybatis-Plus进阶分页与乐观锁插件及通用枚举和多数据源详解

    2023-11-23 11:00:58
  • PyQt5内嵌浏览器注入JavaScript脚本实现自动化操作的代码实例

    2023-11-26 15:05:59
  • java二维数组实现推箱子小游戏

    2022-08-31 10:18:40
  • java实现1M图片压缩优化到100kb实现示例

    2022-08-08 03:59:43
  • 使用C#发送Http请求实现模拟登陆实例

    2023-06-22 22:25:07
  • Android实现多点触控功能

    2021-10-18 08:39:33
  • Java常用的八种排序算法及代码实现+图解

    2022-04-09 13:30:06
  • asp之家 软件编程 m.aspxhome.com