I used Python to Measure Traffic Flow in Cities: Skylines

preview_player
Показать описание
I made a script in Python that detects, tracks and counts vehicles coming across an intersection. This demo is with a Cities: Skylines recording but it can work for IRL stream as well. I plan to use it to compare different intersection designs and do head-to-head battles between interchange types. At this stage the script is not in a state where I feel confident enough to share it. Once I make it more user-friendly I’ll definitely consider it.

This is NOT A MOD for Cities: Skylines, but a Python program that takes a video file as input. I don’t have the knowledge to make it into a mod (and there are probably better native ways to track traffic). However, if you think it there is a way to do it, please let me know :)

In the center you can see the foreground detection at work. It relies on the OpenCV library. Subtract the average background from each frame and apply a threshold, and you get the moving vehicles as white blobs. This step involves a lot of filtering and parameters, hence why it is not user-friendly just yet.

Using the motion vectors (in light blue), we can predict and track each individual vehicle position within the blue square. The throughput is defined as the number of vehicles exiting that blue square per unit of time. The live throughput in the graph is based on a 10s EMA, it’s also the metric used for the “max throughput” in the summary. The delay is the time at vehicle stays within the blue square.

The roundabout used in this demo is nothing much, just a highway circle with some TMPE tweaks, not optimized in any way. The traffic is generated by 3 equally sized “cities”, and held up by a traffic light to create a constant flow rate. In theory all movements should have the same flow, however you can see vehicles piling up in the two left lanes, that’s why I marked these movements as saturated. In these conditions the right-turns still have some unused capacity, that's why they make up for more than a third of the total.

Let me know in the comments if you have some questions or suggestions. For instance, which intersections you would like me to compare first?
Рекомендации по теме
Комментарии
Автор

To be honest I'm surprised there aren't any mods that do this in game already. That'd be far better.

MrMartinSchou
Автор

As a python beginner and a City skylines player, this truly amazed

ansafm.t.
Автор

This calls for a colaboration with either biffa or yumbl on their intersection flow vids

CakeboyRiP
Автор

Just for your own benefit. You can use a program called Junctions 10 to model irl roundabouts and priority junctions. If it is signal controlled you can use TRANSYT. For more complicated networks you can use Vissum, Aimsun, amongst others! Some of the outputs you get are similar to what you used python for here which I think was very impressive for you to use it in this way!

MrIkillmonkey
Автор

I have worked with a similar program (Data From Sky) with real life roundabouts. It uses so called gates (basicly a line you put down that detects if a car) in different forms. F.E a entry gate detects a vehicle entering the crossing and a exit gate does the same but this info can be combined to determin the direction the car takes (north to south for example). The program also has Waiting Gates. These detect the cars when they are stopped and how many cars are waiting behind it. Of course you have to give it definition of a stopped vehicle (speed lower then 3 km/h). Maybe that way you can solve your stopped car problem.

For that the junction needs to be in full view in order to detect a car.
In short DFS (data from sky) detects cars and tracks speed, tragectory etc and that combined with the mentioned gates can give you a load of information to solve your specific problems in CS.

I was looking for something like this, just to determin the overall flow of the intersection instead of eyeballing it haha. Keep it up and lets hope it makes it to the game :D

TurboVeer
Автор

That graphic show why you should make a separated right turning lane in a roundabount

mrjaman
Автор

This is great! You can also email city skyline for this, someday you hire as their dev 😊

bergZig
Автор

Are you uploading the code somewhere???? I was just about to write my own version (I did manage to count cars moving through a single pixel) and then someone pointed me to your reddit post! I don't care if the code is messy, I totally wanna see what the heck's going on

JKTCGMV
Автор

You should've also tracked the delays of the vehicles.
You could start counting when the vehicle speed drops below the max speed, and stop counting once they're back up to max speed.

NewBuildmini
Автор

Hi Your code is perfect! However, I still have some question about the code that you are sharing in the Github. What does the CSV file is using for in your code? I am working on the classwork for doing traffic flow simulation on I-80

guangyanli
Автор

It'll be cool to add throuthput to radius corellation

kreeg
Автор

Hey, what do I need to do to make it work?

colare