Episode 31 | Speed Estimation using Ultralytics YOLOv8

Ultralytics
1 Mar 202407:32

Summary

TLDRThis video demonstrates how to use Ultralytics YOLOv8 for vehicle speed estimation. It walks through the Ultralytics documentation on setting up a speed estimation pipeline, explaining the code needed to track objects over video frames and calculate speed based on pixel movement and frame rate. The video shows sample footage of traffic scenes, running the code to display estimated speeds overlayed, around 30km/hr in one clip and 90-100km/hr matching speed limit signs in the other. It encourages trying the open-sourced code yourself for custom applications like traffic control, autonomous navigation or surveillance.

Takeaways

  • 😀 Uses YOLOv8 model from Ultralytics for object detection
  • 🚗 Applies tracking to detected objects over frames to estimate speed
  • 📝 Calculates speed by tracking pixel distance traveled across frames
  • ⏱ Frame rate from video/camera needed to convert pixel distances to speed
  • 🎥 Shows examples using traffic/driving videos to estimate vehicle speeds
  • 🛣️ Gets reasonable speed estimates on highway video based on speed limit signs
  • 📊 Speed estimates not highly accurate due to unknown real-world distances
  • ⚙️ Can be used for various applications like traffic control and navigation
  • 🖥 Runs inference and tracking on live webcam or pre-recorded video
  • 💡 Suggestions welcomed to improve speed estimation system

Q & A

  • What model is used for object detection in the speed estimation application?

    -A pre-trained YOLOv8 model from Ultralytics is used for object detection.

  • How is speed estimated using object detection?

    -By tracking detected objects over multiple frames, calculating the pixel distance traveled, and using the frames per second rate to estimate speed.

  • What are some use cases for speed estimation?

    -Managing traffic flow, precise autonomous vehicle navigation, enhanced surveillance and security.

  • Why may the speed estimate not be completely accurate?

    -Because only the pixel distance traveled is known, not the real-world distance, so it is only an estimate.

  • How could the speed estimation be improved?

    -By using sensors to get real-world distance traveled rather than just pixel distance, or by calibrating the video footage to real-world distances.

  • What information is needed to estimate speed?

    -Detected and tracked objects over time, distance traveled in pixels between frames, and frames per second rate.

  • Can a custom trained model be used instead of YOLOv8?

    -Yes, you can train your own custom object detection model and integrate it into the speed estimation pipeline.

  • Does the speed estimate vary based on hardware?

    -Yes, GPU speed can affect the speed estimation, with faster GPUs potentially producing more accurate estimates.

  • Where can the code for speed estimation be found?

    -The code is available in the Ultralytics documentation and GitHub repo.

  • What libraries are used for speed estimation?

    -The core libraries are Ultralytics for object detection and tracking, OpenCV for image processing, and NumPy/Python for the pipeline.

Outlines

00:00

😊 Introducing speed estimation with YOLOv8 in Ultralytics

The first paragraph introduces the topic of speed estimation using a YOLOv8 model from Ultralytics to detect cars and estimate their speed from videos. It outlines the process of using object detection and tracking over frames to calculate speed based on pixel distance traveled over time.

05:01

👍 Showing sample speed estimation results on traffic videos

The second paragraph shows sample results of speed estimation on two traffic video samples - one with a closeup view estimating around 30km/hr, and one from a highway estimating around 90-100km/hr. It evaluates the accuracy to be reasonably good and suggests trying it on your own videos.

Mindmap

Keywords

💡Speed estimation

Speed estimation refers to calculating the speed of moving objects, like cars on a highway, using computer vision techniques. It is the main focus of the video. It involves detecting objects with YOLO models, tracking them across video frames, and estimating speed based on distance traveled between frames.

💡YOLO model

YOLO (You Only Look Once) is a type of neural network model used for real-time object detection. The video uses a pretrained YOLOv8 model from Ultralytics to detect cars in the input videos before tracking them.

💡Object tracking

Object tracking means following the path of detected objects across consecutive video frames over time. It is needed to estimate how far the object has moved, which is used to calculate speed.

💡Pixel distance

Pixel distance refers to the distance traveled by objects in terms of pixels in the video frames. This gives an estimate of real-world distance traveled used to calculate speed, but is less accurate than real measurements.

💡Frames per second

Frames per second (FPS) indicates the rate at which consecutive images are captured in a video. Knowing the FPS allows speed calculation by determining how much time elapsed between frames.

💡Traffic monitoring

Monitoring vehicle traffic flow and speed on roads and highways is a major use case for speed estimation. It can aid traffic management, control, and security.

💡Autonomous navigation

Self-driving vehicles can use speed estimation for functions like adaptive cruise control. It allows them to gauge their speed and the speed of other objects around them.

