**1. Introduction**

Today, labour on farms and orchards relies heavily on manual labour by skilled farmers, which can lead to increased time and production costs. Smart farming has become a popular concept with the development of precision farming and information technology [1]. China is a major apple-producing country globally, and apple sorting has a high economic application value [2]. With increased economic development, people have higher requirements for fruit quality [3,4]. As a critical element in improving apple quality and liberating orchard labour, apple grading technology is of great significance in increasing the added value of products, improving market competitiveness, and alleviating labour shortages in orchards. Therefore, a high precision and speed grading method is needed for the effective and objective grading of apples.

In the research of fruit grading based on traditional machine learning, Abdullah et al. [5] detected the quality features of poppy peaches by machine learning, the features considered mainly included fruit surface color and fruit shape, and developed automatic machine vision detection software to detect the ripeness grade of poppy peaches by linear discriminant analysis and multilayer neural network. Marchant et al. [6] studied the method of automatic potato detection and grading based on a computer vision system. Moallem et al. [7]

**Citation:** Xu, B.; Cui, X.; Ji, W.; Yuan, H.; Wang, J. Apple Grading Method Design and Implementation for Automatic Grader Based on Improved YOLOv5. *Agriculture* **2023**, *13*, 124. https://doi.org/10.3390/ agriculture13010124

Academic Editor: Xiuliang Jin

Received: 22 December 2022 Revised: 29 December 2022 Accepted: 30 December 2022 Published: 2 January 2023

**Copyright:** © 2023 by the authors. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https:// creativecommons.org/licenses/by/ 4.0/).

proposed a computer vision-based grading algorithm for golden crown apples where texture and geometric features were extracted from the defective areas. Finally, a support vector machine (SVM), a multilayer perception (Muti-Layer Perception), and a K-Nearest Neighbor classifier were used to classify the apples into first-class, second-class, and out-ofclass fruits. Gui et al. [8] proposed a wavelet rectangle-based apple classification method based on apple shape, which classified apples into normal fruit shape, mild deformity, and severe deformity with a classification accuracy of 86.2%, 85.8%, and 90.8%, respectively. In the above machine learning classification methods, preprocessing of images is often required, and the classification relies on single features, which has the problems of poor real-time performance and low robustness.

In the research of fruit grading based on deep learning, Fan et al. [9] used a convolutional neural network (CNN) architecture for apple quality recognition, trained a convolutional neural network, and achieved an accuracy of 96.5% in the test set, designed classification software for CNN-based convolutional neural networks, and used a computer vision module to sort at a rate of 5/s on a four-threaded fruit sorter. The classification accuracy reached 92%. However, the model was large, and the computational efficiency was relatively low. Raikar et al. [10] studied the quality grade of okra and used three deep learning models, AlexNet, GoogLeNet, and ResNet50, to classify okra into four types based on length: small, medium, large and extra large, where the accuracy of the ResNet deep learning model reached over 99%. Luna et al. [11] proposed a deep learning-based method for single tomato defect area detection, implemented through the OpenCV library and Python programming. He collected 1200 tomato images of different qualities using an image capture box and used the images for training VGG16, InceptionV3, and ResNet50 deep learning models, respectively, compared the experimental results and found that VGG16 was the best deep learning model for defect recognition. However, there are still problems of insufficient model optimization and poor real-time performance in the above deep learning model grading methods.

In terms of research on automatic fruit grading equipment, Cubero et al. [12] designed a computer vision-based automated citrus sorting device. The sorting device was deployed on a mobile platform, and the low-power industrial camera image acquisition and powerful lighting system enabled the device to work better in the field. Experiments showed that the sorting device could achieve a sorting speed of up to eight per second. Baigvand et al. [13] developed a machine learning-based fig sorting system, which first uses a feeder and a belt. The figs were first transported under a CCD camera by a feeder and belt conveyor. The figs were classified into five categories by extracting fig characteristics from the pictures taken by the CCD camera, including size, colour, segmentation size and fig centre position, etc. The experiments verified that the grading system was 95% accurate in recognizing the five categories of figs. However, the designed automatic fruit grader tends to be large and more suitable for large assembly line working modes and is not suitable for the needs of small and medium-sized farmers for detection and grading.

Although the above methods have achieved specific results in terms of fruit feature detection and equipment implementation, there are still problems, such as insufficient model optimization and equipment implementation. Based on this, this paper takes red Fuji apples as the research object. It provides an in-depth discussion on the grading detection of apple features and the implementation of automatic apple sorting equipment. An apple grading algorithm based on the improved YOLOv5 is proposed, using the Mish activation function instead of the original Relu activation function to improve the model generalization ability. A loss function (DIou\_Loss) is introduced to speed up the rate of edge regression and improve localization accuracy. The attention mechanism squeeze excitation (SE) module is embedded into the backbone feature network to improve the feature extraction ability of the model. Experimental results show that the improved method can improve the model detection without increasing the model training cost. Finally, the automatic apple grader designed based on this paper was experimentally validated, and some conclusions were obtained.
