Unity实现识别图像中主体及其位置
作者:CoderZ1010 发布时间:2022-03-04 09:48:56
EasyDL图像分割介绍
创建应用
1.进入百度AI开放平台打开控制台:
2.在左上角打开产品服务列表,找到EasyDL零门槛AI开放平台:
3.打开EasyDL图像:
4.在公有云部署-应用列表中创建一个应用:
5.创建完成后获取到AppID、API Key、Secret Key:
创建模型
1.进入EasyGL图像分割:
2.创建模型:
3.创建数据集:
4.数据导入:
上传图片,图片的数量尽量多些
导入完成后查看并标注:
框选目标所在范围:
添加标签并为框选的目标设置标签:
设置完成后保存当前标注:
5.训练模型:(开始训练后需要等待一定时间)
6.发布模型:
发布完成后,拿到接口地址,来到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内容的:
有了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;
}
}
测试图片:
测试代码:
using System.IO;
using UnityEngine;
public class Example : MonoBehaviour
{
private void Start()
{
ImageSegmentation.SendRequest(File.ReadAllBytes(Application.dataPath + "/1.jpg"));
}
}
返回结果:
拿到了定位数据后,接下来将其区域绘制出来, 响应说明中解释(left,top)构成左上顶点,但是从返回值来看top为16,减去一个高度312的话,左下顶点的坐标已经是负数,这里姑且猜想它构成的是左下顶点:
首先创建一个Image来放置我们的测试图片,Canvas、Image大小也设为测试图片的大小640 * 359:
以下是测试脚本,将其挂载于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);
}
}
}
emmm... 区域大概准确吧,可能测试的模型数据集足够丰富的话检测会更精确。
来源:https://blog.csdn.net/qq_42139931/article/details/123029331


猜你喜欢
- 本文为大家分享了android studio3.4安装指南,供大家参考,具体内容如下1、双击运行Android Studio安装包,点击ne
- 前言:顺序表的问题及思考1. 顺序表中间/头部的插入删除,时间复杂度为O(N)2. 增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗
- Java调用shell命令涉及管道、重定向时不生效近日,因项目需求需要用java调用shell命令实现清理过时图片任务,发现代码生成出来的s
- 本文实例讲述了JAVA获取任意http网页源代码。分享给大家供大家参考,具体如下:JAVA获取任意http网页源代码可实现如下功能:1. 获
- 前言支持圆形裁剪框,裁剪后生成圆形图案。代码基于开源项目修改,github上项目链接:https://github.com/shengge/
- 今天跟大家分享一个利用外部Jar包来实现Java操作CSV文件一.资源下载1.直接下载Jar包:javacsv-2.0.jar2.利用Mav
- C#操作注册表全攻略相信每个人对注册表并不陌生,在运行里面输入“regedit”就可以打开注册表编辑器了。这东西对Windows系统来说可是
- 对列表进行排序也是我们经常遇到的问题,这里缩小一下范围,使用map来对列表排序。相信大家都有过TreeMap排序的经历,不过Map.Entr
- 批注是一种富文本注释,常用于为指定的Excel单元格添加提示或附加信息。 Free Spire.XLS for Java为开发人员免费提供了
- 现象说明maven的java项目,测试用例和main所在的源码文件均符合缺省写法和格式,但是在使用mvn clean sonar:sonar
- 本文实例为大家分享了Android实现底部滚轮式选择弹跳框的具体代码,供大家参考,具体内容如下先看效果:调用方法:SlideDialog s
- 前言Android通过设置Alpha值图片淡化、透明度大家应该都知道,下面是段简单的示例代码:View v = findViewById(R
- 什么是JWTJSON Web Token(JWT)是一个开放的标准(RFC 7519),它定义了一个紧凑且自包含的方式,用于在各方之间以JS
- java解决动态配置字段需求是否在开发中遇到有像下图一样管理员配置多个字段让用户填写的需求我的实现方式是通过数据库存储动态json的显示实现
- 一、synchronized 有不足新事物的出现要不是替代老事物,要么就是对老事物的补充JUC 的 locks 就是对 synchroniz
- 本文实例讲述了Android编程之ListView和EditText发布帖子隐藏软键盘功能。分享给大家供大家参考,具体如下:在Android
- 前言Dagger2作为依赖注入神器,相信很多朋友都听说过它的大名。只不过它的有些概念,理解起来并不是那么清晰,并且在使用的过程中,也比较迷糊
- 本文以eclipse4.7安装sts3.9.0为例,解决报错An error occurred while collecting items
- 前言.NET中的委托是一个类,它定义了方法的类型,是一个方法容器。委托把方法当作参数,可以避免在程序中大量使用条件判断语句的情况。项目名为T
- OAuth 2.0 是一种工业级的授权协议。OAuth 2.0是从创建于2006年的OAuth 1.0继承而来的。OAuth 2.0致力于帮