Traffic vehicle counting method based on improved YOLOv8
By introducing the LSKA attention mechanism and ByteTrack algorithm in the YOLOv8 network, the traffic vehicle counting method is improved, and the false alarm and missed response problems of traditional methods in complex environments are solved, achieving high-precision and low-cost real-time vehicle tracking counting.
Patent Information
- Application Number
- CN202411781730.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-05
- Publication Date
- 2025-04-18
AI Technical Summary
Traditional vehicle counting methods are prone to false alarms and missed alarms in complex environments, and data processing cannot be monitored and dynamically adjusted in real time, which is costly.
Based on the improved YOLOv8 traffic vehicle counting method, the LSKA attention mechanism is introduced, and the improved neural network and ByteTrack algorithm are used to realize accurate identification of vehicles and real-time tracking counting.
It improves the accuracy of vehicle identification, realizes real-time and dynamic vehicle tracking and counting in complex environments, and reduces the cost requirements of hardware equipment.
Smart Images

Figure CN119851221A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of target detection technology, specifically relating to a traffic vehicle counting method based on an improved YOLOv8. Background Technology
[0002] With the continuous increase in the number of vehicles in cities, increasing pressure has been placed on road traffic order and road safety, making vehicle counting a crucial aspect of traffic management and urban planning. Traditional vehicle counting primarily relies on physical detection by sensors, video surveillance combined with image processing, ground loops, and radar sensor technology.
[0003] Traditional methods are affected by environmental factors, such as complex weather and terrain, which can easily lead to false alarms and missed alarms. Furthermore, they cannot monitor and adjust data in real time, and the cost of deploying sensors and cameras is enormous. Summary of the Invention
[0004] The technical problem of this invention is to propose a traffic vehicle counting method based on an improved YOLOv8, which can accurately identify and count traffic vehicles during the tracking and counting process at road intersections, regardless of the lighting environment or vehicle obstruction.
[0005] The technical solution of the present invention is a traffic vehicle counting method based on an improved YOLOv8. According to the image difference characteristics of past vehicles, the LSKA attention mechanism is introduced to improve YOLOv8. The counting method includes the following steps: S1: Capture vehicles coming and going at city road intersections, preprocess the captured video to obtain dataset D1; S2: Construct an improved YOLOv8 neural network; S3: Input the training set from dataset D1 into the improved YOLOv8 network for training; S4: Validate the trained improved YOLOv8 network on the test set in dataset D1; S5: Optimize the trained model using the ByteTrack algorithm.
[0006] Furthermore, in step S1, the act of photographing vehicles passing through the city road intersection includes using drones and high-definition cameras to photograph cars, trucks, and buses at the road intersection.
[0007] Preferably, in step S1, the preprocessing of the captured video includes the following sub-steps: 1) Trim and crop the captured video; 2) The processed images are rotated, flipped, color-changed, noise-added, translated, and perspective-transformed for data augmentation; 3) After manually marking the enhanced image, perform format conversion.
[0008] Furthermore, the improved YOLOv8 neural network includes a Backbone module, a Neck module, and a Head module. The Backbone module contains multiple convolutional layers, multiple C2F layers, and an SF_LA layer connected in sequence. The Neck module contains multiple upsampling layers, a Concat layer, and a C2F layer. The Head module is used for object detection and classification.
[0009] Furthermore, the SF_LA layer adds an LSKA attention mechanism to the SPPF structure of the YOLOv8 network, enhancing feature extraction and fusion capabilities.
[0010] Preferably, the LSKA attention mechanism decomposes the two-dimensional convolutional kernel of the deep convolutional layer into cascaded horizontal and vertical one-dimensional convolutional kernels, captures the local contextual information of the image through standard deep convolution, and then uses deep dilated convolution with a larger receptive field kernel to simulate long-range dependencies.
[0011] Furthermore, step S5 also includes tracking and counting vehicles in the video using the ByteTrack algorithm, as shown below: S51: Obtain the detection box and the corresponding detection score through the detector, divide the detection box into a high confidence group and a low confidence group, and then match the detection box with the tracker; S52: Based on the matching status of the detection boxes and trackers in S51, create trackers for new detection boxes that have not been matched with trackers; for trackers that have been matched, update their state using the corresponding detection box information, and predict the vehicle's position in the next frame based on the motion model, thereby continuously tracking the vehicle. S53: Unmatched detection boxes and trackers are processed separately. Unmatched detection boxes are analyzed to determine whether they are newly appearing vehicles or false detections to decide whether to keep or discard them. Unmatched trackers are judged according to a set frame threshold to determine whether the vehicle has disappeared. If the threshold is exceeded, the tracker is deleted to avoid invalid tracking. S54: Determine the counting rules, such as counting when a vehicle enters a specific area or passes through a counting segment and is tracked stably, and set a counting marker to avoid counting the same vehicle repeatedly, so as to ensure accurate statistics.
[0012] Compared with the prior art, the beneficial effects of the present invention include: 1) This invention is based on an improved YOLOv8 traffic vehicle counting method. On the basis of the SPPF structure of the YOLOv8 network, the LSKA attention mechanism is introduced to increase feature extraction and fusion capabilities and improve the accuracy of passing vehicle recognition.
[0013] 2) This invention is based on an improved YOLOv8 traffic vehicle counting method. Through real road scenarios, it is unaffected by environmental factors and complex weather and terrain, and tracks passing vehicles at road intersections in real time and dynamically.
[0014] 3) This invention is based on an improved YOLOv8 traffic vehicle counting method. Deploying it to hardware devices to increase the counting of passing vehicles does not require replacing the equipment, does not incur huge costs, and has high real-time performance. Attached Figure Description
[0015] The present invention will be further described below with reference to the accompanying drawings and embodiments.
[0016] Figure 1 This is a flowchart of a traffic vehicle counting method according to an embodiment of the present invention; Figure 2 This is a structural diagram of the improved YOLOv8 model according to an embodiment of the present invention; Figure 3 This is a diagram of the SF_LA layer structure of the Backbone module in the YOLOv8 model according to an embodiment of the present invention. Figure 4 This is a structural diagram of the SF_LA layer in an embodiment of the present invention. Detailed Implementation
[0017] like Figure 1 As shown, a traffic vehicle counting method based on improved YOLOv8 is proposed. According to the image difference characteristics of past vehicles, the LSKA attention mechanism is introduced to improve YOLOv8. The counting method includes the following steps: S1: Capture vehicles coming and going at city road intersections, preprocess the captured video to obtain dataset D1; To film vehicles coming and going at city intersections, a DJI Mavic 3 drone was deployed over the Ronggui Bridge from the airport to film cars, trucks, and buses.
[0018] In step S1, the captured video is preprocessed, including the following sub-steps: 1) The captured video was extracted using OpenCV to obtain 3000 images, which were then cropped to obtain 12000 images with a resolution of 1920×1080. 2) The processed images are rotated, flipped, color-changed, noise-added, translated, and perspective-transformed for data augmentation; 3) After manually labeling the enhanced image using LabelImg, save it in XML format and convert the format to serve as input for the YOLOv8 model.
[0019] The dataset D1 was divided into a training set, a validation set, and a test set in a ratio of 7:2:1, resulting in a training set of 8400 images, a validation set of 2400 images, and a test set of 1200 images. S2: Construct an improved YOLOv8 neural network; like Figure 2 As shown, the improved YOLOv8 neural network includes a Backbone module, a Neck module, and a Head module. The Backbone module contains multiple convolutional layers, multiple C2F layers, and an SF_LA layer connected in sequence. The Neck module contains multiple upsampling layers, a Concat layer, and a C2F layer. The Head module is used for object detection and classification.
[0020] like Figure 3 As shown, the SF_LA layer, based on the SPPF structure of the YOLOv8 network, adds an LSKA attention mechanism to enhance feature extraction and fusion capabilities. Its calculation formula is as follows: ; ; ; ; In the formula, d represents the dilation rate, C represents the number of input channels, H and W are the feature map height and width, respectively, and * represents convolution. Represents the Hadamard product. This represents the output of a depthwise convolution. This represents the output of a depthwise convolution obtained by convolving kernel W with the input feature map F. This represents the attention feature map obtained after convolution following depthwise convolution. Indicates the input feature map, Representation of attention feature map With input feature map The Hadamard product.
[0021] like Figure 4 As shown, the LSKA attention mechanism decomposes the two-dimensional convolutional kernel of the deep convolutional layer into cascaded horizontal and vertical one-dimensional convolutional kernels, captures the local contextual information of the image through standard deep convolution, and then uses deep dilated convolution with a larger receptive field kernel to simulate long-range dependencies.
[0022] S3: Input the training set from dataset D1 into the improved YOLOv8 network for training; S4: Validate the trained improved YOLOv8 network on the test set in dataset D1; S5: Optimize the trained model using the ByteTrack algorithm.
[0023] Step S5 also includes tracking and counting vehicles in the video using the ByteTrack algorithm, as shown below: S51: Obtain the detection box and the corresponding detection score through the detector, divide the detection box into a high confidence group and a low confidence group, and then match the detection box with the tracker; S52: Based on the matching status of the detection boxes and trackers in S51, create trackers for new detection boxes that have not been matched with trackers; for trackers that have been matched, update their state using the corresponding detection box information, and predict the vehicle's position in the next frame based on the motion model, thereby continuously tracking the vehicle. S53: Unmatched detection boxes and trackers are processed separately. Unmatched detection boxes are analyzed to determine whether they are newly appearing vehicles or false detections to decide whether to keep or discard them. Unmatched trackers are judged according to a set frame threshold to determine whether the vehicle has disappeared. If the threshold is exceeded, the tracker is deleted to avoid invalid tracking. S54: Define the counting rules, such as counting when a vehicle enters a specific area or passes through a counting segment and is tracked stably, and set a counting marker to avoid counting the same vehicle repeatedly to ensure accurate statistics.
[0024] To enable those skilled in the art to better understand the present invention, the above embodiments are merely preferred technical solutions and should not be considered as limitations on the present invention. The scope of protection of the present invention should be defined by the technical solutions described in the claims, including equivalent substitutions of the technical features described in the claims. That is, equivalent substitutions and improvements within this scope are also within the scope of protection of the present invention.
Claims
1. A traffic vehicle counting method based on improved YOLOv8, characterized in that: According to the difference characteristics of past vehicle images, the LSKA attention mechanism is introduced to improve YOLOv8; The counting method comprises the following steps: S1: Shoot vehicles passing through the intersection of urban roads, pre-process the captured videos, and obtain data set D1; S2: Build and improve the YOLOv8 neural network; S3: Input the training set in the dataset D1 into the improved YOLOv8 network for training; S4: Use the test set in dataset D1 to verify the trained improved YOLOv8 network; S5: Optimize the trained model using the ByteTrack algorithm.
2. According to the traffic vehicle counting method based on improved YOLOv8 as claimed in claim 1, it is characterized in that: In step S1, photographing vehicles passing through urban road intersections includes photographing cars, trucks and buses at road intersections using drones and high-definition cameras.
3. According to the traffic vehicle counting method based on improved YOLOv8 as claimed in claim 1, it is characterized in that: In step S1, the preprocessing of the captured video includes the following sub-steps: 1) Capture and crop the captured video; 2) Rotate, flip, change color, add noise, translate, and transform perspective of the processed images for data enhancement; 3) After manually marking the enhanced images, perform format conversion.
4. The traffic vehicle counting method based on improved YOLOv8 according to claim 1, characterized in that: The improved YOLOv8 neural network includes a Backbone module, a Neck module and a Head module. The Backbone module includes multiple convolutional layers, multiple C2F layers and SF_LA layers connected in sequence. The Neck module includes multiple upsampling layers, Concat layers and C2F layers. The Head module is used for target detection and classification.
5. A traffic vehicle counting method based on improved YOLOv8 according to claim 4, characterized in that: The SF_LA layer adds the LSKA attention mechanism on the basis of the SPPF structure of the YOLOv8 network to increase the feature extraction and fusion capabilities. The calculation formula is: ; ; ; ; In the formula, d represents the expansion rate, C represents the number of input channels, H and W represent the height and width of the feature map respectively, * represents convolution, represents the Hadamard product, represents the output of the depthwise convolution, represents the output of the deep convolution obtained by convolving the kernel W with the input feature map F, Represents the attention feature map obtained after deep convolution. represents the input feature map, Represents the attention feature map With the input feature map The Hadamard product.
6. A traffic vehicle counting method based on improved YOLOv8 according to claim 5, characterized in that: The LSKA attention mechanism decomposes the two-dimensional convolution kernel of the deep convolution layer into cascaded horizontal and vertical one-dimensional convolution kernels, captures the local context information of the image through standard depth convolution, and then uses deep expansion convolution with larger receptive field kernel to simulate long-range dependency.
7. The traffic vehicle counting method based on improved YOLOv8 according to claim 1, characterized in that: The step S5 also includes tracking and counting vehicles in the video using the ByteTrack algorithm, as shown below: S51: Obtain a detection frame and a corresponding detection score through the detector, and divide the detection frame into a high confidence group and a low confidence group, and then match the detection frame with the tracker; S52: according to the matching status of the detection frame and the tracker in S51, a tracker is created for the new detection frame that is not matched with the tracker; for the matched tracker, its status is updated using the corresponding detection frame information, and the position of the vehicle in the next frame is predicted based on the motion model, so as to continuously track the vehicle; S53: Process the unmatched detection frames and trackers separately. The unmatched detection frames are analyzed to determine whether they are new vehicles or false detections to decide whether to keep or discard them. The unmatched trackers are judged according to the set frame number threshold to determine whether the vehicle disappears. If the threshold is exceeded, the trackers are deleted to avoid invalid tracking. S54: Determine counting rules, such as counting vehicles when they enter a specific area or pass through a counting line segment and are stably tracked, and set counting marks to avoid repeated counting of the same vehicle to ensure accurate statistics.
Citation Information
Patent Citations
Tunnel traffic flow detection system and method based on YOLOv8 algorithm
CN117746649A
Road defect target detection method and system based on SCDD-YOLO network
CN118115444A
High body seriola quinqueradiata detection method based on YOLOv8 network structure
CN118279935A
Road damage detection method based on improved YOLOv8
CN118521869A