Click here to Skip to main content
15,117,882 members
Articles / Artificial Intelligence
Article
Posted 1 Jan 2019

Stats

7.5K views
8 bookmarked

Apple-tron an AI for farmers .

Rate me:
Please Sign up or sign in to vote.
3.55/5 (11 votes)
1 Jan 2019CPOL12 min read
Apple-tron a quick AI for small to medium enterprise farmers that applies stickers to fruits .

Introduction

Ai is amazing  and it is very exciting to learn and make new solutions using Artificial intelligence .

In this article we will try and  develop a low costing , scale-able   AI that will be able to classify apple’s based on it’s color,height and  diameter  into various categories of red and green apples and than further allocate a corresponding  sticker based on the apples actual size .

Moreover as some apples may be below quality  standard’s,The Ai  will also make decisions for us  to  reject some apples for being oversized or undersized

For the purpose of our example our custom  sticker’s labels will consist of   #small R ,# Mid R ,#Big R or #small G,# Mid G or # Big Green as represented by figure 1.0 below.

This article will  also provide information on which sensor’s you can actually  use to solve key issue’s of automating measurements and than proceed to provide code on AI development.

The end goal of this article will be  to create a Ai that can replicate human thinking and decide on which sticker is to be applied to an apple that is being evaluated.

 

figure 1.0

Image 1

 

The  table below represents the  6 different categories of apple Stickers that we are looking to work with.

Red apple Category

Green apple Category

#Small R

#Small G

#Mid R

#Mid G

#Big R

#Big Green

 

Background

Who will this Article Help ?

The following article intends to help farmers  and small  re-seller enterprises who wish to devise automation mechanisms  using AI  to value add  to there products ,increase productivity and expand there operations while incurring minimal labour costs with respect to sorting and labeling fruits.

As we all know that branding  and  labeling of product’s is a pleasant requirement now days  as it is an important factor in distinguishing products of value people prefer to buy labeled products over unlabeled products.

In addition to the above small micro enterprises  or small farmers exist in huge quantities however no such low cost solution exists to aid them to engage in development of  automated fruit  labeling and sorting systems that focus on diameter size and height of the produce as well.

How will the AI component  help ?

Normally when apples are picked they come in all shapes and sizes and have to be further sorted and categorized during processing therefore with AI the sorting and labeling would become automated .

Saving Labour costs.

Saving significant equipment purchasing costs.

 Increasing  product  processing speed which will increase delivery speed

 Provide  a fully re -use-able AI that can be trained to identify and label other fruits and vegetable’s

 

What will this Article provide ?

It will recommend information about which sensors  to possibly use

It will provide code and explain how to build,test and use  the AI component.

 

Processing Fruit sorting and labeling without AI

In-order to plan our AI build we need to first identify the issues that it needs to solve :

If you look at the above illustration it would be very ,time-consuming ,tiresome  and resource intensive to manually sort ,measure and label each apple between the 6 various sticker categories.

Since no one tree produces apples of uniform size ,Imagine if you had 30,000 apples to process between various sizes and colour.

As a farmer you may end up facing many additional challenges .

Such as trying to find temporary workers to accommodate the workload

Fruits rotting and getting spoilt because of slow processing times

Missing Supply deadlines due to slow packaging and labeling since you and your workers would not be operating for 24 hours a day around the clock .

Prone to error’s in labeling.

Increased cost of labeling as the more workers you higher the more manual label-ling machines you would need to buy .

 

 Getting started with Artificial intelligence :

So to get started the first thing I would like to do is replicate my thinking and decision making ability onto a machine in our case a windows 10 Intel Core 7 cpu with 4 GB ram by setting up a development environment, algorithms and providing it with training data so that it can learn from it.

Once the machine has finished it’s training,we provide it with some testing data to see how accurately it can replicate my abilities and what it’s error rate is.

Finally if our Ai model’s error rate is low and accuracy is reasonably high we will consider development complete and start using it for production.

 

Challenges of automating Height and Diameter Measurement :

Before we dive into development of the AI it is important to acknowledge how the AI will determine the apple’s colour and measure the apple diameter sizes.

Often tutorial’s give you information on how to make a AI but there is no clue on how to collect data  cheaply in a automated fashion which is the most important part.

Below we explain on what sensor’s  we can  plan to use to collect data with.

Because If  we simply cannot collect data feasibly for the AI to process than that defeat’s the purpose of the AI’s development because it will not make sense to overspend our  budget to utilize our AI.

A major problem lie’s ahead how do we measure a Apple’s Diameter and it’s Height using sensors  ?

Is there a cheap cost effective sensor available that is easy to use and easily replaceable ?

Right now lack there-off information exists on how to measure diameter’s of objects easily without having to use intermediate  mathematics and vision with extensive callibration  so for our scenario we developed a unique solution to approximate and measure the diameter of the Apple using  3 Ultrasonic Sensor’s namely HC-sr04.

The concept  is illustrated by figure 1.1 below. 

Note: ultrasonic sensor  image taken from fritzing

Image 2

 

 

Measuring an  apples Diameter :

To measure the diameter of the apple  we place the Left ultrasonic sensor  and  Right ultrasonic sensor vertically  30 centimeters  apart  and denote it as  Total Distance.

The apple is than  placed  in center of our sensor’s via  a conveyor belt .

Next sequentially we  fire each ultrasonic sensor to get a reading from each side.

When we fire our left sensor it gives a reading of  11.8cm and when we fire the right sensor in this example it also reads  11.8 cm.

Finally we subtract the left hand side’s  sensor reading and right hand sides sensor reading from the Total distance between the 2 sensor’s.

The result from the above operations is an approximated Diameter of our apple .

The above  can be represented using the below formula

Approximated Diameter= Total Distance-(Left Sensor reading + Right Sensor reading)

6.4cm  =     30cm  -(11.8cm +11.8cm)

 

Measuring an  apples Height

Similarly to measure the diameter  of the apple  

We place the Ultrasonic sensor 15 centimeters  above ground  denoted by ground height and than fire the ultrasonic sensor sequentially.

Next we take the reading from the top sensor and subtract it from our Ground height

This can be represented using the below formula:

Apple Height  = Ground Height - Top sensor reading 

 6.8cm  = 15cm -8.2 cm

The result from the above operations is an approximated height  of our apple .

Of course you can use laser distance sensor’s instead of the ultrasonic sensor’s as well, I chose the ultrasonic sensor since it is cheap and easily, replaceable and readily  available in my area.

The current setup is perfectly changeable to accommodate other  fruit’s or other item’s  with larger height’s and  diameter’s as well without the need for much  complex calculations.

The above robust conceptual setup is very cheap and  allows you to quickly and easily adjust your sensor’s to accommodate other items that may need measurement  such as ,pumpkins , dalo, potatoes, pine apple’s, rock melon’s ,passion fruit’s where ever height and diameter play an important factor in pricing or as quality determinant’s

Image 3

Note :cliparts  under CC0  license no attribution required.

 

Detecting The color of an Apple

The next  cheap and cost effective sensor that we plan  to use to  measure the color of the item concerned  in our case is  a TCS3200 color sensor .

We measure the  R= Red value’s  and G = green value’s to determine the difference in colors of the apple.

Since for our scenario we are developing for the apple production line that processes 2 varieties red and green on one production line, The color detection will be for red or green values only.

Image 4

In our concept once all the sensor’s have collected the relevant data such as  color, diameter and height  , we post it to our AI model for evaluation.

So having effectively  determined and  solved  our problem of what sensor’s we can use to automate sensory input for evaluation,  we can now proceed to create our AI  which this article focuses on.

 

Developing The AI

Artificial intelligence is our attempt to replicate human like thinking onto a machine.

So in-order to do this firstly we must  source and create training data based of what our current actions and ways of doing work  are .

In our case since we currently sort , measure,detect color and  than apply a appropriate sticker to the apple .

For the purposes of training our ai to think like us  , we will write down   lets say  600 observations of  our manual labeling process across the various categories and note 3 main parameters that we use for decision making such as Diameter ,Height and color   in a CSV file, along with our classification.

The below is a small excerpt of what our csv file looks like.

Diameter_cm

Height_cm

Color

classification

6.42

6.8

R

#BigR

5.46

6.0

R

#MidR

4.69

5.5

G

#MidG

 

If you have a look at the Color and Classification columns you notice that they are non numeric and varchars.

So be for we can further proceed it is always good to  convert text into a numeric form.

for our case we assign the numeric  value of 1 to Red  and  the numeric value of 2 to green to represent each color under the Color column.

Because we will classify the apple according to 7 different categories 

Red apple Category

Green apple Category

#Small R =1

#Small G =2

#Mid R= 3

#Mid G=4

#Big R=5

#Big Green=6

                      Rejected =7

 

So after representing the categorical  data in numeric form our final csv looks like the below:

 

Diameter_cm

Height_cm

Color

classification

6.42

6.8

1

5

5.46

6.0

1

3

4.69

5.5

2

4

 

Next  we will   take another  600 observations and  reserve it for testing, notice when it comes to testing we only use 3  inputs diameter , height and color of the apple .

The below is a small excerpt of what our csv file looks like.

