# Fast Recognition Method for Multiple Apple Targets in Complex Occlusion Environment Based on Improved YOLOv5

Authors: Qian Hao; Xin Guo; Feng Yang
Journal: Journal of Sensors
 (2023)

---

## Abstract

The mechanization and intelligentization of the production process are the main trends in research and development of agricultural products. The realization of an unmanned and automated picking process is also one of the main research hotspots in China’s agricultural product engineering technology field in recent years. The development of automated apple-picking robot is directly related to imaging research, and its key technology is to use algorithms to realize apple identification and positioning. Aiming at the problem of false detection and missed detection of densely occluded targets and small targets by apple picking robots under different lighting conditions, two different apple recognition algorithms are selected based on the apple shape features to study the traditional machine learning algorithm: histogram of oriented gradients + support vector machine (HOG + SVM) and a fast recognition method for multiple apple targets in a complex occlusion environment based on improved You-Only-Look-Once-v5 (YOLOv5). The first is the improvement of the CSP structure in the network. Using parameter reconstruction, the convolutional layer (Conv) and the batch normalization (BN) layer in the CBL (Conv + BN + Leaky_relu activation function) module are fused into a batch-normalized convolutional layer Conv_B. Subsequently, the CA (coordinate attention) mechanism module is embedded into different network layers in the improved designed backbone network to enhance the expressive ability of the features in the backbone network to better extract the features of different apple targets. Finally, for some targets with overlapping occlusions, the loss function is fine-tuned to improve the model’s ability to recognize occluded targets. By comparing the recognition effects of HOG + SVM, Faster RCNN, YOLOv6, and baseline YOLOv5 on the test set under complex occlusion scenarios, theF1 value of this method was increased by 13.47%, 6.01%, 1.26%, and 3.63%, respectively, and the F1 value of this method was increased by 19.36%, 13.07%, 1.61%, and 4.27%, respectively, under different illumination angles. The average image recognition time was 0.27 s faster than that of HOG + SVM, 0.229 s faster than that of Faster RCNN, and 0.006 s faster than that of YOLOv6. The method is expected to provide a theoretical basis for apple-picking robots to choose a pertinent image recognition algorithm during operation.


---

## Body

## 1. Introduction

As a large apple producing country, China’s apple production accounts for half of the global production. With population mobility and changes in land policies, the mechanization and intelligence of agricultural production are the main trends in future development [1]. Until now, the domestic apple-picking method was based on manual picking. However, mature apples are large in quantity and require much human labor and financial resources. Realize the intelligent and unmanned apple picking, thus freeing workers from repetitive manual labor [2]. Accurate and fast identification of apple targets is the premise of automatic picking. However, in the orchard environment, there are a lot of sticky, bagged, dense, occluded, and overlapping apples in long-distance pictures taken under different lighting conditions. Achieving rapid detection of multiple apple targets in a complex occlusion environment is crucial to the realization of intelligent apple picking. In the process of mechanization and intelligentization of production equipment, scholars in China and abroad have done much research on automated picking robots. Williams et al. developed a picking robot with four manipulators and a neural network-based method regarding fruit detection and picking of kiwi fruit cultivated in a pergola [3]. In the United States, an apple-picking robot was developed in the form of a “vacuum cleaner,” which sucks ripe fruits from fruit trees [4]. The fruit picking is achieved by the vacuum suction system, and the picking speed can reach up to 1 s/piece. There are also related research results in image recognition algorithms. Huang et al. proposed a multiscale feature fusion convolutional neural network for indoor small target detection. Using image enhancement technology to set up and amplify a date set, the Faster RCNN, YOLOv5, SSD, and SSD target detection models based on multiscale feature fusion were trained on an indoor scene data set based on transfer learning [5]. Lin and Li proposed an integrated circuit board (ICB) object detection and image augmentation fusion model based on YOLO. First, collect and use different types of ICBs as model training data sets and establish a preliminary image recognition model that can classify and predict different types of ICBs based on different feature points. Finally, there is a discussion of the applicability of the model to detect and recognize the ICB directionality in <1 s with a 98% accuracy rate to meet the real-time requirements of smart manufacturing [6]. Gan et al. use thermal imaging to address color similarity between immature citrus and leaves. Using the temperature difference between the fruit and the leaf surface, a tracking fruit counting algorithm is established to count the fruit in the thermal video [7]. Lei et al. used an RGB camera to collect bayberry images and established an adaptive image equalization model. The watershed transformation algorithm and convex hull theory were used to precisely localize the bayberry area against a complex background [8]. In deep learning, Yang et al. proposed an improved You-Only-Look-Once-v5 (YOLOv5) algorithm to detect the growth status of flowers at different flowering stages. The experimental results show that the accuracy of the improved algorithm is 5.4% higher than that of YOLOv5, which proves the effectiveness of the algorithm [9]. Wang et al. proposed a long-close distance-coordinated control strategy for a litchi picking robot. A long distance uses the YOLOv5 target detection network and the DBSCAN point cloud clustering method and uses the Mask RCNN instance segmentation method to segment the more distinctive bifurcated stems in the field of view. Through the processing of segmentation masks, a “Point + Line” dual reference model is proposed to guide robotic pick-up. Through the experiment, the success rate in the positioning of fruit branches reaches 88.46% [10]. Wu et al. proposed rachis detection and three-dimensional localization of cut-off point for vision-based banana robot. By building a new YOLOv5-B model and improving the loss function to improve the accuracy and speed, the contour of the rachis is segmented using an edge detection algorithm, and the optimal cut-off point is obtained as a scalar. Experiments show that the multitarget recognition rate of the YOLOv5-B model for bananas is 93.2%, and the average image processing time is only 0.009 s/piece, which can meet the robot’s requirements for rachis segmentation [11]. Most of the existing apple detection algorithms cannot distinguish between the apples that are occluded by tree branches and occluded by other apples. Yan et al. proposed a lightweight apple target detection method based on improved YOLOv5s. BottleneckCSP module was improved designed to BottleneckCSP-2 module. SE module was inserted to the proposed improved backbone network. Finally, the initial anchor box size of the original network was improved [12]. Ji et al. proposed an apple object detection method based on Shufflenetv2-YOLOX to quickly and accurately detect and locate apples in the orchard natural environment. Using the lightweight network Shufflenetv2 added with the convolutional block attention module (CBAM) as the backbone, an adaptive spatial feature fusion (ASFF) module is added to the PANet to improve the detection accuracy. The trained network AP value of Shufflenetv2YOLOX is increased by 6.24%, and the detection speed is increased by 18% [13]. Yang et al. proposed a fast recognition method for multiple apple targets in dense scenes. The method drew on the idea of “point is the target” and realized the rapid identification of apple targets by predicting the center point of apple and the width and height of apple. By improving the CenterNet network, the Tiny Hourglass-24 lightweight backbone network was designed, and the residual module was optimized to improve the target recognition speed [14].In summary, scholars in China and abroad have conducted in-depth research on the problem of fruit identification with numerous research results. Apples grow in complex agricultural environments where there are many uncertainty factors. How to select an algorithm model that is not affected by the complex environment of the orchard and train an algorithm model with high recognition accuracy and strong robustness is the research focus and difficulty in the field of fruit detection. Here, an apple detection algorithm is studied from two aspects. First, using shape features, apple recognition and localization are examined based on the HOG + SVM algorithm. Second, aiming at the problems of false detection and missed detection of sticking, bagging, dense, occluded, and overlapping apples under different lighting conditions, an improved deep learning algorithm YOLOv5 was proposed. In the fusion of convolutional layer (Conv) and batch normalization (BN) layer in CSP1_X structure, the CA mechanism module is embedded into the backbone network of the improved design to enhance the expressive ability of the features in the mobile network. Finally, for some occluded and overlapping targets, the IOU in nms is changed to DIOU_nms, which greatly improves the model’s ability to recognize occluded targets. In comparison with the target detection methods using a sliding window, the proposed structure is more efficient, causes less false detection of the background, and is easier to achieve in real time. The experiments indicate that the recognition rate and operating efficiency of the developed algorithm are improved. The process steps of this study are shown in Figures1(a) and 1(b).Figure 1
Diagram of two algorithms and key steps. (a) Flow chart of fruit identification and positioning based on HOG + SVM algorithm. (b) Flow chart of fruit identification and positioning based on YOLOv5 algorithm.
(a)(b)


