Last active
March 27, 2018 16:30
-
-
Save germanattanasio/643ff3e52002b92a84726161e09cffe5 to your computer and use it in GitHub Desktop.
Call classify for Visual Recognition v3 using the fetch API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// When testing in node you will have to install `form-data` and `node-fetch`. You don't need them in React Native. | |
// const fetch = require('node-fetch'); | |
// const FormData = require('form-data'); | |
const VISUAL_RECOGNITION_API = 'https://gateway-a.watsonplatform.net/visual-recognition/api'; | |
const CLASSIFY_ENDPOINT = '/v3/classify'; | |
const apiKey = 'API_KEY_HERE'; // replace with a valid API Key | |
/** | |
* Classifies an image using the Visual Recognition service | |
* @param {object} params - The parameters | |
* @param {Array} [params.classifier_ids] - The array with classifiers ids | |
* @param {object} [params.image] - The image to classify | |
* @param {string} [params.api_key] - The Visual Recognition api key | |
*/ | |
const classifyImage = params => { | |
const formData = new FormData(); | |
formData.append('classifier_ids', params.classifier_ids.join(',')); | |
if (params.image) { | |
formData.append('images_file', { | |
uri: params.image.uri, | |
type: params.image.type, | |
name: '_', | |
}); | |
} | |
if (params.url) { | |
formData.append('url', params.url); | |
} | |
return fetch( | |
`${VISUAL_RECOGNITION_API}${CLASSIFY_ENDPOINT}?api_key=${params.api_key}&version=2016-05-20`, | |
{ | |
method: 'POST', | |
body: formData, | |
} | |
); | |
}; | |
// Classify a url in node | |
classifyImage({ | |
api_key: apiKey, | |
classifier_ids: ['windshields_1975276866', 'default'], | |
url: 'https://visual-recognition-demo.ng.bluemix.net/images/samples/1.jpg', | |
// Uncomment the 3 lines below to classify an image in React Native | |
// image: { | |
// uri:'content://media/external/images/media/15', | |
// type: 'image/jpg', | |
// }, | |
}) | |
.then(res => res.json()) | |
.catch(error => console.error('Error:', error)) | |
.then(response => console.log('Success:', JSON.stringify(response, null, 2))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[germana:~/visual-recognition]$ node fetch.js | |
Success: { | |
"images": [ | |
{ | |
"classifiers": [ | |
{ | |
"classifier_id": "windshields_1975276866", | |
"name": "windshields", | |
"classes": [ | |
{ | |
"class": "Repair", | |
"score": 0.728 | |
} | |
] | |
}, | |
{ | |
"classifier_id": "default", | |
"name": "default", | |
"classes": [ | |
{ | |
"class": "autoclave", | |
"score": 0.602 | |
}, | |
{ | |
"class": "rocket engine", | |
"score": 0.602, | |
"type_hierarchy": "/machine/motor/engine/jet engine/rocket engine" | |
}, | |
{ | |
"class": "jet engine", | |
"score": 0.723 | |
}, | |
{ | |
"class": "engine", | |
"score": 0.724 | |
}, | |
{ | |
"class": "motor", | |
"score": 0.724 | |
}, | |
{ | |
"class": "machine", | |
"score": 0.725 | |
}, | |
{ | |
"class": "spacecraft", | |
"score": 0.578, | |
"type_hierarchy": "/vehicle/craft/spacecraft" | |
}, | |
{ | |
"class": "craft", | |
"score": 0.578 | |
}, | |
{ | |
"class": "vehicle", | |
"score": 0.578 | |
}, | |
{ | |
"class": "turbojet engine", | |
"score": 0.527, | |
"type_hierarchy": "/machine/motor/engine/jet engine/turbojet engine" | |
}, | |
{ | |
"class": "furnace", | |
"score": 0.5, | |
"type_hierarchy": "/enclosure/furnace" | |
}, | |
{ | |
"class": "enclosure", | |
"score": 0.5 | |
}, | |
{ | |
"class": "instrument", | |
"score": 0.597 | |
}, | |
{ | |
"class": "equipment", | |
"score": 0.599 | |
}, | |
{ | |
"class": "coal black color", | |
"score": 0.657 | |
}, | |
{ | |
"class": "reddish orange color", | |
"score": 0.641 | |
} | |
] | |
} | |
], | |
"source_url": "https://visual-recognition-demo.ng.bluemix.net/images/samples/1.jpg", | |
"resolved_url": "https://visual-recognition-demo.ng.bluemix.net/images/samples/1.jpg" | |
} | |
], | |
"images_processed": 1, | |
"custom_classes": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment