Next Article in Journal
A CFD–DEM Study on Non-Spherical Cutting Transport in Extended-Reach Wells Under Rotary Drilling
Previous Article in Journal
Ignition and Emission Study of an Ammonia–Coal Co-Firing Flame in a Lab-Scale Dual-Swirl Burner
 
 
Font Type:
Arial Georgia Verdana
Font Size:
Aa Aa Aa
Line Spacing:
Column Width:
Background:
Article

Machine Learning and Queuing Algorithm Integration for Real-Time Citrus Size Classification on an Industrial Sorting Machine

by
Yahir Hernández-Mier
1,
Marco Aurelio Nuño-Maganda
1,*,
Said Polanco-Martagón
1,
Ángel Dagoberto Cantú-Castro
1,
Rubén Posada-Gómez
2 and
José Hugo Barrón-Zambrano
3
1
Intelligent Systems Department, Polytechnic University of Victoria, Ciudad Victoria 87138, Tamaulipas, Mexico
2
Division of Postgraduate and Research Studies, Tecnológico Nacional de México/Instituto Tecnológico de Orizaba, Orizaba 92670, Veracruz, Mexico
3
Digital Systems Group, Electronics Department, Instituto Nacional de Astrofísica Óptica y Electrónica, San Andres Cholula 72840, Puebla, Mexico
*
Author to whom correspondence should be addressed.
Processes 2026, 14(1), 164; https://doi.org/10.3390/pr14010164
Submission received: 31 October 2025 / Revised: 25 November 2025 / Accepted: 3 December 2025 / Published: 4 January 2026
(This article belongs to the Section Process Control and Monitoring)

Abstract

The classification of lemons by size is a crucial industrial process that ensures specific quality standards. Lemon sorting is typically performed by hand or often using expensive, outdated machines. In this article, we develop Machine Learning and Queuing algorithms, program them on low-cost hardware—specifically, a microcontroller and a single-board computer—and integrate them with an existing fruit-sorting machine, which classifies lemons by size. We acquired a dataset of 3127 lemon images in six industry-standardized sizes. We developed algorithms to extract geometric features, including one based on the peduncle location, which is estimated using a pre-trained Faster Objects, More Objects (FOMO) model. We used these features to train and evaluate five machine learning models, with the best-performing model achieving 87.22% accuracy over a set of lemons acquired under controlled conditions. We tested the proposed system in a real industrial environment, proving its feasibility by sorting 1558 lemons and obtaining an accuracy of 78.00%, despite the industrial-standard sizes having considerable overlap.

1. Introduction

Lemon is a citrus fruit whose origin is estimated to be in India. It is a typical fruit of tropical regions with multiple uses in the food and pharmaceutical industries. Mexico produces three varieties of lemons. Colima, Michoacán, and Guerrero states produce the most common variety, the Mexican lemon (also named Colima lemon) variety (Citrus aurantifolia). Veracruz, Quintana Roo, Oaxaca, Yucatán, and Tabasco produce the second most common lemon variety, the Persian lemon (Citrus latifolia). Finally, Tamaulipas and San Luis Potosí produce the Italian (also named Yellow) lemon variety (Citrus limon).
According to statistics from the United States Department of Agriculture (USDA), in 2023, Mexico was the second-largest producer of lemons and limes [1]. In addition, Mexico’s Ministry of Agriculture and Rural Development (SADER) confirms Tamaulipas as one of the top lemon-producing states nationwide. Furthermore, based on statistics from the Tamaulipas State Government, in 2016 this state was the largest exporter of Italian lemons, estimating some 22 thousand tons, with the main client being the United States [2]. Italian lemons are mainly used for sale as fresh fruit, although they are also of interest to industries dedicated to juice production.
In the harvesting, handling, and distributing of fruits and vegetables, the grading process is essential to maintain current quality standards and regulations that companies must comply with. The environmental impact of lemon harvesting and distribution is high. In [3], the authors estimate the carbon, water, and energy footprints of the production and process of the Persian lemon in Mexico. Therefore, developing an efficient and compact lemon sorting software with minimum power requirements is indispensable. The developed software is the system’s brain, coupled to a conveyor band to separate the fruit by activating the appropriate actuators.
A sorting machine allows the user to sort fruits by some characteristic (size, ripeness level) faster than manual sorting. Companies adapt fruit-sorting machines to requirements and quality standards goals. Some artificial vision systems allow for discarding fruits in lousy condition or affected by a pest and classify them by size or maturity level, ensuring a quality product and value in the market [4].
In the state-of-the-art, we found several articles reporting work on the classification of lemons. Most of them report classification results under ideal conditions, from images captured with good illumination and sensors that handle high resolutions, generally without motion-generated noise, which favors classification algorithms in obtaining high classification results. The other class includes works that report algorithms trained and/or tested on functional separating machines, which introduce certain complications to the classification stage, such as variations in illumination, motion noise, and environmental variations, even in a controlled environment. Concerning the grading algorithms, there is a great diversity of approaches to classifying lemons. Some use classical approaches (extracting features that feed a classifier), while others use methods based on deep learning (where feature extraction is not required). Both approaches are valid, and the authors of the related research highlight their advantages and disadvantages.
Recent studies have explored various computer vision and deep learning approaches for grading and defect detection in lemons and citrus fruits. Early works [5,6,7], focused on traditional image processing and regression-based systems for volume and size estimation, achieving accuracies above 98%. More recent research has shifted toward deep learning architectures, including YOLO variants, ResNet, VGG, and ANNs, for the automated classification of lemons based on quality, ripeness, size, and defects. For instance, ref. [8,9,10] obtained accuracies of 92%, 93.14% and 96.6%, respectively, while [11] reported a 100% accuracy in defect detection using CNNs. Systems such as YOLOv4 [12] and YOLOv5s [13] have improved object detection and grading performance by integrating real-time processing and feature fusion techniques.
Regarding citrus quality [14] and defect detection [15], VGG16, VGG19, and ResNet50 models are used to detect citrus defects, achieving up to 96% accuracy with ResNet50 and Softmax classifiers. Other researchers have proposed embedded and robotic implementations—for example, ref. [16] reports that lightweight CNN-based classifiers achieve 94% accuracy, while [17,18] integrate robotic arms and LSTM-based predictors for automated sorting. Recent hybrid architectures, such as Stacked Auto-Encoders with CNNs [19], have reached nearly 99% accuracy. Overall, the literature demonstrates a clear trend toward combining deep learning, real-time vision, and robotic systems to achieve high-accuracy, automated, and scalable solutions for lemon grading. Related to the implementation of citrus classification on SBC, there are CNN-based approaches [20] and a lightweight CNN-based approach called Sortnet [21].
In this project, we designed an embedded vision system adapted to work with an operating (old) mechanical lemon sorter. The problem with the current sorter is that it is not possible to obtain the necessary spare parts to keep its original vision system (which has been in operation for more than 20 years) because they have been discontinued or are not in stock, which causes an increase in operating costs due to the downtime of the sorter. Still, a PC running Windows 2000 and the manufacturer’s citrus sorting software controls the machine. We implemented a lightweight machine-learning-based vision system on an SBC. To train this machine learning algorithm, we developed image processing algorithms to extract the required classification features, including a small model to locate the lemon’s peduncles. We also implemented a queuing algorithm on a microcontroller to determine the timing required to extract the lemon based on the classification.
The main contributions of this article are:
  • A lightweight lemon classification model (LCM) by size implemented on an SBC in real time.
  • A peduncle detection model (PDM) whose results generate a feature that is useful for the lemon size classification algorithm. These features, in combination with other geometric features, are used to train the LCM. The inference time for the lemon peduncle detection is low enough to be executed in real time as part of the feature extraction process.
  • An image processing algorithm that extracts features from lemon images, which a trained model evaluates.
  • The queuing algorithm is executable on an 8-bit microcontroller, which manages the activation times of the ejector solenoids that send the lemons to the appropriate chutes.
  • The integration of the above modules into a lemon classification system in an existing industrial lemon sorting system. Due to its modularity, this system is easy to maintain and cost-effective.
This paper is structured as follows. In Section 2, we describe the material and methods required to sort lemons by size. In Section 3, we present and discuss the results obtained and analyze the advantages of the proposed system. Finally, in Section 4, we conclude and provide directions for future work.

2. Materials and Methods

2.1. Analysis of Existing Lemon Classifier Machine

After visiting the sorting machine for which we will propose an improvement, we analyzed its components to carry out a modification proposal. The original sorter has eight roller that transport the lemons at high speed; on the roller, there is a camera that is in charge of acquiring the image; underneath the image, at different distances from each other, there are the people in charge of transferring the lemons to the next roller. The solenoids transfer the lemons to the next roller based on the classification assigned by that vision system. Figure 1 shows the main components of the industrial sorting machine. Figure 1a–f describe each component.
The photoelectric sensor coordinates the components since each pulse sent when detecting a citrus fruit sends a pulse to the camera to capture a citrus image, keeping a count to know the activation order and offset of the solenoids. A PC coordinates the latter since it requires both processing and memory in conjunction with the control cards, which define the solenoid activation in the proper order. A power management stage is needed between the machine components since the solenoid works with 24 V and the photoelectric sensor works with 10 V. The machine allows the operator to adjust the parameters through a computer interface, which ensures that all the belts sort the citrus similarly. The operator changes the sorter parameters to adapt to the lemon shapes, which can be either more elongated or rounded.

2.2. Hardware and Components

The hardware components are:
  • Single Board Computer (Raspberry Pi 4 model B+, Raspberry Pi Holdings plc, South Wales, United Kingdom).
  • Microcontroller (Arduino Mega 2560, Arduino AG, Monza, Italy).
  • Camera (Model: HT-SUA134GC-T1V Huateng USB 3.0, Huateng Vision, Shenzhen, China).
  • Mosfet-based solenoid driver board.
  • Opto-isolation and attenuation module.
  • Led lighting ring.
Figure 2 shows the block diagram of the new vision system coupled to the industrial citrus sorting machine. The SBC and the microcontroller receive, through an opto-isolation and attenuation module, the pulses generated by a photoelectric sensor coupled to the conveyor chain. At each pulse, the SBC requests a frame from the USB camera, and when the frame is ready, the SBC processes the received frame to obtain the class to which the observed lemon belongs. The SBC sends the lemon class and the corresponding number of pulses to wait to the microcontroller via an SPI interface through a logic level converter. The microcontroller receives the information from the SBC and queues the pulses to wait, depending on the class to which the lemon observed by the camera corresponds. Once the counter of pulses to wait reaches zero, the microcontroller sends activation pulses to the solenoids via the solenoid driver, which activates the ejectors located at each chute of the sorting machine.
A roller conveyor transports the lemons, which pass under the camera where the ring light illuminates the fruit. A photoelectric sensor produces pulses synchronized with the position of the pockets on which the lemons travel. With each pulse, the vision system captures an image of the lemon, analyzes it, determines the class to which it belongs, and sends the information to the microcontroller to activate the solenoids.
The software components are:
  • The SBC hosts the software that captures the images of lemons from the camera, processes those images, and determines the size to which the citrus fruit belongs. Based on the generated size, it sends commands to the microcontroller to activate the solenoids responsible for separating the lemons. Another feature of this software is that it receives configuration parameters to perform various classifications of lemons according to the user’s requirements.
  • The microcontroller hosts the software routines that receive input signals from the OBC and send signals to the solenoids via a signal amplifier board to separate the lemon fruits.

2.3. Lemon Images Dataset

Figure 3 shows a mechanical lemon-size gauge, which is a caliper specifically designed to measure lemon size according to citrus industry standards. The device has adjustable jaws that measure the minor axis diameter of the lemon. This diameter determines the fruit size code (“count”), which indicates how many lemons fit into a standard 40-pound box. We can highlight that, for some lemon sizes, the available measurement ranges overlap, which can lead to classification errors, and that this is often subject to user interpretation.
To generate the lemon dataset, we captured the images using the camera connected to the SBC. We turned on the sorting machine and passed a set of previously graded lemon fruits to simulate the on-field operating conditions of the sorting machine. Once we obtained the lemon image dataset, we applied threshold- and contour-based image processing to detect the features used for training the classifiers. We generated a dataset of 3127 lemon images that includes six variants in lemon size. We adhered to the lemon industry standards, which specify a numbering system to indicate lemon size, ensuring uniformity, efficiency, and transparency throughout the supply chain. Each size number corresponds to the approximate number of lemons that fit into a standard 40-pound (18-kg) box. The lemon sizes included in the generated dataset are 75, 95, 115, 140, 165, and 200. Table 1 provides information on the number of images for each variant as well as the total number of lemon images in the dataset. Figure 4 shows selected images from the lemon dataset generated in this article, which correspond to the previously mentioned industry-standard sizes.

2.4. FOMO-Based Peduncle Position Estimation

Based on the images of lemons in the dataset, we generated a second dataset that labeled the Peduncle region in each lemon image. We do the peduncles labeling in the EdgeImpulse platform, which streamlines the development, deployment, and scaling of machine learning models for edge and embedded (TinyML) systems [22]. Figure 5 shows selected input images with the labeled peduncle. The Edge Impulse platform provides a comprehensive workflow that encompasses data acquisition, preprocessing, training/optimization, evaluation, and deployment. It includes several object detection models that users can generate and customize using their provided images. The FOMO model is best suited for applications that require real-time object detection on low-power or resource-limited devices, such as microcontrollers or embedded systems, where models like MobileNet or YOLO are too computationally intensive to run efficiently. Unlike YOLO, which provides full bounding boxes and high accuracy but demands significant processing power, or MobileNet, which is mainly for classification tasks, FOMO offers a middle ground by delivering lightweight, grid-based object localization with minimal memory and energy use, making ideal for edge AI tasks where speed, efficiency, and deployability on tiny hardware are more critical than achieving the highest precision. Based on the results, we choose the FOMO model, which is an object detection algorithm that divides the image into a grid of smaller cells and predicts a class label (or no object) for each cell [23]. The FOMO model enables real-time detection of multiple objects while using less memory and processing power than other models. It uses a compact CNN backbone (such as MobileNetV2 or EfficientNet-Lite) to produce a feature map.
Once we set the peduncle labels for the input lemon images and the model type in the EdgeImpulse platform, we generate a float32-precision TensorFlow Lite (.tflite) model that can be used on any PC to estimate peduncle localization. We use this model to estimate the previously described peduncle/radius ratio feature.

2.5. Feature Extraction for Size-Based Lemon Classification

Figure 6 illustrates a block diagram outlining the operation of the image processing routines for extracting features from lemon images. The roller conveyor starts operating and acquires a lemon image at each pulse. To obtain the features for the classification algorithm, we apply the following image processing algorithm to each RGB captured image. As the first step, we convert the image from the RGB color space to the CIELab color space. We observed that in the chromaticity channels, a and b, of this color space, the citrus color information appeared well segmented from the background. We also observed that in channel a, the pixels corresponding to green lemons were better segmented, and that in channel b, the pixels corresponding to the fruit for yellow lemons were better segmented than in channel a. Based on these observations, we decided to process both channels, a and b, to produce the binary mask from which we extracted the classification features. After converting the RGB image to CIELab space, we applied a median filter to smooth the lemon surface in both channels. Next, we thresholded the filtered images using the Otsu algorithm and detected contours in the resulting binary images. We then filter the contours by area, keeping the one with the largest area and discarding the others. The latter operation isolates the binary mask corresponding to the lemon, and we then use a bitwise OR operator to produce the final binary image. Using the previously computed lemon contour, we extract the following features:
  • Contour area (in pixels).
  • Contour perimeter (in pixels).
  • Length of major axis LMaA (in pixels).
  • Length of minor axis LMiA (in pixels).
  • LMaA/LMiA ratio.
  • Peduncle over major axis ratio. To estimate this ratio, the software detects the lemon peduncle and computes the distance to the centroid of the binary images. Then, the software computes the ratio of this distance and one half of the major axis.
