Рет қаралды 14,199
Source code: github.com/LupascuAndrei/nfs_...
This is a short video showcasing my simple Python A.I. algorithm for the self driving car. It's made with computer vision through OpenCV.
-----------------------------------
Script:
Hi guys
Today we’re going to build self-driving AI for Need For Speed Most Wanted using real-time image processing.
“But why Need For Speed most wanted? ” - nobody asked because i have no fanbase.
Mostly because my life is a pile of questionable life choices.
Our AI will be a background process which records the game screen, analyzes it, makes decisions and sends keyboard input back to the game. We will code everything using only Python and OpenCV. The code is publicly available on github and the link is in the video description, so feel free to play with it as you wish.
We will describe the whole process in 5 easy steps.
Recording the screen
Our first step is recording the screen. We do this by repeatedly capturing our monitor screen using a python module. Easy peasy.
screenshot cu parte din cod care copiaza ecranul
Detecting the road
Our second step is detecting the road, so we know when to steer left or right.
A precise approach is filtering the road pixels and calculating their trajectory.
The issue with this method is that, while it is accurate, it would take too much time to process, and by the time our AI would decide how to steer, it would be too late and the car would crash.
We need to compromise some accuracy in exchange for computing speed. So we need to find a more hack-ish solution.
We will, instead, decide how to steer based on the minimap.
By filtering only the white pixels, we can calculate their positions and approximate the direction of the road ahead. It is as simple as falling in love with the wrong person.
Detecting our current speed
Our third step is detecting our current speed in order to determine how much to steer when we are taking a corner. If we take a look at our car’s speedometer, we notice that it’s a number of 3 digits displayed in a digital alarm clock font. To read a digit, we’ll filter the black pixels, detect existing black lines and hardcode all possible line combinations into numbers. And we’ll repeat this algorithm for all the three digits.
Sending key input to our game
Our fourth step is sending keyboard input back to our game. Our AI will use some heuristics based on our current speed and the road ahead to keep the car from crashing. I made the heuristics myself so let’s see how this goes because I don’t deserve my driving license.
???.
The last required step is complimenting you. You are awesome and you deserve to be loved.
So let’s see how the AI behaves so far. I only finetuned the car up to a certain speed, so if you want to finetune it even further, feel free to play with the code, which is available on github.