Build “SeeFood” from HBO’s Silicon Valley in under 10 minutes
I’m going to walk you through setting up your own “Shazam for food” using Watson services. It’s wickedly easy to do and can do way more than just hotdogs. Out of the box Watson comes equipt with the ability to recognize food you take pictures of. I’m going to take it even further and show you how to build your own custom models. The first step is signing up for Bluemix. This is probably the most difficult step, so here are some photos of Watson in action to keep you strong through this tough task.
Bluemix
In order to use Watson, you need to create the service and generate credentials on IBM Bluemix. Bluemix is IBM’s PaaS offering that lets you deploy and manage your cloud applications.
Once you sign-up you should see the Visual Recognition service, if not, go to the Catalog and you should find the Visual Recognition service under Watson.
After creating the service, you should be able to find your API key by clicking View Credentials in the Service Credentials sections.
The Food Model
With your API key you gain access to the Visual Recognition Tool.
Find a nice picture of food and drop it on the “Food” tile. That was easy 😀 You can use all of the tiles you see here via a simple REST API. You can of course use Curl, but there are also SDKs for NodeJS, Python, Unity, Swift, Java, and Android!
To try this out with Curl, run this command:
curl -X POST \
-F "images_file=@{IMAGE.jpg}" \
"https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?version=2016-05-20&threshold=0.0&api_key={API_KEY}&classifier_ids=food"
Note: Replace {API_KEY}
and {IMAGE.jpg}
.
Custom Classifiers
As great as this food model is, a lot of us don’t actually need a food model. Most of the time we need something custom fit. For some strange reason I, an avid Star Wars fan, can never remember the difference between Darth Vader and Darth Maul, so I need Watson to be able to tell me 😉
Get data to train with
For this demo, I’m using a Chrome extension to download pictures from Google search. The pictures we use could come from anywhere, but ideally they’re pictures you’ve collected yourself. This not only prevents being sued for copyright infringement, but also ensures the pictures are as specific to your use case as possible.
I ended up grabbing 100 images of Darth Maul, 100 of Darth Vader, and an additional 15 test images for each.
Note: The minimum we need is 10, but let’s be overachievers.
Build the model
Click the “Create Classifier” button and you should be taken to a page that looks something like this:
Fill in the required fields and upload the zip files containing your 100 images. Once it’s trained, you can test the classifier by dropping an image onto the tile.
Take note of the classifier_id, located under the classifier name. You can use this id to access your trained model with Curl or one of the SDKs.
curl -X POST \
-F "images_file=@{IMAGE.jpg}" \
"https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?version=2016-05-20&threshold=0.0&api_key={API_KEY}&classifier_ids={CLASSIFIER_ID}"
Note: Replace {API_KEY}
, {IMAGE.jpg}
and {CLASSIFIER_ID}
.
You should get something along the lines of this as your output:
{
"custom_classes": 2,
"images": [{
"classifiers": [{
"classes": [
{"class": "Darth Maul", "score": 0.559958},
{"class": "Darth Vader", "score": 0.047293}
],
"classifier_id": "StarWars_1680254220",
"name": "Star Wars"
}],
"image": "my_test_photo.jpg"
}],
"images_processed": 1
}
Final Thoughts
Hopefully that wasn’t too hard and you learned a new skill 🙌 Visual recognition is super cool to play with and can definitely be useful in a lot of situations. It’s important to note, however, that it isn’t the answer to all problems, it’s another tool in the toolbox. Have fun, be creative and keep on hacking!