Fruits' Classification With Orangelib
Tags: python, machinelearning, computervision, ai
Published: 2020-01-22 06:24:58


*Classification problem* is one of the major challenges in machine learning. *Computer vision* is an important aspect of classification problem in which objects recognized by a computer can be classified into specific categories. 
Implementing computer vision can be challenging, such as training a model to perform accurate prediction on an object. Training a model to accurately recognize a specific object can be tasking and it becomes more tasking to implement a computer vision model that will be able to classify the difference between one object and another.



## Orangelib: ##
*Orangelib* is a simple library created to facilitate easy implementation of computer vision in real problems. It is a library for classifying three categories of fruits:

-*Ripe and Unripe Oranges*
-*Ripe and Unripe Bananas*
-*Green and Red Apples*

We can easily classify these three categories of fruits using few lines of code.

 
## Install Orangelib with: ##
**pip install orangelib**

## Classification of Oranges with Orangelib ##

Orangelib library can easily classify ripe and unripe oranges using few lines of code.

## Code for Single Image prediction##

```python
from orangelib.model import OrangeClassifier

classifier = OrangeClassifier("orange_model.h5")
fruit_name, prediction_confidence = classifier.predict("path_to_image")
print("Fruit Name: ", fruit_name)
print("Prediction Confidence: ", prediction_confidence)

```

Let's look into each line of code:
```python
from orangelib.model import OrangeClassifier
```
The class *Orangeclassifier* is imported from orangelib to classify ripe and unripe oranges. 

```python
classifier = OrangeClassifier("orange_model.h5")
```
We load the trained model for performing predictions on images. The trained model can be downloaded as a release from [github](https://github.com/ayoolaolafenwa/orangelib/releases/download/v0.1.0/orange_model.h5).

```python
fruit_name, prediction_confidence = classifier.predict("path_to_image")
```
The path to the image to be predicted is loaded.


```python
print("Fruit Name: ", fruit_name)
print("Prediction Confidence: ", prediction_confidence)

```
We print out the fruit name and the level of prediction confidence.

We shall perform predictions on some images using the class *OrangeClassifier*.

**sample1**

![sample1](https://thepracticaldev.s3.amazonaws.com/i/18j73dluh6sxrk21nsg0.jpg)

We intend to classify this image above.


```python
from orangelib.model import OrangeClassifier

classifier = OrangeClassifier("orange_model.h5")
fruit_name, prediction_confidence = classifier.predict("sample1.jpg")
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**

![sample2](https://thepracticaldev.s3.amazonaws.com/i/lg2fo943h5g4dqe7qdqa.jpg)

```python
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: ##

**Sample3**

![sample3](https://thepracticaldev.s3.amazonaws.com/i/8zihu1nc47h2hcv3fnou.jpg)


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.


We make use of this code below for multiple predictions.


``` python
from orangelib.model import OrangeClassifier

classifier = OrangeClassifier("orange_model.h5")
fruit_names_list, confidence_list = classifier.predictBatch(["sample1.jpg","sample2.jpg","sample3.jpg"])
for fruit_names, confidence in zip(fruit_names_list,confidence_list):
    print("Fruit Name: ",fruit_name)
    print("Prediction Confidence: ", confidence)
```



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

The major differences are:

```python
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*.

**Note:** There is no limit to the number of images that can be predicted with the *predictBatch function*.

```python
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.jpg**

![sample4](https://thepracticaldev.s3.amazonaws.com/i/p4ap7a8qdjvnframinyf.jpeg)

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

```python
from orangelib.model import BananaClassifier

classifier = BananaClassifier("banana_model.h5")
fruit_name, prediction_confidence = classifier.predict("sample4.jpg")
print("Fruit Name: ", fruit_name)
print("Prediction Confidence: ", prediction_confidence)
```
``` python
from orangelib.model import BananaClassifier
```
The class *BananaClassifier* is imported from Orangelib.

``` python
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](https://github.com/ayoolaolafenwa/orangelib/releases/download/0.1.2/banana_model.h5).

**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**

![sample5](https://thepracticaldev.s3.amazonaws.com/i/4qbpd35en3g3aomknco5.jpeg)

``` python
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**

![sample6](https://thepracticaldev.s3.amazonaws.com/i/cn4pzsvycjojnvzn85s0.jpeg)


``` python
from orangelib.model import BananaClassifier

classifier = BananaClassifier("banana_model.h5")


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

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

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

```python
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.*
 
```python
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.


## Implementing Prediction On A Single Image With AppleClassifier: ##

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

**Sample7**

![sample7](https://thepracticaldev.s3.amazonaws.com/i/jvn86v7lf5dhs0mb0et5.jpeg)

```python
from orangelib.model import AppleClassifier

classifier = AppleClassifier("apple_model.h5")
fruit_name, prediction_confidence = classifier.predict("sample7.jpg")
print("Fruit Name: ", fruit_name)
print("Prediction Confidence: ", prediction_confidence)
```

``` python
from orangelib.model import AppleClassifier
```
The class AppleClassifier is imported from orangelib.

```python
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](https://github.com/ayoolaolafenwa/orangelib/releases/download/v0.1.1/apple_model.h5).

**Output7:**

```
Fruit Name:  green apple 
Prediction Confidence:  99.94303
```

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


**Sample8**

![sample8](https://thepracticaldev.s3.amazonaws.com/i/m28ic91nfbgl7w0at8l4.jpeg)



``` python
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.



**Sample9**

![sample9](https://thepracticaldev.s3.amazonaws.com/i/k3oy5e7x5wnwaav9ol3s.jpeg)


## Implementing multiple predictions on images with AppleClassifier: ##


```python
from orangelib.model import AppleClassifier

classifier = AppleClassifier("apple_model.h5")

#predict an array of images
fruit_names_list, confidence_list = classifier.predictBatch(["sample7.jpg","sample8.jpg","sample9.jpg"])

#loop over the array of results for each predictions
for fruit_names, confidence in zip(fruit_names_list,confidence_list):
    print("Fruit Name: ",fruit_names)
    print("Prediction Confidence: ", confidence)
```


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

``` python
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.*
 
```python
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.

Visit the official **[github repository of Orangelib](https://github.com/ayoolaolafenwa/orangelib).**