OpenCV Object Detection in Games Python Tutorial #1

Learn how to use OpenCV for object detection in video games. This intro tutorial will show you how to install OpenCV for Python and get started with simple image template matching. This will serve as our foundation as we explore many different computer vision techniques in future videos.
Full tutorial playlist: • OpenCV Object Detectio...
GitHub Repo: github.com/learncodebygaming/...
OpenCV documentation: docs.opencv.org/4.2.0/
Official template matching tutorial: docs.opencv.org/4.2.0/d4/dc6/...
0:24 How to install OpenCV
1:45 How OpenCV relates to PyAutoGUI
2:52 How to navigate the OpenCV documentation
4:53 Comparison methods visualized
5:39 Writing code for cv.matchTemplate()
OpenCV is an open source computer vision library with hundreds of functions for processing and understanding images. In this tutorial, I'm going to show you how to get started with OpenCV in Python by using it to find an image inside another image. This simple form of object detection will be a good starting point before we move on to more advanced image recognition techniques.
The quickest way to get started with OpenCV is: pip install opencv-python
Once installed, you can use the library by importing cv2. Numpy is used extensively when working with OpenCV data, so the top of your Python files will look like this:
import cv2 as cv
import numpy as np
That's all there is for setup. Now let's grab an image we want to process. I'm going to be using this screenshot from Albion Online, but any screenshot will do.
What we're going to do is crop out a small section from our screenshot, save that as a separate image file, and then we're going to use OpenCV to find the position of the smaller image inside our entire screenshot. From my screenshot, I'll crop out one of the cabbages.
The OpenCV function we'll be focusing on is called matchTemplate(). In the documentation, we can see we're going to give this function an image to search over, an image to search for, and a method type for doing the comparison. And we'll end up with a result array. You'll want to experiment with the different comparison methods to see what works best for your use-case.
Alright, let's write some code. The first thing we want to do is load our image files.
haystack_img = cv.imread('albion_farm.jpg', cv.IMREAD_UNCHANGED)
needle_img = cv.imread('albion_cabbage.jpg', cv.IMREAD_UNCHANGED)
The "haystack" image is our screenshot, and we'll be search that for the "needle" image we cropped out. In imread() the first parameter is the image file path, and the second parameter is a flag that allows us to do some pre-processing when loading the images. In this case, we're loading them in unchanged.
Now that we have our images loaded, we can go ahead and call matchTemplate(). I've had good luck using the TM_CCOEFF_NORMED comparison algorithm.
result = cv.matchTemplate(haystack_img, needle_img, cv.TM_CCOEFF_NORMED)
We can quickly see the results from matchTemplate() by displaying that data with imshow().
cv.imshow('Result', result)
cv.waitKey()
In imshow(), the first parameter is the window name and the second is the image we want to show. I've also called waitKey() to pause our script while we review the image. Without this, our script would quickly close before we could see the image. Pressing any key on the keyboard will trigger waitKey() to stop waiting, thus ending our script.
In this result image, the bright white pixels represent the positions that best match the cropped image. The black pixels are the worst matches. Note that these best match positions correspond with the upper left corner of where you'd place the needle image.
Now that we've visualized the results of matchTemplate(), let's get those best match coordinates. We can do that using minMaxLoc().
The minMaxLoc() function returns four values. First are the confidence values for the worst and best matches, on a scale from 0 to 1. These are how black or how white the darkest/brightest pixels are in our result image, where 0 would be perfect black and 1 would be perfect white. The last two values minMaxLoc() returns are the positions of those worst/best match pixels in the form of an (X,Y) tuple.
For every needle image that we give matchTemplate(), we will always get back some values from minMaxLoc(), even if that cropped image appears nowhere in the haystack. We can tell when we didn't find a good match because the max confidence value will be low. How low is too low depends on the images you're working with and what you're trying to achieve.
Now that we've found a good match, let's outline where we found it in the haystack image. We can do that using OpenCV's rectangle() function.
Continue with the written tutorial here: learncodebygaming.com/blog/op...
Join me on Discord: / discord

