Fast Window Capture - OpenCV Object Detection in Games #4

preview_player
Показать описание
Learn how to capture window data in real-time as a video stream for processing with OpenCV. We try several different methods searching for the fastest way possible. In this tutorial I also discuss the importance of good Google search skills as a programmer, and we revisit some basic object-oriented programming concepts.

Research discussed:

0:47 Main capture loop
3:16 Using PyAutoGUI screenshot()
6:24 Measure FPS
8:27 Using Pillow ImageGrab
9:31 Using Pywin32 for screenshots
13:48 Converting win32ui.CreateBitmap() for OpenCV
17:23 Confining screenshots to a specific window
18:42 Creating a WindowCapture class
25:05 Trimming the window capture
28:15 Image to screen position conversion
29:35 Wrap up

Up to this point, we've been using OpenCV to detect objects in static images. Now we're ready to apply those same techniques to video games in real time.

Remember that video is just a series of still images shown in rapid succession. In this tutorial our goal is to capture screenshots as fast as possible and display them in an OpenCV window, so that we get a real time video stream of the game we're interested in. This will set us up to begin processing image data with OpenCV in real-time.

OpenCV has a tutorial on "Getting Started with Videos" that will serve as the basis for our code. Our starting point differs from the official tutorial only in that we are preparing to work with screenshot data instead of frames from a camera.

And this would work, but there are several benefits to calling the Windows API directly instead. Firstly, we approach the theoretical limit for how fast we can take these screenshots by dealing right with the operating system itself. Secondly, the Windows API has methods that will allow us to grab the screen data for only the window we're interested in, even when it's minimized or off screen.

To do this, we must first pip install pywin32 to get access to the Win32 API in Python.

The next step is to modify this function so that instead of saving an image file, it instead returns the image data, formatted to work with OpenCV.

Now we can call this function from our original infinite loop and get a real-time stream of our desktop.

To improve upon this, we can use win32gui.FindWindow(None, window_name) to capture just the window we're interested in. Replace the window_name with a string that contains the name found in the title bar of the window you want to capture. Doing so will allow you to capture the frames from that window even when it's hidden behind other windows.

If you're having trouble figuring out the name of the window you want, you can use this code to list the names of all your existing windows:

We can improve our code further by trimming off the excess around the window we're interested in. When you run the above code, you will notice black space to the right and below the window image, as well as the window borders and title bar. Removing these will not only clean things up, it will also improve our frame rate. We can also get improvements by not calling win32gui.FindWindow() on every call to get_screenshot(), so let's turn this into a class.

Finally, we'll need a way to convert positions we detect in our screenshots back to pixel positions on our actual monitor. In the WindowCapture class constructor, I've already included code to calculate the window offset using the window position data from win32gui.GetWindowRect(). Let's add a method to our class that uses this offset to return that converted screen position.

Рекомендации по теме
Комментарии
Автор

If you're having trouble with a black screen when you try to do the window capture, continue on to video #5 for a fix.

LearnCodeByGaming
Автор

The most honest programmer I have seen in Youtube, keep continuing what you are doing. I rarely put comment for someone... Thanks!

navidchaichi
Автор

Man, I seriously enjoy your style of explaining things.
It's the small stuff like that remark at 6:10 for example.
Kudos and keep up the good work! :)

PainfulDeath
Автор

I can't stress enough how your style of explanation helps us understand both the concept and the practicality of OpenCV. Huge huge thanks!!

andykim
Автор

You are quite possibly the best instructor I have ever had...in any subject. Fantastic!

psiborger
Автор

I’ve only seen one of your videos so far, and skimmed through this one but I can definitely say that your unique content has gotten me very motivated to dedicate even more time for programming.

PureForloko
Автор

I would honestly put you in the top 1% of coders on YouTube who are actually good teachers as well.

Wow.. just wow.. I'm amazed at how clear and concise you made everything

I'm subscribing for sure

CrazyFanaticMan
Автор

this is by far the best episode of the series. I always love the idea of the author walking through the mental process on how solve the problem. Appreciate your effort Ben.

Take care

Newton-vivs
Автор

Thank you for your calm style of teaching; you alone have helped me learn more than anybody else on YouTube.

andrewgreen
Автор

This is the first playlist on YouTube I've been waiting on the next episode, learning tons thanks for this resource!

sackr
Автор

This is one of the finest tutorial I've searched. It goes step by step and everything was explained. I'm glad I didn't miss this one.

TWBIG
Автор

I have no words to describe how much I like your tutorials. All I can think of is Thank you.

TabishTabby
Автор

It’s quite nice you included your thought process when you were googling stuff, for beginners like me that could be quite inspiring and i haven’t seen any other tutorial videos which have done this

y.m.
Автор

Underrated channel. I'd be happy to see even more videos from you, no pressure.

Al_Gonzo
Автор

I really like the way you teach. You are not a "know-it-all" guy. I appreciate that you showed how real programmers work.
Sometimes the most basic concepts are forgotten and just a simple google search for an example function is what needed.
You got a subscriber here, keep it up! :)

doctorglock
Автор

I appreciate your emphasis on problem-solving and research. It's almost like the specific purpose of your video is second to the more general process of research/coding/bug-fixing.

methodiconion
Автор

Forever grateful for your videos. I haven't started diving into Python but watching you explain a lot of stuff that are not often explained to beginners is sooo helpful. Keep it up!

kitsab
Автор

The way you teach is awesome. Very precise and to the point.

Mark my words. You will easily be one of the most followed teachers for programming in the future.

Looking forward to your videos.

Please do consider integrating ML into game automation for your next series.

ramborambo
Автор

this series keeps getting more interesting!!! aw man thank you so much

thedeveloper
Автор

Instead of removing alpha by looping through the image array you can run cv.cvtColor(screenshot, cv.COLOR_RGBA2RGB) before cv.imshow('Computer Vision', screenshot). That gives back ~10 FPS.

rebeYell