The selected features allows us to characterize the actual fruit body and grade the lemon based on industry-standard size categories (e.g., 75, 95, 115 mm).

2.6. Queuing Algorithm to Activate the Ejector Solenoids

We propose a queuing algorithm that enables us to parallelize solenoid activation on a sequential device. We implemented this algorithm on a 8-bit microcontroller, which performs the operations described next. The queuing algorithm begins by initializing the global pulse counter and the pulse output variable to 0. Next, we configure the pulse output port to generate parallel output. In the next step, we initialize the matrix storing the queue data for each chute to the maximum value for the type of variable used to store each element of the matrix (in this case, uint8). We defined the matrix size as the number of chutes, d, and the number of pockets, p, along the roller conveyor line between the camera position and the last chute. In our case, we had 10 chutes and 78 pockets. The next step in the algorithm is to initialize a vector of length d, which contains the values to write to the output port when the backwards pulse counting reaches 0. We then configure the hardware interrupts, including the pin number and the interrupt service routine (ISR) to execute for each event. Before entering an infinite loop, we define a variable to store the duration of the positive pulse generated by the outputs and initialize a flag that will be set to true when the interrupt pin receives an external pulse. Inside the infinite loop, we poll the SBC for SPI data that contains the chute number and the number of pulses to wait before writing a high state to the output. If we receive new data, we push the number of pulses to wait before activating the outputs into the queue index corresponding to the chute number. We also register the current number of elements in the queue in a vector, using the chute number as the index. The algorithm verifies two conditions: the elapsed time since the last output activation is greater than or equal to the pulse width, and it also sets the output activation flag to true. If the algorithm finds both conditions true, we write 0 to the whole output port and set the output activation flag to false. Otherwise, the algorithm returns to the beginning of the infinite loop.
On the other hand, the microcontroller executes an ISR when receiving an external pulse. This routine begins by subtracting one from every element of the queue matrix. Next, the algorithm enters a for loop executed for each chute index. If the current component of the first row of the queue matrix is 0, we shift the elements of the column corresponding to the current chute index up one row in the queue matrix. We then update the number of elements in the queue for the current chute index, set the pulse output variable to high for that chute index, and write it to the output ports. Finally, we update the pulse output flag to true and return to the beginning of the for loop. The output flag controls the width of the high pulse generated by the microcontroller. Figure 7 illustrates the flow diagram for the complete queuing algorithm previously described.
Figure 8 illustrates how the queuing algorithm works for a hypothetical eight-pulse case in which the classification system detects lemons at each pulse. We can highlight that each chute is assigned a specific number of pulses, which increases proportionally to the chute’s distance from the camera. Therefore, the number of pulses to queue will always be greater than the number of pulses already queued. In this example, we have assigned chutes C 1 , C 2 C c values 1, 2, …, c, respectively, which represent the number of pulses to wait before solenoid activation. In the initial state of the queue matrix (Figure 8a), the algorithm initializes all its elements to the maximum value the variable can store (255 in this case, as its type is uint8). The dimension of the matrix is p × c , where p is the number of pockets (space between adjacent rollers of the conveyor), and c is the number of chutes (outputs to the belt conveyors) used in the sorting machine. Also, the vector S contains the state of the variable to output through the microcontroller port. The initial state of each vector element is 0. Figure 8b shows that at the first external pulse, the SBC indicates to the microcontroller that the fruit it has processed must exit through chute C 3 , thus three pulses are queued in the matrix in the first available cell corresponding to the referenced chute. The queued numbers will decrease by one at each pulse (if they are greater than 0), emulating a countdown before activating the corresponding solenoid. Figure 8c shows that at the second external pulse, the SBC indicates to the microcontroller that the processed fruit has to exit through chute C 2 . Once the microcontroller queues the number of pulses to wait in the corresponding chute column, the already-queued pulses are decremented by 1, so the number of pulses to wait in chute C 3 changes to 2. In the third external pulse (Figure 8d), the SBC signals the microcontroller to send the processed fruit through chute C 3 . Since a previous element was in the queue, the microcontroller places the new number of pulses to wait in the second row of the corresponding chute column. In the fourth pulse (Figure 8e), the countdown of the elements in the first row for the chutes C 2 and C 3 reaches 0, hence the state of the output vector, S, changes to 1 for the second and third elements, activating in that moment the corresponding solenoids. This state remains high for a specified period of time, then the microcontroller resets the state to 0. After activating the solenoids, the microcontroller shifts the queued elements up by one. In the fifth pulse (Figure 8f), the SBC indicates the microcontroller to queue one pulse for chute C 1 , and the already queued elements are decreased by one. Figure 8g illustrates that at the sixth pulse, the SBC indicates to the microcontroller to queue four pulses for the chute C 4 . At the same time, the countdown for the first three chutes reaches 0, causing the microcontroller to set the corresponding cells of vector S to 1 and activate the attached solenoids by setting the corresponding output pins high. Before the end of the external pulse, the microcontroller sets the output vector elements to 0. In the seventh pulse (Figure 8h), the SBC indicates the microcontroller to queue one pulse for chute C 1 , while decreasing the queued element in the fourth column by one. In the eighth pulse (Figure 8i), the SBC sends the microcontroller a command to queue one pulse for chute C 1 . The microcontroller also decrements the queued elements by 1, thus setting them to 2 for chute C 4 and 0 for chute C 1 , activating the corresponding output pin via vector S.

3. Results

Table 2 summarizes the results of the addition of minority samples by the SMOTE algorithm for the selected classifiers. We performed a comparative analysis demonstrating that SMOTE augmentation improves baseline performance across most architectures. Still, we also observe that increasing synthetic samples from 20% to 50% yields no additional gain in accuracy. The inclusion of the peduncle-over-major-axis-ratio feature enhances ensemble-based methods, such as the Random Forest classifier, which achieved an accuracy of 87.22%. In contrast, it introduces noise that degrades the performance of SVM and Decision Trees. Consequently, the combination of Random Forest with the additional morphological feature and moderate data balancing constitutes the most robust modeling strategy for this dataset.

3.1. Color Space Selection

We selected the CIELab color space because Otsu’s thresholding algorithm produced binary images that required less post-processing to extract the region of interest corresponding to a citrus fruit. In addition, we found that the yellow and green color information characteristic of lemons appeared more complete in the A and B color channels of this color space than in other color spaces (HSV, YUV, and XYZ). Figure 9 shows the thresholding of the channels of the HSV, YUZ, XYZ, and CIELab color spaces, where we can observe that the thresholding of the A and B channels of the CIELab color space yields cleaner binary images.

3.2. Peduncle Detection

To generate a model that enables us to detect the peduncle location, we trained four models on the Edge Impulse platform. We choose the FOMO model, which has proven its reliability in segmentation tasks. Table 3 presents the parameters of the generated models and the accuracy results obtained. We integrated the best-performing model into the feature-detection algorithm to estimate the peduncle/radius ratio, thereby enabling more accurate classification of lemons by size.
Regarding the trained models, we carried out tests with two image sizes (96 × 96 and 224 × 224). For each image size, we use the grayscale or the RGB version. The table reports accuracies for the 32-bit (float-32) and 8-bit (Int8) trained models. We also test three image-resizing methods: fit-long, fit-short, and squash (interpolation). Among the trained models, the best was the one that used an input image with dimensions 224 × 224 in RGB format, achieving the highest accuracy in both vectorized and non-vectorized versions, regardless of the data type (float32 or int8).

