This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference_service.dart
192 lines (173 loc) ยท 4.78 KB
/
inference_service.dart
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import 'dart:async';
import 'dart:collection';
import 'dart:developer';
import 'dart:isolate';
import 'package:flutter/services.dart';
import 'package:camera/camera.dart';
import 'isolate_utils.dart';
class InferenceService {
static const MethodChannel _channel =
MethodChannel('com.prod.visualpt/posedetection');
final IsolateUtils _isolateUtils = IsolateUtils();
bool isDetecting = false;
Queue<CameraImage> frameQueue = Queue();
InferenceService() {
_isolateUtils.initIsolate();
}
dispose() {
_isolateUtils.dispose();
}
Future<List<Offset>?> getPoseDetection(CameraImage image) async {
try {
if (!isDetecting) {
frameQueue.add(image);
final detection = await processVideoFrames();
return detection?.toOffsets();
}
} catch (e) {
log('Error during pose detection: $e');
return [];
}
}
Future<PoseDetectionResult?> processVideoFrames() async {
if (isDetecting) {
return null;
}
isDetecting = true;
final CameraImage frame = frameQueue.removeFirst();
final pose = await detectPose(frame);
isDetecting = false;
return pose;
}
Future<void> inference({
required IsolateUtils isolateUtils,
required Function handler,
required CameraImage image,
}) async {
final responsePort = ReceivePort();
isolateUtils.sendMessage(
handler: handler,
params: {
'image': image,
},
sendPort: isolateUtils.sendPort,
responsePort: responsePort,
);
final data = await responsePort.first;
responsePort.close();
}
static Future<PoseDetectionResult> detectPose(CameraImage image) async {
try {
final Uint8List imageData = _cameraImageToBytes(image);
final Map<String, dynamic> args = {
"image": imageData,
"width": image.width,
"height": image.height,
};
final dynamic result =
await _channel.invokeMethod('getPoseDetection', args);
final poses = PoseDetectionResult.fromJson(result.first["landmarks"]);
return poses;
} catch (e) {
log('Error during pose detection: $e');
return PoseDetectionResult(poses: []);
}
}
static Uint8List _cameraImageToBytes(CameraImage image) {
final planes = image.planes;
final numBytes = planes
.map((plane) => plane.bytes.lengthInBytes)
.reduce((value, element) => value + element);
final bytes = Uint8List(numBytes);
int offset = 0;
for (var plane in planes) {
bytes.setRange(offset, offset + plane.bytes.length, plane.bytes);
offset += plane.bytes.length;
}
return bytes;
}
}
class PoseDetectionResult {
static final Map<String, int> _landmarkIndices = {
'Nose': 0,
'LeftEyeInner': 1,
'LeftEye': 2,
'LeftEyeOuter': 3,
'RightEyeInner': 4,
'RightEye': 5,
'RightEyeOuter': 6,
'LeftEar': 7,
'RightEar': 8,
'MouthLeft': 9,
'MouthRight': 10,
'LeftShoulder': 11,
'RightShoulder': 12,
'LeftElbow': 13,
'RightElbow': 14,
'LeftWrist': 15,
'RightWrist': 16,
'LeftPinkyFinger': 17,
'RightPinkyFinger': 18,
'LeftIndexFinger': 19,
'RightIndexFinger': 20,
'LeftThumb': 21,
'RightThumb': 22,
'LeftHip': 23,
'RightHip': 24,
'LeftKnee': 25,
'RightKnee': 26,
'LeftAnkle': 27,
'RightAnkle': 28,
'LeftHeel': 29,
'RightHeel': 30,
'LeftToe': 31,
'RightToe': 32,
};
late List<PoseLandmarks> poses;
PoseDetectionResult({required this.poses});
List<Offset> toOffsets() {
if (poses.isNotEmpty) {
final List<Offset> offsets = List.filled(33, Offset.zero);
for (final landmark in poses) {
final int index = _landmarkIndices[landmark.type] ?? -1;
if (index != -1) {
offsets[index] = Offset(landmark.x, landmark.y);
} else {
throw "Invalid landmark detected ${landmark.type}";
}
}
return offsets;
}
return [];
}
factory PoseDetectionResult.fromJson(List<Object?> data) {
late List<PoseLandmarks> poses = List.empty(growable: true);
for (var element in data) {
final json = element as Map<dynamic, dynamic>;
poses.add(PoseLandmarks.fromJson(json));
}
return PoseDetectionResult(poses: poses);
}
}
class PoseLandmarks {
final String type;
final double x;
final double y;
final double z;
final double inFrameLikelihood;
PoseLandmarks(
{required this.type,
required this.x,
required this.y,
required this.z,
required this.inFrameLikelihood});
factory PoseLandmarks.fromJson(Map<dynamic, dynamic> json) {
return PoseLandmarks(
type: json['type'] as String,
x: json['x'] as double,
y: json['y'] as double,
z: json['z'] as double,
inFrameLikelihood: json['inFrameLikelihood'] as double,
);
}
}