## 2. Methods

The color features of images are easily affected by light, which causes color feature-based image recognition algorithms to make errors when detecting apples. In this section, we consider the apple shape as the key feature.


### 2.1. Image Normalization

During the testing process of the apples, there are obvious differences in exposure because of the influence of weather. This results in occluded views and increased background interference, making fruit detection more difficult. To solve this problem, the industry has adopted gamma correction [15]. This method can reduce the influence of shadows in some areas of the picture as much as possible, to meet the requirements. In general, there are two types of processing methods, i.e., square root and logarithmic. The square root method is used in this study, and the expression is provided in
(1)Ix,y=Ix,yγ,where I represents the image, x,y represents the pixel point of the image, and γ generally takes on the value of 1/2.


### 2.2. Apple Shape Feature Extraction Based on HOG

HOG is an algorithm for summarizing features using histograms of oriented gradients. The principle is to express the gradient information of pixels or blocks of the picture and then calculate the feature information of the picture [16]. HOG is a shape feature operator, which is used to describe shape features such as image contours or edges. The basic idea of this operator is that the density of the gradient direction can describe the contour or edge of the object. The process of describing the shape of the object and generating the feature vector by the HOG operator is shown in Figure 2.Figure 2
Schematic diagram of process for feature vector generation.The first step is to normalize the image and perform the gamma correction operation to improve the robustness of the image to illumination changes while reducing the interference of noise. The next step is to solve the gradient information in the image, including the gradient of the image in thex and y directions. This is expressed in Equation (2) as follows:
(2)Gxx,y=Hx+1,y−Hx−1,y,(3)Gyx,y=Hx,y+1−Hx,y−1.In Equations (2) and (3), Gxx,yis the gradient in the x direction, and Gyx,y is the gradient in the y direction at pixel coordinates x,y. Hx,y is the pixel value. The gradient direction and magnitude at this pixel point are obtained by calculating the gradient in each direction:
(4)Gx,y=Gxx,y2+Gyx,y2,Gx,y=arctanGyx,yGxx,y.The next step divides the image into a series of small cell units. Histograms are obtained for the direction and magnitude of gradients in cell units. Then, adjacent cell units are combined into blocks, i.e., statistical histograms and normalization. Next, the gradient information of each block is distributed into the direction histogram according to the gradient direction. The value range of the direction of the histogram can be between 0 and 180 degrees or 0 and 360 degrees. The specific range depends on whether the calculated gradient is positive or negative. Finally, by normalizing the block histogram, the local contrast adjustment is obtained to reduce the influence of illumination. Then, the histograms of all blocks in the image are concatenated to obtain the image histogram, i.e., the HOG feature vector of the image. According to the flowchart and aforementioned steps, after feature extraction is performed for apple and nonapple objects, the features are visualized, as shown in Figure3.Figure 3
HOG feature visualization of target image.
(a)(b)(c)(d)


### 2.3. Target Image HOG Feature Visualization

SVM is responsible for training and classifying the feature vectors extracted by HOG. The specific process is that after inputting the image to be detected, the SVM classifier performs a correlation operation on each feature in the image to obtain the category information of the features. The loss function and corresponding label are then used to calculate the parameters, and the descent method is iteratively applied to update the parameters until an optimal value is reached. The key to fruit identification and localization in the candidate area lies in the change range of the sliding window and step sizes during the traversal process. If the variation range is too small, the amount of computation increases; if the variation range is too large, the recognition and positioning accuracy may be reduced.The principle of the SVM classifier is to find a hyperplane that separates the 2-class samples at the largest interval. We consider the setD=xi,yi, where xi is the ith eigenvector, and where yi represents the class label corresponding to vector xi [17]. Positive samples in the dataset are marked as +1, and negative samples are marked as -1. The set D contains two different types of data. As shown in Figure 4, a solid line is drawn to separate all points belonging to one class from those belonging to another class. Then, a hyperplane in which two-dashed lines are parallel to the solid line is drawn with a distribution symmetrical about the solid line as the center, passing through the sample points closest to the optimal hyperplane in the two types of data.Figure 4
SVM segmentation diagram.The hyperplane expression is shown in(5)wx+b=0,where x is a point in the set xi,yi and w is a normal vector of the linear function. The two-dashed lines are parallel to the solid line and symmetrically distributed around it. The expressions of the two dotted lines are given by
(6)xi∙w+b=1,(7)xi∙w+b=−1.The maximum distance between the two types of data is the distance between the two-dashed lines, which is2/w. When u is smaller, the spacing is larger.


## 2.1. Image Normalization

During the testing process of the apples, there are obvious differences in exposure because of the influence of weather. This results in occluded views and increased background interference, making fruit detection more difficult. To solve this problem, the industry has adopted gamma correction [15]. This method can reduce the influence of shadows in some areas of the picture as much as possible, to meet the requirements. In general, there are two types of processing methods, i.e., square root and logarithmic. The square root method is used in this study, and the expression is provided in
(1)Ix,y=Ix,yγ,where I represents the image, x,y represents the pixel point of the image, and γ generally takes on the value of 1/2.


## 2.2. Apple Shape Feature Extraction Based on HOG

HOG is an algorithm for summarizing features using histograms of oriented gradients. The principle is to express the gradient information of pixels or blocks of the picture and then calculate the feature information of the picture [16]. HOG is a shape feature operator, which is used to describe shape features such as image contours or edges. The basic idea of this operator is that the density of the gradient direction can describe the contour or edge of the object. The process of describing the shape of the object and generating the feature vector by the HOG operator is shown in Figure 2.Figure 2
Schematic diagram of process for feature vector generation.The first step is to normalize the image and perform the gamma correction operation to improve the robustness of the image to illumination changes while reducing the interference of noise. The next step is to solve the gradient information in the image, including the gradient of the image in thex and y directions. This is expressed in Equation (2) as follows:
(2)Gxx,y=Hx+1,y−Hx−1,y,(3)Gyx,y=Hx,y+1−Hx,y−1.In Equations (2) and (3), Gxx,yis the gradient in the x direction, and Gyx,y is the gradient in the y direction at pixel coordinates x,y. Hx,y is the pixel value. The gradient direction and magnitude at this pixel point are obtained by calculating the gradient in each direction:
(4)Gx,y=Gxx,y2+Gyx,y2,Gx,y=arctanGyx,yGxx,y.The next step divides the image into a series of small cell units. Histograms are obtained for the direction and magnitude of gradients in cell units. Then, adjacent cell units are combined into blocks, i.e., statistical histograms and normalization. Next, the gradient information of each block is distributed into the direction histogram according to the gradient direction. The value range of the direction of the histogram can be between 0 and 180 degrees or 0 and 360 degrees. The specific range depends on whether the calculated gradient is positive or negative. Finally, by normalizing the block histogram, the local contrast adjustment is obtained to reduce the influence of illumination. Then, the histograms of all blocks in the image are concatenated to obtain the image histogram, i.e., the HOG feature vector of the image. According to the flowchart and aforementioned steps, after feature extraction is performed for apple and nonapple objects, the features are visualized, as shown in Figure3.Figure 3
HOG feature visualization of target image.
(a)(b)(c)(d)