3.3. Dataset Balancing Strategy

We can infer from the class counter shown in Table 1 that the dataset is imbalanced. To mitigate class imbalance, we used the SMOTE (Synthetic Minority Over-sampling Technique) algorithm, which addresses class imbalance in machine learning by generating synthetic samples by interpolating existing minority examples and their nearest neighbors [24]. This process effectively increases the representation of the minority class, reducing bias toward the majority class in classifiers and improving model generalization on imbalanced datasets.
Regarding the key parameters of the SMOTE algorithm, for the sampling_strategy parameter, we use the “minority” option to balance all minority classes to the number of samples in the majority class. For the random_state parameter, we set it randomly (42) to ensure consistent results across multiple runs when generating synthetic samples. We did not specify the remaining parameters of the SMOTE algorithm.
Empirically, we conducted three tests to add a percentage of the minority class. We used 0%, 20%, and 50% as the percentages, and evaluated the accuracy of the classifiers we used. The tests confirm that the best-performing classifiers are those where we added 50% of the minority class samples.

3.4. Evaluation of Classifiers

To train the selected classifiers, we divided the dataset into 80% for training and 20% for testing. From the test set, we performed an analysis of the dataset to balance the data using the majority class count criterion, testing without adding synthetic samples (0%) and adding 20% and 50% of the minority class sample count, respectively, as synthetic samples to integrate the new dataset, using the SMOTE algorithm.
We run the GridSearchCV algorithm to find the optimal parameters for the lemon image classifiers that maximize accuracy, thereby achieving the best possible performance. The Grid Search algorithm is an optimization method that performs an exhaustive parameter search [25]. It defines a grid (set) of possible values for each hyper-parameter, then trains and evaluates the model on every possible combination of those values. For each combination, the cross-validation scheme evaluates the model’s performance using a combination that yields the best performance metric. Table 4 displays the classifiers used, along with the parameters obtained by the GridSearchCV algorithm after its execution.
Table 5 compares six classifiers (KNN, SVM, Decision Tree, Gradient Boosting, MLP, and Random Forest), and the metrics exhibit consistent patterns that highlight their strengths and limitations in multiclass classification. Random Forest shows the highest micro-average (0.8658), indicating the best overall performance when weighting all samples equally, and it also achieves the strongest performance across most individual classes, particularly classes 3 and 5, where recall is exceptionally high. SVM and Gradient Boosting follow closely with micro averages of 0.8530 and 0.8498, respectively; both maintain balanced precision–recall trade-offs across classes, though SVM struggles slightly on class 4 and Boosting on classes 2 and 4. KNN and Decision Tree show similar mid-level performance (micro-averages around 0.8498 and 0.8466), with KNN slightly better for classes 0 and 5. At the same time, the Decision Tree shows greater inconsistency, with drops in class 4 recall. MLP exhibits the lowest micro-average (0.8387), mainly due to weaker performance on class 4 and somewhat unstable precision patterns, despite strong recall for classes 2 and 5. Overall, Random Forest consistently delivers the most reliable and stable classification across all six classes.
Across the six classes, performance varies notably, with some classes being easier to classify than others. Class 0 and class 3 show consistently high precision, recall, and F1-scores across all models, with SVM, Gradient Boosting, and especially Random Forest achieving the best balance. Classes 1 and 2 exhibit moderate difficulty: Random Forest and SVM lead due to their strong recall and balanced metrics, while other models show greater variability. Class 4 is clearly the most challenging, with all classifiers achieving low recall, though Random Forest remains the top performer. In contrast, class 5 is the easiest to identify, with uniformly high recall across all models and the best F1 scores achieved by Random Forest and Gradient Boosting. Overall, Random Forest consistently provides the strongest per-class performance, followed by SVM and Gradient Boosting, while KNN, Decision Tree, and MLP show more inconsistent behavior across classes.
Although all classifiers exhibit reasonably strong performance across the six classes, Random Forest emerges as the most effective model, achieving the highest micro-average and balanced precision–recall scores across nearly every class. SVM and Gradient Boosting also perform competitively, offering strong generalization with only minor weaknesses in specific classes. KNN and Decision Tree produce acceptable results but are more variable, showing sensitivity to class imbalance and class-specific complexity. MLP, while generally competent, yields the lowest macro- and micro-average scores due to inconsistent class-level behavior. Overall, ensemble-based methods—primarily Random Forest—provide the most robust and consistent performance in this multiclass scenario.
To show the performance of the size classifiers on the lemon images, we show a confusion matrix for each classifier. The confusion matrices illustrate the model performance across six classes labeled 75, 95, 115, 140, 165, and 200, with the diagonal values representing correct predictions. Figure 10 shows the confusion matrices for the selected classifiers.
For the KNN classifier (Figure 10a), most samples were correctly classified, with the highest accuracy observed for classes 75 (100 correct), 140 (106 correct), and 200 (102 correct). Some confusion occurs between neighboring classes, specifically between class 115 (4 misclassified cases), class 165 (32 misclassified cases), and class 200 (7 misclassified cases).
The SVM classifier predicted correctly 102 samples of class 75, 89 of class 95, 68 of class 115, 105 of class 140, 69 of class 165, and 101 of class 200. However, there are minor misclassifications: class 95 (18 cases), class 115 (4 cases), class 140 (20 cases), class 165 (35 cases) and 200 (8 cases) (Figure 10b).
For the decision tree classifier (Figure 10c), the classes with the highest error are for classes 200 (25 samples classified as 165) and 115 (15 samples classified as 95 and 11 samples classified as 140). In the case of classes 75 and 165, the classification error is lower, with only 17 samples classified as 95. Additionally, in the case of class 165, nine samples were classified as 140 and five as 200.
The Gradient Boost classifier (Figure 10d) performs well for most classes, especially with classes 75 (99 correct out of 106), 95 (92/102), 140 (107/122), and 200 (103/130), showing strong accuracy and consistent predictions. However, there is moderate confusion between classes 115 and 140. For class 115, 8 were classified as 95 and 10 were classified as 140. for class 140, 5 were classified as 115, and 10 were classified as 165.
For the MLP classifier (Figure 10e), the classes with the highest error are for classes 165 (8 samples classified as 140, and 26 classified as 200) and 95 (11 samples classified as 115 and 9 samples classified as 75). In the case of class 75, the classification error is lower, with only five samples classified as 95. Additionally, in the case of class 200, eight samples were classified as 165.
The RF classifier (Figure 10f) correctly classified most samples, with the highest accuracy observed for classes 75 (102 correct), 115 (66 correct), and 200 (104 correct). Some confusion occurs between neighboring classes, specifically class 95 (five samples classified as class 75, and eight samples classified as class 115), class 115 (one sample classified as class 95 and five samples classified as class 140), class 165 (eight samples classified as class 140 and 24 samples classified as class 200).
All classifiers agree on the most accurate classes, which are 75, 95, and 200. In addition, the highest number of misclassified cases is between classes 115 (KNN) and 165 (SVM, DT, GB, RF). We can also observe that the mislabeled samples belong to contiguous classes in the feature space, so it is possible to confuse a lemon from case 75 with one from 95, but not with one from 115 or 140. Overall, the matrices reflect strong classification accuracy, with moderate confusion among adjacent or visually identical categories, which is typical for the tested classifiers, as there is slight overlap in class boundaries in feature space.
Regarding the CV score obtained by the GridSearchCV algorithm to find the classifier’s optimal parameters, the RF classifier achieved the best score (0.8778). The CV score measures a model’s generalization performance by averaging the scores across all cross-validation folds for a given hyper-parameter combination. Regarding the classifiers’ accuracy, the best was the RF classifier, which achieved an accuracy of 0.8722. The accuracy obtained by the best classifier (RF) does not exceed 90%. This accuracy is due to the similarity between the dimensions of the geometric characteristics extracted for lemons in adjacent categories, as shown by the calibrator in Figure 3.