💡Surveillance

Speed estimation can enhance video surveillance by detecting suspicious speed profiles - like people moving too fast in restricted areas.

💡Custom models

The video mentions the option to train custom YOLO models on custom data instead of using pretrained models. This allows detecting new classes of objects to track.

💡GPU speed

GPU hardware accelerates neural network inference. But differences in GPU speeds can affect the accuracy of speed estimation across systems.

Highlights

We'll use a YOLOv8 model for object detection to track cars over frames and estimate their speed

Speed estimation helps with traffic control, autonomous navigation, and enhanced surveillance

We track detected objects over time to calculate the distance they travel across frames

Combining object tracking with frame rate, we can estimate speed in pixels per second

The code can work with a video file or webcam stream for real-time speed estimation

We draw a line to trigger speed calculation when objects cross it

Close-up traffic view initially shows speeds around 30 km/h which seems reasonable

Highway view correctly estimates speeds around 90-100 km/h matching signs

Varying speeds demonstrate accuracy, like slower traffic merging onto highway

Improvements could better handle dense traffic with more missed detections

Try this on your own videos to estimate speed for custom objects

Use these speed estimation techniques in your own applications

Throw suggestions to improve the system in the comments

Follow ultralytics on YouTube for more machine learning content

Use this speed estimation tutorial in your own projects for inspiration

Transcripts

play00:00

hey guys WC to video in this video here we're  going to see how we can do speed estimation with  

play00:03

ultra ltic we're going to use a YOLO V8 model a  pre-trend model for detecting cars and then we're  

play00:08

going to do beat estimation of cars driving in  different videos we're going to show you a bunch  

play00:12

of different examples I'm going to show you how  you can set up the code and also the alter ltic  

play00:16

documentation so let's just jump straight into it  and see how it works so let start inside our alter  

play00:20

litics documentation if we go inside this guides  tab we can find all the guides and also the real  

play00:25

world projects that we have you can also find them  on our GitHub but right now let's scroll down to  

play00:29

the real world projects we have a lot of different  tutorials in here you can directly go in and copy  

play00:34

paste Coast nit using your own applications and  projects if you scroll a bit further down we can  

play00:38

now see we have this speed estimation first of  all you can read about what is speed estimation  

play00:43

what can be used for and so on so the basic idea  behind speed estimation is that we're going to  

play00:47

use a Yol V8 model from ultral litics for optic  detection then we're going to apply tracking on  

play00:53

top of that so we're actually tracking our Optics  over a number of frames so we track our Optics  

play00:57

over time with that information we can actually  use it to calculate and estimate the speed that  

play01:02

the cars and objects are moving with so it can act  like be an arbitrary object it doesn't have to be  

play01:06

cars it can be whatever object that you have you  can also train your own custom models and use it  

play01:11

directly in here as well once we have all trct  objects in the frame we also know like how many  

play01:16

frames per seconds we get from either a video or  our webcam stream by using that information we  

play01:21

actually like know the whole time frame so we get  our detections over time so now we actually have  

play01:26

all the information for calculating and estimating  our speed so we have our track objects over time  

play01:31

we also know the distance in the frame that  it has traveled once we have that information  

play01:35

together with knowing the number of frames per  seconds either from our camera the the video  

play01:40

that we're processing could be like for a CCTV  camera sitting alongside Highway for example  

play01:45

then we have all the information we can calculate  this speed and at least estimate it because right  

play01:50

now we're just talking about a pixel distance  and not like a real world absolute distance so  

play01:55

here we can see some of the advantages of speed  estimation so efficient traffic control ACC ACC  

play02:00

speed estimation AIDS in managing traffic flow  and so on precise autonomous navigation and also  

play02:04

enhanced surveillance security so it can either  be like how far are like people walking around  

play02:09

inside a store could also be like how fast our  car is driving at a highway which is probably  

play02:14

like the best use case for this specific speed  estimation application so we're going to show a  

play02:19

couple of examples for videos on a highway where  we can see the results for the speed estimations  

play02:25

real well applications transportation and here  we can see the coast nit you can directly take  

play02:29

it cover paste it into your own python script and  use it in your own applications and projects we  

play02:33

just need to specify our model could also be our  own custom model that we are trained with ultra  

play02:38

ltic so just be aware here that the speed is an  estimate we can't really get like very accurate  

play02:43

values because again we can't really know the  exact distance that the car has traveled in the  

play02:48

real world only based on pixels so that is one of  the main problems with this one here but again we  

play02:53

will get estimates we get a rough estimate and  we kind of like know how Optics are moving and  

play02:58

also how fast so speed will be an estimate and  not may not be completely accurate Additionally  

play03:02