## 2.3. Target Image HOG Feature Visualization

SVM is responsible for training and classifying the feature vectors extracted by HOG. The specific process is that after inputting the image to be detected, the SVM classifier performs a correlation operation on each feature in the image to obtain the category information of the features. The loss function and corresponding label are then used to calculate the parameters, and the descent method is iteratively applied to update the parameters until an optimal value is reached. The key to fruit identification and localization in the candidate area lies in the change range of the sliding window and step sizes during the traversal process. If the variation range is too small, the amount of computation increases; if the variation range is too large, the recognition and positioning accuracy may be reduced.The principle of the SVM classifier is to find a hyperplane that separates the 2-class samples at the largest interval. We consider the setD=xi,yi, where xi is the ith eigenvector, and where yi represents the class label corresponding to vector xi [17]. Positive samples in the dataset are marked as +1, and negative samples are marked as -1. The set D contains two different types of data. As shown in Figure 4, a solid line is drawn to separate all points belonging to one class from those belonging to another class. Then, a hyperplane in which two-dashed lines are parallel to the solid line is drawn with a distribution symmetrical about the solid line as the center, passing through the sample points closest to the optimal hyperplane in the two types of data.Figure 4
SVM segmentation diagram.The hyperplane expression is shown in(5)wx+b=0,where x is a point in the set xi,yi and w is a normal vector of the linear function. The two-dashed lines are parallel to the solid line and symmetrically distributed around it. The expressions of the two dotted lines are given by
(6)xi∙w+b=1,(7)xi∙w+b=−1.The maximum distance between the two types of data is the distance between the two-dashed lines, which is2/w. When u is smaller, the spacing is larger.


## 3. Apple Recognition Algorithm Based on Improved YOLOv5

First, this section introduces and analyzes the overall improved framework structure of YOLOv5. Second, a loss function is introduced to ensure that the loss value of the network model during the training process displays a gradient descent trend.


### 3.1. Overall YOLOv5 Structure

The You-Only-Look-Once (YOLO) algorithm [18] was proposed at the 2016 Computer Vision Summit (CVPR); this neural network only needs to look once to identify the category and location of the target in the image. This algorithm takes an image as the input and provides the category and location of the target as output. After continuous advancements of the YOLO algorithm, the version v1 was updated to the version v7. Some improvements were added based on the original YOLOv5. The first is the improvement of the CSP structure in the network. Using parameter reconstruction, the convolutional layer (Conv) and the batch normalization (BN) layer in the CBL module were fused into a batch-normalized convolutional layer Conv_B. It is equivalent to fusing the parameters of convolution and batch normalization, so that the Conv_B convolution layer has the characteristics of batch normalization. The improved framework structure of YOLOv5 is shown in Figure 5.Figure 5
Framework structure of improved YOLOv5.The YOLOv5 network structure is mainly composed of input, backbone, neck, and output. First, when an apple image is input, the input terminal uses mosaic to expand the dataset. The pictures are randomly cropped and zoomed, and multiple photos are stitched to complete the adaptive anchor frame setting of the dataset [19]. Then, in the backbone network, the Focus module cuts the input image through the slicing operation and slices and splices the original image to a size of 608×608×3. The convolutional layer outputs a feature map of 304×304×32. In the CSP1_X module, X represents X residual components. The initial input is sent to the two branches, respectively, and convolution operations are performed on these two branches to reduce the number of channels of the feature map by half. Among them, one branch performs a batch-normalized convolutional layer, and after passing through the Conv2d layer, another branch is used to connect the output feature map using a concat operation. Finally, the output feature map is obtained after passing through the BN layer and convolution layer, in turn. The Neck network adopts the multiscale fusion of the FPN feature pyramid and PAN structure to better solve the scale problem of target detection. PAN is a bottom-up structure, which can strengthen the ability of network feature fusion and complete the positioning function. Finally, the output typically includes CIOU_loss and DIOU_nms. CIOU_loss generally solves the problem of nonoverlapping bounding boxes and bounding box scale information. Considering the location information of the center point of the bounding box, DIOU_nms is more suitable for target detection and provides better results for detection in complex scenes.


### 3.2. The Addition of the Attention Mechanism Module

Due to the complexity of the apple background, the branches and leaves in the environment interfere with the image recognition. Therefore, to improve the recognition accuracy of the fruit picking method, the attention mechanism CA module is introduced into the YOLOv5 network to better extract different fruits. The features of the image can improve the generalization ability of the model.The CA module is a new mobile network that decomposes the acquired two-dimensional features into two one-dimensional vector encoding processes and aggregates the features along two different directions in space. The purpose of this is to encode the generated feature maps as a pair of orientation-aware and position-sensitive feature maps, which can be complementarily applied to the input feature maps to enhance the features of objects of interest. The structure diagram of the CA module is shown in Figure6.Figure 6
Structure diagram of CA module.The next step is to insert the CA module into the YOLOv5 network. The location where the attention mechanism is placed is not unique; thus, the location of the insertion must be first determined. We observe the network frame diagram of YOLOv5; the output has a detection layer of76∗76∗255, which is concat spliced by the fifth layer of the backbone network and the seventh layer after twice upsampling in the Neck network and finally output by CSP2_1. Therefore, the CA module is added after the fifth layer of the backbone network, so that the high-dimensional feature map in the Neck network is fused with the features of the CA module, and the detection effect of large-scale objects is improved.Subsequently, the CA module is added to the seventh layer of the backbone network, and it is spliced with the high-dimensional feature map concat obtained after the first upsampling of the Neck network, so that the output is a38∗38∗255 detection layer. The detection effect of medium-sized objects is improved. Finally, the CA module is added to the eleventh layer of the backbone network to improve the detection ability of the 19∗19∗255 detection layer for small targets. The network is optimized from three scales to improve the overall recognition effect of the YOLOv5 detection model.


### 3.3. YOLO Loss Function

The YOLO algorithm has an important concept, i.e., the loss function, the structure of which is displayed in Figure6. When analyzed by the YOLO algorithm, there are three types of errors, i.e., position, C value, and classification; thus, a loss function is introduced to balance these deviations. The loss of the entire algorithm consists of three parts, i.e., the coordinate error generated by the predicted and real frames, confidence error of the object, and error of predicted category.
(8)λcoord∑i=0s2∑j=0B1ijobjxi−x⌢i2+yi−y⌢i2+λcoord∑i=0s2∑j=0B1ijobjωi−ω⌢i2+hi−h⌢i2+∑i=0s2∑j=0B1ijobjCi−C⌢i2+λnoobj∑i=0s2∑j=0B1ijnoobjCi−C⌢i2+∑i=0s21iobj∑c∈classespic−p⌢ic2.Among them, the five parameters (x, y, w, h, and c) represent the x-axis coordinate, y-axis coordinate, target width, target height, and target category, respectively; pic is the confidence level of the predicted target; λcoord and λnoobj are weight variables with the role of balancing the proportions. S is every grid cell; B is every bounding box; obj is the detection frame containing the target; and noobj indicates that the detection frame does not contain a target.


### 3.4. Experimental Evaluation

