OpenCV Object Detection in Games Python Tutorial #1

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 มิ.ย. 2024
  • 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

ความคิดเห็น • 320

  • @ilkayatil
    @ilkayatil 3 ปีที่แล้ว +114

    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 ปีที่แล้ว +1

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

    • @E30Andy
      @E30Andy 7 หลายเดือนก่อน +1

      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.

  • @Charvin
    @Charvin 4 ปีที่แล้ว +41

    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.

  • @brednbudder
    @brednbudder 3 ปีที่แล้ว +4

    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!

  • @saint-jiub
    @saint-jiub 4 ปีที่แล้ว +12

    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.

  • @wingchangwow
    @wingchangwow 3 ปีที่แล้ว +24

    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!

  • @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

  • @mangojango3745
    @mangojango3745 4 ปีที่แล้ว +62

    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 ปีที่แล้ว +4

      Thanks means a lot!

    • @diegorivera_cr
      @diegorivera_cr 4 ปีที่แล้ว +1

      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

  • @warpcode
    @warpcode 3 ปีที่แล้ว +1

    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 :)

  • @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 :).

  • @setokibah4476
    @setokibah4476 4 ปีที่แล้ว +5

    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!

  • @Joshua-kr5fq
    @Joshua-kr5fq 4 ปีที่แล้ว +7

    Nice to see you taking it to the next level

  • @fierce1340
    @fierce1340 2 ปีที่แล้ว

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

  • @johannes-euquerofalaralema4374
    @johannes-euquerofalaralema4374 3 ปีที่แล้ว +1

    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!

  • @pollos4157
    @pollos4157 4 ปีที่แล้ว +2

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

  • @oswaldogerardino
    @oswaldogerardino 3 หลายเดือนก่อน

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

  • @dazealex
    @dazealex 6 หลายเดือนก่อน

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

  • @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

  • @redegs_
    @redegs_ 2 ปีที่แล้ว +1

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

  • @deusexpersona95
    @deusexpersona95 3 ปีที่แล้ว +7

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

  • @darkferiousity
    @darkferiousity 3 ปีที่แล้ว

    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.

  • @hansenliu2000
    @hansenliu2000 3 ปีที่แล้ว +1

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

  • @cavaleirosemcavalo69
    @cavaleirosemcavalo69 2 ปีที่แล้ว

    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

  • @onion9377
    @onion9377 4 ปีที่แล้ว +1

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

  • @SquallHart05
    @SquallHart05 ปีที่แล้ว

    You're an EXCELLENT teacher. Thank you!

  • @iamrockstar2101
    @iamrockstar2101 3 ปีที่แล้ว +5

    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.

  • @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.

  • @ramborambo5677
    @ramborambo5677 4 ปีที่แล้ว +2

    Thank you for this. Keep em coming :)

  • @MrBru96
    @MrBru96 4 ปีที่แล้ว +1

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

  • @teslar735
    @teslar735 3 ปีที่แล้ว +1

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

  • @devpatel5597
    @devpatel5597 3 ปีที่แล้ว +1

    Underrated TH-camr. Great content!

  • @topkek239
    @topkek239 4 ปีที่แล้ว +1

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

  • @tower1990
    @tower1990 3 ปีที่แล้ว

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

  • @OsmioGonzalez
    @OsmioGonzalez 3 ปีที่แล้ว

    (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.

  • @JordhanRDZ
    @JordhanRDZ 4 ปีที่แล้ว +6

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

  • @LordXavier79
    @LordXavier79 3 ปีที่แล้ว

    Thx, People like you make the world a better place

  • @redmenace5536
    @redmenace5536 4 ปีที่แล้ว +4

    Thank you for your tutorials!Really appreciate it :)

  • @shadowhuntzgaming3055
    @shadowhuntzgaming3055 3 ปีที่แล้ว

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

  • @MrFox-uf7kv
    @MrFox-uf7kv ปีที่แล้ว +1

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

  • @vromel4331
    @vromel4331 3 ปีที่แล้ว +8

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

  • @jaski8143
    @jaski8143 3 ปีที่แล้ว +6

    absolutely amazing content !

  • @kaioshin9143
    @kaioshin9143 3 ปีที่แล้ว

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

  • @EvolMe
    @EvolMe 11 หลายเดือนก่อน

    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

  • @GoeieWoofer
    @GoeieWoofer ปีที่แล้ว

    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

  • @javagenbu7085
    @javagenbu7085 2 ปีที่แล้ว

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

  • @thedeveloper643
    @thedeveloper643 4 ปีที่แล้ว +4

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

  • @lamvandinh789
    @lamvandinh789 ปีที่แล้ว

    Thank you for the great tutorial!

  • @daltonrinck3617
    @daltonrinck3617 10 หลายเดือนก่อน

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

  • @GoldmediaSubscribeme
    @GoldmediaSubscribeme ปีที่แล้ว

    This me first time watching your videos and loved it!

  • @PythonHarry
    @PythonHarry 11 หลายเดือนก่อน

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

  • @DamafiakingzDMK
    @DamafiakingzDMK 3 ปีที่แล้ว +1

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

  • @indirectthought8150
    @indirectthought8150 2 ปีที่แล้ว

    Thank you so much for these videos, this is gold

  • @AskAKill99
    @AskAKill99 6 หลายเดือนก่อน

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

  • @alchercananea5975
    @alchercananea5975 3 ปีที่แล้ว +3

    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 ปีที่แล้ว +1

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

  • @sanyamchaurasia1542
    @sanyamchaurasia1542 2 ปีที่แล้ว

    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?

  • @mcleber100
    @mcleber100 3 หลายเดือนก่อน

    Thank you for sharing your knowledge. Greetings from Brazil.

  • @nolancao2878
    @nolancao2878 ปีที่แล้ว

    thanks for these 9 masterpiece videos

  • @shi-woonyi2605
    @shi-woonyi2605 4 ปีที่แล้ว +7

    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 ปีที่แล้ว +1

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

  • @SolusAU
    @SolusAU 2 ปีที่แล้ว

    Your videos are super clear. Thankyou :)

  • @ae171092
    @ae171092 3 ปีที่แล้ว

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

  • @kevinflynn4867
    @kevinflynn4867 ปีที่แล้ว

    Thank you Ben , very interesting content !

  • @dzdeparsio4676
    @dzdeparsio4676 3 ปีที่แล้ว

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

  • @n1guild
    @n1guild 4 ปีที่แล้ว +5

    Great content as always!

  • @shdwrzr07
    @shdwrzr07 4 ปีที่แล้ว +1

    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!

  • @Googlrr
    @Googlrr 2 ปีที่แล้ว

    hey this was insanely helpful, thank you very much

  • @Lucasbc
    @Lucasbc 11 หลายเดือนก่อน

    Amazing explanation!

  • @uzzifuzzi9884
    @uzzifuzzi9884 4 ปีที่แล้ว +2

    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.

  • @user-ei9dv9nr2p
    @user-ei9dv9nr2p 2 ปีที่แล้ว

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

  • @RK-de3uu
    @RK-de3uu 3 ปีที่แล้ว

    thanks for the clear explanation!

  • @renaud6904
    @renaud6904 2 ปีที่แล้ว

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

  • @spadedaman4958
    @spadedaman4958 2 ปีที่แล้ว

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

  • @zarakikenpachi1238
    @zarakikenpachi1238 25 วันที่ผ่านมา

    Thank you for the video :)
    That s very interesting

  • @imperatoreTomas
    @imperatoreTomas 3 ปีที่แล้ว

    Keep making videos. This is awesome!

  • @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

  • @angelsdemons8520
    @angelsdemons8520 3 ปีที่แล้ว +1

    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 :)

  • @AhmedKamel-ol6qv
    @AhmedKamel-ol6qv 4 ปีที่แล้ว +1

    Great playlist

  • @VersusBR
    @VersusBR 2 ปีที่แล้ว +1

    print("Hello from Brazil")
    Thx bro

  • @rcpinto
    @rcpinto 5 หลายเดือนก่อน

    Wonderful video!

  • @billsultani8967
    @billsultani8967 3 ปีที่แล้ว

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

  • @S25plus
    @S25plus ปีที่แล้ว

    🙏 Thank you sensei!

  • @lordraphi
    @lordraphi 4 ปีที่แล้ว +1

    Nice video!
    Good for starting programming a mmorpg bot!

  • @AlejoCuartas
    @AlejoCuartas 4 ปีที่แล้ว

    Great tutorial, thanks!

  • @Joshua-kr5fq
    @Joshua-kr5fq 4 ปีที่แล้ว +4

    I just did the sentdex opencv tutorial so this is perfect

    • @LearnCodeByGaming
      @LearnCodeByGaming  4 ปีที่แล้ว +2

      Awesome hope it's helpful for you

    • @Joshua-kr5fq
      @Joshua-kr5fq 4 ปีที่แล้ว +3

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

  • @situwang1831
    @situwang1831 3 ปีที่แล้ว

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

  • @vampirefox9981
    @vampirefox9981 2 ปีที่แล้ว +1

    Thank you!

  • @jesusyanez100
    @jesusyanez100 2 ปีที่แล้ว

    ur the goat @code by gaming

  • @amirhosseinfalahati4388
    @amirhosseinfalahati4388 3 ปีที่แล้ว

    just found good stuff on yo channel, Thanks!

  • @krocodilnaohote1412
    @krocodilnaohote1412 3 ปีที่แล้ว

    Very good, thx for your videos

  • @lightningcode7603
    @lightningcode7603 3 ปีที่แล้ว

    Great video!

  • @youngjordan5619
    @youngjordan5619 2 ปีที่แล้ว

    awesome, thank you

  • @slttcoding4530
    @slttcoding4530 2 ปีที่แล้ว

    Great Video!

  • @ibnelaiq
    @ibnelaiq 4 ปีที่แล้ว

    Nice Need More Like This

  • @mikeygoby6154
    @mikeygoby6154 ปีที่แล้ว

    good explanation, ty

  • @bimamrth
    @bimamrth 3 ปีที่แล้ว +1

    Nice this verry useful for me 👍

  • @ron_jak
    @ron_jak 4 ปีที่แล้ว +1

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

  • @silvangavaziuc3890
    @silvangavaziuc3890 2 ปีที่แล้ว

    So helpful

  • @szymonduranowski4510
    @szymonduranowski4510 3 ปีที่แล้ว

    great explanation !!!

  • @marsell2149
    @marsell2149 5 หลายเดือนก่อน

    You're cool, thanks.

  • @xdeymin1612
    @xdeymin1612 2 ปีที่แล้ว

    Very nice Video!

  • @andresilius5790
    @andresilius5790 7 หลายเดือนก่อน

    Amazing

  • @spaska1184
    @spaska1184 2 ปีที่แล้ว

    great tutorial