Пікірлер: 323

  • @ilkayatil
    @ilkayatil3 жыл бұрын

    I just found out your video and even as a senior python and opencv developer I enjoyed watching all of it. Learning image processing through games is an excellent idea. Your presentation is clear and precise, I disagree with comments which states it is a bit boring. Your explanation level is just right for the beginners, not overexplaining things and exploding beginner's minds :D I wanted to take my time and display my support with a comment because you clearly dedicate your time and enthusiasm to create a helpful content for the community. You just earned a subscriber, keep up the good work my friend. p.s. About the not so perfect matching score, it is probably due to using jpeg format's lossy compression. After you saved those images as jpeg, they will have very small differences invisible to eye but clearly visible to code as in this example. You might prefer using PNG for lossless image saving or even BMP to get faster image write/read times (high fps).

  • @josh9761

    @josh9761

    2 жыл бұрын

    second this by far the best explanation i have came across, zoomed in code, well explained step by step

  • @E30Andy

    @E30Andy

    9 ай бұрын

    I went through this tutorial but deviated by using png files instead. ultimately this makes the tutorial not work as written because the data was no longer in the expected format of CV_8U or CV_32F for the matchTemplate function. Just a warning for those that like to experiment lol. My understanding is that you can easily convert to the proper structure, but me, being a noob, did not find success with this. I saved my files to .jpg and moved on with the tutorial instead of getting hung up and everything worked.

  • @eliotbicak8296
    @eliotbicak8296 Жыл бұрын

    It took two years but I am very happy that I managed to find your channel! Thank you for the great tutorials and videos

  • @wingchangwow
    @wingchangwow3 жыл бұрын

    Honestly, the quality of your videos and tutorials is simply astounding. I'm constantly amazed that you don't have hundreds of thousands of subscribers....yet!

  • @warpcode
    @warpcode3 жыл бұрын

    I've just started getting into object detection in opencv along with tensorflow, yolov4 etc. Whilst I'm not using it for gaming, these tutorials have been great for real dynamic and practical applications and for that i want to say thank you :)

  • @Charvin
    @Charvin4 жыл бұрын

    This was something I was wondering about, Thank you. I don't think there are much interactive content like this out there. Glad to have found this channel. Thank you once again.

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Glad you like it!

  • @saint-jiub
    @saint-jiub4 жыл бұрын

    I'm on part #3 of your series so far but wanted to revisit this to say - you've been helping me get out of tutorial hell. Thank you so much Ben! You have an amazing teaching style; The documentation and comments along the way in both video & blog posts provides more clarity than other tutorials. Your channel is one of the best out there, please don't stop. Peace.

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Thanks man!

  • @setokibah4476
    @setokibah44764 жыл бұрын

    Here I was wondering how i was going to pull of object detection, then my man presents the answer to me just like that! Always appreciate you, never fail to expedite my thought process!

  • @raakapena
    @raakapena Жыл бұрын

    Your OpenCV videos inspired me so much, been learning constantly new things and doing some low profile botting too. Best channel I've stumbled upon in a long time :).

  • @cavaleirosemcavalo69
    @cavaleirosemcavalo692 жыл бұрын

    Thanks for the video, I'm from Brazil and here we don't have many good videos about OpenCv and Python , the simple and objective way you speak is perfect for me who doesn't is a native and thanks for not using so many slangs

  • @Joshua-kr5fq
    @Joshua-kr5fq4 жыл бұрын

    Nice to see you taking it to the next level

  • @brednbudder
    @brednbudder3 жыл бұрын

    I love your videos. they make it more fun to learn to code. thank you very much for taking the time to make these, especially into easily digestible chunks in a playlist. youre awesome!

  • @deusexpersona95
    @deusexpersona953 жыл бұрын

    This channel is God sent. Keep going and you'll grow very fast

  • @OsmioGonzalez
    @OsmioGonzalez3 жыл бұрын

    (Español) Muy buen video, conciso pero detallado. Atiende a problemas pequeños de forma que permite aprender poco a poco y construir el código lentamente y de una forma muy natural. Mis felicitaciones sinceras, espero continúes con este trabajo de divulgación tan pulido. Solo me queda felicitarte de nuevo y seguir tu canal y aportes. Mucha suerte para el futuro. (English by google translate) Very good video, concise but detailed. It attends to small problems in a way that allows you to learn little by little and build the code slowly and in a very natural way. My sincere congratulations, I hope you continue with this polished disclosure work. I can only congratulate you again and follow your channel and contributions. Good luck for the future.

  • @darkferiousity
    @darkferiousity3 жыл бұрын

    Wow dude been watching a lot of videos on open cv and yours is far the best I have seen. Just the way you teach is awesome.

  • @mangojango3745
    @mangojango37454 жыл бұрын

    I was JUST looking up info on this. Who knew it would be from the guy I subbed to from a few days ago. Keep up the good work your putting out unique and great content

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Thanks means a lot!

  • @diegorivera_cr

    @diegorivera_cr

    4 жыл бұрын

    Thanks alot for this video! Do you think you can cover how to find an object and also detect when it is gone in a video game?

  • @jamiestwrt

    @jamiestwrt

    3 жыл бұрын

    Trueeee

  • @mangojango3745

    @mangojango3745

    3 жыл бұрын

    @Carroll Horsely stop scamming people

  • @Tradie1

    @Tradie1

    2 жыл бұрын

    Exact same here! What an underrated channel

  • @johannes-euquerofalaralema4374
    @johannes-euquerofalaralema43743 жыл бұрын

    Hi there, this is the best channel about this content. I have watched your Videos about Python at least 20 times each! I am a beginner and was able to follow your explanation and even to costumize the code for my needs! Really amazing!

  • @user-oc8on5gz5s
    @user-oc8on5gz5s Жыл бұрын

    I have started learning computer vision not long ago and basically you are the best. Thank you very much for your tutorials

  • @fierce1340
    @fierce13402 жыл бұрын

    Just stumbled upon your channel! Love the style and format! Can't wait to check out more videos!

  • @oswaldogerardino
    @oswaldogerardino5 ай бұрын

    I'm from Latin America, and I understand your tutorials better than the ones in my language. You're great, thanks

  • @teslar735
    @teslar7354 жыл бұрын

    this channel is so much underrated.Very good content bro,keep it up

  • @hansenliu2000
    @hansenliu20003 жыл бұрын

    God I love you and this series, the quality content we all need but do not deserve

  • @EvolMe
    @EvolMe Жыл бұрын

    hey i JUST found this channel today, MapleStory is my childhood favorite game so this has motivated me to learn to code and use maplestory as my game, my younger self is screaming right now lol love your content

  • @tower1990
    @tower19903 жыл бұрын

    Thanks Ben :). I really like your teaching style. Please do more if you’re available!

  • @GoeieWoofer
    @GoeieWoofer2 жыл бұрын

    Yooooo Ben i hope you are still Alive and coding! I honestly cant thank you enough for all your content! It has finally gotten me started with programming and coding, im studying to become a python developer and ive truly found my passion and calling in life now. The way you teach and explain things is amazing, wish i would have gotten help like this sooner in life! Much love for you

  • @redegs_
    @redegs_2 жыл бұрын

    You did a great job at making this easy to understand, I followed it all the way through and it worked, Thank you

  • @dazealex
    @dazealex8 ай бұрын

    Brilliant channel. Enjoyed every bit of it. Love the short length. Subbed!

  • @JordhanRDZ
    @JordhanRDZ4 жыл бұрын

    YES !! This is going to be very interesting ! =D

  • @onion9377
    @onion93774 жыл бұрын

    Wicked video! thank you for the tutorial. After fumbling around with a few other guides, it was yours that really got me into it.

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Awesome!

  • @WhyNotProgram
    @WhyNotProgram Жыл бұрын

    Thank you, I love how you guide us through the documentation! This will help me start creating AI's to play games and automate tasks.

  • @pollos4157
    @pollos41574 жыл бұрын

    Whoever disliked this needs their head checking, this was one of the best programming tutorials I have seen yet.

  • @devpatel5597
    @devpatel55973 жыл бұрын

    Underrated KZreadr. Great content!

  • @MrFox-uf7kv
    @MrFox-uf7kv Жыл бұрын

    why this guy don't have above 100k subs.. he explained it so good. +1

  • @alchercananea5975
    @alchercananea59753 жыл бұрын

    Hi there Ben! I stumbled upon your channel because I wanna learn how to code. I watched this video to see if it interests me though I don't have any knowledge yet. But boy! You explain things so well that I get the idea of what you are doing. I am glad I've found your channel. +1 sub!

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Awesome, always happy to see new people giving code a try! Thanks for subscribing.

  • @daltonrinck3617
    @daltonrinck3617 Жыл бұрын

    Thank u very much for this video. It was Informative and engaging. Makes me want to learn more about OpenCV!

  • @MrBru96
    @MrBru964 жыл бұрын

    Thank you so much. Your explanations are so clear. I learn a lot from you. You are the best!

  • @iamrockstar2101
    @iamrockstar21013 жыл бұрын

    GOLD CONTENT thanks , i just needed something like this to complete my project you earned a new subscriber :D

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Glad you like it! Good luck on your project.

  • @angelsdemons8520
    @angelsdemons85203 жыл бұрын

    Open CV is great no denying but the library is just huge , it's full of functions to do all sorts of thing , this is a great advantage(keeps ur code short and neat) and disadvantage ( I really find it difficult to remember these lol) like think about it for a sec, there is function for like literally everything and parameters which makes no sense for newbies (like me myself), but nevertheless great video mate , really liked how u walked through the documentation (encouraging viewers to explore) and explained as much as possible while writing the code and made the video as short as it possibly could have been u certainly deserve more subs and recognition mate :)

  • @topkek239
    @topkek2394 жыл бұрын

    This is my new favorite channel, thanks for good content ^_^

  • @ramborambo5677
    @ramborambo56774 жыл бұрын

    Thank you for this. Keep em coming :)

  • @LordXavier79
    @LordXavier794 жыл бұрын

    Thx, People like you make the world a better place

  • @SquallHart05
    @SquallHart05 Жыл бұрын

    You're an EXCELLENT teacher. Thank you!

  • @shdwrzr07
    @shdwrzr074 жыл бұрын

    Hey man just want to let you know that I'm so happy right now as a gamer and python programmer :) You just earned a new subscriber!

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Glad other people like this stuff too!

  • @thedeveloper643
    @thedeveloper6434 жыл бұрын

    this guy should get more subscribers! thank you for the great contents

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Thanks!

  • @ae171092
    @ae1710923 жыл бұрын

    great stuff. no idea why the views are not in millions. kudos

  • @shadowhuntzgaming3055
    @shadowhuntzgaming30553 жыл бұрын

    YOUR TUTORIALS ARE THE BEST IVE SEEN, THENK YOU SO MUUUCH!!!!!!!

  • @sanyamchaurasia1542
    @sanyamchaurasia15422 жыл бұрын

    LOVED IT! Please, please, please! I would love more tutorials like this! Can we also make basic games like tower defense, basic shooter, or something similar?

  • @javagenbu7085
    @javagenbu70852 жыл бұрын

    Thanks for the video my man, very useful and well explained.

  • @theweebsamurai
    @theweebsamuraiАй бұрын

    Thanks a lot man , This Video really helped me start using openCV

  • @PythonHarry
    @PythonHarry Жыл бұрын

    love you so much, I love this course, you just got a Chinese Subscriber!!!

  • @DamafiakingzDMK
    @DamafiakingzDMK3 жыл бұрын

    You are great bruh I'd literally crown u as king.

  • @dzdeparsio4676
    @dzdeparsio46763 жыл бұрын

    thanks i will watch ur vids and learn this library with u keep up the good work

  • @mcleber100
    @mcleber1005 ай бұрын

    Thank you for sharing your knowledge. Greetings from Brazil.

  • @jaski8143
    @jaski81433 жыл бұрын

    absolutely amazing content !

  • @renaud6904
    @renaud69042 жыл бұрын

    thank you so much Ben ! I viewed your video, red your web site, and i have done my new project in few minutes !

  • @AskAKill99
    @AskAKill997 ай бұрын

    Amazing videos!thankyou so much sir!! Much praise for your work

  • @GoldmediaSubscribeme
    @GoldmediaSubscribeme2 жыл бұрын

    This me first time watching your videos and loved it!

  • @shi-woonyi2605
    @shi-woonyi26054 жыл бұрын

    Thanks for the tutorials, I like all of them, it's much more fun to learn coding/new libraries if the examples are interesting topics too in this case, games! keep up the good work, looking forward to future tutorials, OpenCV looks awesome.

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Yes, that's the idea of my channel! Thanks!

  • @lamvandinh789
    @lamvandinh789 Жыл бұрын

    Thank you for the great tutorial!

  • @FollyOx
    @FollyOx Жыл бұрын

    Thanks dude, I want to make a very simple bot to automate a PIA element for a really old game and this is a good excuse to learn Python a bit more

  • @kevinflynn4867
    @kevinflynn4867 Жыл бұрын

    Thank you Ben , very interesting content !

  • @vromel4331
    @vromel43313 жыл бұрын

    Bruh ty so much! You save so much time for as !

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    You're welcome!

  • @redmenace5536
    @redmenace55364 жыл бұрын

    Thank you for your tutorials!Really appreciate it :)

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    You're welcome, glad you like them!

  • @spadedaman4958
    @spadedaman49583 жыл бұрын

    good video and a writeup! You are doing a great job!

  • @kaioshin9143
    @kaioshin91433 жыл бұрын

    Thanks for the video, I now learned what I needed. Cheers!

  • @n1guild
    @n1guild4 жыл бұрын

    Great content as always!

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Thanks Shane!

  • @SolusAU
    @SolusAU3 жыл бұрын

    Your videos are super clear. Thankyou :)

  • @Lucasbc
    @Lucasbc Жыл бұрын

    Amazing explanation!

  • @lordraphi
    @lordraphi4 жыл бұрын

    Nice video! Good for starting programming a mmorpg bot!

  • @nolancao2878
    @nolancao2878 Жыл бұрын

    thanks for these 9 masterpiece videos

  • @zarakikenpachi1238
    @zarakikenpachi12382 ай бұрын

    Thank you for the video :) That s very interesting

  • @Siuulolñ
    @Siuulolñ2 жыл бұрын

    Thank you so much for these videos, this is gold

  • @imperatoreTomas
    @imperatoreTomas4 жыл бұрын

    Keep making videos. This is awesome!

  • @uzzifuzzi9884
    @uzzifuzzi98844 жыл бұрын

    Can't wait to see where this goes

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Same! I need to slow down on coding and catch up with making the videos.

  • @krocodilnaohote1412
    @krocodilnaohote14123 жыл бұрын

    Very good, thx for your videos

  • @sangdilbiswal30
    @sangdilbiswal30Ай бұрын

    you are such a great teacher

  • @lightningcode7603
    @lightningcode76033 жыл бұрын

    Great video!

  • @RK-de3uu
    @RK-de3uu3 жыл бұрын

    thanks for the clear explanation!

  • @situwang1831
    @situwang18313 жыл бұрын

    Thank you for your videos, you are very good, I have received a lot of inspiration

  • @rcpinto
    @rcpinto7 ай бұрын

    Wonderful video!

  • @slttcoding4530
    @slttcoding45302 жыл бұрын

    Great Video!

  • @AhmedKamel-ol6qv
    @AhmedKamel-ol6qv4 жыл бұрын

    Great playlist

  • @shiny_apricot
    @shiny_apricot3 жыл бұрын

    Nice explanation

  • @user-mh1ki8ke9p
    @user-mh1ki8ke9p Жыл бұрын

    It has been two years already, where is the continuation? Why did you abandon your channel? You explain better than all my teachers! You're the best! It has been 2 years already, perhaps you have improved object detection, bot performance, and their movement between points, or maybe there's something else interesting to see!

  • @user-ei9dv9nr2p
    @user-ei9dv9nr2p2 жыл бұрын

    ความรู้ทั้งนั้นขอบคุณมากครับ

  • @xdeymin1612
    @xdeymin16122 жыл бұрын

    Very nice Video!

  • @bimamrth
    @bimamrth3 жыл бұрын

    Nice this verry useful for me 👍

  • @Googlrr
    @Googlrr2 жыл бұрын

    hey this was insanely helpful, thank you very much

  • @VersusBR
    @VersusBR2 жыл бұрын

    print("Hello from Brazil") Thx bro

  • @ibnelaiq
    @ibnelaiq4 жыл бұрын

    Nice Need More Like This

  • @jesusyanez100
    @jesusyanez1002 жыл бұрын

    ur the goat @code by gaming

  • @billsultani8967
    @billsultani89673 жыл бұрын

    Your a legend mate thank you so much!!!!!!!

  • @amirhosseinfalahati4388
    @amirhosseinfalahati43883 жыл бұрын

    just found good stuff on yo channel, Thanks!

  • @mikeygoby6154
    @mikeygoby61542 жыл бұрын

    good explanation, ty

  • @ron_jak
    @ron_jak4 жыл бұрын

    Hi, love the vids! Hope you're doing well. Keep up the content. Cheers.

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Thanks Ron!

  • @szymonduranowski4510
    @szymonduranowski45103 жыл бұрын

    great explanation !!!

  • @amardeep100
    @amardeep1003 жыл бұрын

    Hey man nice video. I was hoping if a make video on playing gta 5 using hand recognition and open cv. Thanks 👍

  • @AlejoCuartas
    @AlejoCuartas4 жыл бұрын

    Great tutorial, thanks!

  • @Joshua-kr5fq
    @Joshua-kr5fq4 жыл бұрын

    I just did the sentdex opencv tutorial so this is perfect

  • @LearnCodeByGaming

    @LearnCodeByGaming

    4 жыл бұрын

    Awesome hope it's helpful for you

  • @Joshua-kr5fq

    @Joshua-kr5fq

    4 жыл бұрын

    @@LearnCodeByGaming Stardew valley is about to get more interesting.

  • @spaska1184
    @spaska11842 жыл бұрын

    great tutorial

  • @MultiBelz
    @MultiBelz2 жыл бұрын

    I am wondering why the needle isn´t found anymore when I am using a perfect cut out needl image with alpha channel. with just a rectangle cutout it works.