In the final result, if the apple fruit is identified as apple fruit, it means that the algorithm judges the correct result as correct; otherwise, it is defined as TP (true positives); if the background is identified as an apple fruit, it means that the algorithm judges the wrong result as correct, and it is defined as FP (false positives); If the apple fruit is judged as the background, it means that the algorithm judges the correct result as an error, and it is defined as FN (false negatives).In model evaluation, the accuracy rate (P), recall rate (R), and F1 value are mainly used.The calculation formula ofP is shown in
(9)P=TPTP+FP.The calculation formula ofR is shown in
(10)R=TPTP+FN.The formula for calculating theF1 value is shown in
(11)F1=2PRP+R.After the training of the dataset is completed, the weight file obtained in this training will be generated. The obtained weight file can be selected by setting the weight path, and the apple fruit can be tested by using the trained weight file.


## 3.1. Overall YOLOv5 Structure

The You-Only-Look-Once (YOLO) algorithm [18] was proposed at the 2016 Computer Vision Summit (CVPR); this neural network only needs to look once to identify the category and location of the target in the image. This algorithm takes an image as the input and provides the category and location of the target as output. After continuous advancements of the YOLO algorithm, the version v1 was updated to the version v7. Some improvements were added based on the original YOLOv5. The first is the improvement of the CSP structure in the network. Using parameter reconstruction, the convolutional layer (Conv) and the batch normalization (BN) layer in the CBL module were fused into a batch-normalized convolutional layer Conv_B. It is equivalent to fusing the parameters of convolution and batch normalization, so that the Conv_B convolution layer has the characteristics of batch normalization. The improved framework structure of YOLOv5 is shown in Figure 5.Figure 5
Framework structure of improved YOLOv5.The YOLOv5 network structure is mainly composed of input, backbone, neck, and output. First, when an apple image is input, the input terminal uses mosaic to expand the dataset. The pictures are randomly cropped and zoomed, and multiple photos are stitched to complete the adaptive anchor frame setting of the dataset [19]. Then, in the backbone network, the Focus module cuts the input image through the slicing operation and slices and splices the original image to a size of 608×608×3. The convolutional layer outputs a feature map of 304×304×32. In the CSP1_X module, X represents X residual components. The initial input is sent to the two branches, respectively, and convolution operations are performed on these two branches to reduce the number of channels of the feature map by half. Among them, one branch performs a batch-normalized convolutional layer, and after passing through the Conv2d layer, another branch is used to connect the output feature map using a concat operation. Finally, the output feature map is obtained after passing through the BN layer and convolution layer, in turn. The Neck network adopts the multiscale fusion of the FPN feature pyramid and PAN structure to better solve the scale problem of target detection. PAN is a bottom-up structure, which can strengthen the ability of network feature fusion and complete the positioning function. Finally, the output typically includes CIOU_loss and DIOU_nms. CIOU_loss generally solves the problem of nonoverlapping bounding boxes and bounding box scale information. Considering the location information of the center point of the bounding box, DIOU_nms is more suitable for target detection and provides better results for detection in complex scenes.


## 3.2. The Addition of the Attention Mechanism Module

Due to the complexity of the apple background, the branches and leaves in the environment interfere with the image recognition. Therefore, to improve the recognition accuracy of the fruit picking method, the attention mechanism CA module is introduced into the YOLOv5 network to better extract different fruits. The features of the image can improve the generalization ability of the model.The CA module is a new mobile network that decomposes the acquired two-dimensional features into two one-dimensional vector encoding processes and aggregates the features along two different directions in space. The purpose of this is to encode the generated feature maps as a pair of orientation-aware and position-sensitive feature maps, which can be complementarily applied to the input feature maps to enhance the features of objects of interest. The structure diagram of the CA module is shown in Figure6.Figure 6
Structure diagram of CA module.The next step is to insert the CA module into the YOLOv5 network. The location where the attention mechanism is placed is not unique; thus, the location of the insertion must be first determined. We observe the network frame diagram of YOLOv5; the output has a detection layer of76∗76∗255, which is concat spliced by the fifth layer of the backbone network and the seventh layer after twice upsampling in the Neck network and finally output by CSP2_1. Therefore, the CA module is added after the fifth layer of the backbone network, so that the high-dimensional feature map in the Neck network is fused with the features of the CA module, and the detection effect of large-scale objects is improved.Subsequently, the CA module is added to the seventh layer of the backbone network, and it is spliced with the high-dimensional feature map concat obtained after the first upsampling of the Neck network, so that the output is a38∗38∗255 detection layer. The detection effect of medium-sized objects is improved. Finally, the CA module is added to the eleventh layer of the backbone network to improve the detection ability of the 19∗19∗255 detection layer for small targets. The network is optimized from three scales to improve the overall recognition effect of the YOLOv5 detection model.


## 3.3. YOLO Loss Function

The YOLO algorithm has an important concept, i.e., the loss function, the structure of which is displayed in Figure6. When analyzed by the YOLO algorithm, there are three types of errors, i.e., position, C value, and classification; thus, a loss function is introduced to balance these deviations. The loss of the entire algorithm consists of three parts, i.e., the coordinate error generated by the predicted and real frames, confidence error of the object, and error of predicted category.
(8)λcoord∑i=0s2∑j=0B1ijobjxi−x⌢i2+yi−y⌢i2+λcoord∑i=0s2∑j=0B1ijobjωi−ω⌢i2+hi−h⌢i2+∑i=0s2∑j=0B1ijobjCi−C⌢i2+λnoobj∑i=0s2∑j=0B1ijnoobjCi−C⌢i2+∑i=0s21iobj∑c∈classespic−p⌢ic2.Among them, the five parameters (x, y, w, h, and c) represent the x-axis coordinate, y-axis coordinate, target width, target height, and target category, respectively; pic is the confidence level of the predicted target; λcoord and λnoobj are weight variables with the role of balancing the proportions. S is every grid cell; B is every bounding box; obj is the detection frame containing the target; and noobj indicates that the detection frame does not contain a target.


## 3.4. Experimental Evaluation

In the final result, if the apple fruit is identified as apple fruit, it means that the algorithm judges the correct result as correct; otherwise, it is defined as TP (true positives); if the background is identified as an apple fruit, it means that the algorithm judges the wrong result as correct, and it is defined as FP (false positives); If the apple fruit is judged as the background, it means that the algorithm judges the correct result as an error, and it is defined as FN (false negatives).In model evaluation, the accuracy rate (P), recall rate (R), and F1 value are mainly used.The calculation formula ofP is shown in
(9)P=TPTP+FP.The calculation formula ofR is shown in
(10)R=TPTP+FN.The formula for calculating theF1 value is shown in
(11)F1=2PRP+R.After the training of the dataset is completed, the weight file obtained in this training will be generated. The obtained weight file can be selected by setting the weight path, and the apple fruit can be tested by using the trained weight file.


## 4. Model Training and Experimental Analysis

### 4.1. Training Environment

The algorithm needs to perform many operations for feature extraction and classification; thus, it requires high performance machines. The details of the implementation are presented in Table1.Table 1
Laboratory environment.
Lab environmentConfigureOperating system64 bit windows 10Graphics cardGTX1070RAM8GBVideo memory4GBProcessorIntel Core i7-6700 3.40GHzDuring training, pretraining weights on the official website are downloaded, and the network model is fine-tuned based on a certain pretraining weight through transfer learning. The advantage of this is that it can speed up the convergence of the model loss function and improve the detection accuracy. Setting reasonable hyperparameters also affect the quality of model training. During model training, the batch size is set to 8. The momentum is set to 0.937, the decay is set to 0.0005, the learning rate is set to 0.01, and the epoch is set to 300.


### 4.2. Training Results

The loss curve of the model training is shown in Figure7. As can be seen from Figure 7, the left side is the training set iteration curve, and the right side is the validation set iteration curve. It can be seen from the figure that the loss value on the training set and the validation set drops rapidly during the first dozens of rounds of training, and after 250 rounds of training, the loss value basically tends to be stable. Therefore, in this study, the model output after 300 rounds of training is determined as the model for apple detection.Figure 7
Model training loss curve.


