forked from reneweb/react-native-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (108 loc) · 3.27 KB
/
index.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
import { NativeModules, Image } from 'react-native';
import uuid from 'uuid/v1';
const { RNImageRecognition, RNTensorFlowInference, RNTensorFlowGraph, RNTensorFlowGraphOperations } = NativeModules;
class TensorFlowOperation {
constructor(id, opName) {
this.id = id
this.opName = opName
}
inputListLength(name) {
return RNTensorFlowGraphOperations.inputListLength(this.id, this.opName, name);
}
name() {
return RNTensorFlowGraphOperations.name(this.id, this.opName)
}
numOutputs() {
return RNTensorFlowGraphOperations.numOutputs(this.id, this.opName)
}
output(index) {
return RNTensorFlowGraphOperations.output(this.id, this.opName, index)
}
outputList(index, length) {
return RNTensorFlowGraphOperations.outputList(this.id, this.opName, index, length)
}
outputListLength(name) {
return RNTensorFlowGraphOperations.outputListLength(this.id, this.opName, name)
}
type() {
return RNTensorFlowGraphOperations.type(this.id, this.opName)
}
}
class TensorFlowGraph {
constructor(id) {
this.id = id
}
importGraphDef(graphDef) {
return RNTensorFlowGraph.importGraphDef(this.id, graphDef)
}
importGraphDefWithPrefix(graphDef, prefix) {
return RNTensorFlowGraph.importGraphDefWithPrefix(this.id, graphDef, prefix)
}
toGraphDef() {
return RNTensorFlowGraph.toGraphDef(this.id)
}
operation(name) {
return new TensorFlowOperation(this.id, name)
}
close() {
return RNTensorFlowGraph.close(this.id)
}
}
class TensorFlow {
constructor(modelLocation) {
this.id = uuid()
const resolvedModelLocation = Image.resolveAssetSource(modelLocation) != null
? Image.resolveAssetSource(modelLocation).uri
: modelLocation
this.init = RNTensorFlowInference.initTensorFlowInference(this.id, resolvedModelLocation)
this.tfGraph = new TensorFlowGraph(this.id)
}
async feed(data) {
await this.init
return RNTensorFlowInference.feed(this.id, data)
}
async run(outputNames, withStats) {
await this.init
return RNTensorFlowInference.run(this.id, outputNames, withStats !== undefined ? withStats : false)
}
async fetch(outputName) {
await this.init
return RNTensorFlowInference.fetch(this.id, outputName)
}
async graph() {
await this.init
return this.tfGraph
}
async reset() {
await this.init
return RNTensorFlowInference.reset(this.id)
}
async close() {
await this.init
return RNTensorFlowInference.close(this.id)
}
}
class TfImageRecognition {
constructor(data) {
this.id = uuid()
data['model'] = Image.resolveAssetSource(data['model']) != null
? Image.resolveAssetSource(data['model']).uri
: data['model']
data['labels'] = Image.resolveAssetSource(data['labels']) != null
? Image.resolveAssetSource(data['labels']).uri
: data['labels']
this.init = RNImageRecognition.initImageRecognizer(this.id, data)
}
async recognize(data) {
await this.init
data['image'] = Image.resolveAssetSource(data['image']) != null
? Image.resolveAssetSource(data['image']).uri
: data['image']
return RNImageRecognition.recognize(this.id, data)
}
async close() {
await this.init
return RNImageRecognition.close(this.id)
}
}
export { TensorFlow, TfImageRecognition }