How to prevent spoiled tomatoes appear on the BC stock…with Custom Vision

Well, this is not going to be the story from one of the projects. In this post I will try to show you the power (and simplicity) of #AI, solving one particular business case.

In this demo, no tomato was hurt, destroyed or whatever 🙂

Business problem

Assume that you purchase tomatoes, and when you receive them on stock you want automatically check their quality. If some tomatoes were spoiled, then you want to return them to you vendor.

General quality management task. Isn’t it?

Old fashioned solution 

Well, in previous days you could add some option like: good quality, bad quality and storekeeper had to mark it. Then you just added some code to prevent posting items with bad quality on stock and job done.

Quick, but not effective. One more field to fill manually. 

Modern solution

Hopefully we live in a cool time, where #AI is born and transform every task in a magical way. So, why not to use Artificial Intelligence here?

The idea is to

  • take a photo of tomatoes
  • analyse them using “some” AI service
  • get the result (if tomato is spoiled or not)
  • use that result in Business Central 

No human activities, no errors, no time losses, no money losses!

Sounds good, how to achieve that?

Any standard AI solution?

Probably you know, that Microsoft has Image Analyzer build in Business Central. 


This is cool extension/service, but it will not solve our task. It works on computer vision engine, which can give us some basic tags: item category, colure, item description – everything that Microsoft decided to teach it. But, it will not give us tag: good tomato or bad tomato. 

Any custom AI solution?

Yes. Very quickly (less than 5 min) we can build custom AI solution, and use it from anywhere. 

The service is called Custom Vision.


So let’s do that!

The AI process

Very simple concept. Computer don’t know how spoiled tomatoes look like, the same for good tomatoes. So we need to train computer, like we train children – showing him pictures and describing what’s is illustrated on them.


We will

  • Find the photos of tomatoes
  • Classify them
  • Train model
  • Get the web service url
  • Consume from Business Central

The Data

Like any other AI service, we need data to train our custom AI model.

Typical question, where to search it and how many data do we need?

  1. Google is your friend
  2. Not much to start, much to use it in a real world

For our demo project i found 24 images of good and 14 images of bad tomatoes. All them should be with the same background. If we had millions of photos, then background could differ. If we have <100 images, background should be the same.


When you save your image files for future classification, save them in different folders, according to your tags. That will simplify tagging process in the future. Also save some images to the test folder – to test model.


Custom vision

Just go to Register using your Microsoft or Work account, and you are ready to build your custom ai model.

Create a project and fill some parameters.


In our case 

  • Project Type: Classification
  • Classification Type: Multiclass 
  • Domains: Food (you can choose General, but Food will increase model accuracy, as it was already pre-trained to recognise food)

And click Create project.

Tag images

In the custom vision project, click on Add images, choose your first folder (with spoiled tomatoes), select all images (that’s why we created separate folders) and upload them.

Then create new tag: spoiled tomato and finish the process.


Do the same with the second folder.

Now, you have tagged images in your project. 


But you did not trained your model yet. You just gathered photos together and signed them. Now you should teach your child.

Train model

Just click on a green Train button, and in 5 seconds you will have your custom AI trained model!


How cool is that! 

Test your model

Remember, I told you to save some photos in a test folder? Good, if you followed my advice!

Click on a Quick Test button, upload one of your images, and check the result.


You will see how model is performing. 


Get Web service parameters

Click on Prediction URL and you will get all needed info to consume this AI model


That’s it! Now you have your custom model published and ready for connections. 

Consume Custom Vision from Business Central

I mentioned earlier, that Business Central has out-of-the-box Image Analyzer extension.

According to the documentation it supports computer vision service, but not custom vision. I investigated api of both services, and found that they are very similar. So, I decided to check if my custom vision can be called using Image Analyzer extension.

Image analyzer to call Custom Vision

Let’s try to configure and Analyze image, using standard image analyzer. 


Create new Item, upload picture of tomato, and we will get


So it works! Even from out-of-the-box. 

Custom AL solution to call Custom Vision

I think, you wonder, why do we need to build custom solution, if you have working base extension. You can have reasons for that.

And the first reason is that Image Analyzer is written in C/Side. Yes! You heard that well. C/Side! Microsoft cheated here a bit – all the UI is build in AL, and all the business logic, including streaming picture to the service and analysing tags – in C/AL (using .Net). 

The second reason, it does not work in some localisations. For example, I found that it works with CA localisation, and don’t work with US. I’ve reported about that.

So, I decided to create new AL extension from scratch.

I will not describe it in detail. The source code can be found in my GitHub repo 

It stream any picture from url or media (or blob) to custom vision service, get tags and save them.


Let’s test


Then you can do with your tags anything, even prevent spoiled Tomatoes to appear on the BC stock!


Publish and Post Purchase order with spoiled tomato inside. 


Now your stock will be always fresh =)

Share Post:

Leave a Reply

About Me


A Microsoft MVP, Business Central architect and a project manager, blogger and a speaker, husband and a twice a father. With more than 15 years in business, I went from developer to company owner. Having a great team, still love to put my hands on code and create AI powered Business Central Apps that just works.

Follow Me

Recent Posts