3.5. Execution Times and Memory Usage Estimation

To estimate the inference time of the selected classification model, we conducted a test in which the classification machine continuously classified 11,660 unlabeled lemons. During this test, we recorded the average inference time (13.69 ms) and the minimum and maximum times (6.60 ms and 87.09 ms, respectively). On the other hand, we measured the time between the photoelectric sensor pulse and the microcontroller’s solenoid activation signal. For the single sample shown in Figure 11, we measured a 72 ms delay. It is worth noting that for this test, we let the microcontroller shift the pulses in the queuing matrix, but forced the write to the output port so that the microcontroller generated the solenoid activation pulse in the current cycle. This time is short enough to process citrus fruits in real time, achieving an average of 14 citrus fruits per second, which is less than the time required to process lemons at the machine’s current acquisition speed (9 lemons per second). Regarding memory usage, we estimated memory consumption during operation of the proposed classification system on the SBC using Python’s 3.12.2 MemoryProfiler library (version 0.61.0). The results show an average memory usage of 153 MB.

3.6. On-Field Test of the Vision System

Figure 12 shows the location of the developed machine vision module during the on-field test and lemon samples used for the classification test. We performed the test in two stages. In a first stage, we used a group of lemons previously sorted by the legacy machine to avoid manual measurement, since this is very time-consuming, and we had limited access time to the industrial sorter. For this test, we used 1558 lemons distributed in five classes, as follows: 175 for class 95, 241 for class 115, 308 for class 140, 390 for class 165, and 444 for class 200. Figure 13 shows the confusion matrix of this live run test. We observe that the accuracy is lower (78% accuracy) than those obtained during training (87% accuracy). The reason for this increase in error is that, in our model, we are not accounting for the classification error introduced by the legacy sorting machine.
The results show that class 95 is the most stable, with a precision and recall of 84.0%. The model confused 26 actual class 95 samples, treating them as 115, and accepted 27 class 115 samples as class 95. Thus, the model distinguishes this class well from the rest (140, 165, 200), but the boundary with class 115 is not clear. The model achieved 82.1% precision and 79.7% recall for class 115. This class confuses samples between classes 95 and 140. After the 95-class, this is the class with the fewest samples (241), which could affect the model’s ability to define more precise limits. Class 140 is where the model produces the most false negatives. It has a precision of 80.6%, but a recall of 67.5%, losing 32.5% of the actual data for this class. Out of 308 cases, the model wrongly classified 83 samples as class 165. The latter could be because features from class 140 are similar to those of class 165, and the model tends to predict the higher class when in doubt. Class 165 has the lowest precision (69.6%) and recall (68.7%), resulting in significant false positives. The low precision could be due to errors propagated from class 140 and to the model sending 92 cases to the next class (200). The class 200 has a precision of 80.4% and the highest recall (91.7%). The precision for this class drops slightly because it is receiving false predictions from class 165 (92 cases). Finally, we observe that the model shows a systematic bias towards predicting a higher class than the actual one, causing the high false negatives in classes 115 and 140.
In a second stage, let the system run for 2 h over non-labeled lemons. In this test, the vision system sorted 11,756 fruits with no downtime or operator intervention. In this test, we observed that the sorting machine failed to sort 96 lemons, sending them to the return conveyor. In 58 of these unsorted lemons, the images contained stacked lemons, producing deformed binary images. In the remaining cases, we observed a lack of ejectors in the pouches transporting the fruits. We will conduct the comparison with the legacy vision system in follow-up work.

4. Conclusions and Future Work

In this work, we proposed an easy-to-maintain, cost-effective machine vision system and the hardware modules for its integration into an existing industrial lemon sorting machine to perform real-time lemon classification. The vision module of this system is based on a lightweight LCM by size implemented on an SBC. We trained the LCM using standard morphological features and a feature based on the PDM-detected peduncle position, which we also trained. Based on these algorithms, the SBC classifies the fruits by size and sends the classification results to an 8-bit microcontroller, where we implemented a queuing algorithm that manages the activation times of the ejector solenoids that send the lemons to the appropriate chutes. The system developed is easily scalable due to the technology used and its low cost, which may be an advantage for its adoption in the regional citrus industry.
To generate the lemon dataset, we captured images of lemons using the camera connected to the SBC. We turned on the sorting machine and fed a set of previously graded lemons to simulate on-field operating conditions. We generated a dataset of 3127 lemon images with six variants in lemon size (75, 95, 115, 140, 165, and 200).
For the PDM, we used the FOMO model available in the Edge Impulse platform. We trained four models and selected the best, which we integrated into the feature-detection algorithm to estimate the peduncle/radius ratio. We found that using a 224 × 224 RGB image resolution yielded an accuracy of 89.7%, using the squash (interpolation) resize mode.
For the LCM, we run GridSearchCV to find the optimal parameters for the lemon image classifiers that maximize accuracy. We chose the following classifiers: K-Nearest Neighbors, Support Vector Machine, Decision Tree, Gradient Boosting, MLP Classifier, and Random Forest. The last classifier achieved the highest CVScore and accuracy (0.8775 and 0.8658, respectively). Based on these results, we extracted the morphological features (including the one based on the peduncle position) from the lemon image in the dataset. We used them to train an RF classifier and obtain the LCM.
We proposed a queuing algorithm adaptable to different configurations of industrial fruit-sorting machines, given the matrix-based design we used. This algorithm enabled us to parallelize solenoid activation on a sequential device.
Among the improvements we can make to this system is incorporating algorithms to classify fruits not only by size but also by ripeness level or defects, creating a more robust solution. The latter would require the acquisition of a new image dataset captured using the proposed system. We can improve the PDM and LCM models by retraining them on an extended lemon dataset, thereby enhancing lemon feature detection. One potential improvement is using a more powerful LED lamp, which would allow the camera to capture images with greater detail than the current configuration. We can explore using cameras across different spectral bands (infrared and ultraviolet) to detect defects with greater certainty. We will also replicate the manufactured module so it can operate on at least four more lines. Finally, it should be possible to add a communication module via wired communication to change the parameters of the fruits, and a remote software interface to facilitate the sorting machine configuration. The comparison with the legacy vision system will be conducted in follow-up work.
For a cost comparison, we provide an estimate of the materials cost for the proposed vision system, which is around 1000 dollars (600 dollars without the case). However, we do not have an estimate of the cost of a machine vision system similar to the legacy system. We do have a quotation for a simple camera-processor system from Keyence, which was 4200.00 dollars in 2020, not including the electronics to interact with the actuators or a case. Furthermore, the latter kind of systems is closed, and adapting them to specific needs is not possible. After asking Google Gemini for information on the cost of industrial vision systems, we found that entry-level systems cost around $ 15,000 per lane.

Author Contributions

Conceptualization, Y.H.-M., M.A.N.-M. and J.H.B.-Z.; methodology, Y.H.-M., M.A.N.-M., S.P.-M. and Á.D.C.-C.; software, Y.H.-M., M.A.N.-M., S.P.-M. and Á.D.C.-C.; validation, Á.D.C.-C. and R.P.-G.; formal analysis, Y.H.-M., S.P.-M. and J.H.B.-Z.; investigation, Y.H.-M., M.A.N.-M., S.P.-M., Á.D.C.-C. and R.P.-G.; resources, Y.H.-M. and Á.D.C.-C.; data curation, M.A.N.-M., S.P.-M., Á.D.C.-C., R.P.-G. and J.H.B.-Z.; writing—original draft preparation, Y.H.-M., M.A.N.-M., S.P.-M. and J.H.B.-Z.; writing—review and editing, Y.H.-M., M.A.N.-M. and S.P.-M.; visualization, Á.D.C.-C., R.P.-G. and J.H.B.-Z.; supervision, S.P.-M. and R.P.-G.; project administration, Y.H.-M., M.A.N.-M., R.P.-G. and J.H.B.-Z. All authors have read and agreed to the published version of the manuscript.

