In this tutorial, we will unleash the power of machine learning and Delphi’s ecosystem to create an ingenious food classification app. Experience the future of food identification, empowering users to make informed choices with real-time analysis, all at their fingertips. Let’s dive into the exciting journey of building an ML-powered food classification app using Delphi.
Table of Contents
What ML Models Are Used For Classification Tasks?
Classification tasks in machine learning utilize various models that have proven effective in producing accurate results. These models include logistic regression for binary classification, decision trees for hierarchical partitioning, random forests for ensemble learning, and support vector machines for optimal hyperplane separation.
In recent years, neural networks, particularly deep learning models, have gained immense popularity due to their ability to learn complex patterns and hierarchies in data. Neural networks, especially transformers, have gained prominence, excelling in NLP and image classification tasks. For instance, the Swin and Vision Transformer (ViT) leverages the power of transformers to process image patches, enabling effective classification based on visual information. Transformers capture complex dependencies and have revolutionized NLP with self-attention and multi-head attention mechanisms. Their adaptability to diverse data domains makes them a powerful choice for accurate classification in machine-learning applications.
Which Tools Can Be Used For Creating GUIs for ML-based Apps?
Delphi CE (Community Edition), Delphi4PythonExporter, and Delphi FMX (FireMonkey) are excellent choices for creating GUIs specifically tailored for ML-based apps. Delphi CE provides a comprehensive, integrated development environment (IDE) that enables rapid application development with various components and controls for creating visually appealing interfaces. It allows seamless integration with ML backends implemented in Python using frameworks like TensorFlow and PyTorch.
Delphi FMX, a cross-platform GUI framework, empowers developers to create visually stunning and responsive interfaces for ML-based apps. With Delphi FMX, developers can design seamless GUIs on various platforms, including Windows, macOS, iOS, and Android. The framework supports integrating ML models developed with Python frameworks, such as TensorFlow, PyTorch, or Hugging Face, into the Delphi FMX-based interfaces.
How to Create a Food Classification Desktop App?
Creating a food classification desktop app involves several steps. Before we get into the detailed tutorial, let’s look at the requirements for this project.
What Are the Prerequisites For this Article?
Install Delphi CE and the FireMonkey GUI framework on your device to create the food classification app. You will need a text editor or IDE that supports Python, such as PyScripter and the Delphi4PythonExporter tool. If you don’t have these tools installed, we recommend checking out the article “Powerful Python GUI Project Setup” to help you get started.
You should also install transformers and pillow Python libraries by running the following commands in the cmd:
1 2 3 |
pip install pillow pip install transformers |
You can grab the code for this tutorial from our GitHub repository using the following link:
https://github.com/Embarcadero/PythonBlogExamples/tree/main/Food_Image_Classifier
How to Download Pre-trained Models From HuggingFace?
You can follow a few simple steps to download pre-trained models from Hugging Face. Start by visiting the Hugging Face Model Hub website at https://huggingface.co/models. Once there, you can search for the specific pre-trained model you need using relevant keywords or for specific model names. For instance, if you are looking for a pre-trained model for food classification, you can search for “food classification” or specific model names associated with food classification.
You can Import the necessary modules in your Python script by replacing <model>
with the specific model you want to use:
1 2 |
from transformers import <model> |
Then load the pre-trained model using the from_pretrained()
method while replacing <model>
with the model class you imported and <model_name>
with the name or path of the pre-trained model:
1 2 |
model = <model>.from_pretrained('<model_name>') |
This will download the pre-trained weights if not already cached on your system. Now, you can use the loaded model for your tasks by calling its appropriate methods or attributes. For instance, you will have to get its feature extractor to extract features from unseen data before providing it to the model. You can refer to the documentation of the specific model class for more details on how to use it.
For this tutorial, we will use a food classification model that classifies images into 7 categories (apple pie, falafel, french toast, ice cream, ramen, sushi, and tiramisu). This pre-trained model is finetuned on the Swin transformer model:
https://huggingface.co/Kaludi/Food-Classification
You can grab the code for this tutorial from our GitHub repository using the following link:
https://github.com/Embarcadero/PythonBlogExamples/tree/main/Food_Image_Classifier
What Kind of a GUI Can We Create For Such an App?
Using the Delphi ecosystem, we can create a very attractive GUI for such an ML task.
How to Create Stylish Delphi Forms?
We will start by opening the Delphi CE application and creating a blank project. To do this, navigate to File > New > Multi-Device Application > Blank Application > Ok
. In this tutorial, we have named our project MLFoodClassifier
.
If you are not familiar with the various sections of the Delphi IDE, we recommend referring to our free eBook bundle, which covers the Delphi Python EcoSystem and all Python GUI offerings.
To start, let’s give our form a name. Right-click on the form and select QuickEdit. Here, we will name our form MainForm
with the caption Food Classification App
.
We will also rename the source file of our MainForm
to Main.pas
to make it easier to keep track of. Next, we will resize the form. So go to the Object Inspector and search for ClientWidth
and ClientHeight
in the properties tab. We will set them to 360
and 500
, respectively.
Next, let’s start by adding a few labels to our app. To do this, navigate to the standard palette and locate the TLabel
component. Next, drag and drop this component onto the form.
Next, we’ll modify the text style by using the TextSettings
property of each label. To do this, navigate to the Object Inspector
and search for TextSettings
. Click on the ellipses button (...
) next to the Font
property to see options for modifying the label based on your preferences. Here we have set our font family as SemiBold Segoe UI with different font sizes for each label. In addition, we will set the HorzAlign
and VertAlign
properties for our labels as Center
.
Here is what our form looks like:
As you can see, we have added 4 TLabel
components, Title
, FileName
, FileNameLabel
, and ResultLabel
. The FileName
label will be used to display the file name of the image to be classified, whereas the ResultLabel
will help us display the output of our classifier.
Next, let’s add a few buttons to our form. For this, we will use the TButton
component, so navigate to the Palette and search for the component.
We will add two buttons, FileSelectButton
, to help us select the file to classify, and a ClassifyButton
, to help us evaluate our image.
To complete our form, we will add a TOpenDialog
component, which will help us open up a dialog box to select a file from our system. So open up the Palette and search for the component. Next, drag and drop this component into your form.
Finally, let’s add some style to our form. For this, you will need to download the free ebook bundle which comes shipped with various styles. Download your free ebook and extract the FMX styles zip to your chosen directory. Next, select the style you want to use. For this we will be using GoldenGraphite.style
available in the bundle. You can find more DelphiFMX styles online, such as at delphistyles.com.
Now head to the Palette and add the TStyleBook
component to your form.
Next, double-click on the StyleBook1
component. This will open up your Style Designer. Now, click on the open button. This will open up a dialog box.
Navigate to the .style
file of your choosing in the dialog box, select your file, and finally click on exit. Now select the form and head over to the Object Inspector. Here we will search for the StyleBook property. Select the drop-down menu and select StyleBook1.
Here is what our MainForm
looks like:
Now that the form is complete, the final step will be to add some functionality to each button in the form. To do this, double-click each button. This will add an OnClick
method to that button. The procedure will be created in the .pas
file corresponding to the .fmx
form. This allows you to add unique functionality to each button when clicked.
Because the Delphi4PythonExporter does not use run-time implementation, we will need to add at least one comment (//
) to each procedure to preserve the function when we export the form.
How to Export These Forms into Python Scripts?
Now that our form is ready, we can export it using the Delphi4Python exporter. Save our project by selecting Tools > Export To Python > Export Current Entire Project
.
Here we will give the project the title MLFoodClassifier
. Next, select the directory of your choosing and click on Export:
Delphi will generate two Python files and one .pyfmx
file. The MLFoodClassifier.py
file will be used to run the application, whereas the Main.py
file contains the class for the MainForm
. The .pyfmx
file contains the visual information for the MainForm
. Below are the contents for each Python file.
Main.py
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import os from delphifmx import * class MainForm(Form): def __init__(self, owner): self.Title = None self.FileNameLabel = None self.ResultLabel = None self.FileName = None self.FileSelectButton = None self.ClassifyButton = None self.OpenDialog1 = None self.StyleBook1 = None self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Main.pyfmx")) def FileSelectButtonClick(self, Sender): pass def ClassifyButtonClick(self, Sender): pass |
MLFoodClassifier.py
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from delphifmx import * from Main import MainForm def main(): Application.Initialize() Application.Title = 'MLFoodClassifier' Application.MainForm = MainForm(Application) Application.MainForm.Show() Application.Run() Application.MainForm.Destroy() if __name__ == '__main__': main() |
How to Develop This Project Into a Fully Functional App?
Now that we are done with the form, all we need to do is add functionality to our form. Let’s start by opening up Main.py
in the text editor of your choice. We will start by importing some libraries:
1 2 3 4 |
# Additional imports from PIL import Image from transformers import AutoModelForImageClassification, AutoImageProcessor |
Next, head over to the __init__
function and initialize your transformer models. Here we will be using the Kaludi food classification model available on HuggingFace. Additionally, we will also set the FileName
label as empty:
1 2 3 4 5 6 |
self.FileName.Text = "" # Loading pre-trained transformer model and feature extractor during app initialization self.extractor = AutoImageProcessor.from_pretrained("Kaludi/Food-Classification") self.model = AutoModelForImageClassification.from_pretrained("Kaludi/Food-Classification") |
Here is what our final __init__
function looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def __init__(self, owner): self.Title = None self.FileNameLabel = None self.ResultLabel = None self.FileName = None self.FileSelectButton = None self.ClassifyButton = None self.OpenDialog1 = None self.StyleBook1 = None self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Main.pyfmx")) self.FileName.Text = "" # Loading pre-trained transformer model and feature extractor during app initialization self.extractor = AutoImageProcessor.from_pretrained("Kaludi/Food-Classification") self.model = AutoModelForImageClassification.from_pretrained("Kaludi/Food-Classification") |
Now let’s code our FileSelectButton
. Head over to the FileSelectButtonClick
method, and start by adding a title to the dialog box:
1 2 3 |
# Filepicker window title self.OpenDialog1.Title = "Select an Image" |
Next, we will filter the files only to show image files in different formats:
1 2 3 |
# Filtering only image formats to show self.OpenDialog1.Filter = "Image files (*.jpg;*.jpeg;*.png;*.gif;*.bmp, *.webp)|*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.webp|All files (*.*)|*.*" |
Finally, if the user presses the open button, we will save the directory of the file in Filename. Here is our FileSelectButtonClick
function code:
1 2 3 4 5 6 7 8 9 10 |
def FileSelectButtonClick(self, Sender): # Filepicker window title self.OpenDialog1.Title = "Select an Image" # Filtering only image formats to show self.OpenDialog1.Filter = "Image files (*.jpg;*.jpeg;*.png;*.gif;*.bmp, *.webp)|*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.webp|All files (*.*)|*.*" if self.OpenDialog1.Execute(): self.FileName.Text = os.path.basename(self.OpenDialog1.FileName) # Display selected image |
Finally, to evaluate the image, we will need to complete the ClassifyButton
. Head over to the ClassifyButtonClick
method and start by opening the image file:
1 2 3 4 |
def ClassifyButtonClick(self, Sender): # Load image from local file image = Image.open(self.OpenDialog1.FileName) |
Next, preprocess the image using the image processor:
1 2 3 |
# Preprocess image inputs = self.extractor(images=image, return_tensors="pt") |
Finally, make the prediction using the model. Here is what our final ClassifyButtonClick
method looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
def ClassifyButtonClick(self, Sender): # Load image from local file image = Image.open(self.OpenDialog1.FileName) # Preprocess image inputs = self.extractor(images=image, return_tensors="pt") # Make prediction outputs = self.model(**inputs) predicted_class_idx = outputs.logits.argmax(-1).item() predicted_class = self.model.config.id2label[predicted_class_idx] self.ResultLabel.Text = "Result: " + predicted_class # Display prediction |
Here is what our final Main.py
file looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import os from delphifmx import * # Additional imports from PIL import Image from transformers import AutoModelForImageClassification, AutoImageProcessor class MainForm(Form): def __init__(self, owner): self.Title = None self.FileNameLabel = None self.ResultLabel = None self.FileName = None self.FileSelectButton = None self.ClassifyButton = None self.OpenDialog1 = None self.StyleBook1 = None self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Main.pyfmx")) self.FileName.Text = "" # Loading pre-trained transformer model and feature extractor during app initialization self.extractor = AutoImageProcessor.from_pretrained("Kaludi/Food-Classification") self.model = AutoModelForImageClassification.from_pretrained("Kaludi/Food-Classification") def FileSelectButtonClick(self, Sender): # Filepicker window title self.OpenDialog1.Title = "Select an Image" # Filtering only image formats to show self.OpenDialog1.Filter = "Image files (*.jpg;*.jpeg;*.png;*.gif;*.bmp, *.webp)|*.jpg;*.jpeg;*.png;*.gif;*.bmp;*.webp|All files (*.*)|*.*" if self.OpenDialog1.Execute(): self.FileName.Text = os.path.basename(self.OpenDialog1.FileName) # Display selected image def ClassifyButtonClick(self, Sender): # Load image from local file image = Image.open(self.OpenDialog1.FileName) # Preprocess image inputs = self.extractor(images=image, return_tensors="pt") # Make prediction outputs = self.model(**inputs) predicted_class_idx = outputs.logits.argmax(-1).item() predicted_class = self.model.config.id2label[predicted_class_idx] self.ResultLabel.Text = "Result: " + predicted_class # Display prediction |
Now that our code is ready, it’s time to test our app. Head over to the MLFoodClassifier.py
and run the file.
Next, press the Select Image button. This will open up a dialog box for your to select an image. Navigate to the directory of your image and open your image. Here we will be using a sample image from wikipedia:
Once you’ve selected your image your file name should appear on the FileName
label:
Now press the Classify button to evaluate your image. Your result should appear in the ResultLabel
:
Are You Ready to Create Your Own Image Classification Apps?
Creating your image classification apps can be exciting and rewarding. With the right tools and techniques, you can develop powerful applications to classify and identify objects in images accurately. We highly recommend exploring the Delphi ecosystem to streamline your development process and unlock even more possibilities. Delphi CE, Delphi4PythonExporter, and Delphi FMX provide a comprehensive suite of tools and frameworks for GUI development and seamless integration with Python ML backends.
What Are the FAQs Related to This Topic?
Can I use pre-trained models for food classification, or is self-training required?
Yes, you can leverage pre-trained models for food classification. Pretrained models trained on large-scale datasets can provide excellent performance. However, fine-tuning or training your models on a specific food dataset can yield better results.
What are the challenges in food classification?
Food classification faces challenges such as variations in food appearance, lighting conditions, occlusions, and complex backgrounds. It requires handling large amounts of data and ensuring the model’s generalization across different food categories.
How do I improve the accuracy of my food classification model?
You can try data augmentation, transfer learning, fine-tuning, model ensemble, and experimenting with different architectures or hyperparameters to improve accuracy.
What is Delphi?
Delphi is an integrated development environment (IDE) that allows developers to build cross-platform applications using the Object Pascal programming language. It provides powerful tools and libraries for creating visually appealing and feature-rich applications.
Is Delphi suitable for beginners?
Yes, Delphi is beginner-friendly. It offers a visually-driven development environment with a drag-and-drop interface, making it easy for beginners to create GUI applications without extensive coding knowledge. Delphi also provides comprehensive documentation and a supportive community.