Diameter_cm

Height_cm

Color

6.42

6.8

1

5.46

6.0

1

4.69

5.5

2

 

Once we begin testing  our  AI  ,some times the AI in the begging will get the answers incorrect and mis- classify  the apples  , we will continually train our AI  until it learns to classify correctly .

The number of times the  AI miss classifies  our apple categories is known as the error rate  but as we continuously train the AI it eventually get’s better and better  and start’s predicting accurately .

 

Coding the AI

Prerequisites 

Install Anaconda development environment
Install Python 3.5.2
Install scikit learn
Install pandas

Importing our Dependencies

Python
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

Next load our observed data  called apples.csv

Python
df=pd.read_csv('apples.csv')

If you would like to see the contents with headers of the file you may use :

Python
print (df.head())

Next  specify which columns ,meaning the input  features that you wish to select  from the csv file .  

In our case we will use 3 input features of our apple such as  it's Diameter,Height  and Color .

Python
 input_features=df.loc[:,'Diameter_cm':'Height_cm':'Color']

Next we select a column that contains our  targetted conclusion that we made after referencing the input features, meaning we looked at the apples Diameter, Height and Color and than drew a conclusion by classifying it into one of the following categories    #small R ,# Mid R ,#Big R or #small G,# Mid G or # Big Green and Rejected.

Python
 targetted_output=df.loc[:,'claffification']

Next we proceed to split the rows inside our apples.csv file into Training and Testing data.

In our case we take a value of  0.3 which is a 30%  split for testing and 80% for training our AI. 

Python
 train_x_data,test_x_data,train_Y_data,test_Y_data = train_test_split(input_features, targetted_output, test_size = 0.3, random_state = 100)

Next initialize the Decision Tree classifier where max_leaf_nodes is the number of leafs that need to be processed  to draw a conclusion , for our case we choose  8 you may change this values to increase your accuracy.

Python
apples_tree=DecisionTreeClassifier(max_leaf_nodes=8,random_state=0)
 

 

With the below one simple line we train our ai using the Gini method.

Python
clf=apples_tree.fit(train_x_data,train_Y_data)
 

Finally lets  take 30 % of our testing data and actually test the accuracy level of our AI.

Python
Test_predications =apples_tree.predict(test_x_data)

print ("Accuracy is", accuracy_score(test_Y_data,Test_predications)*100)
 

After testing is complete the accuracy will be displayed as follows , In our case it was around 93.47 % with still room for improvement .

Image 5

But it's good enough for our case so with the testing done lets move onto actually meaning fully using our ai on a single apple .

So  for our scenario we want to apply a  sticker or reject an apple for being under sized , we do this by feeding a single array to the predict () function  feeding Diameter , Height and color.

Python
 apple_prediction=apples_tree.predict([[6.4,6.5,1]])

The result of the prediction is a Big red Apple  so sticker of   #Big R  is to be applied , which is correct.

Image 6

So using very simple if than else logic we print the prediction of our AI on which sticker to apply .

Ofcourse  in production we would remove the print statements and call subroutines to hardware that applies the actuall sticker onto the apples.

Python
if apple_prediction==1:
    print("#SmallR")
if apple_prediction==2:
    print("#Small G")
if apple_prediction==3:
    print("#Mid R")
if apple_prediction==4:
    print("#Mid G")
if apple_prediction==5:
    print("#Big R")
if apple_prediction==6:
    print("#Big Green")
if apple_prediction==7:
    print("Rejected")
 

Points of Interest

Thanks very much for reading my article if you like  please leave a rating .

I learned that it takes more than just one component to build a meaningfull product and also that no such cheap solutions exist for farmers untill now for  application of stickers based on the size of a fruits diameter.

History

31/12/2018- added pictures and notes

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Prilvesh K
Software Developer
Fiji Fiji
Prilvesh is a Front end and Back end developer who holds certificates from Google , Microsoft and Oracle who Specializes in Web development and automation using Python and PHP.
He has experience designing developing and building Api's and secure platforms that can handle enterprise level transactions with encryption.

He often simplifies complex problems and solves them through processes and procedures that are efficient and easily understandable even by non programmers including management.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manuel Cecconi20-Oct-19 23:11
MemberManuel Cecconi20-Oct-19 23:11 
QuestionAI ? Pin
Member 130288502-Jan-19 5:28
MemberMember 130288502-Jan-19 5:28 
AnswerRe: AI ? Pin
Prilvesh K2-Jan-19 20:45
professionalPrilvesh K2-Jan-19 20:45 
GeneralRe: AI ? Pin
Member 130288504-Jan-19 0:50
MemberMember 130288504-Jan-19 0:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.