Funding

This research received no external funding.

Data Availability Statement

The dataset used to train the machine learning models reported in this work are available at the following GitHub link: https://github.com/saidPolancoUPV/lemon-size-classification (accessed on 2 December 2025).

Acknowledgments

We thank to Citricos Las Hadas S. P. R. de R. L. for their support in the acquisition of the Dataset used in this manuscript.

Conflicts of Interest

The authors declare no conflicts of interest. The research was conducted without any commercial or financial relationships that could be interpreted as a potential conflict of interest.

References

  1. Lozano, E. Citrus Annual; Technical Report MX2023-0064; United States Department of Agriculture (USDA), Monterrey Agricultural Trade Office: Monterrey, Mexico, 2023.
  2. Secretaría de Desarrollo Rural, Pesca y Acuacultura. Tamaulipas Líder en Exportación de Limón Italiano. 2017. Available online: https://www.tamaulipas.gob.mx/desarrollorural/2017/04/tamaulipas-lider-en-exportacion-de-limon-italiano/ (accessed on 2 December 2025).
  3. Castillo-González, E.; De Medina-Salas, L.; Giraldi-Díaz, M.R.; Velásquez-De La Cruz, R.; Jiménez-Ochoa, J.R. Environmental Impacts Associated with the Production and Packing of Persian Lemon in Mexico through Life-Cycle Assessment. Clean Technol. 2024, 6, 551–571. [Google Scholar] [CrossRef]
  4. Peleg, K. Produce Handling, Packaging and Distribution; AVI Publishing Company: Westport, CT, USA, 1985. [Google Scholar]
  5. Zacarias, J.R.O.; Sullca-Mendoza, Y.C.; Valenzuela-Lino, Y.S.; Del Carpio-Ramirez, S.I.; Moggiano, N.; Coaquira-Rojo, C. Simulation of an Automated Tahitian lemon grading system based on computer vision. In Proceedings of the 2022 IEEE 13th Annual Ubiquitous Computing, Electronics & Mobile Communication Conference (UEMCON), New York, NY, USA, 26–29 October 2022; pp. 0283–0289. [Google Scholar] [CrossRef]
  6. Khojastehnazhand, M.; Omid, M.; Tabatabaeefar, A. Development of a lemon sorting system based on color and size. Afr. J. Plant Sci. 2010, 4, 122–127. [Google Scholar]
  7. Phate, V.R.; Malmathanraj, R.; Palanisamy, P. Classification and weighing of sweet lime (Citrus limetta) for packaging using computer vision system. J. Food Meas. Charact. 2019, 13, 1451–1468. [Google Scholar] [CrossRef]
  8. Hernández, A.; Ornelas-Rodríguez, F.J.; Hurtado-Ramos, J.B.; González-Barbosa, J.J. Accuracy Comparison Between Deep Learning Models for Mexican Lemon Classification. In Proceedings of the Telematics and Computing: 10th International Congress, WITCOM 2021, Virtual Event, 8–12 November 2021; Proceedings 10. Springer: Cham, Switzerland, 2021; pp. 62–73. [Google Scholar]
  9. Rawung, B.H.; Djamal, E.C.; Yuniarti, R. Classification of lemon fruit ripe using convolutional network. AIP Conf. Proc. 2023, 2714, 030029. [Google Scholar] [CrossRef]
  10. Granados-Vega, B.V.; Maldonado-Flores, C.; Gómez-Navarro, C.S.; Warren-Vega, W.M.; Campos-Rodríguez, A.; Romero-Cano, L.A. Development of a Low-Cost Artificial Vision System as an Alternative for the Automatic Classification of Persian Lemon: Prototype Test Simulation. Foods 2023, 12, 3829. [Google Scholar] [CrossRef] [PubMed]
  11. Jahanbakhshi, A.; Momeny, M.; Mahmoudi, M.; Zhang, Y.D. Classification of sour lemons based on apparent defects using stochastic pooling mechanism in deep convolutional neural networks. Sci. Hortic. 2020, 263, 109133. [Google Scholar] [CrossRef]
  12. Hanh, L.D.; Bao, D.N.T. Autonomous lemon grading system by using machine learning and traditional image processing. Int. J. Interact. Des. Manuf. (IJIDeM) 2023, 17, 445–452. [Google Scholar] [CrossRef]
  13. Zhu, Q.; Zhang, S. Research on lemon quality grading detection based on improved YOLOv5s. In Proceedings of the Third International Conference on Electronic Information Engineering, Big Data, and Computer Technology (EIBDCT 2024), Beijing, China, 26–28 January 2024; SPIE: Bellingham, WA, USA, 2024; Volume 13181, pp. 606–610. [Google Scholar]
  14. Sanyal, S.; Adhikary, R.; Choudhury, S.J. Revolutionizing lemon grading: An automated CNN-based approach for enhanced quality assessment. Int. J. Inf. Technol. 2024, 16, 4155–4166. [Google Scholar] [CrossRef]
  15. Arifin, K.N.; Rupa, S.A.; Anwar, M.M.; Jahan, I. Lemon and Orange Disease Classification using CNN-Extracted Features and Machine Learning Classifier. arXiv 2024, arXiv:2408.14206. [Google Scholar] [CrossRef]
  16. Mukhtar, T.; Jamil, S.; Arif, U.; Razzaq, W.; Wasif, M. Lemon Grading and Sorting Using Computer Vision. Eng. Proc. 2021, 12, 55. [Google Scholar]
  17. Chen, Y.; An, X.; Gao, S.; Li, S.; Kang, H. A deep learning-based vision system combining detection and tracking for fast on-line citrus sorting. Front. Plant Sci. 2021, 12, 622062. [Google Scholar] [CrossRef] [PubMed]
  18. Yu, Y.; An, X.; Lin, J.; Li, S.; Chen, Y. A vision system based on CNN-LSTM for robotic citrus sorting. Inf. Process. Agric. 2024, 11, 14–25. [Google Scholar] [CrossRef]
  19. Yılmaz, E.K.; Adem, K.; Kılıçarslan, S.; Aydın, H.A. Classification of lemon quality using hybrid model based on Stacked AutoEncoder and convolutional neural network. Eur. Food Res. Technol. 2023, 249, 1655–1667. [Google Scholar] [CrossRef]
  20. Janampa-Paitan, C.; Flores-Llallico, J.E.; Orellana-Garcia, B.F.; Baca, H.A.V. Automated Sorting System for Tahiti Lemons Using Raspberry PI. In Proceedings of the 2023 8th Asia-Pacific Conference on Intelligent Robot Systems (ACIRS), Xi’an, China, 7–9 July 2023; pp. 128–134. [Google Scholar]
  21. Chakraborty, S.K.; Subeesh, A.; Dubey, K.; Jat, D.; Chandel, N.S.; Potdar, R.; Rao, N.G.; Kumar, D. Development of an optimally designed real-time automatic citrus fruit grading–sorting machine leveraging computer vision-based adaptive deep learning model. Eng. Appl. Artif. Intell. 2023, 120, 105826. [Google Scholar] [CrossRef]
  22. Hymel, S.; Banbury, C.; Situnayake, D.; Elium, A.; Ward, C.; Kelcey, M.; Baaijens, M.; Majchrzycki, M.; Plunkett, J.; Tischler, D.; et al. Edge impulse: An mlops platform for tiny machine learning. arXiv 2022, arXiv:2212.03332. [Google Scholar]
  23. Banbury, C.; Jongboom, J.; Situnayake, D.; Moreau, L.; Reddi, V.J. FOMO: Faster Objects, More Objects—Real-Time Object Detection on Constrained Devices. arXiv 2022, arXiv:2204.03635. [Google Scholar]
  24. Chawla, N.V.; Bowyer, K.W.; Hall, L.O.; Kegelmeyer, W.P. SMOTE: Synthetic Minority Over-sampling Technique. J. Artif. Intell. Res. 2002, 16, 321–357. [Google Scholar] [CrossRef]
  25. Pedregosa, F.; Varoquaux, G.; Gramfort, A.; Michel, V.; Thirion, B.; Grisel, O.; Blondel, M.; Prettenhofer, P.; Weiss, R.; Dubourg, V.; et al. Scikit-learn: Machine Learning in Python. J. Artif. Intell. Res. 2011, 12, 2825–2830. [Google Scholar]
