This repository has been archived by the owner on Mar 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
CustomVisionObjectDetectionService.cs
81 lines (68 loc) · 3.69 KB
/
CustomVisionObjectDetectionService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using LabAssistVision;
using LabAssistVision.Detection;
using Microsoft.MixedReality.Toolkit.Utilities;
using UnityEngine;
using UnityEngine.Assertions;
namespace Microsoft.MixedReality.Toolkit.Extensions
{
[MixedRealityExtensionService(SupportedPlatforms.WindowsStandalone | SupportedPlatforms.WindowsUniversal)]
public class CustomVisionObjectDetectionService : BaseExtensionService, IObjectDetectionService, IMixedRealityExtensionService
{
[NotNull] private readonly ObjectDetectionServiceProfile _objectDetectionServiceProfile;
private int _concurrentCount;
// ReSharper disable once NotNullMemberIsNotInitialized
[NotNull] private readonly Logger _logger;
private readonly IObjectDetector _objectDetector;
public CustomVisionObjectDetectionService(string name, uint priority, BaseMixedRealityProfile profile) : base(name, priority, profile)
{
if (profile == null) throw new ArgumentNullException(nameof(profile));
ObjectDetectionServiceProfile serviceProfile = profile as ObjectDetectionServiceProfile;
if (serviceProfile == null) throw new ArgumentNullException(nameof(serviceProfile));
_objectDetectionServiceProfile = serviceProfile;
_logger = new Logger(new LogHandler());
Assert.IsNotNull(_logger, "_logger != null");
if (_objectDetectionServiceProfile.useLocalModel)
{
_objectDetector = new CustomVisionLocal(_objectDetectionServiceProfile.modelFile, _objectDetectionServiceProfile.labels,10,0.5f);
}
else
{
_objectDetector = new CustomVision(_objectDetectionServiceProfile.predictionApi, _objectDetectionServiceProfile.predictionKey);
}
}
public async Task<List<DetectedObject>> DetectAsync(CameraFrame frame)
{
if (frame == null) throw new ArgumentNullException(nameof(frame));
if (_concurrentCount > _objectDetectionServiceProfile.maxConcurrentRequestLimit) return null;
Interlocked.Increment(ref _concurrentCount);
List<DetectedObject> detectedObjects = await _objectDetector.DetectAsync(frame);
detectedObjects = detectedObjects.Where(detectedObject => detectedObject.Probability > _objectDetectionServiceProfile.minimalPredictionProbability).ToList();
detectedObjects.ForEach((detectedObject) => _logger.Log($"Detected Object: {detectedObject}"));
Interlocked.Decrement(ref _concurrentCount);
return detectedObjects;
}
public void ChangeMinimalPredictionProbability(double value)
{
_objectDetectionServiceProfile.minimalPredictionProbability = value;
}
public void ChangeMaxConcurrentRequestLimit(int value)
{
if (_concurrentCount > 0) _logger.LogError("Changing limit during execution is not yet supported.");
_objectDetectionServiceProfile.maxConcurrentRequestLimit = value;
}
public double minimalPredictionProbability => _objectDetectionServiceProfile.minimalPredictionProbability;
public int maxConcurrentRequests => _objectDetectionServiceProfile.maxConcurrentRequestLimit;
public bool detectOnRepeat => _objectDetectionServiceProfile.detectOnRepeat;
public void ToggleDetectOnRepeat()
{
_objectDetectionServiceProfile.detectOnRepeat = !_objectDetectionServiceProfile.detectOnRepeat;
Debug.Log($"Changed detect on repeat to {detectOnRepeat}");
}
}
}