Giter Club home page Giter Club logo

ibm / watson-calorie-counter Goto Github PK

View Code? Open in Web Editor NEW
53.0 18.0 59.0 23.27 MB

WARNING: This repository is no longer maintained ⚠️ This repository will not be updated. The repository will be kept available in read-only mode. A mobile app that uses Watson Visual Recognition to provide nutritional analysis of captured food images.

Home Page: https://developer.ibm.com/patterns/create-an-android-calorie-counter-app/

License: Apache License 2.0

Java 31.21% CSS 17.35% HTML 11.32% JavaScript 27.55% Objective-C 6.68% Dockerfile 5.89%
watson-visual-recognition nutritionix-api phone nodejs watson-services android-platform android-studio android-sdk visual-recognition watson

watson-calorie-counter's Introduction

Build Status

Create a calorie counter mobile app using Watson Visual Recognition

In this Code Pattern, we will create a calorie counter mobile app using Apache Cordova, Node.js and Watson Visual Recognition. This mobile app extracts nutritional information from captured images of food items.

Currently this mobile app only runs on Android, but can be easily ported to iOS.

Architecture diagram

Flow

  1. User interacts with the mobile app and captures an image.
  2. The image is passed to the server application which uses Watson Visual Recognition Service to analyze the images and Nutritionix API to provide nutritional information.
  3. Data is returned to the mobile app for display.

Included components

  • Watson Visual Recognition: Visual Recognition understands the contents of images - visual concepts tag the image, find human faces, approximate age and gender, and find similar images in a collection.

Featured Technologies

  • Mobile: Systems of engagement are increasingly using mobile technology as the platform for delivery.
  • Nutritionix API: The largest verified database of nutrition information.
  • Node.js: An asynchronous event driven JavaScript runtime, designed to build scalable applications.

Watch the Video

Steps

NOTE: The Watson Visual Recognition service required for this patten only exists in the US-South/Dallas region (as of 11/30/18). You will only be able to deploy and/or use this code there.

This Code Pattern contains several pieces. The app server communicates with the Watson Visual Recognition service. The mobile application is built locally and run on the Android phone.

Deploy the Server Application to IBM Cloud

Deploy to IBM Cloud

  1. Press the above Deploy to IBM Cloud button and then click on Deploy.

  2. In Toolchains, click on Delivery Pipeline to watch while the app is deployed.

Toolchain pipeline

  1. To see the app and services created and configured for this Code Pattern, use the IBM Cloud dashboard. The app is named watson-calorie-counter with a unique suffix. The following services are created and easily identified by the wcc- prefix:
    • wcc-visual-recognition

Note: Make note of the watson-calorie-counter URL route - it will be required for later use in the mobile app.

To complete the installation, perform the following steps:

  1. Clone the repo

  2. Obtain a Nutritionix API ID and key

  3. Update config values for the Mobile App

  4. Perform either 4a or 4b.

    4a. Install dependencies to build the mobile application

    4b. Run mobile application build in Docker container

  5. Add Android platform and plug-ins

  6. Setup your Android device

  7. Build and run the mobile app

1. Clone the repo

Clone the watson-calorie-counter repo locally. In a terminal, run:

$ git clone https://github.com/IBM/watson-calorie-counter.git
$ cd watson-calorie-counter

2. Obtain a Nutritionix API ID and key

Nutritionix data is used to gather nutritional information of an analyzed image. Instructions for obtaining a key can be found at Nutritionix.com.

Note: Make note of the API ID and key - they will be required for later use in the mobile app.

3. Update config values for the Mobile App

Edit mobile/www/config.json and update the setting with the values retrieved previously.

"BLUEMIX_SERVER_URL": "<add-bluemix-server-url>",
"NUTRITIONIX_APP_ID": "<add-nutritionix-app-id>",
"NUTRITIONIX_APP_KEY": "<add-nutritionix-app-key>"

4a. Install dependencies to build the mobile application

Building the mobile application requires a few dependencies that you can either manually install yourself, or you can use Docker by skipping to Run mobile application build in Docker container

Using manually-installed dependencies

For this Code Pattern, you'll need to install the prerequisites, by following their respective documentation:

You'll need to install the specific SDK appropriate for your mobile device. From Android Studio, download and install the desired API Level for the SDK. We are using Android API Level 23 as this is widely supported on most phones as of January, 2018. To do this:

  • Launch Android Studio and accept all defaults.
  • Click on the SDK Manager icon in the toolbar.
  • Navigate to Appearance & Behavior -> System Settings -> Android SDK
  • Select Android API level of your choice (Recommended Android 6.0 (Marshmallow) (API Level 23) and above).
  • Click apply to download and install.

Note: the mobile/config.xml is configured to build for Android API Level 23. Adjust this if you wish to build for a different API:

<preference name="android-targetSdkVersion" value="23" />

Once you have completed all of the required installs and setup, you should have the following environment variables set appropriately for your platform:

  • JAVA_HOME
  • ANDROID_HOME
  • PATH

Note: For additonal help setting these environment variables, refer to the Troubleshooting section below.

4b. Run mobile application build in Docker container

If you are running Docker, build the mobile app in a Docker container.

Either download the image:

docker pull scottdangelo/cordova_build

Or build locally:

$ cd watson-calorie-counter/mobile
$ docker build -t cordova_build .
[truncated output]
Successfully built <image-id>

Now create the following alias for cordova and the commands for cordova will run inside the container. Use cordova_build in place of scottdangelo/cordova_build if you have built the container locally.

alias cordova='docker run -it --rm --privileged  -v $PWD:/mobile scottdangelo:cordova_build cordova'

Note: the mobile/config.xml is configured to build for Android API Level 23. Adjust this if you wish to build for a different API:

<preference name="android-targetSdkVersion" value="23" />

5. Add Android platform and plug-ins

Start by adding the Android platform as the target for your mobile app.

$ cd watson-calorie-counter/mobile
$ cordova platform add android

Ensure that everything has been installed correctly:

$ cordova requirements

Finally, install the plugins required by the application:

$ cordova plugin add cordova-plugin-camera
$ cordova plugin add cordova-plugin-file-transfer

6. Setup your Android device

In order to run the application on your Android device, you will need to be prepared to transfer the application's .apk file to your device (created in the next step). There are multiple ways for developers to achieve this.

Android Studio will handle the transfer for you if you tether your Android device to your computer, and enable both developer options and web debugging.

Note: Please refer to documentation on your specific phone to set these options.

For Mac users, Android File Transfer will facilitate simple file transfers between your computer and Android device.

7. Build and run the mobile app

$ cd watson-calorie-counter/mobile
$ cordova build android

An .apk file should appear at watson-calorie-counter/mobile/platforms/android/app/build/outputs/apk/debug/, which contains the Android application.

You can then either manually transfer the .apk to your device and run it yourself, or if your device is tethered (as described in the previous step), then you can run:

$ cordova run android

However, a Docker container does not have access to your host's USB devices, unless you explicitly allow them to be passed through. You can expose your device to the Docker container, and allow Cordova to do the transfer for you. To accomplish that, you'll need to know which USB device to pass through. Discover your USB devices on a Linux machine with lsusb.

For example, in this case, I know that my Android device is Bus 001, Device 002:

$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 046d:082d Logitech, Inc. HD Pro Webcam C920
Bus 001 Device 002: ID 18d1:4ee6 Google Inc.
Bus 001 Device 005: ID 046d:c085 Logitech, Inc.
Bus 001 Device 004: ID 045e:02e6 Microsoft Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

I can then pass my device through to the container using --device=/dev/bus/usb/<bus-number>/<device-number> and allow Cordova to access it. The new alias for the cordova command would then be:

alias cordova='docker run -it --rm --privileged  --device=/dev/bus/usb/001/002 -v $PWD:/mobile scottdangelo:cordova_build cordova'

Then we can run:

cordova run android

At this point, the app named Calorie Counter should be on your mobile device. Use the camera icon to take a photo of a food item, and allow Watson to analyze the image and fetch the calorie results.

Sample Output

Troubleshooting

  • cordova run android error: Failure [INSTALL_FAILED_UPDATE_INCOMPATIBLE]

