OpenCV - Python plays Grand Theft Auto 5 p.2
Skills:
CV Basics90%
Key Takeaways
The video demonstrates using OpenCV for computer vision tasks, such as converting images to grayscale and applying edge detection, on screen data from Grand Theft Auto 5. OpenCV tools like cv2 are utilized for image processing and display.
Full Transcript
Hello everybody and welcome to yet another Python plays a GTA 5 tutorial. In this tutorial, what we're going to be doing is just kind of building off the last one. The last one we proved that yes, we can actually stream this game to OpenCV/Python at a rate of about 10 frames a second. Um, also just in case that's not you, like for example, if you're on like a probably like a I don't know if you're on any computer that can play GTA 5, I feel like you should, unless your computer's horribly imbalanced, have a processor that can stream that image back. So, um, so I'm playing Grand Theft Auto 5 in an uh it's 800 by 600. Um, so, so that as long as you have a computer that can normally play Grand Theft Auto 5 at full screen on like high settings, uh, you should have probably a processor that can stream Grand Theft Auto 5 at like 10 frames a second, which to be honest, it's actually pretty impressive now that I'm like thinking about it because I'm I'm almost positive that all of Open CV is actually on the CPU. So, that the CPU can play Grand Theft Auto 5 at 10 frames a second is actually kind of cool. Anyway, um yeah, let's um the next thing that I want to cover here is um okay, so we used OpenCV to well actually we didn't even use all we've done is actually used Open CV to we did convert the color but we also um are really just using it to show the window. So So we haven't really seen that. Sure, we could do some manipulations on this and not sacrifice like five frames a second. Okay, so the next thing we need to really test is whether or not we can actually do OpenCV stuff on this data. So, um just in case you're not familiar, uh you don't have to go through this series, um but if you want to reference, um some stuff with OpenCV, you can go to python programming.net, um data analysis, image and video analysis, and bada boom. And then this is just a bunch of stuff that we can do with OpenCV. This is just a bunch of examples, but if you want to kind of get comfortable with doing things and especially like masks and regions of interest, um you might want to check out um check out OpenCV uh the tutorials. Anyway, um so the first thing that we're going to do is just kind of do some typical processing of the the actual screen that we're pulling here. So, um I think what I'll do is eventually we'll get rid of this uh print just because actually the act of printing um especially in idle is especially over a long period of time is going to negatively impact our frames a second. So, um you won't want to always have that there, especially if you're running an idle. If you're running through like a command prompt or terminal or something like that, it won't be as impactful and you you could leave it there for like debugging purposes. Um anyway, uh let's go ahead and do some real OpenCV tasks. So I'm going to define process image. Um and then we'll pass image into it. And then we're just going to say the original image equals image. So the reason why we're doing this is and in fact we probably could just leave it as image, but I like the name original image so it's clear but we are kind of duplicating things. Ah, we could call this original image. I could change change that. We could do original image like that. Then we're not copying that image actually. Okay, we'll do that. So, we'll call it original image. Now, what we're going to say is processed image. So, why are we doing this? So event what happens when you t a typical kind of workflow with OpenCV is you have an original image and then you dumb that image down crazy to like do something like find edges or even more dumb like in our case what we're going to do is we're going to attempt to use OpenCV to find lanes in a road. So, as you um do that, you're going to simplify that image so much so that eventually you just have two lanes, right? But then what we kind of want to be able to do is take those two lanes and put them back on the real original image. So, you always kind of want to save the original image. Um so, anyway, original or process image now will be cv2 uh convert color. We want to convert the color of original image. I'm pretty confident that doesn't happen in place. We're going to find out. But anyway, hopefully it doesn't. Anyway, cv2.color. Um, and then this will be BGR2 [Music] gray. Um, we're converting to to gray. We're converting to cray. Uh, we're converting to gray because we want to have much simpler data. So BGR is um you know it's it's a it's a um an array of values from 0 to 255 right so it's three values per pixel whereas with gray gray is simply a you know each pixel is a single value it's obviously much simpler and we can feed that directly through you know a convolutional neural network or something if it comes to that initially though my my plan is to try to use open CV and um more like rules first before we try to you know throw you can't just throw neural networks at things. That's not how it works. So, so, so we really I'd like some sort of way to to to determine whether or not we are in a lane. And even if we're not 100% like even if we don't have 100% perfection at that, what we can do is every time we have two lanes that we're very confident or two lanes, we can take that data and save that data to later train a neural network. So, um, anyway, that's why we're doing that. The other option too is to just play Grand Theft Auto for like a few hours. Especially like if we just drive the same road over and over um and then take the image data and then the input that we made as we drove that road and train a neural network off that. That's possible to do. And in fact, just recently I saw Nvidia released a thing where you can actually do that. Like Nvidia will log basically what you've done and then you can train a model to play the game like you play it. So, um I'm very curious about that. I wonder how much training data it really takes, but interesting concept. So, anyway, back to what we're doing. Um process image. So, we converted it to gray. Now, what we're going to go ahead and do is just do like some typical canny edge detection. So, we can say we're going to just keep the processed image. Processed image equals cv2.canny. Um and here we pass the processed image. And then you have two thresholds. Um, so we'll say threshold 1 equals 200 and then threshold 2 equals 300. You can tweak these numbers. Um, probably a lot is going to depend on the game. A lot will depend on just so many things. So um, this will be tweakable for sure. Um, now let's go ahead and return the process image. And now what we're going to do is under screen we'll keep we'll keep that happening. Um, and in fact under screen we'll do this. So we're we're going to say now new screen equals process image original image. And then now rather than showing the original screen, this one, we'll just comment this out. Eventually, we are going to show pretty much the original screen with some new things drawn on it. So, I want to keep that there. Um, but for now, we're actually just going to do cv2. Show and we'll call this um I don't know. Well, we we we can call this window. And let's just call this window two before we forget. um window and then cv2 dot um actually we don't need to convert anything. Let's just do new screen. Cool. All right. Let's save that and let's run that and see how we're doing up to this point. Move this out up. New screen. Process image. Original image. Nope. We need to process screen. Derp again. Beautiful. So here we have the edge detection. So this is a little unfortunate. I wish I could make this I'm not sure why this is so big. Maybe we'll have to resize this to eight I guess 86 because what's happening is my monitor is a 4K monitor. So this this is real 800 by 600. This or I'm sorry this is a scaled 800 by 600 to like fit the monitor because the monitor does autocaling. Oh dear, what have I done? come back. Ah, we've been trolled by Windows shake. [Laughter] Uh, let me pull up the script again. There we go. And again, actually, we're still seeing 10 frames a second, which is actually really exciting for me. I'm very glad that we're we're still hitting that. Anyway, we have edges. Beautiful. Beautiful. Can you imagine playing GTA with edge detection? That would be pretty brutal. But yeah, we still are operating a really good frames a second. So very cool. Very cool. Now, the last thing that we need to do is a thing that I kind of put off initially and again I've already I know the solution, right? Um, and that is sending input to the
Original Description
Now that we're confident that we can at least get the screen data, the next thing we want to do is run some OpenCV operations on the data that are typical for vision tasks.
For example, we'll want to conver the image to grayscale to simplify things (one value vs RGB's 3 values per pixels) and edge detection to eventually be used for finding the lines that will be our lanes.
Project Github: https://github.com/sentdex/pygta5
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
More on: CV Basics
View skill →Related Reads
📰
📰
📰
📰
Day 30 : From Chaos to Order
Medium · Programming
What Is mAP (Mean Average Precision)?
Medium · Data Science
Beyond Binary: Taming Image Noise with Local Ternary Patterns (LTP)
Medium · Machine Learning
I Built an AI Face-Analysis Suite From Scratch — Here's What I Learned
Dev.to · Ahsan Fayyaz
🎓
Tutor Explanation
DeepCamp AI