# Quality and Defect Inspection of Green Coffee Beans Using a Computer Vision System
## 4 Results and Analysis
### Classification Accuracy of the Machine Vision  
To test the accuracy percentage of the machine vision when carrying out the quality and defect inspection, 946 coffee beans were inspected. Comparisons between visually graded and machine vision-graded beans were made as shown in Tables 2 and 3. Figure 16: 2D plot of eccentricity and area features used for the \(k\)-nearest neighbor algorithm using the training set. Very low quality refers to beans that have a quality lower than 25%. Low-quality refers to beans that have a quality between 25% and 50%. High-quality refers to beans that have a quality between 50% and 75%, and very high-quality beans refers to beans with a quality higher than 75%. The algorithm uses \(k=10\) neighbors for this test. Finally, an average accuracy percentage is taken for the quality and defect type inspection. Table 2 shows the confusion matrix for the quality inspection. The percentage of accuracy seems to be large for all types of quality beans. Although sometimes the machine vision does not classify the quality of beans correctly, most of the beans evaluated are correctly classified. The percentage of accuracy is higher for very low quality and very high quality coffee beans. This means that the system works better when identifying the quality of normal coffee beans or very damaged coffee beans. This is expected because very low quality beans usually have several defects that make it easier to detect their bad quality percentage. On the other hand, very high quality beans usually do not have any defects which explains why there is a high accuracy classification as well. Table 3 shows the confusion matrix for the defect type inspection. The machine vision works very well when classifying black coffee beans' defect types, showing 97.04% accuracy. This can be explained by the functioning of the \(k\)-nearest neighbor (KNN) algorithm. The color feature is very distinctive for black coffee beans, as can be seen in Figure 8, where black coffee beans are located far from the other beans in the 3D plot, making it easier for the KNN algorithm to find nearest neighbors when classifying black beans. Furthermore, the machine vision shows only 92.12% accuracy when identifying the sour defect type. This is due to difficulty in identifying the color of sour coffee beans as the color varies from light brown to dark brown. When predicting defects, the machine vision can fail to predict correctly due to features' similarities; that is, different defect types can have similar feature values, and this makes the prediction more difficult. Sour and black defect types are identified mainly by the color feature when some sour beans can be incorrectly classified as black beans and vice versa, as seen in Table 3. A high accuracy of 98.05% was achieved in identifying very long berry coffee beans. This is because there are two quantities, eccentricity and roundness, that set them apart in the plot from all the other beans, while the other defect types are being identified using only one quantity. This result is important since it allows us to conclude that using several quantities to identify every defect type would improve the accuracy of the system in a considerable manner. \begin{table}
\begin{tabular}{c c c c c c} \hline \hline
**Actual Quality** & \multicolumn{5}{c}{**Predicted Quality**} \\ \hline  & **Very Low** & **Low** & **High** & **Very High** & **\% Accuracy** \\ \hline Very low & 234 & 7 & 0 & 0 & 97.10\% \\ Low & 9 & 181 & 5 & 0 & 92.82\% \\ High & 0 & 9 & 189 & 7 & 92.20\% \\ Very high & 0 & 0 & 9 & 296 & 97.05\% \\ \multicolumn{5}{c}{Average accuracy} & 94.79\% \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Confusion matrix for quality inspection using \(k=10\).