### 4.3. Experimental Settings

The dataset consists of real apple data captured in apple orchards; whereby, a total of 3000 apple images were collected. Mosaic was introduced to expand the dataset; particularly, random scaling adds many small targets, which improves the generalization ability of the apple detection model [20]. Then, all the images were divided into three sets: the training set, test set (70% of images), and validation set (30% of randomly selected images). When studying the apple recognition algorithm based on shape features, because the HOG shape feature operator does not need to consider the color, a variety of fruit images of different colors were introduced as positive samples. When researching the improved YOLOv5 apple recognition algorithm, pictures with different occlusion degrees, growth stages, and states as training data were found. These different data samples are shown in Figure 8.Figure 8
Different data samples for the two algorithms.
(a)(b)(c)(d)(e)(f)(g)(h)


### 4.4. Results and Analysis

#### 4.4.1. Comparison of the Results of Different Target Detection Algorithms

The experiments randomly tested the recognition effect of the algorithm for various real-world images. To verify the accuracy of the algorithm for apple recognition and localization in different distribution states, in this study, the test was divided into five categories according to the above criteria, and statistics were carried out. Figures9(a)–9(e) show the recognition effects of the HOG + SVM, Faster RCNN, baseline YOLOv5, YOLOv6, and improved YOLOv5 algorithms for images in the unobstructed, fruit sticking, and bagging states.Figure 9
Results of identification and positioning of apples in different states by four algorithms. (a) Recognition effect of HOG + SVM algorithm in different states. (b) Recognition effect of Faster RCNN algorithm in different states. (c) Recognition effect of YOLOv6 algorithm in different states. (d) Recognition effect of baseline YOLOv5 algorithm in different states. (e) Recognition effect of the improved YOLOv5 algorithm in different states.
(a)(b)(c)(d)(e)In the figure, it is observed that the image collected in the unobstructed state (first picture of each group) is brighter in both the fruit and background. The outline shape of the apple is very clear. In the image collected in the state of fruit adhesion (second picture of each group), the nearby fruit is easy to identify. However, after the fruit branches and leaves in the distance are blocked, the light is dark, and identification becomes difficult. The images collected in the bagging state (third picture in each group) have a relatively bright background, but the outline of the fruit is partially deformed by the bag. In the above comparison, many deep learning models are selected, and the traditional machine learning model is only HOG + SVM, because HOG + SVM is widely used in traditional target detection algorithms. HOG can better describe shape features and maintain good invariance to image geometric and optical deformation. Classify the HOG feature vector through SVM and use the trained SVM classifier to complete the classification of the localized fruit. In the detection results, the HOG + SVM algorithm displays better recognition ability for apples with clear shape features and little difference in size. However, in the state of adhesion and bagging, due to the lack of light and outline, some features of the apple are missing. The other two algorithms show stronger feature extraction ability, because the deep neural network displays better recognition effect compared to HOG + SVM.


#### 4.4.2. Recognition of Different Lighting Angles by Different Detection Algorithms

In order to verify the superiority of the method proposed in this paper, we added the more advanced version YOLOv6 to the comparative experiment. In this test, we used the light angle of the camera as a control variable, which are three shooting lights: backlight, side light, and front light. Fifty images were randomly selected with different lighting angles in the complex occlusion environment including backlight, side light, and front light, as the test set data. The detection effects of the HOG + SVM, Faster RCNN, YOLOv6, baseline YOLOv5, and improved YOLOv5 algorithms are compared under the above conditions. The evaluation indicators are the accuracy rate precision,F1 comprehensive evaluation score, recall rate, and detection speed to evaluate the algorithm performance. The calculated evaluation values are weighted, averaged, and recorded in Table 2, and the overall results are shown in Figure 10.Table 2
Evaluation results of 5 algorithms under different lighting angles.
MethodPrecision/%Recall/%F1/%Average image recognition time/sHog + SVM82.2379.8581.50.296Faster RCNN91.7686.3488.960.255YOLOv694.8192.6593.710.024Baseline YOLOv590.5492.1691.340.032Improved YOLOv596.3594.694.210.026Figure 10
Results of identification and positioning of apples under different lighting angles by four algorithms. (a) Recognition effect of HOG + SVM algorithm under different illumination angles. (b) Recognition effect of Faster RCNN algorithm under different illumination angles. (c) Recognition effect of YOLOv6 algorithm under different illumination angles. (d) Recognition effect of the baseline YOLOv5 algorithm under different illumination angles. (e) Recognition effect of the improved YOLOv5 algorithm under different illumination angles.
(a)(b)(c)(d)(e)As shown in Figure10, apples have clear veins and average surface light intensity in the case of side light, making identification easy. In the case of backlight, the light intensity of the appearance of apples and branches is significantly reduced, and it is difficult to distinguish between the two. Additionally, the detection of smaller objects behind is difficult. In the case of smooth light, the light intensity of part of the surface of the apple becomes higher, and the outer surface shows a daytime color without the characteristics of veins [21].As presented in Table2, HOG + SVM is a fruit recognition method based on shape features, and the light has little influence on it. The disadvantage it faces is that it can only accurately identify targets with obvious shape features. The F1 of the improved YOLOv5 was 1.26% and 3.63% higher than that of YOLOv6 and the baseline YOLOv5, respectively, and the precision was 0.54% and 4.81% higher than that of YOLOv6 and the baseline YOLOv5, respectively. The F1 of Faster RCNN was 6.01% lower than that of the improved YOLOv5, and the precision was 3.59% lower than that of the improved YOLOv5. The F1 of the improved YOLOv5 was 13.47% higher than that of HOG + SVM, and the precision was 12.12% higher than that of HOG + SVM. It can be concluded that the evaluation index of the deep learning algorithm is generally higher than that of the traditional algorithm, because the model trained by the neural network has a strong generalization ability and still has a good effect when the color features are not obvious. The detection speed of the improved YOLOv5 and YOLOv6 is similar, leading the other two algorithms as a single-stage detection algorithm.


#### 4.4.3. Recognition Results of Different Detection Algorithms for Different Occlusions

