128 lines
4.4 KiB
C#
128 lines
4.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using RenderHeads.Media.AVProLiveCamera;
|
|
using UnityEngine;
|
|
|
|
public class AABB : MonoBehaviour
|
|
{
|
|
public WebSocketCameraSender webSocketCameraSender;
|
|
[SerializeField] private AVProLiveCamera _liveCamera;
|
|
[Range(0,20)]public float width = 20f;
|
|
|
|
public Color linkTrueColor=Color.green;
|
|
public Color linkFalseColor = Color.red;
|
|
|
|
private RenderTexture _renderTexture;
|
|
private RenderTexture tempRenderTex;
|
|
private DetectionRoot detectionRoot;
|
|
Material _material;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
_material=new Material(Shader.Find("Unlit/AABB"));
|
|
webSocketCameraSender.OnMessageReceived.AddListener(AnalyticJson);
|
|
webSocketCameraSender.OnCaptureTex.AddListener(DrawAABB);
|
|
}
|
|
|
|
|
|
|
|
|
|
void AnalyticJson(string json)
|
|
{
|
|
detectionRoot=JsonUtility.FromJson<DetectionRoot>(json);
|
|
// if (detectionRoot.detections.Count > 0)
|
|
// Debug.Log(detectionRoot.detections[0].class_name+" "+detectionRoot.detections[0].box.x1+" "+detectionRoot.detections[0].box.x2+" "+detectionRoot.detections[0].box.y1+" "+detectionRoot.detections[0].box.y2);
|
|
}
|
|
|
|
void DrawAABB(RenderTexture renderTexture)
|
|
{
|
|
//没有就创建渲染贴图
|
|
if (_renderTexture == null)
|
|
{
|
|
_renderTexture = new RenderTexture(
|
|
renderTexture.width,
|
|
renderTexture.height,
|
|
renderTexture.depth,
|
|
renderTexture.format
|
|
);
|
|
_renderTexture.antiAliasing = renderTexture.antiAliasing;
|
|
_renderTexture.enableRandomWrite = true;
|
|
// 激活新纹理(可选,根据业务需求决定是否激活)
|
|
_renderTexture.Create();
|
|
|
|
Shader.SetGlobalTexture("boundBoxTex", _renderTexture);
|
|
}
|
|
tempRenderTex = RenderTexture.active;
|
|
RenderTexture.active = _renderTexture;
|
|
GL.PushMatrix();
|
|
GL.LoadPixelMatrix(0, renderTexture.width, renderTexture.height, 0);
|
|
|
|
_material.SetColor("linkColor",Color.clear);
|
|
Rect rectBlack = new Rect(0, 0, renderTexture.width, renderTexture.height);
|
|
Graphics.DrawTexture(rectBlack,Texture2D.whiteTexture,_material,0);
|
|
|
|
|
|
for (int i = 0; i < detectionRoot.detections.Count; i++)
|
|
{
|
|
if(detectionRoot.detections[i].class_name=="link-true") _material.SetColor("linkColor",linkTrueColor);
|
|
else _material.SetColor("linkColor",linkFalseColor);
|
|
Rect rect = new Rect(detectionRoot.detections[i].box.x1-width, detectionRoot.detections[i].box.y1-width,
|
|
Mathf.Abs(detectionRoot.detections[i].box.x1-detectionRoot.detections[i].box.x2)+width*2, Mathf.Abs(detectionRoot.detections[i].box.y1-detectionRoot.detections[i].box.y2)+width*2);
|
|
Graphics.DrawTexture(rect,Texture2D.whiteTexture,_material,0);
|
|
|
|
_material.SetColor("linkColor",Color.clear);
|
|
Rect rectScoop = new Rect(detectionRoot.detections[i].box.x1, detectionRoot.detections[i].box.y1,
|
|
Mathf.Abs(detectionRoot.detections[i].box.x1-detectionRoot.detections[i].box.x2), Mathf.Abs(detectionRoot.detections[i].box.y1-detectionRoot.detections[i].box.y2));
|
|
Graphics.DrawTexture(rectScoop,Texture2D.whiteTexture,_material,0);
|
|
}
|
|
|
|
|
|
GL.PopMatrix();
|
|
RenderTexture.active = tempRenderTex;
|
|
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
webSocketCameraSender.OnMessageReceived.RemoveListener(AnalyticJson);
|
|
webSocketCameraSender.OnCaptureTex.RemoveListener(DrawAABB);
|
|
|
|
_renderTexture?.Release();
|
|
Destroy(_renderTexture);
|
|
_renderTexture = null;
|
|
|
|
Destroy(_material);
|
|
_material = null;
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class DetectionRoot
|
|
{
|
|
public string model;
|
|
public int count;
|
|
public int class_count;
|
|
public List<string> classes_detected;
|
|
public List<DetectionItem> detections;
|
|
}
|
|
|
|
// 检测结果子项
|
|
[System.Serializable]
|
|
public class DetectionItem
|
|
{
|
|
public int class_id;
|
|
public string class_name;
|
|
public float confidence;
|
|
public BoxData box;
|
|
}
|
|
|
|
// 矩形框坐标数据
|
|
[System.Serializable]
|
|
public class BoxData
|
|
{
|
|
public float x1;
|
|
public float y1;
|
|
public float x2;
|
|
public float y2;
|
|
} |