I programmed an A.I. for Need For Speed: Most Wanted

  Рет қаралды 14,199

Codeazy

Codeazy

3 жыл бұрын

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.

Пікірлер: 51
@simplesky4110
@simplesky4110 3 жыл бұрын
Quite impressive, since all of this was done in python and plays better than me.
@codeazy
@codeazy 3 жыл бұрын
Thank you! It also crashes less often than me irl.
@sergeibaskakov1014
@sergeibaskakov1014 3 жыл бұрын
It's sad to realize, but this AI plays NFS much better than me... Good job, my friend! 👏
@sanketkundu3657
@sanketkundu3657 3 жыл бұрын
The effort you have given deserves recognition! God knows how much time you have given, hats off ,man,i loved ur commentary!
@fauxhound5061
@fauxhound5061 3 жыл бұрын
Outstanding work, it drives like a real player
@derekwest8636
@derekwest8636 Жыл бұрын
That's dope. Your videos deserve to be upvoted ❤
@Soroosh.S83
@Soroosh.S83 2 ай бұрын
This is amazing bro, You Are AMAZING! I didn't see anyone coming up with such a simple but briliant idea, i wanted to implement somthing similar into my own racing game and thanks to you, now I can somewhat understand how to do it, thanks for your great help, I will defiantly Credit you in the game 💜
@me-df9re
@me-df9re 3 жыл бұрын
Awesome. Please continue good work.
@codeazy
@codeazy 3 жыл бұрын
Thank you, that's the plan!
@Outofthedust
@Outofthedust 3 жыл бұрын
For a raw run based on the minimap that's pretty darn good; Better than I expected anyway. With some refinement, it could be taught a better race line to stay out off the dirt and on the road
@janm8027
@janm8027 3 жыл бұрын
Looks awesome!
@osdever
@osdever 3 жыл бұрын
Whoa, this is actually impressive. I actually tried doing something similar about a year ago but with the actual AI path data and A* pathfinding, it worked pretty well lol
@ralph8016
@ralph8016 2 жыл бұрын
How did u do it? :D I can't find the files for it.
@teriyaki8643
@teriyaki8643 3 жыл бұрын
Very cool! I'd buy you an ice-cream for this well-done job.
@codeazy
@codeazy 3 жыл бұрын
Thank you! That is so cool and sweet. (hi hi hi)
@alokkumar5123
@alokkumar5123 2 жыл бұрын
This is really cool actually
@SarveshKumar-ml5jd
@SarveshKumar-ml5jd 3 жыл бұрын
good effort
@montdedan
@montdedan 3 жыл бұрын
Good Work Man
@codeazy
@codeazy 3 жыл бұрын
Thank you!
@shankyoncloud7420
@shankyoncloud7420 3 жыл бұрын
The biggest problem with the algorithm is that you aren't accounting for fixed objects in the middle of the track like bridge columns or dividers. Pretty interesting idea to use the minimal tho. What happens if the car accidentally points the wrong way
@codeazy
@codeazy 3 жыл бұрын
You are right ! if it crashes and starts pointing the other way around, it has no way to know this and will keep running the wrong way ! It also is blind to objects in the middle of the road or enemy cars.
@dhu3131
@dhu3131 3 жыл бұрын
@@codeazy which makes it even more impressive that it managed to finish the race
@arpitshrimankar7787
@arpitshrimankar7787 3 жыл бұрын
very nice sir keep it up :)
@ragingfred
@ragingfred 3 жыл бұрын
Could you use this in combination with reading the road pixels at least to *roughly* detect what is road and what is grass/dirt or where the boundaries are?
@codeazy
@codeazy 3 жыл бұрын
Yes, you are correct, that could be a huge improvement! i.e. looking at the minimap to see the general direction, then use that hint to know where to check the road
@hamsi1792
@hamsi1792 4 ай бұрын
This looks like if someone played Most Wanted like its a simulator game.
@sabah8312
@sabah8312 3 жыл бұрын
Nice 👍🏻
@codeazy
@codeazy 3 жыл бұрын
Thank you !
@antiw_
@antiw_ 3 жыл бұрын
can you do one for euro truck sim ?
@doominator-117
@doominator-117 3 жыл бұрын
Oustanding work gentlemen.
@codeazy
@codeazy 3 жыл бұрын
Thank you, kind gentleman!
@albertusaswin2012
@albertusaswin2012 2 жыл бұрын
Does NOS work with your AI?
@lindensd6273
@lindensd6273 2 жыл бұрын
Would this work with modern need for speed as 2015?
@deibgaming9363
@deibgaming9363 5 ай бұрын
where to put the files? i don;t work
@secretc.5214
@secretc.5214 2 жыл бұрын
thx
@its_ntm
@its_ntm Жыл бұрын
i lvoe this :D
@codeazy
@codeazy Жыл бұрын
Thank you !!
@muhammadhanifkurnaen6689
@muhammadhanifkurnaen6689 2 жыл бұрын
I just want that someone could end rubberbanding issue on nfs series
@parthpatel9602
@parthpatel9602 3 жыл бұрын
Hey I'm not the greatest programmer but don't you think that putting some things like checkpoints for every track and then hardcoding the keyinputs should work better ?🤔
@codeazy
@codeazy 3 жыл бұрын
it might work better, but then you'd have to manually hardcode each track and it won't work on new tracks or roads. And it's a much uglier approach ! similarly we can also hardcode all the desire input for each track (i.e. speed up for the first 3 seconds, then turn left for two seconds etc.)
@jacobpillai
@jacobpillai 4 ай бұрын
Can you make one for another nfs game?🤔 I would like to learn how to code this as well 😅😅since im still learning how 😅😅
@deepbrar1
@deepbrar1 2 жыл бұрын
wow , this is really awesome. Just how much time it took u to make this ...
@codeazy
@codeazy Жыл бұрын
Thank you! Around 60 hours I believe, I am a slow editor!
@mrdlandrei
@mrdlandrei 3 жыл бұрын
Ez pez. When are we installing it in real cars?
@user-dc9zo7ek5j
@user-dc9zo7ek5j 3 жыл бұрын
I was impressed but when I saw the code was so small I was even more! Is it possible to keep the performance but still from time to time, lets say few seconds to actually look at the screen and see if it's actually moving, the strategy of sliding on the walls is not a viable strategy because there are maps where the corners stick out, not really problem of the algorithm but the game. Also is this the coding standart in python ? Why are there so many ifs and repeating conditions and why did you write 12/10 instead of 1.2 ? And why do you always calculate getImageDigitalNumber if the counter is always there and could be calculated once per race ? I've never programmed in python and I'm interested.
@codeazy
@codeazy 3 жыл бұрын
Thank you and i'm glad you liked it ! getImageDigitalNumber () should be called on each frame, because the speedometer might change on each separate frame depending on the car's speed; this is why i'm calling it in the while loop the 12/10 is the same as 1.2, it doesn't matter how you write it !
@user-dc9zo7ek5j
@user-dc9zo7ek5j 3 жыл бұрын
@@codeazy Why don't just write 1.2, it's much more to the point than 12/10. About the speeometer, I was talking about why always find the "spotsToCheck" if the speedometer is always going to have the same height and width and the same position, and why should a parameter "name" be passed if it's not used, couldn't it just be a comment ? Also saw that you've removed some romanian comments, so greetings from Bulgaria.
@codeazy
@codeazy 3 жыл бұрын
@@user-dc9zo7ek5j o ye that could be an improvement regarding the spots to check, but i dont think it would matter on the performance in a noticeable way; but nice catch ! also greetings, neighbour !
@codeazy
@codeazy 3 жыл бұрын
Also regarding 12/10, y i should change that to 1.2 for it to be more clear; sometimes i prefer writing numbers as fractions, dunno why (i.e. 3/4 instead of 0.75)
@user-dc9zo7ek5j
@user-dc9zo7ek5j 3 жыл бұрын
@@codeazy Well, it's not about the performance, it's about the ability to make it simple. I really like to remove all moving parts from my code and to make it as static as possible, it makes the code easy to read and highly predictable, because it also makes the functions do less, it's less noise overall and once set those variables you can forget about them. Performance wise, after all we're talking about python, there is no point in optimizing something that is meant to convey thoughts instead of bare instructions.
Need for Speed World: The True End Of The Black Box Era
17:36
I'M IN NEED FOR SPEED MOST WANTED?! (Pepega Mod)
10:47
BlackPanthaa
Рет қаралды 398 М.
Ouch.. 🤕
00:30
Celine & Michiel
Рет қаралды 26 МЛН
小宇宙竟然尿裤子!#小丑#家庭#搞笑
00:26
家庭搞笑日记
Рет қаралды 15 МЛН
ОБЯЗАТЕЛЬНО СОВЕРШАЙТЕ ДОБРО!❤❤❤
00:45
NFS Most Wanted Spectating the AI in Catch Up Mode
4:09
INTEL HD 5500 GAMING
Рет қаралды 233 М.
I Made a CRT Game Boy
20:44
James Channel
Рет қаралды 204 М.
"Mafia" is a Racing Game
10:17
Sepi
Рет қаралды 375 М.
AI Learns to DESTROY old CPUs | Mario Kart Wii
9:54
AI Tango
Рет қаралды 1,4 МЛН
IS EARL REALLY THAT HARD? | NFS MOST WANTED 2005
8:49
KEWER MENGAKAK
Рет қаралды 232 М.
I Installed 40 Need for Speed Most Wanted Mods
9:23
Fueled with Boost
Рет қаралды 1,2 МЛН
Ouch.. 🤕
00:30
Celine & Michiel
Рет қаралды 26 МЛН