CodeDelphiDelphiFMXLearn PythonProjectsPythonPython GUI

How To Create An ML-Powered Food Classification App Using The Python Delphi Ecosystem

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem

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. 

What ML Models Are Used For Classification Tasks?

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem a person editing some Python code

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?

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem The Delphi ecosystem

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?

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem the RAD Studio IDE

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:

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:

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:

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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem new multi device application

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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem free e book

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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem naming the form

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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem selecting TLabel from the palette

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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem setting the horizontal alignment

Here is what our form looks like:

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem form 1

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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem adding a button

We will add two buttons, FileSelectButton, to help us select the file to classify, and a ClassifyButton, to help us evaluate our image.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem form

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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem TOpenDialog
How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem 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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem stylebook
How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem 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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem the style designer

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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem the stylebook

Here is what our MainForm looks like:

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem the main form

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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem adding code

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 Create An ML Powered Food Classification App Using The Python Delphi Ecosystem select button code

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.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem exporting the project to Python

Here we will give the project the title MLFoodClassifier. Next, select the directory of your choosing and click on Export:

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem project exporting

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:

MLFoodClassifier.py:

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:

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:

Here is what our final __init__ function looks like:

Now let’s code our FileSelectButton. Head over to the FileSelectButtonClick method, and start by adding a title to the dialog box:

Next, we will filter the files only to show image files in different formats:

Finally, if the user presses the open button, we will save the directory of the file in Filename. Here is our FileSelectButtonClick function code:

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:

Next, preprocess the image using the image processor:

Finally, make the prediction using the model. Here is what our final ClassifyButtonClick method looks like:

Here is what our final Main.py file looks like:

Now that our code is ready, it’s time to test our app. Head over to the MLFoodClassifier.py and run the file.

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem form

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:

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem a strawberry ice cream image
Source httpsenwikipediaorgwikiStrawberry ice cream

Once you’ve selected your image your file name should appear on the FileName label:

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem form

Now press the Classify button to evaluate your image. Your result should appear in the ResultLabel:

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem form

Are You Ready to Create Your Own Image Classification Apps?

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem the TWebBrowser

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.

So, are you ready to dive into the world of image classification apps? Get started with Delphi and transform your ML-based app development journey now!

What Are the FAQs Related to This Topic?

How To Create An ML Powered Food Classification App Using The Python Delphi Ecosystem The FAQ Web page

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.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And OpenCV Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Radial Basis Function Networks (RBFNs)

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

Leave a Reply

Your email address will not be published. Required fields are marked *