图片识别的方式,对图片特征点的多少和分布要求比较严格。自定义识别可以减少下载导入的环节,对图片的依赖程度比较低。
-
实现思路:通过点击按钮,触发User Define Tracker脚本的BulidNewTarget()函数,其中自己定义的BuildNewTarget()函数主要是利用UserDefinedTargetBuildingBehaviour中的BuildingNewTarget(),自动的调用OnFrameQualityChanged()。
-
实现步骤:
新建了两个脚本,第一个主要是自定义识别功能的实现,第二个主要是展示自定义识别所拍摄场景的识别度的展示。 -
实现过程:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;
public class UserDefineTracker : MonoBehaviour,IUserDefinedTargetEventHandler {
private FrameQuality frameQualityShow;
private UserDefinedTargetBuildingBehaviour m_UserDefinedTargetBehaviour;//获取自定义识别的脚本,将整个脚本挂在UserDefinedTargetBuilder中
private ObjectTracker m_ObjectTracker;//定位自己的追踪器
//定义一个自己的数据集
DataSet m_DataSet;
public ImageTargetBehaviour imageTargetTemplate;//追踪图片
//追踪图片序列
int imageTargetCount = 0;
ImageTargetBuilder.FrameQuality m_frameQuality = ImageTargetBuilder.FrameQuality.FRAME_QUALITY_NONE;//保存识别场景的质量
// Use this for initialization
void Start () {
m_UserDefinedTargetBehaviour = this.GetComponent<UserDefinedTargetBuildingBehaviour>();
//将其注册进来
if (m_UserDefinedTargetBehaviour)
{
m_UserDefinedTargetBehaviour.RegisterEventHandler(this);//下一步将会做初始化的操作
Debug.Log("Registering done");
}
frameQualityShow = FindObjectOfType<FrameQuality>();
}
// Update is called once per frame
void Update ()
{
}
/// <summary>
/// 不需要手动去调用,触发相应的事件,将会自动去调用
/// </summary>
/// <param name="frameQuality"></param>
public void OnFrameQualityChanged(ImageTargetBuilder.FrameQuality frameQuality) //获取当前识别场景的质量 ,负责创建数据集
{
this.m_frameQuality = frameQuality;
if (m_frameQuality==ImageTargetBuilder.FrameQuality.FRAME_QUALITY_LOW)
{
Debug.Log("Frame Low");
}
frameQualityShow.ShowFrameColor(this.m_frameQuality);
}
public void OnInitialized()//目标:获取追踪器,激活创建的数据集
{
m_ObjectTracker = TrackerManager.Instance.GetTracker<ObjectTracker>();//获取追踪器
if (m_ObjectTracker!=null)
{
m_DataSet = m_ObjectTracker.CreateDataSet();//创建一个数据集
m_ObjectTracker.ActivateDataSet(m_DataSet);//激活数据集
}
}
public void OnNewTrackableSource(TrackableSource trackableSource)//将新的目标添加到数据集中
{
imageTargetCount++;
m_ObjectTracker.DeactivateDataSet(m_DataSet);//先禁用数据集
if (m_DataSet.HasReachedTrackableLimit())//达到数据集的上限
{//采用队列的形式
//获取所有的追踪对象
IEnumerable<Trackable> trackables = m_DataSet.GetTrackables();
//保存当前的追踪目标
Trackable lastSet = null;
foreach (Trackable trackable in trackables)
{
if (trackable.ID<lastSet.ID||lastSet==null)
{
lastSet = trackable;
}
}
if (lastSet != null)
{
m_DataSet.Destroy(lastSet, true);
Debug.Log("Destory lastSet");
}
}
//实例化追踪图片
ImageTargetBehaviour imageTargetCopy = Instantiate(imageTargetTemplate);
//追踪图片命名
imageTargetCopy.gameObject.name = "UserDefined" + imageTargetCount;
//创建到数据集当中
m_DataSet.CreateTrackable(trackableSource, imageTargetCopy.gameObject);
//激活数据集
m_ObjectTracker.ActivateDataSet(m_DataSet);
}
public void BuildNewTarget()
{
if (m_frameQuality == ImageTargetBuilder.FrameQuality.FRAME_QUALITY_LOW)
{
Debug.Log("Donot Creat,due to Camera Frame Quality");
}
else
{
string name = "UserTarget" + imageTargetCount;//初始化对象
m_UserDefinedTargetBehaviour.BuildNewTarget(name, imageTargetTemplate.GetSize().x);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Vuforia;
public class FrameQuality : MonoBehaviour {
public UnityEngine.UI.Image[] FrameColor ;
void SetFrameColor(Color Low,Color Middle,Color High)
{
if (FrameColor.Length==3)
{
if (FrameColor[0])
{
FrameColor[0].color = Low;
}
if (FrameColor[1])
{
FrameColor[1].color = Middle;
}
if (FrameColor[2])
{
FrameColor[2].color = High;
}
}
}
public void ShowFrameColor(Vuforia.ImageTargetBuilder.FrameQuality qulity)
{
switch (qulity)
{
case (Vuforia.ImageTargetBuilder.FrameQuality.FRAME_QUALITY_NONE):
SetFrameColor(Color.gray, Color.gray, Color.gray);
break;
case (Vuforia.ImageTargetBuilder.FrameQuality.FRAME_QUALITY_LOW):
SetFrameColor(Color.red, Color.gray, Color.gray);
break;
case (Vuforia.ImageTargetBuilder.FrameQuality.FRAME_QUALITY_MEDIUM):
SetFrameColor(Color.gray, Color.yellow, Color.gray);
break;
case (Vuforia.ImageTargetBuilder.FrameQuality.FRAME_QUALITY_HIGH):
SetFrameColor(Color.gray, Color.gray, Color.green);
break;
default:
break;
}
}
}
- 实现结果:
当显示为绿色时,点击按钮,所采集的环境将是你的识别图,当识别到将会出现一个Cube,离开识别区域,Cube丢失。重新点击按钮,将会采集下一张识别图
当看到绿色按下按钮