Figure 1. Main components of the industrial sorting machine. (a) Solenoid attached to ejector mechanism. (b) Conveyor rollers. (c) Photoelectric pulse sensor. (d) PC remote hosting the image acquisition and processing boards. (e) Power, communication and solenoid drivers boards. (f) Detailed view of the communication and solenoid driver boards.
Figure 1. Main components of the industrial sorting machine. (a) Solenoid attached to ejector mechanism. (b) Conveyor rollers. (c) Photoelectric pulse sensor. (d) PC remote hosting the image acquisition and processing boards. (e) Power, communication and solenoid drivers boards. (f) Detailed view of the communication and solenoid driver boards.
Processes 14 00164 g001
Figure 2. Block diagram of the proposed vision system coupled to the industrial citrus sorting machine.
Figure 2. Block diagram of the proposed vision system coupled to the industrial citrus sorting machine.
Processes 14 00164 g002
Figure 3. Mechanical lemon-size caliper.
Figure 3. Mechanical lemon-size caliper.
Processes 14 00164 g003
Figure 4. Selected images of the lemon dataset, including industry-standardized sizes. Sample lemon images of 75-size (a,b). Sample lemon images of 95-size (c,d). Sample lemon images of 115-size (e,f). Sample lemon images of 140-size (g,h). Sample lemon images of 165-size (i,j). Sample lemon images of 200-size (k,l).
Figure 4. Selected images of the lemon dataset, including industry-standardized sizes. Sample lemon images of 75-size (a,b). Sample lemon images of 95-size (c,d). Sample lemon images of 115-size (e,f). Sample lemon images of 140-size (g,h). Sample lemon images of 165-size (i,j). Sample lemon images of 200-size (k,l).
Processes 14 00164 g004
Figure 5. Selected peduncle labeled images of the dataset used to train the FOMO model for peduncle detection. (af) Selected images from the training dataset with manually labeled peduncles, framed with a red rectangles.
Figure 5. Selected peduncle labeled images of the dataset used to train the FOMO model for peduncle detection. (af) Selected images from the training dataset with manually labeled peduncles, framed with a red rectangles.
Processes 14 00164 g005
Figure 6. Thresholding algorithm to obtain the binary image for feature extraction. L represents the luminance channel, and a and b are the chromatic channels of the CIELab color space. Channels a and b contain color information for segmenting green and yellow fruits. The red contour outlines the segmented mask for each channel. We processed both channels to obtain an OR version of the binary images produced by Otsu’s algorithm. The algorithm uses this binary image to extract the classification features. The top right binary image contains a red and blue line indicating the major and minor axes, respectively, and the green contour outlines the pixels considered to compute the area and perimeter. The small red rectangle in the bottom right image indicates the position of the detected peduncle used to compute the distance (blue line) to the centroid (a small red dot near the center of the image) of the binary images.
Figure 6. Thresholding algorithm to obtain the binary image for feature extraction. L represents the luminance channel, and a and b are the chromatic channels of the CIELab color space. Channels a and b contain color information for segmenting green and yellow fruits. The red contour outlines the segmented mask for each channel. We processed both channels to obtain an OR version of the binary images produced by Otsu’s algorithm. The algorithm uses this binary image to extract the classification features. The top right binary image contains a red and blue line indicating the major and minor axes, respectively, and the green contour outlines the pixels considered to compute the area and perimeter. The small red rectangle in the bottom right image indicates the position of the detected peduncle used to compute the distance (blue line) to the centroid (a small red dot near the center of the image) of the binary images.
Processes 14 00164 g006
Figure 7. Flow diagram of the queuing algorithm.
Figure 7. Flow diagram of the queuing algorithm.
Processes 14 00164 g007
Figure 8. Depiction of how the queuing algorithm works for a hypothetical case. The red color indicates the last processed pulse in the illustrated pulse sequence, and we also used it in the queuing matrix to identify the current lemon classification data received from the SBC. The blue color indicates changes in the previously queued data. (a) Initial state of the queuing matrix. (bi) is the state of the queuing matrix after receiving pulses from 1 to 8, respectively.
Figure 8. Depiction of how the queuing algorithm works for a hypothetical case. The red color indicates the last processed pulse in the illustrated pulse sequence, and we also used it in the queuing matrix to identify the current lemon classification data received from the SBC. The blue color indicates changes in the previously queued data. (a) Initial state of the queuing matrix. (bi) is the state of the queuing matrix after receiving pulses from 1 to 8, respectively.
Processes 14 00164 g008
Figure 9. Thresholding field test with image-capture hardware on the citrus sorting machine. Comparison of the thresholding of the channels of an image of a lemon acquired in RGB in different color spaces. Conversion of the image to HSV (a), YUV (c), XYZ (e), and CIELab (g) spaces. Thresholding using Otsu’s algorithm for HSV (b), YUV (d), XYZ (f), and CIELab (h) spaces. The a and b channels of the CIELab color space facilitate thresholding of the citrus silhouette, reducing post-processing requirements.
Figure 9. Thresholding field test with image-capture hardware on the citrus sorting machine. Comparison of the thresholding of the channels of an image of a lemon acquired in RGB in different color spaces. Conversion of the image to HSV (a), YUV (c), XYZ (e), and CIELab (g) spaces. Thresholding using Otsu’s algorithm for HSV (b), YUV (d), XYZ (f), and CIELab (h) spaces. The a and b channels of the CIELab color space facilitate thresholding of the citrus silhouette, reducing post-processing requirements.
Processes 14 00164 g009
Figure 10. Confusion matrices of the evaluated classifiers. (a) K-Nearest Neighbors (KNN). (b) Support Vector Machine (SVM). (c) Decision Tree. (d) Gradient Boosting. (e) MLP Classifier. (f) Random Forest.
Figure 10. Confusion matrices of the evaluated classifiers. (a) K-Nearest Neighbors (KNN). (b) Support Vector Machine (SVM). (c) Decision Tree. (d) Gradient Boosting. (e) MLP Classifier. (f) Random Forest.
Processes 14 00164 g010
Figure 11. Time elapsed between the reception of a pulse generated by the photoelectric sensor (yellow line) and the generation of the solenoid activation pulse by the microcontroller (blue line). In this case, Δ x represents the elapsed time (72 ms). Circle a (colored in cyan) indicates the time the system received the sensor pulse. Circle b (colored in cyan) indicates the time the system sent the activation pulse to the corresponding ejector solenoid.
Figure 11. Time elapsed between the reception of a pulse generated by the photoelectric sensor (yellow line) and the generation of the solenoid activation pulse by the microcontroller (blue line). In this case, Δ x represents the elapsed time (72 ms). Circle a (colored in cyan) indicates the time the system received the sensor pulse. Circle b (colored in cyan) indicates the time the system sent the activation pulse to the corresponding ejector solenoid.
Processes 14 00164 g011
Figure 12. On-field settings of the developed machine vision system coupled to the legacy industrial sorting machine. (a) prototype system placed over a lane of the lemon sorting machine (b) lemon samples used in the classification test.
Figure 12. On-field settings of the developed machine vision system coupled to the legacy industrial sorting machine. (a) prototype system placed over a lane of the lemon sorting machine (b) lemon samples used in the classification test.
Processes 14 00164 g012
Figure 13. Confusion matrix of the on-field test.
Figure 13. Confusion matrix of the on-field test.
Processes 14 00164 g013
Table 1. Lemon image dataset.
Table 1. Lemon image dataset.
Lemon Size Class LabelNumber of Images
75545
95535
115361
140624
165519
200543
Total3127
Table 2. Classification results for different percentages of samples (0, 20, and 50) added to the minority class by the SMOTE algorithm. We obtained accuracy results for random forest (RF), K-nearest neighbors (KNN), support vector machines (SVM), gradient boosting (GB), decision trees (DT), and multilayer perceptron (MLP) classifiers.
Table 2. Classification results for different percentages of samples (0, 20, and 50) added to the minority class by the SMOTE algorithm. We obtained accuracy results for random forest (RF), K-nearest neighbors (KNN), support vector machines (SVM), gradient boosting (GB), decision trees (DT), and multilayer perceptron (MLP) classifiers.
ClassifierSMOTE %Accuracy Without Penduncle over Major Axis RatioAccuracy with Penduncle over Major Axis Ratio
RF00.86420.8706
200.86100.8722
500.86100.8722
KNN00.84020.8382
200.84980.8530
500.84980.8530
SVM00.85620.8530
200.86100.8530
500.86100.8530
DT00.85140.8466
200.85630.8544
500.85630.8544
GB00.85620.8594
200.86460.8695
500.86460.8695
MLP00.85940.8546
200.86910.8699
500.86910.8699
Table 3. Comparison of FOMO models to detect lemon peduncles.
Table 3. Comparison of FOMO models to detect lemon peduncles.
ModelImg_
Width
Img_
Height
Resize_
Mode
Col_dep32-Bit Accuracies8-Bit Accuracies
ValidationTestValidationTest
1224224fit-longGrayscale88.6%61.8%87.2%63.2%
29696squashGrayscale89.1%78.8%86.9%78.8%
39696fit-shortRGB87.8%75.0%86.7%75.0%
4224224squashRGB89.7%73.8%88.0%70.0%
Table 4. Used classifiers and the best parameters found by the GridSearchCV algorithm.
Table 4. Used classifiers and the best parameters found by the GridSearchCV algorithm.
ClassifierBest ParametersSMOTE %Improves with Penduncle-Based AttributeCV ScoreAccuracy
RF‘max_depth’: 10, ‘max_features’: ‘sqrt’, ‘min_samples_leaf’: 2, ‘n_estimators’: 20050%YES0.87780.8722
KNNn_neighbors: 9, p: 1, weights: ‘distance’50%YES0.84980.8530
SVM‘C’: 5.0, ‘gamma’: ‘auto’, ‘kernel’: ‘rbf’50%NO0.86460.8610
DT‘criterion’: ‘gini’, ‘max_depth’: None, ‘min_samples_leaf’: 1650%NO0.85440.8563
GB‘learning_rate’: 0.1, ‘max_depth’: 5, ‘n_estimators’: 100, ‘subsample’: 0.850%NO0.87110.8646
MLP‘activation’: ‘relu’, ‘alpha’: 0.01, ‘hidden_layer_sizes’: (50, 50, 50, 50, 50, 50, 50), ‘learning_rate’: ‘constant’, ‘solver’: ‘adam’50%YES0.86840.8699
Table 5. Detailed classification results for the trained classifiers, measuring per-class and micro, macro, and weighted average, precision, recall, and F1-score.
Table 5. Detailed classification results for the trained classifiers, measuring per-class and micro, macro, and weighted average, precision, recall, and F1-score.
ClassifierClassPrecisionRecallF1-Score
KNN750.9339620.9082570.920930
950.8640780.8317760.847619
1150.7386360.9027780.812500
1400.8750000.8400000.857143
1650.8452380.6826920.755319
2000.8240000.9449540.880342
macro avg0.8468190.8517430.845642
weighted avg0.8538910.8498400.848610
micro avg0.8498400.8498400.849840
SVM750.9444440.9357800.940092
950.9166670.8224300.866995
1150.7333330.9166670.814815
1400.8842980.8560000.869919
1650.8235290.6730770.740741
2000.8015870.9266060.859574
macro avg0.8506430.8550930.848689
weighted avg0.8584430.8530350.852038
micro avg0.8530350.8530350.853035
Decision Tree750.9333330.8990830.915888
950.8557690.8317760.843602
1150.7529410.8888890.815287
1400.8661420.8800000.873016
1650.8571430.6346150.729282
2000.8046880.9449540.869198
macro avg0.8450030.8465530.841045
weighted avg0.8508530.8466450.844270
micro avg0.8466450.8466450.846645
Gradient Boosting750.9523810.9174310.934579
950.8653850.8411210.853081
1150.7325580.8750000.797468
1400.8760330.8480000.861789
1650.8607590.6538460.743169
2000.8015270.9633030.875000
macro avg0.8481070.8497840.844181
weighted avg0.8554940.8498400.848170
micro avg0.8498400.8498400.849840
MLP750.9687500.8532110.907317
950.8363640.8598130.847926
1150.7204300.9305560.812121
1400.8750000.8400000.857143
1650.8354430.6346150.721311
2000.7968750.9357800.860759
macro avg0.8388100.8423290.834430
weighted avg0.8467670.8386580.837189
micro avg0.8386580.8386580.838658
Random Forest750.9444440.9357800.940092
950.9090910.8411210.873786
1150.7674420.9166670.835443
1400.8991600.8560000.877049
1650.8571430.6923080.765957
2000.8076920.9633030.878661
macro avg0.8641620.8675300.861832
weighted avg0.8706860.8658150.864508
micro avg0.8658140.8658140.865814
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content.

