Fruits' Classification With Orangelib:

Ayoola Olafenwa
6 min readJan 5, 2020

--

Orangelib is a library built for the purpose of achieving easy implementation of computer vision in real problems.

In the articleClassification of Oranges With Orangelib” , we discussed how to classify oranges with Orangelib.

Orangelib-- 0.4.0: this is the latest version of Orangelib. It had been upgraded to perform prediction on more classes of fruits:Apples and Bananas. Multiple predictions on images had also been integrated.

With Orangelib, you can easily classify the following classes of fruits using few lines of code:

  • Oranges
  • Bananas
  • Apples

Install Orangelib with:

  • pip install orangelib

If installed, upgrade to the latest version with:

  • pip install orangelib --upgrade

Implementing OrangeClassifier With Orangelib:

Orangeclassifier is used to classify ripe and unripe oranges.

If you have read the first article on classifying oranges with Orangelib, you can skip the part of implementing prediction on a single image with OrangeClassifier.

Implementing Prediction On A Single Image With OrangeClassifier:

Sample1:

sample1.jpg

We intend to classify this image above.

Classification of a single image into either ripe or unripe orange can be implemented with the code below.

from orangelib.model import OrangeClassifier

We import in the class Orangeclassifier from Orangelib to classify oranges.

classifier = OrangeClassifier(“orange_model.h5”)

The trained model to perform prediction on the image is loaded. The model can be downloaded as a release from github.

fruit_name, confidence = classifier.predict(“sample1.jpg”)

The path to the image to be predicted is loaded.

print(“Fruit Name: “, fruit_name)print(“Prediction Confidence: “, prediction_confidence)

Output1:

Fruit Name:  ripe orange
Prediction Confidence: 99.45345

The fruit name and the level of prediction confidence is printed.

It is able to detect that the orange is ripe with a high prediction confidence of over 99%.

Sample2:

fruit_name, confidence = classifier.predict(“sample2.jpg”) 

Output2:

Fruit Name:  unripe orange
Prediction Confidence: 99.92031

It successfully predicted the image as an unripe orange with a prediction confidence of over 99%.

Implementing Multiple predictions on images with OrangeClassifier:

We may not need to stress ourselves predicting a single image at a time especially when we need to predict many images. Imagine we want to predict about three images at once.

Sample3:

We make use of this code below for multiple predictions.

We import in the class OrangeClassifier and load the trained_model on oranges.

The major differences are:

fruit_names_list, confidence_list = classifier.predictBatch(["sample1.jpg","sample2.jpg","sample3.jpg"])

We perform predictions on an array of images using the predictBatch function.

  • There is no limit to the number of images that can be predicted with the predictBatch function.
for fruit_names, confidence in zip(fruit_names_list,confidence_list):  print("Fruit Name: ",fruit_name)  print("Prediction Confidence: ", confidence)

We loop through the array of results of the predictions on the images, print out the name and prediction confidence for each of the images.

Outputs for the images:

Fruit Name:  ripe orange
Prediction Confidence: 99.45345
Fruit Name: unripe orange
Prediction Confidence: 99.92031
Fruit Name: ripe orange
Prediction Confidence: 99.99149

It achieved accurate predictions on the three images. It was able to produce the same results we got for sample1 and sample2 when predicted individually.

Implementing Prediction On A Single Image With BananaClassifier:

The class BananaClassifier is used to classify ripe and unripe bananas.

Sample4:

We intend to predict the kind of fruit in this image above.

from orangelib.model import BananaClassifier

The class BananaClassifier is imported from Orangelib.

classifier = BananaClassifier(“banana_model.h5”)

We make use of the trained model on Bananas. The model for predicting bananas can be downloaded as a release from github.

Output4:

Fruit Name:  ripe banana 
Prediction Confidence: 99.99490

It successfully predicted the fruit as a ripe banana with a prediction confidence of over 99%.

Sample5:

fruit_name, confidence = classifier.predict(“sample5.jpg”)

Output5:

Fruit Name:  unripe banana 
Prediction Confidence: 99.99994

It successfully predicted the image as an unripe banana with a prediction confidence of over 99%.

Implementing Multiple predictions on images with BananaClassifier:

Sample6:

We import in the class BananaClassifier and load the trained_model on bananas.

fruit_names_list, confidence_list = classifier.predictBatch(["sample4.jpg","sample5.jpg","sample6.jpg"])

We perform predictions on an array of images using the predictBatch function.

 
for fruit_names, confidence in zip(fruit_names_list,confidence_list):
print("Fruit Name: ",fruit_names)
print("Prediction Confidence: ", confidence)

We loop through the array of results of the predictions on the images, print out the name and prediction confidence for each of the images.

Outputs for the images:

Fruit Name:  ripe banana 
Prediction Confidence: 99.99490


Fruit Name: unripe banana
Prediction Confidence: 99.99994

Fruit Name: ripe banana
Prediction Confidence: 99.99983

It achieved accurate predictions on the three images. It was able to produce the same results we got for sample5 and sample6 when predicted individually.

Good results! just like we implemented with OrangeClassifier.

Implementing Prediction On A Single Image With AppleClassifier:

The class AppleClassifier is used to classify green and red apples.

Sample7:

from orangelib.model import AppleClassifier

The class AppleClassifier is imported from orangelib.

classifier = AppleClassifier(“apple_model.h5”)

We make use of the trained model on Apples. The model for predicting apples can be downloaded as a release from github.

Output7:

Fruit Name:  green apple 
Prediction Confidence: 99.94303

It successfully predicted sample7 as a green apple with over 99% prediction confidence.

Sample8:

fruit_name, confidence = classifier.predict(“sample8.jpg”)

Output8:

Fruit Name:  red apple  Prediction Confidence:  100.0

Wow! Successfully detected sample8 as a red apple with 100% accuracy.

Implementing multiple predictions on images with AppleClassifier:

Sample9:

We import in the class AppleClassifier and load the trained_model on apples.

fruit_names_list, confidence_list = classifier.predictBatch(["sample7.jpg","sample8.jpg","sample9.jpg"])

We perform predictions on an array of images using the predictBatch function.

 
for fruit_names, confidence in zip(fruit_names_list,confidence_list):
print("Fruit Name: ",fruit_name)
print("Prediction Confidence: ", confidence)

We loop through the array of results of the predictions on the images, print out the name and prediction confidence for each of the images.

Outputs of predictions:

Fruit Name:  green apple 
Prediction Confidence: 99.94303

Fruit Name: red apple
Prediction Confidence: 100.0
Fruit Name: green apple
Prediction Confidence: 99.21522

It achieved accurate predictions on the three images. It was able to produce the same results we got for sample7 and sample8 when predicted individually.

Excellent 👍 Results! with all the fruit classifiers available in Orangelib.

Install Orangelib and test it with as many images of oranges, bananas and apples as you desire.

--

--

Ayoola Olafenwa
Ayoola Olafenwa

No responses yet