A vehicle reverse driving detection algorithm based on centroid tracking and yolov5
By introducing Shuffle Attention and SIoU loss function into the YOLOv5 model and combining it with the centroid tracking algorithm, the accuracy problem of vehicle reverse driving detection under different lighting conditions was solved, achieving a detection effect with low false positives and false negatives.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-20
- Publication Date
- 2026-03-17
AI Technical Summary
Existing methods for detecting vehicles driving in the wrong direction are not very accurate under different lighting conditions, and magnetic sensor systems are easily affected by external factors, resulting in high rates of missed detections and false positives.
A vehicle inversion detection algorithm based on centroid tracking and Yolov5 is adopted, and the Shuffle Attention mechanism and SIoU loss function are introduced. By combining spatial and channel attention mechanisms, the computational load is reduced and the detection performance is improved. At the same time, the vehicle direction is detected in consecutive frames through the centroid tracking algorithm.
It effectively reduces the rate of missed detections and false judgments of vehicles driving in the wrong direction, and improves the accuracy and efficiency of detection, especially performing excellently under complex lighting conditions.
Smart Images

Figure CN116453072B_ABST
Abstract
Description
Technical Field
[0001] This invention proposes a vehicle reverse driving detection algorithm based on centroid tracking and YOLOv5, which is based on centroid tracking and YOLOv5. It introduces a Shuffle Attention mechanism module, adds group convolution to reduce the amount of computation, and uses spatial and channel attention mechanisms to improve the detection performance of the algorithm. In addition, considering the vector angle between expected regression, the SIoU loss function is adopted to effectively reduce the false negative rate and false positive rate of vehicle reverse driving. Background Technology
[0002] Drivers driving against traffic for various reasons increase traffic flow on one side and the likelihood of head-on collisions. Computer vision technology is widely used in traffic monitoring systems. It can analyze video clips captured by roadside cameras to identify the causes of traffic accidents, congestion, and traffic violations, track vehicles, and calculate their speeds. In the past, such detection was very difficult due to high computational costs and unclear data sources. However, with improvements in machine learning and deep learning algorithms, advanced image processing algorithms, and more cost-effective cameras and monitoring equipment, the entire detection system has become much more efficient.
[0003] To date, we have designed many systems for detecting vehicles traveling in the wrong direction. Existing methods can be broadly categorized into sensor-based methods and optical flow estimation-based methods. Sensor-based methods primarily use magnetic sensors to detect vehicle direction. During vehicle operation, the Earth's magnetic field is subject to interference, and magnetic sensors detect vehicle direction based on these changes in the magnetic field. However, systems like magnetic sensors are susceptible to many unforeseen factors, resulting in a high degree of randomness and sometimes unsatisfactory detection results. Another approach uses optical flow calculations to detect the driving direction and compares it with the modeled lane direction to determine if the vehicle is traveling in the wrong direction. However, this system is affected by lighting conditions, and detection results can vary significantly under different lighting conditions. Summary of the Invention
[0004] This invention aims to more accurately detect vehicles traveling in the wrong direction under varying lighting and other external factors. Therefore, it proposes a vehicle wrong-way detection algorithm based on centroid tracking and YOLOv5. Building upon YOLOv5, a ShuffleAttention mechanism is introduced, along with spatial and channel attention mechanisms to improve detection performance. Furthermore, considering the vector angle between expected and regressive vectors, a SIoU loss function is adopted. Combined with centroid tracking technology, this effectively reduces the false negative and false positive rates for vehicles traveling in the wrong direction.
[0005] To achieve the above objectives, the technical solution of the present invention is as follows:
[0006] A vehicle reverse driving detection algorithm based on centroid tracking and YOLOv5 includes the following steps:
[0007] Step 1: Input video frames captured by surveillance cameras. Through the introduction of the Shuffle Attention mechanism module and the SIoU loss function of the Yolov5 model, feature extraction, grouping and fusion are performed, and the output is a predicted bounding box.
[0008] Step 2: Input the vehicle's bounding box into the centroid tracking algorithm based on the centroids of the old and new objects;
[0009] Step 3: Detect vehicles with incorrect orientation by comparing the centroid height of consecutive frames.
[0010] The specific details of step one are as follows:
[0011] This experiment utilizes surveillance to record the road environment, and then feeds the video frames into an improved YOLOv5 model for vehicle identification.
[0012] The improvements in the YOLOv5 model include the introduction of a Shuffle Attention mechanism and a SIoU loss function. Attention mechanisms can be broadly categorized into two types: spatial attention and channel attention. Using both can effectively improve detection performance but also increases computational cost. The Shuffle Attention mechanism efficiently combines these two mechanisms. Its main function is feature grouping, which is then aggregated after applying both spatial and channel attention.
[0013] Given feature map C, H, and W represent the number of channels, spatial height, and width, respectively. ShuffleAttention divides X into G groups along the channel dimension: ,in For sub-features, during training, each sub-feature sequentially acquires a response and generates a coefficient through the attention module. At the moment each attention unit begins responding, it will... The passage splits into two branches, namely These two branches are the spatial and channel attention mechanisms, respectively. In the channel attention mechanism, global average pooling is used to generate channel statistics.
[0014]
[0015] in: Furthermore, channel attention achieves adaptive feature selection through a gating mechanism, implemented using a sigmoid activation function. The final output of the channel attention mechanism is:
[0016]
[0017] in: Both are used for translation and scaling.
[0018] In spatial attention mechanisms, Use Group Norm to obtain spatial statistics, and then utilize To enhance the feature representation, the final output is:
[0019]
[0020] in: .
[0021] Among them , The parameters of Group Norm are just those in the Shuffle Attention module. In a single module, the number of channels per branch is C / 2G, and the total number of parameters is 3C / G, with G mostly being 32 or 64. Therefore, the number of parameters is very small, and the entire module is lightweight.
[0022] The outputs from the two branches are concatted to ensure the number of channels matches the number of inputs. The concatted output is then shuffled to reassemble the feature maps, enabling information exchange between different groups. This facilitates cross-group communication and makes Shuffle Attention easily integrated into YOLOv5 networks.
[0023] For detecting vehicles driving in the wrong direction, a loss function with fast convergence speed and high efficiency is often required. However, arbitrarily changing the position of the prediction box during training may result in a poor-performing model. Therefore, SIoU adds the vector angle between the expected regression and redefines a penalty metric.
[0024] The SIoU loss function consists of four cost functions: Angle cost, Distance cost, Shape cost, and IoU cost.
[0025] Angle cost was proposed to minimize the number of distance-related variables. The model attempts to first bring the prediction to the X or Y axis, using the closest one, and then continues to approach along the relevant axis. If the angle α between the predicted box and the ground truth box and the X axis is less than or equal to 45°, then α is minimized first during convergence; otherwise, the angle β between the predicted box and the Y axis is minimized.
[0026]
[0027] To achieve this goal, a loss function component is introduced:
[0028]
[0029] in
[0030]
[0031]
[0032]
[0033] in The distance between the center points of the ground truth bounding box and the predicted bounding box. The height difference between the center points of the ground truth bounding box and the predicted bounding box can be calculated using the Pythagorean theorem. , These are the coordinates of the center of the true bounding box. , These are the coordinates of the center of the prediction box.
[0034] Taking angle loss into account, SIoU redefines distance cost:
[0035]
[0036] in
[0037]
[0038] , The width and height of the minimum bounding rectangle for the ground truth bounding box and the predicted bounding box.
[0039] The shape cost is as follows:
[0040]
[0041] in
[0042]
[0043] These are the width and height of the predicted bounding box and the ground truth bounding box, respectively. Control the level of attention paid to shape loss.
[0044] The Intersection over Union (IoU) Cost is defined as follows:
[0045]
[0046] Where B is the predicted bounding box. It is a real frame.
[0047] Finally, the regression loss function is defined as:
[0048]
[0049] During implementation, simply adding a Shuffle Attention module to the Head part of the YOLOv5 network structure and adding SIoU to the Loss can improve YOLOv5.
[0050] The specific details of step two are as follows:
[0051] The centroid tracking algorithm takes the bounding box output by the improved Yolov5 as input, receives the coordinates and uses them to calculate the centroid, assigning a unique ID to each calculated centroid.
[0052] After obtaining the ID, for each subsequent frame in the video stream, to avoid compromising object tracking, the centroid tracking algorithm first needs to determine whether a new object centroid can be associated with an old one. That is, it determines whether a new centroid is formed due to the movement of an old centroid or is a completely new centroid entering the algorithm, rather than directly assigning a new ID to each detected object. The algorithm assumes that each object moves very little between subsequent frames. If the distance between the new and old centroids is minimized, it can be considered that the object has been identified before, rather than being a new object, and the object's centroid will be updated. To accomplish this, the algorithm calculates the Euclidean distance between each pair of old and new centroids. (Pixels) The Euclidean distance is:
[0053]
[0054] If there are fewer objects in the current frame than in the previous frame, it means that one or more objects have disappeared from the detection area. Since their centroids are not within the detection area, the IDs of these vehicles will be deleted. However, if the number of objects in the current frame exceeds that of the previous frame, there must be a new object. In this case, the algorithm determines the old object based on the Euclidean distance, updates the centroid of the old object, and the remaining objects are the new objects, which need to be assigned new IDs. In this way, the algorithm can track each vehicle independently.
[0055] The specific details of step three are as follows:
[0056] The algorithm detects whether a vehicle is traveling in the wrong direction by measuring the vehicle's centroid height across consecutive frames. Each tracked vehicle has its own centroid height. When a vehicle is identified and given a unique ID, its centroid height is calculated. It is stored in the file corresponding to the ID, and in the next frame, the height of its new centroid after the movement is calculated. And its ID is stored in another file. It will be continuously updated in each consecutive frame; if the vehicle moves, the vehicle's... and They will be unequal, by comparison. and The size of the object can be used to predict the direction of the vehicle.
[0057] Assuming the surveillance camera in a real-world scenario faces the right lane, and a vehicle moving towards the camera is considered to be traveling in the correct direction, then if a vehicle moves away from the camera, it will be detected as traveling in the wrong direction. Therefore, if So, the vehicle is heading towards the monitoring point and driving in the correct direction; otherwise, the vehicle is driving in the wrong direction. The opposite situation can also be defined by changing the conditions. Upon detecting a vehicle driving in the wrong direction, a frame will be automatically captured for further inspection. Attached Figure Description
[0058] Figure 1 This is a schematic diagram of the improved Yolov5 model used in this invention;
[0059] Figure 2 This is a structural diagram of the Shuffle Attention mechanism module used in this invention;
[0060] Figure 3 This is a schematic diagram of the Angle cost portion of the SIoU used in this invention;
[0061] Figure 4 This is a schematic diagram of the Distance Cost part in the SIoU used in this invention;
[0062] Figure 5 This is the overall flowchart of the present invention. Implementation
[0063] For those skilled in the art, certain well-known structures and their descriptions in the accompanying drawings may be omitted. The technical solution of the present invention will be further described below with reference to the accompanying drawings and embodiments.
[0064] This invention provides a vehicle reverse driving detection algorithm based on centroid tracking and YOLOv5. This method has high accuracy and fast speed in the process of vehicle reverse driving detection, and has excellent performance in handling detection when affected by external factors.
[0065] First, video frames captured by traffic surveillance are input into an improved Yolov5 object detection algorithm. Then, the predicted bounding boxes generated by the improved Yolov5 algorithm are input into a centroid tracking algorithm. This algorithm tracks each vehicle detected by the improved Yolov5 algorithm. Finally, by calculating the centroid height of each vehicle in consecutive frames, it determines whether the vehicle is moving away from or close to the surveillance camera. The overall accuracy of the system depends on the detection performance of the improved Yolov5 algorithm. If the detection is correct, the false positive rate of this algorithm is very low.
[0066] The specific implementation steps are as follows:
[0067] Step 1.1 Select a suitable dataset of traffic camera footage, such as a public collection of enhanced random screenshots from Chicago traffic cameras: A Novel Camera Network Dataset for Traffic Flow;
[0068] Step 1.2: Add a lightweight Shuffle Attention mechanism module to the YOLOv5 model. Add a 24th Shuffle Attention layer to the Head part of the YOLOv5 network structure. The feature maps extracted from the video frames are processed through spatial and channel attention mechanisms, and the results are concatn and reassembled using channel shuffle.
[0069] Step 1.3 Add an SIoU loss function to the Yolov5 model. Define the loss function from four aspects: Angle cost, Distance cost, Shape cost, and IoU cost, and set the alpha value to 1.
[0070] Step 1.4 Adjust the input video frame size to 640×640 and set the batch size to 2;
[0071] Step 1.5 After training, the video frames captured by traffic monitoring are input into the network for detection to generate bounding boxes.
[0072] Step 2.1 The centroid tracking algorithm accepts the bounding box through the update method, calculates the centroid through the bounding box coordinates, initializes a NumPy array to store the centroids, and uses register to register new objects, assigning a unique ID to each calculated centroid;
[0073] Step 2.2 Calculate the Euclidean distance between the new bounding box and existing objects. The object with the smallest Euclidean distance between the old and new centroids is considered an existing object. All other objects are considered newly created objects and assigned a new ID.
[0074] Step 2.3 Update the centroid of the old object and use deregister to reset the disappeared centroid.
[0075] Step 3.1 After identifying the centroid, calculate the height change of the centroid of this object in consecutive frames. The most direct method is to record the change in the vertical axis value of the centroid coordinates. Calculate the centroid height of the object in the previous frame. The centroid height of the next frame is calculated and stored in file 1 along with its unique ID. ,Will The ID is stored in file 2;
[0076] Step 3.2 When the object is moved, Update in each consecutive frame. Compare and The size of the sensor can predict whether a vehicle is moving toward or away from the monitoring station.
[0077] Step 3.3 Define normal driving and reverse driving based on the orientation of the monitoring camera. Assuming the camera is facing the right lane, then driving towards the camera is considered normal driving. Therefore, if... If the centroid of the next frame is higher than that of the previous frame, and the vehicle is moving away from the monitoring, then the vehicle is identified as driving in the wrong direction.
Claims
1. A monitoring under vehicle reverse detection algorithm based on centroid tracking and improved Yolov5, characterized in that, The method comprises the following steps: Step1: input the monitoring video frame into the improved and trained Yolov5 target detection model, wherein a 24th layer Shuffle Attention lightweight attention mechanism module is added to the Head part of the Yolov5 network structure, so that the feature maps extracted from the video frame are subjected to feature calculation through the spatial attention mechanism and the channel attention mechanism respectively, the results of the two attention mechanisms are subjected to Concat processing, and the channels are reorganized by channel shuffle to obtain enhanced features for detection, a SIoU loss function is used in the bounding box regression of the Yolov5 target detection model, the SIoU loss function is composed of Angle cost, Distance cost, Shape cost and IoU cost and is weighted by α=1, and a vehicle prediction box is output by the improved Yolov5 target detection model; Step2: the center point of each vehicle prediction box is taken as the centroid of the corresponding vehicle in the current frame, and a centroid tracking algorithm with Euclidean distance as the matching criterion is used for cross-frame association, wherein a distance matrix is formed by a new frame centroid and a registered centroid of the last frame, the pair with the smallest distance and less than the threshold is selected for updating, the unpaired centroid is registered as a new object and is assigned a unique ID, and an ID that is not matched for more than a preset number of frames is unregistered; Step3: after the vehicle centroid is identified, the height change of the centroid of the vehicle in the continuous frames is calculated, and the vertical axis numerical change of the centroid coordinates is recorded; for each tracked vehicle, the centroid height H1 in the previous frame is calculated and saved in file 1 together with the corresponding unique ID, the centroid height H2 in the next frame is calculated and saved in file 2 together with the corresponding ID, the H1 and H2 are continuously updated in each continuous frame when the vehicle moves, and the movement direction of the vehicle, i.e. towards or away from the monitoring, is predicted by comparing the size of H1 and H2; when the camera is a head-up monitoring camera for the right lane and the vehicle moves towards the camera in the normal driving direction, if H2>H1, it indicates that the centroid in the next frame is higher than that in the previous frame, i.e. the vehicle moves away from the monitoring, and the vehicle is identified as a reverse vehicle and a reverse vehicle warning is output.
2. The monitoring vehicle reverse driving detection algorithm based on centroid tracking and improved Yolov5 according to claim 1, characterized in that, The specific process in Step 1 is as follows: Step1.1: select a suitable traffic camera dataset, such as the public collection of enhanced random screenshots of Chicago traffic cameras: A Novel Camera Network Dataset for Traffic Flow; Step1.2: add a Shuffle Attention lightweight attention mechanism module to the Yolov5 target detection model, add a 24th layer Shuffle Attention layer to the Head part of the Yolov5 network structure; the feature maps extracted from the video frame are subjected to spatial and channel attention mechanisms, and the results are subjected to Concat processing and channel shuffle reorganization of the features; Step1.3 Add SIoU loss function in Yolov5 target detection model, define loss function from Angle cost, Distance cost, Shape cost, IoU cost four aspects, and set alpha value to 1; Step1.4 Adjust the input video frame size to 640*640, and set Batch size to 2; Step1.5 After training, input the video frame captured by traffic monitoring camera into the network detection to generate the bounding box.
3. The monitoring vehicle reverse driving detection algorithm based on centroid tracking and improved Yolov5 according to claim 1, characterized in that, The specific process in Step 2 is as follows: Step2.1 Centroid tracking algorithm accepts the bounding box through update method, calculates the centroid through the bounding box coordinates, initializes NumPy array to store the centroid, and registers the new object by using register, and assigns a unique ID to each calculated centroid; Step2.2 Calculate the Euclidean distance between the new bounding box and the existing object;By calculating the Euclidean distance between the old centroid and the new centroid, the smallest distance means that it is an existing object;In addition to the existing object, other objects are identified as new objects, and a new ID is assigned; Step2.3 Update the centroid of the old object, and reset the disappeared centroid by using deregister.
4. The monitoring vehicle reverse driving detection algorithm based on centroid tracking and improved Yolov5 according to claim 1, characterized in that, The specific process in Step 3 is as follows: Step3.1 After identifying the centroid, calculate the height change of the centroid of this object in the continuous frame, the most direct method is to record the vertical axis value change of the centroid coordinates;Calculate the centroid height H1 of the object in the previous frame, and save it in file 1 with its unique ID;Calculate the centroid height H2 of the next frame, and save H2 and ID in file 2; Step3.2 When the object moves, H2 is updated in each continuous frame;Compare the size of H1 and H2, you can predict whether the vehicle is moving towards the monitor or away from the monitor; Step3.3 Define normal driving and reverse driving according to the direction of the monitor, assuming that the monitor is the right lane head-up monitor, then if the vehicle moves towards the monitor, it is forward;Therefore, if H1 < H2, it means that the centroid in the next frame is higher than that in the previous frame, which is away from the monitor, then the vehicle is identified as reverse driving.