-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6ac87b
commit e42be3e
Showing
3 changed files
with
213 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,141 +1,154 @@ | ||
using Assets.Scripts; | ||
using Assets.Scripts.TextureProviders; | ||
using NN; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Serialization; | ||
using Unity.Barracuda; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.Profiling; | ||
using UnityEngine.UI; | ||
|
||
public class Detector : MonoBehaviour | ||
{ | ||
[Tooltip("File of YOLO model.")] | ||
[SerializeField] | ||
protected NNModel ModelFile; | ||
|
||
[Tooltip("RawImage component which will be used to draw resuls.")] | ||
[SerializeField] | ||
protected Material material_t; | ||
|
||
[Range(0.0f, 1f)] | ||
[Tooltip("The minimum value of box confidence below which boxes won't be drawn.")] | ||
[SerializeField] | ||
protected float MinBoxConfidence = 0.3f; | ||
|
||
[SerializeField] | ||
protected TextureProviderType.ProviderType textureProviderType; | ||
|
||
[SerializeReference] | ||
protected TextureProvider textureProvider = null; | ||
|
||
[Tooltip("deploy on hololens")] | ||
[SerializeField] | ||
protected bool holo = false; | ||
|
||
protected NNHandler nn; | ||
protected Color[] colorArray = new Color[] { Color.red, Color.green, Color.blue, Color.cyan, Color.magenta, Color.yellow }; | ||
|
||
YOLOv8 yolo; | ||
|
||
float time = 0; | ||
float cycle = 0.5f; | ||
|
||
private void OnEnable() | ||
{ | ||
nn = new NNHandler(ModelFile); | ||
yolo = new YOLOv8Segmentation(nn); | ||
|
||
textureProvider = GetTextureProvider(nn.model); | ||
textureProvider.Start(); | ||
} | ||
|
||
private void Update() | ||
{ | ||
time += Time.deltaTime; | ||
if (time > cycle) | ||
{ | ||
time = 0; | ||
|
||
YOLOv8OutputReader.DiscardThreshold = MinBoxConfidence; | ||
Texture2D texture = GetNextTexture(); | ||
|
||
var boxes = yolo.Run(texture); | ||
DrawResults(boxes, texture); | ||
material_t.mainTexture = texture; | ||
} | ||
|
||
} | ||
|
||
protected TextureProvider GetTextureProvider(Model model) | ||
{ | ||
var firstInput = model.inputs[0]; | ||
int height = firstInput.shape[5]; | ||
int width = firstInput.shape[6]; | ||
|
||
TextureProvider provider; | ||
switch (textureProviderType) | ||
{ | ||
case TextureProviderType.ProviderType.WebCam: | ||
if (holo) | ||
{ | ||
provider = new WebCamTextureProvider(textureProvider as WebCamTextureProvider, width, height, true); | ||
using Assets.Scripts; | ||
using Assets.Scripts.TextureProviders; | ||
using NN; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Serialization; | ||
using Unity.Barracuda; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.Profiling; | ||
using UnityEngine.UI; | ||
|
||
public class Detector : MonoBehaviour | ||
{ | ||
[Tooltip("File of YOLO model.")] | ||
[SerializeField] | ||
protected NNModel ModelFile; | ||
|
||
[Tooltip("RawImage component which will be used to draw resuls.")] | ||
[SerializeField] | ||
protected Material material_t; | ||
|
||
[Range(0.0f, 1f)] | ||
[Tooltip("The minimum value of box confidence below which boxes won't be drawn.")] | ||
[SerializeField] | ||
protected float MinBoxConfidence = 0.3f; | ||
|
||
[SerializeField] | ||
protected TextureProviderType.ProviderType textureProviderType; | ||
|
||
[SerializeReference] | ||
protected TextureProvider textureProvider = null; | ||
|
||
[Tooltip("deploy on hololens")] | ||
[SerializeField] | ||
protected bool holo = false; | ||
|
||
protected NNHandler nn; | ||
protected Color[] colorArray = new Color[] { Color.red, Color.green, Color.blue, Color.cyan, Color.magenta, Color.yellow }; | ||
|
||
YOLOv8 yolo; | ||
|
||
float time = 0; | ||
float cycle = 2f; | ||
|
||
bool detectionOn = false; | ||
|
||
public void ToggleDetection() | ||
{ | ||
detectionOn = !detectionOn; | ||
} | ||
|
||
private void OnEnable() | ||
{ | ||
nn = new NNHandler(ModelFile); | ||
yolo = new YOLOv8Segmentation(nn); | ||
|
||
textureProvider = GetTextureProvider(nn.model); | ||
textureProvider.Start(); | ||
} | ||
|
||
private void Update() | ||
{ | ||
if (detectionOn) | ||
{ | ||
time += Time.deltaTime; | ||
if (time > cycle) | ||
{ | ||
time = 0; | ||
|
||
YOLOv8OutputReader.DiscardThreshold = MinBoxConfidence; | ||
Texture2D texture = GetNextTexture(); | ||
|
||
var boxes = yolo.Run(texture); | ||
DrawResults(boxes, texture); | ||
material_t.mainTexture = texture; | ||
} | ||
} | ||
else | ||
{ | ||
time = cycle / 2f; | ||
} | ||
} | ||
|
||
protected TextureProvider GetTextureProvider(Model model) | ||
{ | ||
var firstInput = model.inputs[0]; | ||
int height = firstInput.shape[5]; | ||
int width = firstInput.shape[6]; | ||
|
||
TextureProvider provider; | ||
switch (textureProviderType) | ||
{ | ||
case TextureProviderType.ProviderType.WebCam: | ||
if (holo) | ||
{ | ||
provider = new WebCamTextureProvider(textureProvider as WebCamTextureProvider, width, height, true); | ||
} | ||
else | ||
{ | ||
provider = new WebCamTextureProvider(textureProvider as WebCamTextureProvider, width, height, false); | ||
} | ||
break; | ||
|
||
case TextureProviderType.ProviderType.Video: | ||
provider = new VideoTextureProvider(textureProvider as VideoTextureProvider, width, height); | ||
break; | ||
default: | ||
throw new InvalidEnumArgumentException(); | ||
} | ||
return provider; | ||
} | ||
|
||
protected Texture2D GetNextTexture() | ||
{ | ||
return textureProvider.GetTexture(); | ||
} | ||
|
||
void OnDisable() | ||
{ | ||
nn.Dispose(); | ||
textureProvider.Stop(); | ||
} | ||
|
||
protected void DrawResults(IEnumerable<ResultBox> results, Texture2D img) | ||
{ | ||
results.ForEach(box => DrawBox(box, img)); | ||
} | ||
|
||
protected virtual void DrawBox(ResultBox box, Texture2D img) | ||
{ | ||
Color boxColor = colorArray[box.bestClassIndex % colorArray.Length]; | ||
int boxWidth = (int)(box.score / MinBoxConfidence); | ||
TextureTools.DrawRectOutline(img, box.rect, boxColor, boxWidth, rectIsNormalized: false, revertY: true); | ||
} | ||
|
||
private void OnValidate() | ||
{ | ||
Type t = TextureProviderType.GetProviderType(textureProviderType); | ||
if (textureProvider == null || t != textureProvider.GetType()) | ||
{ | ||
if (nn == null) | ||
textureProvider = RuntimeHelpers.GetUninitializedObject(t) as TextureProvider; | ||
else | ||
{ | ||
textureProvider = GetTextureProvider(nn.model); | ||
textureProvider.Start(); | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
break; | ||
|
||
case TextureProviderType.ProviderType.Video: | ||
provider = new VideoTextureProvider(textureProvider as VideoTextureProvider, width, height); | ||
break; | ||
default: | ||
throw new InvalidEnumArgumentException(); | ||
} | ||
return provider; | ||
} | ||
|
||
protected Texture2D GetNextTexture() | ||
{ | ||
return textureProvider.GetTexture(); | ||
} | ||
|
||
void OnDisable() | ||
{ | ||
nn.Dispose(); | ||
textureProvider.Stop(); | ||
} | ||
|
||
protected void DrawResults(IEnumerable<ResultBox> results, Texture2D img) | ||
{ | ||
results.ForEach(box => DrawBox(box, img)); | ||
} | ||
|
||
protected virtual void DrawBox(ResultBox box, Texture2D img) | ||
{ | ||
Color boxColor = colorArray[box.bestClassIndex % colorArray.Length]; | ||
int boxWidth = (int)(box.score / MinBoxConfidence); | ||
TextureTools.DrawRectOutline(img, box.rect, boxColor, boxWidth, rectIsNormalized: false, revertY: true); | ||
} | ||
|
||
private void OnValidate() | ||
{ | ||
Type t = TextureProviderType.GetProviderType(textureProviderType); | ||
if (textureProvider == null || t != textureProvider.GetType()) | ||
{ | ||
if (nn == null) | ||
textureProvider = RuntimeHelpers.GetUninitializedObject(t) as TextureProvider; | ||
else | ||
{ | ||
textureProvider = GetTextureProvider(nn.model); | ||
textureProvider.Start(); | ||
} | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.