Share and Cite

MDPI and ACS Style

Hernández-Mier, Y.; Nuño-Maganda, M.A.; Polanco-Martagón, S.; Cantú-Castro, Á.D.; Posada-Gómez, R.; Barrón-Zambrano, J.H. Machine Learning and Queuing Algorithm Integration for Real-Time Citrus Size Classification on an Industrial Sorting Machine. Processes 2026, 14, 164. https://doi.org/10.3390/pr14010164

AMA Style

Hernández-Mier Y, Nuño-Maganda MA, Polanco-Martagón S, Cantú-Castro ÁD, Posada-Gómez R, Barrón-Zambrano JH. Machine Learning and Queuing Algorithm Integration for Real-Time Citrus Size Classification on an Industrial Sorting Machine. Processes. 2026; 14(1):164. https://doi.org/10.3390/pr14010164

Chicago/Turabian Style

Hernández-Mier, Yahir, Marco Aurelio Nuño-Maganda, Said Polanco-Martagón, Ángel Dagoberto Cantú-Castro, Rubén Posada-Gómez, and José Hugo Barrón-Zambrano. 2026. "Machine Learning and Queuing Algorithm Integration for Real-Time Citrus Size Classification on an Industrial Sorting Machine" Processes 14, no. 1: 164. https://doi.org/10.3390/pr14010164

APA Style

Hernández-Mier, Y., Nuño-Maganda, M. A., Polanco-Martagón, S., Cantú-Castro, Á. D., Posada-Gómez, R., & Barrón-Zambrano, J. H. (2026). Machine Learning and Queuing Algorithm Integration for Real-Time Citrus Size Classification on an Industrial Sorting Machine. Processes, 14(1), 164. https://doi.org/10.3390/pr14010164

Note that from the first issue of 2016, this journal uses article numbers instead of page numbers. See further details here.

Article Metrics

Back to TopTop