-
Notifications
You must be signed in to change notification settings - Fork 11
/
module.js
332 lines (331 loc) · 8.56 KB
/
module.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { NativeModules, NativeEventEmitter } from "react-native";
const RNVNative = NativeModules.RHDVisionModule;
//#region Event Management
var cachedEmitter = null;
const getEmitter = () => {
if (!cachedEmitter) {
cachedEmitter = new NativeEventEmitter(RNVNative);
}
return cachedEmitter;
};
var cachedListener = null;
const addListener = (region, key, cb) => {
const newKey = region + "_" + key;
if (!cachedListener) {
cachedListener = getEmitter().addListener("RNVision", masterHandler);
}
cachedHandlers[newKey] = cb;
};
const masterHandler = body => {
const region = String(body["region"]);
const thisKey = String(body["key"]);
const key = region + "_" + thisKey;
if (typeof cachedHandlers[key] == "function") {
cachedHandlers[key](body);
} else {
console.log("NO handler for ", key, region, thisKey);
}
};
var cachedHandlers = {};
const removeListener = (region, key) => {
const newKey = String(region) + "_" + String(key);
delete cachedHandlers[newKey];
};
//#endregion
//#region Lifecycle management
const start = async cameraFront => {
return await RNVNative.start(cameraFront);
};
const stop = async () => {
cachedHandlers = {};
cachedListener.remove();
cachedEmitter = null;
return await RNVNative.stop();
};
const attachCameraView = async () => {
return await RNVNative.attachCameraView();
};
const isCameraFrame = async isTrue => {
return await RNVNative.isCameraView(isTrue);
};
const getImageDimensions = async () => {
return await RNVNative.getImageDimensions();
};
var ImageDimensionListener = null;
const setImageDimensionListener = cb => {
if (!cb) return removeImageDimensionListener();
if (typeof cb != "function")
throw new Error("Argument must be a function in setImageDimensionListener");
if (ImageDimensionListener) removeImageDimensionListener;
ImageDimensionListener = getEmitter().addListener("RNVisionImageDim", cb);
return true;
};
const removeImageDimensionListener = () => {
if (ImageDimensionListener) ImageDimensionListener.remove();
ImageDimensionListener = null;
return true;
};
//#endregion
//#region Save Frame
const saveFrame = async (region, disposition, callback) => {
//Add a listener
addListener(region, "saveFrame", callback);
return await RNVNative.saveFrame(disposition, region);
};
const removeSaveFrame = async region => {
removeListener(region, "saveFrame");
return await RNVNative.removeSaveFrame(region);
};
const saveFrameOnce = (region, disposition) => {
return new Promise((resolve, reject) => {
saveFrame(region, disposition, async body => {
await removeSaveFrame(region);
return body;
});
});
};
//#endregion
//#region Face Detection
const detectFaces = async (region, handler) => {
const key = await RNVNative.detectFaces(region); // Key should be "detectFaces"
addListener(region, key, body => {
return handler(body.data);
});
return key;
};
const removeDetectFaces = async region => {
removeListener(region, "detectFaces");
return await RNVNative.removeDetectFaces(region);
};
const detectFacesOnce = region => {
return new Promise((resolve, reject) => {
detectFaces(region, body => {
removeDetectFaces(region);
resolve(body);
});
});
};
//#endregion
//#region Object Tracking
var boxHandlers = {};
var boxListener = null;
const trackObject = async (region, name, boxDictionary, callback) => {
if ((await RNVNative.trackObject(name, region, boxDictionary)) !== null) {
addListener(region, name, callback);
return true;
} else return false;
};
const removeTrackObject = async (region, name) => {
removeListener(region, name);
return await RNVNative.removeTrackObject(name, region);
};
const removeTrackObjects = async region => {
const data = await RNVNative.removeTrackObjects(region);
if (data.removedKeys) {
data.removedKeys.forEach(removedKey => {
removeListener(removedKey);
});
}
return true;
};
//#endregion
//#region Region Management
const setRegion = async (region, rectangle) => {
return await RNVNative.setRegion(region, rectangle);
};
const removeRegion = async region => {
return await RNVNative.removeRegion(region);
};
//#endregion
//#region Machine Learning Models
const applyMLClassifier = async (
region,
modelURL,
maxResults,
callback = null
) => {
if (typeof maxResults == "function") {
callback = maxResults;
maxResults = 5;
}
const key = await RNVNative.applyMLClassifier(modelURL, region, maxResults);
if (key) {
addListener(region, key, body => {
callback(body.data);
});
}
return key;
};
const applyMLClassifierOnce = (region, modelURL, maxResults) => {
return new Promise((resolve, reject) => {
applyMLClassifier(region, modelURL, maxResults, body => {
removeML(region, modelURL);
resolve(body);
});
});
};
const applyMLGenerator = async (region, modelURL, handler, callback) => {
const key = await RNVNative.applyMLGenerator(modelURL, region, handler);
if (handler != "view" && typeof callback == "function") {
addListener(region, key, data => {
callback(data.data);
});
}
return key;
};
const applyMLBottleneck = async modelURL => {
return await RNVNative.applyMLBottleneck(modelURL);
};
const applyMLGeneric = async (region, modelURL, callback) => {
const key = await RNVNative.applyMLGeneric(modelURL, region);
if (key) {
addListener(region, key, body => {
callback(body.data);
});
}
return key;
};
const applyMLGenericOnce = (region, modelURL) => {
return new Promise((resolve, reject) => {
applyMLGeneric(region, modelURL, body => {
removeML(region, modelURL);
resolve(body);
});
});
};
const removeML = async (region, modelURL) => {
removeListener(region, modelURL);
return await RNVNative.removeML(modelURL, region);
};
//#endregion
//#region ML Bottlenecks
const REGION_ALL = "";
const applyBottleneckClassifier = async (
modelURL,
region,
toModelURL,
maxResults,
callback = null
) => {
if (typeof maxResults == "function") {
callback = maxResults;
maxResults = 5;
}
const key = await RNVNative.applyBottleneckClassifier(
modelURL,
region,
toModelURL,
maxResults
);
if (key) {
addListener(key, body => {
callback(body.data);
});
}
};
const applyBottleneckGenerator = async (
modelURL,
region,
toModelURL,
handlerOrCallback
) => {
const handler =
typeof handlerOrCallback == "function" ? "sendEvent" : handlerOrCallback;
const key = await RNVNative.applyBottleneckGenerator(
modelURL,
handler,
region,
toModelURL
);
if (key && handler == "sendEvent") addListener(key, handlerOrCallback);
};
const applyBottleneckBottleneck = async (modelURL, region, toModelURL) => {
return await RNVNative.applyBottleneckBottleneck(modelURL, toModelURL);
};
const applyBottleneckGeneric = async (
modelURL,
region,
toModelURL,
callback
) => {
const key = await RNVNative.applyBottleneckGeneric(
modelURL,
region,
toModelURL
);
if (key) {
addListener(key, body => {
callback(body.data);
});
}
};
const removeBottleneck = async (modelURL, region, fromModelURL) => {
removeListener(modelURL);
return await RNVNative.removeBottleneck(modelURL, region, fromModelURL);
};
const removeBottlenecks = async (region, fromModelURL) => {
const out = await RNVNative.removeBottlenecks(region, fromModelURL);
if (out) {
if (out.removedBottlenecks) {
out.removedBottlenecks.forEach(key => {
removeListener(key);
});
}
}
};
//#endregion
//#region MultiArray access
//Returns URL of saved file
const saveMultiArray = async name => {
return await RNVNative.saveMultiArray(name);
};
//#endregion
//#region Metadata Capture
var MDListener = null;
const handleMetadata = async callback => {
removeMetadataListener();
MDListener = getEmitter().addListener("RNVisionMetaData", callback);
};
const removeMetadataListener = {
if(MDListener) {
MDListener.remove();
}
};
//#endregion
//#region Exports
export {
REGION_ALL,
start,
stop,
attachCameraView,
isCameraFrame,
getImageDimensions,
setImageDimensionListener,
removeImageDimensionListener,
saveFrame,
saveFrameOnce,
removeSaveFrame,
detectFaces,
detectFacesOnce,
removeDetectFaces,
trackObject,
removeTrackObject,
setRegion,
removeRegion,
applyMLClassifier,
applyMLClassifierOnce,
applyMLGenerator,
applyMLBottleneck,
applyMLGeneric,
applyMLGenericOnce,
applyBottleneckClassifier,
applyBottleneckGenerator,
applyBottleneckBottleneck,
applyBottleneckGeneric,
removeML,
removeBottleneck,
removeBottlenecks,
handleMetadata,
removeMetadataListener
};
//#endregion