The Calorie Counter app is already installed on your phone and incompatible with the version you are now trying to run. Uninstall the current version and try again.

  • cordova run android error: No target specified and no devices found, deploying to emulator

Ensure that your phone is plugged into your computer and you can access it from the Android File Transfer utility (see Step #6 above).

  • How to determine proper values for environment variables:

Open Android Studio and navigate to File -> Project Structure -> SDK Location. This location value will serve as the base for your environment variables. For example, if the location is /users/joe/Android/sdk, then:

$ export ANDROID_HOME=/users/joe/Android/sdk
$ export ANDROID_SDK_HOME=/users/joe/Android/sdk/platforms/android-<api-level>
$ export PATH=${PATH}:/users/joe/Android/sdk/platform-tools:/users/joe/Android/sdk/tools
  • Error: Server error, status code: 502, error code: 10001, message: Service broker error: {"description"=>"Only one free key is allowed per organization. Contact your organization owner to obtain the key."}

Only one free key is allowed per organization. Binding the service to an application triggers a process that tries to allocate a new key, which will get rejected. If you already have an instance of Visual Recognition and an associated key, you can bind that instance to your application or update the API key in your server code to tell the app which key to use.

  • When using Docker, first cordova command fails with:
-bash: cordova: command not found

Test that you have the proper alias with:

alias |grep cordova

If you do not see the alias cordova=... output from 4b, re-execute the alias command above.

Links

Learn more

  • Artificial Intelligence Code Patterns: Enjoyed this Code Pattern? Check out our other AI Code Patterns.
  • AI and Data Code Pattern Playlist: Bookmark our playlist with all of our Code Pattern videos
  • With Watson: Want to take your Watson app to the next level? Looking to utilize Watson Brand assets? Join the With Watson program to leverage exclusive brand, marketing, and tech resources to amplify and accelerate your Watson embedded commercial solution.

License

This code pattern is licensed under the Apache Software License, Version 2. Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.

Apache Software License (ASL) FAQ

watson-calorie-counter's People

Contributors

dolph avatar imgbot[bot] avatar kant avatar ljbennett62 avatar markstur avatar rhagarty avatar riyamroy04 avatar sanjeevghimire avatar scottdangelo avatar stevemar avatar stevemart avatar tqtran7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

watson-calorie-counter's Issues

Need more info on apiURL

What is the new field apiURL var used for? (in index.js)

Need to add this to the readme instructions. (what it's for and where to get the URL)

`cordova platform add android` fails

Command fails:

Scotts-MBP:mobile scottda$ cordova platform add android
-bash: cordova: command not found

Does Cordova need to be installed globally for this to work? That needs to be documented explicitly, and the link to Cordova should point to https://cordova.apache.org/docs/en/latest/guide/cli/index.html
where this is stated, instead of the platform page

create a video

as part of the journey process we also create a video as collateral

Error building cordova android version

rhagarty@Richs-MBP-15 mobile (master) $ cordova platform version android
Installed platforms:
android 6.2.3
Available platforms:
blackberry10 ~3.8.0 (deprecated)
browser ~4.1.0
ios ~4.4.0
osx ~4.0.1
webos ~3.7.0
rhagarty@Richs-MBP-15 mobile (master) $ cordova build android --verbose
No scripts found for hook "before_build".
No scripts found for hook "before_prepare".
Checking config.xml and package.json for saved platforms that haven't been added to the project
Config.xml and package.json platforms are the same. No pkg.json modification.
Package.json and config.xml platforms are different. Updating config.xml with most current list of platforms.
Using this version of Cordova with older version of cordova-android is deprecated. Upgrade to [email protected] or newer.
Error: Your android platform does not have Api.js

Shorten bluemix service URL

set random-route to false in manifest.yml to reduce the length of the generated URL. The appended timestamp will ensure the URL is unique.

create a blog

as part of the journey process we also need a blog that either a) introduces the journey, or b) explains a unique aspect of this journey, or c) a technical issue you overcame

ENV variables are not appropriately set

According to the README 4. Install dependencies to build the mobile application after installing the dependencies:

Once you have completed all of the required installs and setup, you should have the following environment variables set appropriately for your platform:

JAVA_HOME
ANDROID_HOME
PATH

