forked from homuler/MediaPipeUnityPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectDetectionGraph.cs
More file actions
57 lines (43 loc) · 1.72 KB
/
Copy pathObjectDetectionGraph.cs
File metadata and controls
57 lines (43 loc) · 1.72 KB
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
using Mediapipe;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public class ObjectDetectionGraph : DemoGraph {
private const string outputDetectionsStream = "output_detections";
private Stack<List<Detection>> detectionLists;
private GCHandle outputDetectionsCallbackHandle;
public override Status StartRun() {
detectionLists = new Stack<List<Detection>>();
graph.ObserveOutputStream<DetectionVectorPacket, List<Detection>>(
outputDetectionsStream, OutputDetectionsCallback, out outputDetectionsCallbackHandle).AssertOk();
return graph.StartRun();
}
protected override void OnDestroy() {
base.OnDestroy();
if (outputDetectionsCallbackHandle.IsAllocated) {
outputDetectionsCallbackHandle.Free();
}
}
public override void RenderOutput(WebCamScreenController screenController, TextureFrame textureFrame) {
List<Detection> detections = null;
lock (((ICollection)detectionLists).SyncRoot) {
if (detectionLists.Count > 0) {
detections = detectionLists.Peek();
detectionLists.Clear();
}
}
RenderAnnotation(screenController, detections == null ? new List<Detection>() : detections);
screenController.DrawScreen(textureFrame);
}
private void RenderAnnotation(WebCamScreenController screenController, List<Detection> detections) {
// NOTE: input image is flipped
GetComponent<DetectionListAnnotationController>().Draw(screenController.transform, detections, true);
}
private Status OutputDetectionsCallback(DetectionVectorPacket packet) {
var value = packet.Get();
lock (((ICollection)detectionLists).SyncRoot) {
detectionLists.Push(value);
}
return Status.Ok();
}
}