Next, the comparison test of the occlusion degree of the apple surface is performed. In this test, the occlusion degree of the apple surface is used as a control variable. Similarly, fifty images of fruit adhesion, bagging, dense, and overlapping situations in complex occlusion environments were randomly selected as test set data and tested through four algorithms. The overall results are shown in Figure11, and the calculated evaluation values are weighted and averaged and recorded in Table 3.Figure 11
Results of identification and positioning of apples under different occlusions by four algorithms. (a) Recognition effect of HOG + SVM algorithm under different occlusions. (b) Recognition effect of Faster RCNN algorithm under different occlusions. (c) Recognition effect of YOLOv6 algorithm under different occlusions. (d) Recognition effect of the baseline YOLOv5 algorithm under different occlusions. (e) Recognition effect of the improved YOLOv5 algorithm under different occlusions.
(a)(b)(c)(d)(e)Table 3
Evaluation results of 5 algorithms under different occlusions.
MethodPrecision/%Recall/%F1/%Average image recognition time/sHog + SVM73.5075.3474.40.296Faster RCNN81.0680.3280.690.255YOLOv693.7690.692.150.024Baseline YOLOv590.2388.7689.490.036Improved YOLOv594.6492.8993.760.026As shown in Figure11, the unobstructed apples have distinct veins and clear outlines and are clearly distinguished from the background. The slightly shaded apples are lighter in color, and some of their shape features are covered by branches and leaves. The heavily occluded surface of the apple interfered with the color and vein characteristics of the apple surface, and the shape of the fruit was partially deformed by the bag. In addition, the redundant part of the plastic bag made the appearance of the apple no longer round; thus, the appearance characteristics were no longer clear.As presented in Table3, although the HOG + SVM algorithm can identify apples, when it is blocked by branches and leaves, the detection effect is not ideal. The precision of the improved YOLOv5 was 0.88% and 4.41% higher than that of YOLOv6 and the baseline YOLOv5, respectively, and the F1 was 1.61% and 4.27% higher than that of YOLOv6 and the baseline YOLOv5 because the improved backbone network extracts the features of different apple targets better and improves the recognition accuracy. For bagged apples, the algorithm recognizes the plastic film as a feature of the apple during training and detects it together during detection, increasing the recognition rate instead. The precision of the improved YOLOv5 is 13.58% higher than that of Faster RCNN, and the F1 is 13.07% higher than that of YOLOv6 because the Faster RCNN region suggests that some small targets are ignored in network prediction, resulting in poor model detection effect.To sum up, in the comparison between the traditional HOG + SVM algorithm and the deep learning algorithm, although the HOG + SVM algorithm can identify apples under various conditions, the precision andF1 value are much lower than the existing deep learning algorithms. The reason why the traditional machine learning model HOG + SVM is chosen is to verify that the traditional method HOG + SVM algorithm has the shortcomings of long detection time and low accuracy. As can be seen from Tables 2 and 3 of the revised manuscript, the improved YOLOv5 algorithm model trained in the text is better than Faster RCNN in any sample, and the gap with YOLOv6 is not obvious. However, in the comparative test, the F1 score is obviously better than that of YOLOv6. The improved YOLOv5 algorithm model trained in the article can be competent for the target recognition of light changes, occlusion, and bagging apples, so that the apple picking robot using this visual algorithm can realize the recognition of multiple apples in a complex occlusion environment. The superiority of the proposed YOLOv5 method is demonstrated.


## 4.1. Training Environment

The algorithm needs to perform many operations for feature extraction and classification; thus, it requires high performance machines. The details of the implementation are presented in Table1.Table 1
Laboratory environment.
Lab environmentConfigureOperating system64 bit windows 10Graphics cardGTX1070RAM8GBVideo memory4GBProcessorIntel Core i7-6700 3.40GHzDuring training, pretraining weights on the official website are downloaded, and the network model is fine-tuned based on a certain pretraining weight through transfer learning. The advantage of this is that it can speed up the convergence of the model loss function and improve the detection accuracy. Setting reasonable hyperparameters also affect the quality of model training. During model training, the batch size is set to 8. The momentum is set to 0.937, the decay is set to 0.0005, the learning rate is set to 0.01, and the epoch is set to 300.


## 4.2. Training Results

The loss curve of the model training is shown in Figure7. As can be seen from Figure 7, the left side is the training set iteration curve, and the right side is the validation set iteration curve. It can be seen from the figure that the loss value on the training set and the validation set drops rapidly during the first dozens of rounds of training, and after 250 rounds of training, the loss value basically tends to be stable. Therefore, in this study, the model output after 300 rounds of training is determined as the model for apple detection.Figure 7
Model training loss curve.


## 4.3. Experimental Settings

The dataset consists of real apple data captured in apple orchards; whereby, a total of 3000 apple images were collected. Mosaic was introduced to expand the dataset; particularly, random scaling adds many small targets, which improves the generalization ability of the apple detection model [20]. Then, all the images were divided into three sets: the training set, test set (70% of images), and validation set (30% of randomly selected images). When studying the apple recognition algorithm based on shape features, because the HOG shape feature operator does not need to consider the color, a variety of fruit images of different colors were introduced as positive samples. When researching the improved YOLOv5 apple recognition algorithm, pictures with different occlusion degrees, growth stages, and states as training data were found. These different data samples are shown in Figure 8.Figure 8
Different data samples for the two algorithms.
(a)(b)(c)(d)(e)(f)(g)(h)


## 4.4. Results and Analysis

### 4.4.1. Comparison of the Results of Different Target Detection Algorithms

The experiments randomly tested the recognition effect of the algorithm for various real-world images. To verify the accuracy of the algorithm for apple recognition and localization in different distribution states, in this study, the test was divided into five categories according to the above criteria, and statistics were carried out. Figures9(a)–9(e) show the recognition effects of the HOG + SVM, Faster RCNN, baseline YOLOv5, YOLOv6, and improved YOLOv5 algorithms for images in the unobstructed, fruit sticking, and bagging states.Figure 9
Results of identification and positioning of apples in different states by four algorithms. (a) Recognition effect of HOG + SVM algorithm in different states. (b) Recognition effect of Faster RCNN algorithm in different states. (c) Recognition effect of YOLOv6 algorithm in different states. (d) Recognition effect of baseline YOLOv5 algorithm in different states. (e) Recognition effect of the improved YOLOv5 algorithm in different states.
(a)(b)(c)(d)(e)In the figure, it is observed that the image collected in the unobstructed state (first picture of each group) is brighter in both the fruit and background. The outline shape of the apple is very clear. In the image collected in the state of fruit adhesion (second picture of each group), the nearby fruit is easy to identify. However, after the fruit branches and leaves in the distance are blocked, the light is dark, and identification becomes difficult. The images collected in the bagging state (third picture in each group) have a relatively bright background, but the outline of the fruit is partially deformed by the bag. In the above comparison, many deep learning models are selected, and the traditional machine learning model is only HOG + SVM, because HOG + SVM is widely used in traditional target detection algorithms. HOG can better describe shape features and maintain good invariance to image geometric and optical deformation. Classify the HOG feature vector through SVM and use the trained SVM classifier to complete the classification of the localized fruit. In the detection results, the HOG + SVM algorithm displays better recognition ability for apples with clear shape features and little difference in size. However, in the state of adhesion and bagging, due to the lack of light and outline, some features of the apple are missing. The other two algorithms show stronger feature extraction ability, because the deep neural network displays better recognition effect compared to HOG + SVM.


### 4.4.2. Recognition of Different Lighting Angles by Different Detection Algorithms

In order to verify the superiority of the method proposed in this paper, we added the more advanced version YOLOv6 to the comparative experiment. In this test, we used the light angle of the camera as a control variable, which are three shooting lights: backlight, side light, and front light. Fifty images were randomly selected with different lighting angles in the complex occlusion environment including backlight, side light, and front light, as the test set data. The detection effects of the HOG + SVM, Faster RCNN, YOLOv6, baseline YOLOv5, and improved YOLOv5 algorithms are compared under the above conditions. The evaluation indicators are the accuracy rate precision,F1 comprehensive evaluation score, recall rate, and detection speed to evaluate the algorithm performance. The calculated evaluation values are weighted, averaged, and recorded in Table 2, and the overall results are shown in Figure 10.Table 2
Evaluation results of 5 algorithms under different lighting angles.
MethodPrecision/%Recall/%F1/%Average image recognition time/sHog + SVM82.2379.8581.50.296Faster RCNN91.7686.3488.960.255YOLOv694.8192.6593.710.024Baseline YOLOv590.5492.1691.340.032Improved YOLOv596.3594.694.210.026Figure 10
Results of identification and positioning of apples under different lighting angles by four algorithms. (a) Recognition effect of HOG + SVM algorithm under different illumination angles. (b) Recognition effect of Faster RCNN algorithm under different illumination angles. (c) Recognition effect of YOLOv6 algorithm under different illumination angles. (d) Recognition effect of the baseline YOLOv5 algorithm under different illumination angles. (e) Recognition effect of the improved YOLOv5 algorithm under different illumination angles.
(a)(b)(c)(d)(e)As shown in Figure10, apples have clear veins and average surface light intensity in the case of side light, making identification easy. In the case of backlight, the light intensity of the appearance of apples and branches is significantly reduced, and it is difficult to distinguish between the two. Additionally, the detection of smaller objects behind is difficult. In the case of smooth light, the light intensity of part of the surface of the apple becomes higher, and the outer surface shows a daytime color without the characteristics of veins [21].As presented in Table2, HOG + SVM is a fruit recognition method based on shape features, and the light has little influence on it. The disadvantage it faces is that it can only accurately identify targets with obvious shape features. The F1 of the improved YOLOv5 was 1.26% and 3.63% higher than that of YOLOv6 and the baseline YOLOv5, respectively, and the precision was 0.54% and 4.81% higher than that of YOLOv6 and the baseline YOLOv5, respectively. The F1 of Faster RCNN was 6.01% lower than that of the improved YOLOv5, and the precision was 3.59% lower than that of the improved YOLOv5. The F1 of the improved YOLOv5 was 13.47% higher than that of HOG + SVM, and the precision was 12.12% higher than that of HOG + SVM. It can be concluded that the evaluation index of the deep learning algorithm is generally higher than that of the traditional algorithm, because the model trained by the neural network has a strong generalization ability and still has a good effect when the color features are not obvious. The detection speed of the improved YOLOv5 and YOLOv6 is similar, leading the other two algorithms as a single-stage detection algorithm.


### 4.4.3. Recognition Results of Different Detection Algorithms for Different Occlusions

Next, the comparison test of the occlusion degree of the apple surface is performed. In this test, the occlusion degree of the apple surface is used as a control variable. Similarly, fifty images of fruit adhesion, bagging, dense, and overlapping situations in complex occlusion environments were randomly selected as test set data and tested through four algorithms. The overall results are shown in Figure11, and the calculated evaluation values are weighted and averaged and recorded in Table 3.Figure 11
Results of identification and positioning of apples under different occlusions by four algorithms. (a) Recognition effect of HOG + SVM algorithm under different occlusions. (b) Recognition effect of Faster RCNN algorithm under different occlusions. (c) Recognition effect of YOLOv6 algorithm under different occlusions. (d) Recognition effect of the baseline YOLOv5 algorithm under different occlusions. (e) Recognition effect of the improved YOLOv5 algorithm under different occlusions.
(a)(b)(c)(d)(e)Table 3
Evaluation results of 5 algorithms under different occlusions.
MethodPrecision/%Recall/%F1/%Average image recognition time/sHog + SVM73.5075.3474.40.296Faster RCNN81.0680.3280.690.255YOLOv693.7690.692.150.024Baseline YOLOv590.2388.7689.490.036Improved YOLOv594.6492.8993.760.026As shown in Figure11, the unobstructed apples have distinct veins and clear outlines and are clearly distinguished from the background. The slightly shaded apples are lighter in color, and some of their shape features are covered by branches and leaves. The heavily occluded surface of the apple interfered with the color and vein characteristics of the apple surface, and the shape of the fruit was partially deformed by the bag. In addition, the redundant part of the plastic bag made the appearance of the apple no longer round; thus, the appearance characteristics were no longer clear.As presented in Table3, although the HOG + SVM algorithm can identify apples, when it is blocked by branches and leaves, the detection effect is not ideal. The precision of the improved YOLOv5 was 0.88% and 4.41% higher than that of YOLOv6 and the baseline YOLOv5, respectively, and the F1 was 1.61% and 4.27% higher than that of YOLOv6 and the baseline YOLOv5 because the improved backbone network extracts the features of different apple targets better and improves the recognition accuracy. For bagged apples, the algorithm recognizes the plastic film as a feature of the apple during training and detects it together during detection, increasing the recognition rate instead. The precision of the improved YOLOv5 is 13.58% higher than that of Faster RCNN, and the F1 is 13.07% higher than that of YOLOv6 because the Faster RCNN region suggests that some small targets are ignored in network prediction, resulting in poor model detection effect.To sum up, in the comparison between the traditional HOG + SVM algorithm and the deep learning algorithm, although the HOG + SVM algorithm can identify apples under various conditions, the precision andF1 value are much lower than the existing deep learning algorithms. The reason why the traditional machine learning model HOG + SVM is chosen is to verify that the traditional method HOG + SVM algorithm has the shortcomings of long detection time and low accuracy. As can be seen from Tables 2 and 3 of the revised manuscript, the improved YOLOv5 algorithm model trained in the text is better than Faster RCNN in any sample, and the gap with YOLOv6 is not obvious. However, in the comparative test, the F1 score is obviously better than that of YOLOv6. The improved YOLOv5 algorithm model trained in the article can be competent for the target recognition of light changes, occlusion, and bagging apples, so that the apple picking robot using this visual algorithm can realize the recognition of multiple apples in a complex occlusion environment. The superiority of the proposed YOLOv5 method is demonstrated.


## 4.4.1. Comparison of the Results of Different Target Detection Algorithms

The experiments randomly tested the recognition effect of the algorithm for various real-world images. To verify the accuracy of the algorithm for apple recognition and localization in different distribution states, in this study, the test was divided into five categories according to the above criteria, and statistics were carried out. Figures9(a)–9(e) show the recognition effects of the HOG + SVM, Faster RCNN, baseline YOLOv5, YOLOv6, and improved YOLOv5 algorithms for images in the unobstructed, fruit sticking, and bagging states.Figure 9
Results of identification and positioning of apples in different states by four algorithms. (a) Recognition effect of HOG + SVM algorithm in different states. (b) Recognition effect of Faster RCNN algorithm in different states. (c) Recognition effect of YOLOv6 algorithm in different states. (d) Recognition effect of baseline YOLOv5 algorithm in different states. (e) Recognition effect of the improved YOLOv5 algorithm in different states.
(a)(b)(c)(d)(e)In the figure, it is observed that the image collected in the unobstructed state (first picture of each group) is brighter in both the fruit and background. The outline shape of the apple is very clear. In the image collected in the state of fruit adhesion (second picture of each group), the nearby fruit is easy to identify. However, after the fruit branches and leaves in the distance are blocked, the light is dark, and identification becomes difficult. The images collected in the bagging state (third picture in each group) have a relatively bright background, but the outline of the fruit is partially deformed by the bag. In the above comparison, many deep learning models are selected, and the traditional machine learning model is only HOG + SVM, because HOG + SVM is widely used in traditional target detection algorithms. HOG can better describe shape features and maintain good invariance to image geometric and optical deformation. Classify the HOG feature vector through SVM and use the trained SVM classifier to complete the classification of the localized fruit. In the detection results, the HOG + SVM algorithm displays better recognition ability for apples with clear shape features and little difference in size. However, in the state of adhesion and bagging, due to the lack of light and outline, some features of the apple are missing. The other two algorithms show stronger feature extraction ability, because the deep neural network displays better recognition effect compared to HOG + SVM.


## 4.4.2. Recognition of Different Lighting Angles by Different Detection Algorithms

In order to verify the superiority of the method proposed in this paper, we added the more advanced version YOLOv6 to the comparative experiment. In this test, we used the light angle of the camera as a control variable, which are three shooting lights: backlight, side light, and front light. Fifty images were randomly selected with different lighting angles in the complex occlusion environment including backlight, side light, and front light, as the test set data. The detection effects of the HOG + SVM, Faster RCNN, YOLOv6, baseline YOLOv5, and improved YOLOv5 algorithms are compared under the above conditions. The evaluation indicators are the accuracy rate precision,F1 comprehensive evaluation score, recall rate, and detection speed to evaluate the algorithm performance. The calculated evaluation values are weighted, averaged, and recorded in Table 2, and the overall results are shown in Figure 10.Table 2
Evaluation results of 5 algorithms under different lighting angles.
MethodPrecision/%Recall/%F1/%Average image recognition time/sHog + SVM82.2379.8581.50.296Faster RCNN91.7686.3488.960.255YOLOv694.8192.6593.710.024Baseline YOLOv590.5492.1691.340.032Improved YOLOv596.3594.694.210.026Figure 10
Results of identification and positioning of apples under different lighting angles by four algorithms. (a) Recognition effect of HOG + SVM algorithm under different illumination angles. (b) Recognition effect of Faster RCNN algorithm under different illumination angles. (c) Recognition effect of YOLOv6 algorithm under different illumination angles. (d) Recognition effect of the baseline YOLOv5 algorithm under different illumination angles. (e) Recognition effect of the improved YOLOv5 algorithm under different illumination angles.
(a)(b)(c)(d)(e)As shown in Figure10, apples have clear veins and average surface light intensity in the case of side light, making identification easy. In the case of backlight, the light intensity of the appearance of apples and branches is significantly reduced, and it is difficult to distinguish between the two. Additionally, the detection of smaller objects behind is difficult. In the case of smooth light, the light intensity of part of the surface of the apple becomes higher, and the outer surface shows a daytime color without the characteristics of veins [21].As presented in Table2, HOG + SVM is a fruit recognition method based on shape features, and the light has little influence on it. The disadvantage it faces is that it can only accurately identify targets with obvious shape features. The F1 of the improved YOLOv5 was 1.26% and 3.63% higher than that of YOLOv6 and the baseline YOLOv5, respectively, and the precision was 0.54% and 4.81% higher than that of YOLOv6 and the baseline YOLOv5, respectively. The F1 of Faster RCNN was 6.01% lower than that of the improved YOLOv5, and the precision was 3.59% lower than that of the improved YOLOv5. The F1 of the improved YOLOv5 was 13.47% higher than that of HOG + SVM, and the precision was 12.12% higher than that of HOG + SVM. It can be concluded that the evaluation index of the deep learning algorithm is generally higher than that of the traditional algorithm, because the model trained by the neural network has a strong generalization ability and still has a good effect when the color features are not obvious. The detection speed of the improved YOLOv5 and YOLOv6 is similar, leading the other two algorithms as a single-stage detection algorithm.


## 4.4.3. Recognition Results of Different Detection Algorithms for Different Occlusions

Next, the comparison test of the occlusion degree of the apple surface is performed. In this test, the occlusion degree of the apple surface is used as a control variable. Similarly, fifty images of fruit adhesion, bagging, dense, and overlapping situations in complex occlusion environments were randomly selected as test set data and tested through four algorithms. The overall results are shown in Figure11, and the calculated evaluation values are weighted and averaged and recorded in Table 3.Figure 11
Results of identification and positioning of apples under different occlusions by four algorithms. (a) Recognition effect of HOG + SVM algorithm under different occlusions. (b) Recognition effect of Faster RCNN algorithm under different occlusions. (c) Recognition effect of YOLOv6 algorithm under different occlusions. (d) Recognition effect of the baseline YOLOv5 algorithm under different occlusions. (e) Recognition effect of the improved YOLOv5 algorithm under different occlusions.
(a)(b)(c)(d)(e)Table 3
Evaluation results of 5 algorithms under different occlusions.
MethodPrecision/%Recall/%F1/%Average image recognition time/sHog + SVM73.5075.3474.40.296Faster RCNN81.0680.3280.690.255YOLOv693.7690.692.150.024Baseline YOLOv590.2388.7689.490.036Improved YOLOv594.6492.8993.760.026As shown in Figure11, the unobstructed apples have distinct veins and clear outlines and are clearly distinguished from the background. The slightly shaded apples are lighter in color, and some of their shape features are covered by branches and leaves. The heavily occluded surface of the apple interfered with the color and vein characteristics of the apple surface, and the shape of the fruit was partially deformed by the bag. In addition, the redundant part of the plastic bag made the appearance of the apple no longer round; thus, the appearance characteristics were no longer clear.As presented in Table3, although the HOG + SVM algorithm can identify apples, when it is blocked by branches and leaves, the detection effect is not ideal. The precision of the improved YOLOv5 was 0.88% and 4.41% higher than that of YOLOv6 and the baseline YOLOv5, respectively, and the F1 was 1.61% and 4.27% higher than that of YOLOv6 and the baseline YOLOv5 because the improved backbone network extracts the features of different apple targets better and improves the recognition accuracy. For bagged apples, the algorithm recognizes the plastic film as a feature of the apple during training and detects it together during detection, increasing the recognition rate instead. The precision of the improved YOLOv5 is 13.58% higher than that of Faster RCNN, and the F1 is 13.07% higher than that of YOLOv6 because the Faster RCNN region suggests that some small targets are ignored in network prediction, resulting in poor model detection effect.To sum up, in the comparison between the traditional HOG + SVM algorithm and the deep learning algorithm, although the HOG + SVM algorithm can identify apples under various conditions, the precision andF1 value are much lower than the existing deep learning algorithms. The reason why the traditional machine learning model HOG + SVM is chosen is to verify that the traditional method HOG + SVM algorithm has the shortcomings of long detection time and low accuracy. As can be seen from Tables 2 and 3 of the revised manuscript, the improved YOLOv5 algorithm model trained in the text is better than Faster RCNN in any sample, and the gap with YOLOv6 is not obvious. However, in the comparative test, the F1 score is obviously better than that of YOLOv6. The improved YOLOv5 algorithm model trained in the article can be competent for the target recognition of light changes, occlusion, and bagging apples, so that the apple picking robot using this visual algorithm can realize the recognition of multiple apples in a complex occlusion environment. The superiority of the proposed YOLOv5 method is demonstrated.


## 5. Conclusion

In order to solve the problem of false detection and missed detection of apple picking robots in complex occlusion environments, this paper studies the traditional machine learning algorithm HOG + SVM and proposes a fast recognition method for multiple apple targets in complex occlusion environments based on improved YOLOv5. First, through the improvement of the CSP structure in the network, the convolutional layer (Conv) and the batch normalization (BN) layer in the CBL (Conv+BN + Leaky_relu activation function) module are fused into a batch-normalized convolutional layer. Subsequently, the CA (coordinate attention) mechanism module is embedded into different network layers in the improved designed backbone network to enhance the expressive ability of the features in the backbone network to better extract the features of different apple targets. For some targets with overlapping occlusions, the IOU in nms is changed to DIOU_nms, which greatly improves the model’s ability to recognize occluded targets. Finally, the experimental comparison shows that under different illumination angles, theF1 value of the improved YOLOv5 algorithm is 13.47%, 6.01%, 1.26%, and 3.36% higher than that of HOG + SVM, Faster RCNN, YOLOv6, and the baseline YOLOv5, respectively. The F1 value of the improved YOLOv5 algorithm under different occlusions is 19.36%, 13.07%, 1.68%, and 4.27% higher than that of the other algorithms, respectively. It has the ability to detect sticking and severely occluded apples, resulting in a high detection accuracy. On averaged image recognition time, the improved YOLOv5 was 0.27 s faster than HOG + SVM and 0.229 s faster than Faster RCNN and close to YOLOv6. The complexity of the occlusion of fruits and branches hinders the detector from learning an infinite variety of overlapping and occlusion situations when the training data are limited. Therefore, a direction worth exploring is the use of existing data to address this problem through unsupervised or semisupervised methods. This research can provide a theoretical basis for robots to quickly and efficiently identify apples in complex environments.



---

*Source: 3609541-2023-02-06.xml*