@capturebridge/sdk 0.12.1 → 0.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ICAO.js CHANGED
@@ -1,86 +1,86 @@
1
- import Plugin from './Plugin.js'
2
- import { BASE_URL } from './Common.js'
3
-
4
- /**
5
- * ICAO
6
- * @classdesc Class for invoking ICAO checks on an image
7
- * @extends Plugin
8
- * @see {@link IFace}
9
- */
10
- class ICAO extends Plugin {
11
- /**
12
- * Instantiate an ICAO plugin
13
- * @constructor
14
- * @param {string} [baseURL] - Protocol, domain, and port for the service.
15
- * @param {string|object} plugin - The ID of the desired plugin to use for
16
- * this instance or an existing Plugin object. The plugin must support the
17
- * `icao` method. Defaults to the `capture_verify_iface` plugin.
18
- *
19
- * @example
20
- * const icao = new ICAO()
21
- */
22
- constructor (plugin = 'capture_verify_iface', baseUrl = BASE_URL) {
23
- super(plugin, baseUrl)
24
- }
25
-
26
- /**
27
- * Check the ICAO compliance of an image
28
- * @param {string|ImageSource} image - A string containing base64 encoded image
29
- * data or an ImageSource object.
30
- *
31
- * @param {string|bool} [crop] - If falsy image will not be cropped, otherwise
32
- * the value will be interpreted as the preferred crop method which will vary
33
- * by plugin implementation.
34
- *
35
- * @param {string} [background=#ffffff] - Hexadecimal color that will fill in
36
- * any part of the cropped image that falls outside the original source image.
37
- *
38
- * @returns {object} Results of the ICAO check, this may vary by plugin
39
- * implementation.
40
- *
41
- * @async
42
- * @example
43
- * // Take a photo with a Canon Camera and perform ICAO checks on the
44
- * // returned image
45
- * const camera = new CanonCamera()
46
- * const photo = await camera.takePhoto('base64')
47
- * const icao = new ICAO()
48
- * const results = await icao.check(photo)
49
- * const img = document.createElement('img')
50
- * img.src = 'data:image/jpeg;base64,' + results.cropped
51
- * console.log(`found ${result.faces_found} faces in the image`)
52
- * document.body.appendChild(img)
53
- */
54
- async check (image, cropMethod = false, cropBackground) {
55
- let imageData
56
-
57
- // If this is an ImageSource object fetch it's image data
58
- if (typeof image === 'object' && image.constructor.name === 'ImageSource') {
59
- imageData = await image.data()
60
- } else if (typeof image === 'string') {
61
- imageData = image
62
- } else {
63
- throw new Error('Unexpected value provided for image parameter')
64
- }
65
-
66
- const body = {
67
- base64: imageData,
68
- crop: Boolean(cropMethod),
69
- crop_method: cropMethod || undefined,
70
- crop_background: cropBackground || undefined
71
- }
72
- const options = {
73
- method: 'POST',
74
- mode: 'cors',
75
- headers: {
76
- 'Content-Type': 'application/json'
77
- },
78
- body: JSON.stringify(body)
79
- }
80
- const url = `${this.baseUrl}/plugin/${this.id}/icao`
81
- const response = await fetch(url, options)
82
- return await response.json()
83
- }
84
- }
85
-
86
- export default ICAO
1
+ import Plugin from './Plugin.js'
2
+ import { BASE_URL } from './Common.js'
3
+
4
+ /**
5
+ * ICAO
6
+ * @classdesc Class for invoking ICAO checks on an image
7
+ * @extends Plugin
8
+ * @see {@link IFace}
9
+ */
10
+ class ICAO extends Plugin {
11
+ /**
12
+ * Instantiate an ICAO plugin
13
+ * @constructor
14
+ * @param {string} [baseURL] - Protocol, domain, and port for the service.
15
+ * @param {string|object} plugin - The ID of the desired plugin to use for
16
+ * this instance or an existing Plugin object. The plugin must support the
17
+ * `icao` method. Defaults to the `capture_verify_iface` plugin.
18
+ *
19
+ * @example
20
+ * const icao = new ICAO()
21
+ */
22
+ constructor (plugin = 'capture_verify_iface', baseUrl = BASE_URL) {
23
+ super(plugin, baseUrl)
24
+ }
25
+
26
+ /**
27
+ * Check the ICAO compliance of an image
28
+ * @param {string|ImageSource} image - A string containing base64 encoded image
29
+ * data or an ImageSource object.
30
+ *
31
+ * @param {string|bool} [crop] - If falsy image will not be cropped, otherwise
32
+ * the value will be interpreted as the preferred crop method which will vary
33
+ * by plugin implementation.
34
+ *
35
+ * @param {string} [background=#ffffff] - Hexadecimal color that will fill in
36
+ * any part of the cropped image that falls outside the original source image.
37
+ *
38
+ * @returns {object} Results of the ICAO check, this may vary by plugin
39
+ * implementation.
40
+ *
41
+ * @async
42
+ * @example
43
+ * // Take a photo with a Canon Camera and perform ICAO checks on the
44
+ * // returned image
45
+ * const camera = new CanonCamera()
46
+ * const photo = await camera.takePhoto('base64')
47
+ * const icao = new ICAO()
48
+ * const results = await icao.check(photo)
49
+ * const img = document.createElement('img')
50
+ * img.src = 'data:image/jpeg;base64,' + results.cropped
51
+ * console.log(`found ${result.faces_found} faces in the image`)
52
+ * document.body.appendChild(img)
53
+ */
54
+ async check (image, cropMethod = false, cropBackground) {
55
+ let imageData
56
+
57
+ // If this is an ImageSource object fetch it's image data
58
+ if (typeof image === 'object' && image.constructor.name === 'ImageSource') {
59
+ imageData = await image.data()
60
+ } else if (typeof image === 'string') {
61
+ imageData = image
62
+ } else {
63
+ throw new Error('Unexpected value provided for image parameter')
64
+ }
65
+
66
+ const body = {
67
+ base64: imageData,
68
+ crop: Boolean(cropMethod),
69
+ crop_method: cropMethod || undefined,
70
+ crop_background: cropBackground || undefined
71
+ }
72
+ const options = {
73
+ method: 'POST',
74
+ mode: 'cors',
75
+ headers: {
76
+ 'Content-Type': 'application/json'
77
+ },
78
+ body: JSON.stringify(body)
79
+ }
80
+ const url = `${this.baseUrl}/plugin/${this.id}/icao`
81
+ const response = await fetch(url, options)
82
+ return await response.json()
83
+ }
84
+ }
85
+
86
+ export default ICAO
package/IFace.js CHANGED
@@ -1,237 +1,237 @@
1
- /**
2
- * @module IFace
3
- */
4
-
5
- import ICAO from './ICAO.js'
6
- import ImageSource from './ImageSource.js'
7
- import { BASE_URL } from './Common.js'
8
-
9
- // This is not exported as we may eventually detect this from the plugin
10
- // See: Iface.matchModes()
11
- const VALID_MODES = ['accurate', 'balanced', 'fast', 'accurate_server']
12
-
13
- /**
14
- * Probe
15
- * @classdesc Probe Class for Innovatrics IFace SDK
16
- *
17
- * @see {@link IFace}
18
- */
19
- export class Probe extends ImageSource {
20
- /**
21
- * Instantiate an IFace Probe
22
- * @constructor
23
- * @param {string} source - The image source for the probe.
24
- * @param {string} [type] - The type of data in the image source, valid values
25
- * are 'url', 'base64', and 'dataurl'. If not provided 'base64' is the default.
26
- *
27
- * @example
28
- * const candidate = new Candidate('/person/123.jpg', '123', 'url')
29
- */
30
- constructor (source, type = 'base64') {
31
- super(source, type)
32
- }
33
- }
34
-
35
- /**
36
- * Candidate
37
- * @classdesc Candidate Class for Innovatrics IFace SDK
38
- * @extends ImageSource
39
- * @see {@link IFace}
40
- */
41
- export class Candidate extends ImageSource {
42
- id
43
-
44
- // TODO: Support setting different modes on individual candidates
45
- // an example usecase for this is to set 'accurate_server' the most recent
46
- // photo, and 'fast' for the remaining set (since they likely have already
47
- // been verified at some point) In the plugin we would have to re-template the
48
- // probe for each additional mode and it doesn't make sense to do so until
49
- // the plugin is checking each photo against the probe in a separate thread.
50
- mode
51
-
52
- /**
53
- * Instantiate an IFace Candidate
54
- * @constructor
55
- * @param {string} source - The image source for the candidate.
56
- * @param {string} id - The ID for the candidate when the match results are
57
- * returned this Id will be returned with it's respective results.
58
- * @param {string} [type] - The type of data in the image source, valid values
59
- * are 'url', 'base64', and 'dataurl'. If not provided 'base64' is the default.
60
- *
61
- * @example
62
- * const candidate = new Candidate('/person/123.jpg', '123', 'url')
63
- */
64
- constructor (source, id, type = 'base64') {
65
- super(source, type)
66
- if (id) {
67
- this.id = id
68
- } else {
69
- throw new Error('Candidate ID not provided')
70
- }
71
- }
72
-
73
- toJSON () {
74
- return {
75
- id: this.id,
76
- base64: this.imageData
77
- }
78
- }
79
- }
80
-
81
- /**
82
- * IFace
83
- * @classdesc Class for Innovatrics IFace SDK
84
- *
85
- */
86
- export class IFace extends ICAO {
87
- /**
88
- * Instantiate an IFace plugin
89
- * @constructor
90
- * @param {string} [baseURL] - Protocol, domain, and port for the service.
91
- *
92
- * @example
93
- * const iface = new IFace()
94
- */
95
- constructor (baseUrl = BASE_URL) {
96
- super('capture_verify_iface', baseUrl)
97
- }
98
-
99
- /**
100
- * Return list of available face matching modes
101
- *
102
- * @returns {string[]} List of available face matching modes
103
- *
104
- * @async
105
- * @example
106
- * const iface = new IFace()
107
- * const modes = await iface.matchModes()
108
- * // do something with modes, such as build a <select> element
109
- * const select = document.createElement('select')
110
- * modes.forEach(mode => {
111
- * const option = document.createElement('option')
112
- * option.value = mode
113
- * select.appendChild(mode)
114
- * })
115
- * document.body.appendChild(select)
116
- */
117
- async matchModes () {
118
- // This is async as we may later get this from the plugin at runtime
119
- return VALID_MODES
120
- }
121
-
122
- matchModesHandler (callback) {
123
- callback(null, VALID_MODES)
124
- }
125
-
126
- /**
127
- * Check the ICAO compliance of an image
128
- * @param {string} image - A base64 encoded image.
129
- *
130
- * @param {string|bool} [crop] - If falsy image will not be cropped, otherwise
131
- * the value will be interpreted as the preferred crop method which will vary
132
- * by plugin implementation.
133
- *
134
- * @param {string} [background=#ffffff] - Hexadecimal color that will fill in
135
- * any part of the cropped image that falls outside the original source image.
136
- *
137
- * @returns {object} Results of the ICAO check, this may vary by plugin
138
- * implementation.
139
- *
140
- * @async
141
- * @example
142
- * // Take a photo with a Canon Camera and perform ICAO checks on the
143
- * // returned image
144
- * const camera = new CanonCamera()
145
- * const photo = await camera.takePhoto('base64')
146
- * const iface = new IFace()
147
- * const results = await iface.icao(photo)
148
- * const img = document.createElement('img')
149
- * img.src = 'data:image/jpeg;base64,' + results.cropped
150
- * console.log(`found ${result.faces_found} faces in the image`)
151
- * document.body.appendChild(img)
152
- */
153
- async icao (image, cropMethod = false, cropBackground) {
154
- return await this.check(image, cropMethod, cropBackground)
155
- }
156
-
157
- /**
158
- * Perform a facial match against one or more photos.
159
- *
160
- * @param {object|string} probe - Either a Probe object or a base64 encoded
161
- * image that is being compared or searched against one or more candidates
162
- *
163
- * @param {object[]} candidates - An array of candidate objects against which
164
- * the probe photo is compared.
165
- * @param {string} candidates[].id - Id of the photo, when the results of the
166
- * matched are returned, this id will be returned so results can be matched
167
- * back to their original image. Must be less than 16 characters.
168
- * @param {string} candidates[].base64 - Base64 encoded string containing the
169
- * photo.
170
- * @param {string} [candidates[].mode] - Matching mode to use for this photo.
171
- * If left unspecified will use the mode provided in the mode parameter.
172
- *
173
- * @param {string} [mode=fast] - Matching mode to use for all images, can be
174
- * overriden on each candidate if desired. Valid values are: 'accurate',
175
- * 'balanced', 'fast', and 'accurate_server'.
176
- *
177
- * @async
178
- * @example
179
- * // Create an iface object
180
- * const iface = new IFace()
181
- *
182
- * // Obtain a photo for the probe photo
183
- * const probe = await camera.takePhoto('base64')
184
- *
185
- * // Create a candidate set from the person's previous photos
186
- * const candidates = [
187
- * new Candidate('/person/1/photo/2.jpg', '3', 'url'),
188
- * new Candidate('/person/1/photo/1.jpg', '2', 'url')
189
- * ]
190
- *
191
- * // Match the probe to all the candidates using the 'balanced' mode
192
- * const results = await iface.match(probe, candidates, 'balanced')
193
- *
194
- * // use the results
195
- * console.log(results)
196
- */
197
- async match (probe, candidates, mode = 'fast') {
198
- if (VALID_MODES.indexOf(mode) === -1) {
199
- throw new Error('Invalid mode provided')
200
- }
201
-
202
- // If this is a Probe object fetch it's image data
203
- if (typeof probe === 'object' && probe.constructor.name === 'Probe') {
204
- await probe.data()
205
- }
206
-
207
- // And fetch data for candidates
208
- const asyncOperations = candidates.map(async candidate => {
209
- if (typeof candidate === 'object' &&
210
- candidate.constructor.name === 'Candidate') {
211
- await candidate.data()
212
- }
213
- })
214
-
215
- // Wait for all async operations to complete
216
- await Promise.all(asyncOperations)
217
-
218
- const body = {
219
- probe,
220
- mode,
221
- candidates
222
- }
223
- const options = {
224
- method: 'POST',
225
- mode: 'cors',
226
- headers: {
227
- 'Content-Type': 'application/json'
228
- },
229
- body: JSON.stringify(body)
230
- }
231
- const url = `${this.baseUrl}/plugin/${this.id}/face/match`
232
- const response = await fetch(url, options)
233
- return await response.json()
234
- }
235
- }
236
-
237
- export default IFace
1
+ /**
2
+ * @module IFace
3
+ */
4
+
5
+ import ICAO from './ICAO.js'
6
+ import ImageSource from './ImageSource.js'
7
+ import { BASE_URL } from './Common.js'
8
+
9
+ // This is not exported as we may eventually detect this from the plugin
10
+ // See: Iface.matchModes()
11
+ const VALID_MODES = ['accurate', 'balanced', 'fast', 'accurate_server']
12
+
13
+ /**
14
+ * Probe
15
+ * @classdesc Probe Class for Innovatrics IFace SDK
16
+ *
17
+ * @see {@link IFace}
18
+ */
19
+ export class Probe extends ImageSource {
20
+ /**
21
+ * Instantiate an IFace Probe
22
+ * @constructor
23
+ * @param {string} source - The image source for the probe.
24
+ * @param {string} [type] - The type of data in the image source, valid values
25
+ * are 'url', 'base64', and 'dataurl'. If not provided 'base64' is the default.
26
+ *
27
+ * @example
28
+ * const candidate = new Candidate('/person/123.jpg', '123', 'url')
29
+ */
30
+ constructor (source, type = 'base64') {
31
+ super(source, type)
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Candidate
37
+ * @classdesc Candidate Class for Innovatrics IFace SDK
38
+ * @extends ImageSource
39
+ * @see {@link IFace}
40
+ */
41
+ export class Candidate extends ImageSource {
42
+ id
43
+
44
+ // TODO: Support setting different modes on individual candidates
45
+ // an example usecase for this is to set 'accurate_server' the most recent
46
+ // photo, and 'fast' for the remaining set (since they likely have already
47
+ // been verified at some point) In the plugin we would have to re-template the
48
+ // probe for each additional mode and it doesn't make sense to do so until
49
+ // the plugin is checking each photo against the probe in a separate thread.
50
+ mode
51
+
52
+ /**
53
+ * Instantiate an IFace Candidate
54
+ * @constructor
55
+ * @param {string} source - The image source for the candidate.
56
+ * @param {string} id - The ID for the candidate when the match results are
57
+ * returned this Id will be returned with it's respective results.
58
+ * @param {string} [type] - The type of data in the image source, valid values
59
+ * are 'url', 'base64', and 'dataurl'. If not provided 'base64' is the default.
60
+ *
61
+ * @example
62
+ * const candidate = new Candidate('/person/123.jpg', '123', 'url')
63
+ */
64
+ constructor (source, id, type = 'base64') {
65
+ super(source, type)
66
+ if (id) {
67
+ this.id = id
68
+ } else {
69
+ throw new Error('Candidate ID not provided')
70
+ }
71
+ }
72
+
73
+ toJSON () {
74
+ return {
75
+ id: this.id,
76
+ base64: this.imageData
77
+ }
78
+ }
79
+ }
80
+
81
+ /**
82
+ * IFace
83
+ * @classdesc Class for Innovatrics IFace SDK
84
+ *
85
+ */
86
+ export class IFace extends ICAO {
87
+ /**
88
+ * Instantiate an IFace plugin
89
+ * @constructor
90
+ * @param {string} [baseURL] - Protocol, domain, and port for the service.
91
+ *
92
+ * @example
93
+ * const iface = new IFace()
94
+ */
95
+ constructor (baseUrl = BASE_URL) {
96
+ super('capture_verify_iface', baseUrl)
97
+ }
98
+
99
+ /**
100
+ * Return list of available face matching modes
101
+ *
102
+ * @returns {string[]} List of available face matching modes
103
+ *
104
+ * @async
105
+ * @example
106
+ * const iface = new IFace()
107
+ * const modes = await iface.matchModes()
108
+ * // do something with modes, such as build a <select> element
109
+ * const select = document.createElement('select')
110
+ * modes.forEach(mode => {
111
+ * const option = document.createElement('option')
112
+ * option.value = mode
113
+ * select.appendChild(mode)
114
+ * })
115
+ * document.body.appendChild(select)
116
+ */
117
+ async matchModes () {
118
+ // This is async as we may later get this from the plugin at runtime
119
+ return VALID_MODES
120
+ }
121
+
122
+ matchModesHandler (callback) {
123
+ callback(null, VALID_MODES)
124
+ }
125
+
126
+ /**
127
+ * Check the ICAO compliance of an image
128
+ * @param {string} image - A base64 encoded image.
129
+ *
130
+ * @param {string|bool} [crop] - If falsy image will not be cropped, otherwise
131
+ * the value will be interpreted as the preferred crop method which will vary
132
+ * by plugin implementation.
133
+ *
134
+ * @param {string} [background=#ffffff] - Hexadecimal color that will fill in
135
+ * any part of the cropped image that falls outside the original source image.
136
+ *
137
+ * @returns {object} Results of the ICAO check, this may vary by plugin
138
+ * implementation.
139
+ *
140
+ * @async
141
+ * @example
142
+ * // Take a photo with a Canon Camera and perform ICAO checks on the
143
+ * // returned image
144
+ * const camera = new CanonCamera()
145
+ * const photo = await camera.takePhoto('base64')
146
+ * const iface = new IFace()
147
+ * const results = await iface.icao(photo)
148
+ * const img = document.createElement('img')
149
+ * img.src = 'data:image/jpeg;base64,' + results.cropped
150
+ * console.log(`found ${result.faces_found} faces in the image`)
151
+ * document.body.appendChild(img)
152
+ */
153
+ async icao (image, cropMethod = false, cropBackground) {
154
+ return await this.check(image, cropMethod, cropBackground)
155
+ }
156
+
157
+ /**
158
+ * Perform a facial match against one or more photos.
159
+ *
160
+ * @param {object|string} probe - Either a Probe object or a base64 encoded
161
+ * image that is being compared or searched against one or more candidates
162
+ *
163
+ * @param {object[]} candidates - An array of candidate objects against which
164
+ * the probe photo is compared.
165
+ * @param {string} candidates[].id - Id of the photo, when the results of the
166
+ * matched are returned, this id will be returned so results can be matched
167
+ * back to their original image. Must be less than 16 characters.
168
+ * @param {string} candidates[].base64 - Base64 encoded string containing the
169
+ * photo.
170
+ * @param {string} [candidates[].mode] - Matching mode to use for this photo.
171
+ * If left unspecified will use the mode provided in the mode parameter.
172
+ *
173
+ * @param {string} [mode=fast] - Matching mode to use for all images, can be
174
+ * overriden on each candidate if desired. Valid values are: 'accurate',
175
+ * 'balanced', 'fast', and 'accurate_server'.
176
+ *
177
+ * @async
178
+ * @example
179
+ * // Create an iface object
180
+ * const iface = new IFace()
181
+ *
182
+ * // Obtain a photo for the probe photo
183
+ * const probe = await camera.takePhoto('base64')
184
+ *
185
+ * // Create a candidate set from the person's previous photos
186
+ * const candidates = [
187
+ * new Candidate('/person/1/photo/2.jpg', '3', 'url'),
188
+ * new Candidate('/person/1/photo/1.jpg', '2', 'url')
189
+ * ]
190
+ *
191
+ * // Match the probe to all the candidates using the 'balanced' mode
192
+ * const results = await iface.match(probe, candidates, 'balanced')
193
+ *
194
+ * // use the results
195
+ * console.log(results)
196
+ */
197
+ async match (probe, candidates, mode = 'fast') {
198
+ if (VALID_MODES.indexOf(mode) === -1) {
199
+ throw new Error('Invalid mode provided')
200
+ }
201
+
202
+ // If this is a Probe object fetch it's image data
203
+ if (typeof probe === 'object' && probe.constructor.name === 'Probe') {
204
+ await probe.data()
205
+ }
206
+
207
+ // And fetch data for candidates
208
+ const asyncOperations = candidates.map(async candidate => {
209
+ if (typeof candidate === 'object' &&
210
+ candidate.constructor.name === 'Candidate') {
211
+ await candidate.data()
212
+ }
213
+ })
214
+
215
+ // Wait for all async operations to complete
216
+ await Promise.all(asyncOperations)
217
+
218
+ const body = {
219
+ probe,
220
+ mode,
221
+ candidates
222
+ }
223
+ const options = {
224
+ method: 'POST',
225
+ mode: 'cors',
226
+ headers: {
227
+ 'Content-Type': 'application/json'
228
+ },
229
+ body: JSON.stringify(body)
230
+ }
231
+ const url = `${this.baseUrl}/plugin/${this.id}/face/match`
232
+ const response = await fetch(url, options)
233
+ return await response.json()
234
+ }
235
+ }
236
+
237
+ export default IFace