the estimation can vary depending on GPU speed  here we can see the different arguments that  

play03:06

you can set for the speed estimation function  I'm going to show you that in just a second and  

play03:11

we also have our model of track for tracking  our objects over a number of frames so let's  

play03:16

just some straight into the code here I've copy  pasted codit from the AL L documentation directly  

play03:20

in here in an empty python script from scratch  so again we're just creating an instance of our  

play03:25

YOLO model right now we're just going to go with  the medium model you need to specify the video  

play03:29

have here or if you want to use your webcam you  can also do that again then we're just going to  

play03:34

set everything up we are going to have a video  writer so we can see the results later on the  

play03:38

only modification that I've done to the code from  the alter L documentation is that we're going to  

play03:42

have some line points and I'm just going to have  a horizontal line in the middle of the image and  

play03:47

it's basically just to determine when do we want  to actually like calculate the speed of our track  

play03:51

objects once it's Crossing that lines then we're  going to set up our speed es theator we can set  

play03:56

the different arguments that I talked about so we  have our points we have the names and we also want  

play04:00

to specify that we want to view the image while  it's doing inference we have a [ __ ] here just  

play04:05

reading in frames from a video or from a webcam  we track the objects here over number frames we're  

play04:10

going to estimate the speed with our tracks and  also with our image and that's pretty much it this  

play04:15

is how we can do speed estimation with ultra ltic  so let's now just go over a couple of examples I  

play04:19

have some video files over here to the left that  we going go through so this is the first example  

play04:23

that we're going to run through it's more like  a close off traffic view then we're also going  

play04:27

to have another video here which is basically  like a view further away we probably want to be  

play04:31

able to detect every single car here in the image  but we're going to get way more um detections out  

play04:36

here then we're going to have the line and try to  see how the speed is on the highway so yeah let's  

play04:41

just copy path here and let's go in and run it  so I'm going to copy the relative path paste it  

play04:46

in up here is the only thing that you have to do  and then you can run it on your own wi dreams as  

play04:51

well use it in your own applications and projects  let's now run the program here and see the results  

play04:55

for our speed estimation so this is the closeup  video of these cars driving now I have to cross  

play05:01

this line before we're going to do the speed  calculations we see that the cars are driving  

play05:04

around 30 km per hour both going out and in of  damage so this is probably pretty pretty accurate  

play05:10

again um sometimes we get some mispredictions here  we have 56 kilm per hour so it's not like very  

play05:16

exact all the time but most of them is act like  around 30 km per hour which makes sense in this  

play05:21

traffic scenario so this is act like a pretty nice  example let's go in and grab the other one so that  

play05:27

will be this video here just copy the relative  path just swap it out everything that you have to

play05:33

do and we can now run our program again and see  the results so this is a bit more further W view  

play05:43

let's see how it performs so we're going to miss a  lot of detections but if we act like able to track  

play05:47

the cars and they're Crossing this line we're  going to estimate the speed on average here when  

play05:52

I'm just taking a look at it it is around like  90 to 100 kilm per hour right now it's going like  

play05:57

relatively fast because I have a GPU just proc  procing every single frame but it's also going to  

play06:01

store a video in your directory so we can go in  and take a look at that afterwards but it looks  

play06:06

pretty good here on the highway around 90 90 km  per hour it's a bit dense traffic so let's go and  

play06:12

take a look at the video we have speed estimation  avi file explorer let's now open it up and take a  

play06:18

look at it so the car here is actually like moving  pretty fast in the video as well so it was not  

play06:22

really just my GPU let's take a look at it here  97 120 km 80 km here so that could actually like  

play06:28

be pretty close to uh to the ground Turf we can  see we have a a traffic sign here saying 100 km  

play06:35

per hour so this is like the maximum speed limit  so this could actually be pretty pretty close like  

play06:40

we have some guys here driving like 908 km per  hours here we can see that we have some examples  

play06:44

with 55 and also 63 here but this is more like a  Runway to the highway uh before they're going to  

play06:50

merge down here um further on here we can see we  have a traffic sign saying 80 so's just take a  

play06:56

look at that so it's actually like going in the  opposite direction but we can see here here once  

play06:59

the car start out they have like a value of 80  and also over here they're driving 64 to 70 km  

play07:06

per hour so that's pretty much it for this video  here definitely try it out on your own computer  

play07:10

you can just take the code name it from the ultra  L documentation test it out on your own videos if  

play07:15

you have any suggestions to how this system  here could be improved definitely throw it  

play07:18

down in the comment section and also follow  allytics here on YouTube so I hope you have  

play07:23

learned a ton this video here and that you can  use this in your own projects and applications  

play07:27

maybe getting some inspiration and then just  see in the next video Until Then happy learning