Food Recognition Selenium using Calorie Mama API


An open-source program used to automate web browsers is called Selenium Webdriver. It offers a platform-independent testing framework for web applications on many platforms and browsers. With the use of deep learning and computer vision algorithms, the food identification API Caloriemama can recognise different foods and their nutritional values from a single photograph.

In this guide, we'll look at how the Selenium Webdriver automates the process of uploading photographs and retrieving the results, making it simple for developers to include food recognition functionality into their apps and provide consumers with correct nutritional information.

Setup

Firefox Executable

  • Download the Firefox browser installer from here

  • Once downloaded, install the browser and an exe file will be placed automatically in C:\Program Files\Mozilla Firefox\firefox.exe. We will be needing it later.

Gecko Driver

  • Windows Users can download the gecko driver from here. For other versions see releases.

  • Extract the zip and place the “geckodriver.exe” file in C:\ directory. We will be referencing it later in our code.

Selenium Python Package

We are going to be working with the latest version of Selenium Webdriver so pip install the following −

pip3 install -U selenium
pip3 install -U webdriver-manager

Image

For our use case we will be using this image as “pasta.jpg”. Save this image to your C:\ drive. Of course, you can replace this food item with any other item of your choice.

Algorithm

  • Set options for Firefox browser using Selenium WebDriver.

  • Launch Firefox browser using Selenium WebDriver and open the Caloriemama API website.

  • Find the file upload button element on the website using Selenium WebDriver.

  • Auto-upload an image file using Selenium WebDriver.

  • Find the recognized dish result element and print it in the console using Selenium WebDriver.

Example

Paste this code block in a Python file and save the .py file in C:\ drive as well where you saved the image.

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
options = Options()

# download and install firefox and it will be in the following path
options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'

# download geckodriver.exe and put it in the same folder as this script
driver = webdriver.Firefox(executable_path=r'C:\geckodriver.exe', options=options)

# launch driver using the selenium webdriver and open the website
driver.get('https://www.caloriemama.ai/api')

# find the upload button element
upload = driver.find_element(By.CLASS_NAME, 'file-upload')

# auto upload the file
upload.send_keys(r"C:\pasta.jpg")
time.sleep(5)

# find the result element
result = driver.find_element(By.CLASS_NAME, 'group-name')

# print it in the console
print(f"Your dish is: {result.text}")

Output

1. Browser will automatically launch

2. The image will automatically be uploaded and submitted for recognition

3. Result will be outputted in the terminal

Your Dish is: Pasta
  • The Caloriemama API, a food recognition API that recognizes various foods and provides their corresponding nutritional information, is automated by this code using the Selenium WebDriver library.

  • The code begins by importing time, selenium, and selenium.webdriver, which are required libraries

  • The By class from selenium.webdriver.common.by is then imported, and it is used to locate elements on web pages

  • The Options class, which is used to set the Firefox browser options, is then created as a new instance.

  • The path where the Firefox binary is stored on the system is set as the binary location option.The webdriver instance is created by referencing it

  • A new Firefox driver instance is created using the Firefox() method. It accepts two arguments: the location of the executable for geckodriver and the options

  • The driver object's get() function is then used to access the Caloriemama API website.

  • The find_element() function of the driver object is then used by the code to locate the "Upload" button on the web page.

  • The element should be found using its class name, according to the By.CLASS NAME parameter. "file-upload" is the class name for the upload button element

  • The send_keys() function is invoked on the upload object to upload a picture of a food item after the upload button has been discovered.

  • The picture file's path is provided as a parameter to send_keys().

  • The application is then put on hold for 5 seconds using the time.sleep() function so that the image may be submitted and processed by the Caloriemama API.

  • The "Result" element on the web page is then located using the find element() function. Again, it is specified that the element should be identified by its class name using the By.CLASS NAME option.

  • The result element has the class "group-name" as its name. The code ends by formatting the text as "Your dish is: [result text]" and printing the text of the result element to the console using the print() method. The name of the food item detected by the Caloriemama API appears in the result text.

Conclusion

This article demonstrated how to automate the steps involved in uploading a photo of a food item to the Caloriemama API and getting the food item's nutritional data using Selenium WebDriver. Developers may build reliable and accurate food identification applications that can help people monitor their diets and make educated nutritional decisions by combining Selenium's automation capabilities with Caloriemama's AI-powered food recognition technology. You may incorporate this technology in your own projects and improve your apps with sophisticated food identification capabilities by following the technical instructions in this article.

Updated on: 18-Apr-2023

269 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements