# Detection of _Camellia oleifera_ Fruit in Complex Scenes by Using YOLOv7 and Data Augmentation
## 2 Materials and Methods
### Data Augmentation  
When establishing a deep learning-based object detection model for _Camellia oleifera_ fruit, a high-quality dataset with a large amount of image data can improve the quality of model training and prediction accuracy. Therefore, the acquired _Camellia oleifera_ fruit images should be augmented [30].  
Several image augmentation methods were utilised for the 606 images of the training set to improve the generalisation ability and avoid the overfitting of the detection model. These methods were based on Pycharm software and its related image processing library. The image augmentation methods included horizontal mirroring, vertical mirroring, brightness enhancement and reduction, multi-angle rotation (90\({}^{\circ}\), 180\({}^{\circ}\), 270\({}^{\circ}\)), adding Gaussian noise and Mosaic data augmentation. The detailed steps of the image augmentation methods are illustrated as follows.  
Multi-angle rotation of an image enables the deep learning model to learn more object features in different positions and directions during training. OpenCV function \"cv2.getRotationMatrix2D\" and \"cv2.warpAffine\" based on Python were employed to rotate the original image. Image rotation was conducted by changing the parameter \"angle\" in the function as 90\({}^{\circ}\), 180\({}^{\circ}\) and 270\({}^{\circ}\).  
Image mirroring (horizontal and vertical mirroring) can increase the viewing angle of the _Camellia oleifera_ fruit. Opencv function 'flip' was used to mirror the original image. The image was divided into left and right parts for symmetrical transformation of the image centred on the vertical axis to achieve horizontal mirroring when the parameter 'dim' was set to 1. The image was divided into upper and lower parts for symmetric transformation of the image centred on the horizontal axis to achieve vertical mirroring.  
Image brightness was enhanced and reduced. The complex light conditions of the plantation caused differences in _Camellia oleifera_ fruit images, thereby interfering with the detection results. Therefore, the values of the three channels of the pixel points of the original image were multiplied by 0.5 and 1.5 to enhance and reduce the brightness of the image. This method improved the robustness of the model.  
Adding Gaussian noise to the image was also conducted. The unclear or blurred images captured by the shaking of the equipment or the branches would affect the accuracy of the detection model. A Gaussian noise with a parameter'sigma' of 25 was added to the original image to simulate the low-quality image that the model may capture in practical applications.  
Mosaic data augmentation was performed referring to CutMix data augmentation method. During training, the input size of the model was assumed as S \(\times\) S and a 2S \(\times\) 2S grey image was marked as a canvas. A point from the rectangle framed by point A (S/2, S/2) and point B (3S/2, 3S/2) was set as the reference point coordinate. Four images were randomly selected and stitched into the image by random scaling, cutting and arrangement. The images and labelled boxes beyond the canvas were ignored. Mosaic data augmentation increased the training data in each BatchSize without increasing the BatchSize to reduce the memory requirements of the model. The mean and variance of each feature layer were calculated during the batch normalisation (BN) operation and were closer to the mean and variance of the entire dataset. Mosaic data augmentation enriched the background of the image, and the image formed by splicing multiple images added numerous small-object _Camellia oleifera_ fruit, thereby improving the detection accuracy of the detection model.  
The final augmented training set consists of 5854 images, including 606 original images and 5248 enhanced images. The detailed distribution of the augmented training set is shown in Figure 3.