However, these are not set. It appears that they must be set manually, and the instructions point to the Troubleshooting link.
But if these must be set, they are not "Troubleshooting", and the instructions should be in the steps, as they are absolutely required.
Also, the Troubleshooting instructions do not mention setting JAVA_HOME. This should also be documented.

Deploy to IBM Cloud fails

The error is not apparent, as the logs don't give any info.
I suspect that it is due to IAM apikey changes with the visual recognition service.

Aspect ratio of captured image is incorrect

The captured image appears to be stretched in the app, like it's being forced into a particular aspect ratio that does not correspond with the image's actual aspect ratio.

Watson sees non-food items

I tried giving Watson this image, which I took through the Calorie Counter app:

Grapes

This is a snippet from what the Calorie Counter app saw, which, in this case, includes the color at the bottom (not a food item) and not "grapes":

Non-food items

The Watson Visual Recognition demo shows a similar list of non-food items as the app on the left (which does not contain "grapes" either), but also shows an accurate list of food items on the right (which contains "grapes" with 100% confidence):

Visual recognition demo

Docker commands fail due to incorrect directory

Scotts-MBP:mobile scottda$ docker run --volume "/Users/scottda/GitRepos/Forkscottdangelo/watson-calorie-counter/mobile:/mobile" calorie-counter cordova platform add android
Error: Current working directory is not a Cordova-based project.

Error code 2

Everything Ok.

But Finally I met error code 2

Can you tell me what is this? Sir?

Incorrect android targets for Docker container

Cordova requirements fail:

Scotts-MBP:mobile scottda$ docker run --volume=/Users/scottda/GitRepos/Forkscottdangelo/watson-calorie-counter/mobile:/mobile calorie-counter cordova requirements

Requirements check results for android:
Java JDK: installed 1.8.0
Android SDK: installed true
Android target: not installed 
Please install Android target / API level: "android-26".

widget ID in mobile/config.xml

Can we come up with a better id value for this?

When I run cordova run android, I still see "Package name: com.example.hello" when it completes. Is this related?

README instructions wrong?

Got errors running these commands:
cordova plugin add org.apache.cordova.camera
cordova plugin add org.apache.cordova.file-transfer

But it worked with these commands:
cordova plugin add cordova-plugin-camera
cordova plugin add cordova-plugin-file-transfer

Cordova requirements check fails

Looks like I need to manually set some env variables. I think we should add this to the README instructions. Here is the error:

$ cordova requirements

Requirements check results for android:
Java JDK: installed 1.8.0
Android SDK: not installed
Failed to find 'ANDROID_HOME' environment variable. Try setting setting it manually.
Failed to find 'android' command in your 'PATH'. Try update your 'PATH' to include path to valid SDK directory.
Android target: not installed
android: Command failed with exit code ENOENT
Gradle: not installed
Could not find gradle wrapper within Android SDK. Could not find Android SDK directory.
Might need to install Android SDK or set up 'ANDROID_HOME' env variable.
Error: Some of requirements check failed

┌──────────────────────────────────────────┐
│ Update available: 7.0.1 (current: 7.0.0) │
│ Run npm install -g cordova to update. │
└──────────────────────────────────────────┘

Put app keys in .env file

We shouldn't have the user update source to set the key vales. Instead, create an env.sample file with something like:
NUTRITIONIX_APP_ID=<add_app_id>
NUTRITIONIX_APP_KEY=<add_app_key>

Then add to instructions that user must:
cp env.sample .env
and update the values
(see https://github.com/IBM/watson-multimedia-analyzer for example of this step)

Then have mobile/www/js/index.js read from the .env file to get these values.
Also, add .env to .gitignore

README architecture diagram and flow section

Remove step 1 from the "Flow" steps. The flow should represent application flow, not installation steps.

It should be something like:

  1. User interacts with the mobile app and captures an image.
  2. The image is passed to the server application which uses Watson Visual Recognition Service to analyze the images and Nutritionix API to provide nutritional information.
  3. Data is returned to the mobile app for display.

The architecture diagram should have 3 sections - User, Cloud, and External.

User - User and mobile app
Cloud - Server application and Watson VR
External - Nutritionix

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.