1.
One way is to set up two light sensors so a vehicle will drive past the first, then the second. Measure the distance between them.
You time the interval between the interruptions. Distance divided by time gives you the speed of a vehicle.
2.
Or do you want a computer to watch a video of the traffic, and measure traffic flow? That will require a sophisticated algorithm.
Each frame must be digitized and sent to the computer.
Your program looks at the region where vehicles first appear.
When a bunch of pixels changes to a different color in that region, then your program calls that a vehicle.
Record the time it crosses a timing mark.
When the next frame arrives, look along the range of positions where a vehicle would normally be.
Keep track of vehicle positions from one frame to the next, until it passes the second timing mark.
As in method #1, you need to measure the real distance between your timing marks.
You calculate the vehicle's speed based on the quantity of frames it takes to travel between the two marks.