Training a Cascade Classifier - OpenCV Object Detection in Games #8

In this tutorial, we train an OpenCV Cascade Classifier entirely on Windows to detect objects in a video game in real-time. Making your own Haar Cascade isn't complicated, but it can be a lot of work. This project is a great intro for beginners to Machine Learning. In this video I explain the Machine Learning basics, and walk you through the implementation of training and using your own model for computer vision in Python.
Full tutorial playlist: • OpenCV Object Detectio...
Grab the code on GitHub: github.com/learncodebygaming/...
Official tutorial on training a Cascade Classifier: docs.opencv.org/4.2.0/dc/d88/...
Official tutorial on using a Cascade Classifier: docs.opencv.org/4.2.0/db/d28/...
Installing OpenCV 3.4 on Windows: docs.opencv.org/3.4.11/d3/d52...
0:42 Machine Learning basics
2:28 Haar Cascade Classifier explained
4:04 Gathering the positive and negative images
7:06 Creating the negative description file
8:59 The positive description file
9:41 Installing opencv_annotation, opencv_createsamples, and opencv_traincascade on Windows
12:19 How to use opencv_annotation
15:17 Fixing Error: Assertion failed...
15:45 How to use opencv_createsamples
18:06 How to train a model with opencv_traincascade
22:24 How machine learning training works for image classifiers
23:06 How to use a trained model for object detection
25:59 How to train a better cascade classifier
29:00 What is overfitting?
30:27 Arguments used for my best Haar Cascade Classifier model
Full written tutorial: learncodebygaming.com/blog/tr...
My gear: learncodebygaming.com/gear
Normally when we program something, like when we write a function (for example), we expect certain inputs (like the parameters in our function). And then at the end we'll end up with some output (like the return part of a function). And in the middle, to get from the input to the output, we write some logic (if statements, loops, all that stuff).
With Machine Learning it's exactly the same, except that middle part is replaced by a Machine Learning model. So with Machine Learning you're not writing any of your own logic anymore, instead you're trusting this mysterious dark jumble of multi-dimensional calculus to transform your inputs into your desired output.
And at first your model won't know how to do... what you want it to do. Its output will be no better than random guesses. To get the output we want, we must first train our model. We do that by showing it lots of input examples, and for each example we tell the model what we want the output to be. Once it has seen enough examples, a well trained model will be able to accurately predict what the output should be given some new set of inputs.
That's the super summarized version of how all Machine Learning works.
In our case, our input is going to be screenshot images from the video game we're playing. And the output we want is a list of rectangles that identify the objects we're trying to detect. And fortunately for us, OpenCV's Cascade Classifiers are designed to do exactly that.
The way a Haar classifier works is it looks for features in an image, very much like the ORB feature detection we talked about in the last video. And it looks for these features in different layers. So at the top layer it will be looking at large features that span nearly the whole image window, down to the bottom layer where it’s looking for very fine details. This makes the end model fast enough to detect objects in real-time, because it can quickly reject areas of the image that fail to match the features in the top-most layers. And it can spend more time analyzing areas of the image that are good candidates, by studying those finer details.
Hopefully you have a general understanding now of how Machine Learning works and what makes a Haar Cascade Classifier unique. The great part is, the code for all of this is very straight forward.
The art of doing this well actually isn't so much in the code, it's more in gathering the data to train your model with. To get good results, you need quality data, and you need lots of it.
Now we need two types of data: We need the positive images - which are images that contain the object we're trying to detect... and we need negative images - which will be screenshots from the game that don't contain our object at all. The Machine Learning algorithm needs to see both what is and what is not the object in order for it to learn.
Continue with the written tutorial here: learncodebygaming.com/blog/tr...

Пікірлер: 250

  • @aivaki-
    @aivaki-3 жыл бұрын

    Bro, you are the best

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Thanks man

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    @wise guy Check out the rest of this tutorial playlist! You probably just need to watch #1, #4, #5 and then you can dive into the machine learning stuff talked about in this video.

  • @van3787
    @van37872 жыл бұрын

    more machine learning content will be greatly appreciated, this is awesome!

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

    Thanks a lot Ben ! This is very exciting ,we really want to see more machine learning content from you !

  • @sswang9815
    @sswang98153 жыл бұрын

    That really helps a lot! Thank you dude! That's the best tutorial of Cascade Classifier in youtube!

  • @andreivladescu7178
    @andreivladescu71782 жыл бұрын

    Best haar cascade tutorial i've watched! Keep up the good work!

  • @steelederic
    @steelederic3 жыл бұрын

    Absolutely fantastic man. I never thought I'd be casually setting up machine learning, debugging, scrapping it and coming up with better results..all testing on my favorite games. Cheers mate.

  • @michael4353
    @michael43532 жыл бұрын

    Great series! I enjoyed each video and walked away with a tool belt filled with new tools. Take care and best of luck in your future endeavors.

  • @zeroAGI
    @zeroAGI2 жыл бұрын

    Wonderful work Ben! Keep inspiring people with your work!

  • @Penguin-kr9do
    @Penguin-kr9do3 жыл бұрын

    Thank you for explaining everything! I looked at a lot of other tutorials that skipped over so much about how to install the commands, how to get everything set up, etc. Yours is a nice up-to-date tutorial from start-to-finish!

  • @rikkujoseph996
    @rikkujoseph9963 жыл бұрын

    You are simply awesome. your interest to teach along with the strain that you took to collect the training data shows it all. Please keep on doing this good work. God bless you.

  • @jctelo24
    @jctelo242 жыл бұрын

    Amazing tutorial! Thank you for sharing the knowledge.

  • @akibkhan4822
    @akibkhan48223 жыл бұрын

    Thank you so much for this in depth tutorial. This is really helpful, cheers mate.

  • @hackercop
    @hackercop2 жыл бұрын

    This and the resources you provided in the description was extremely helpful thanks. Liked and subscribed!

  • @WvyyFPS
    @WvyyFPS3 жыл бұрын

    Super underrated. Thanks a lot for all of the information

  • @RoloProduction
    @RoloProduction3 жыл бұрын

    Great content! Thank you for sharing.

  • @georgecarder4695
    @georgecarder46959 ай бұрын

    Great video! Exactly what I needed for a project I'm working on. It's great to get a walkthrough to "grandfather" us through the process.

  • @psmith5573
    @psmith55733 жыл бұрын

    I don't always give a comment, but when I do it's to the epic series! Damn good bro. Keep up the good job. I'm watching you.

  • @mohammadsaleh1117
    @mohammadsaleh11172 жыл бұрын

    I've been looking for a tutorial and I didn't got anything from them. and thankfully I found your channel you are describing things easily and I understood everything I need to. thank you so much Ben. keep going building great tutorial

  • @jaroslawbajon512
    @jaroslawbajon5123 жыл бұрын

    Best of best, thank you so much!

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

    Great video! seen countless others that were hard to follow. This made it make perfect sense!

  • @actellimQT
    @actellimQT7 ай бұрын

    This was AWESOME!!! Much more comprehensive than the docs!

  • @hammadharoon9557
    @hammadharoon95572 жыл бұрын

    Such a great tutorial!!! Thank you!!

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

    This is literally the best programming channel I’ve ever seen

  • @Spasmomen
    @Spasmomen3 жыл бұрын

    This was a great tutorial! Impossible to find all this online. Thanks for the pointer towards the compiled apps you need to train the model.

  • @gmnvh
    @gmnvh2 жыл бұрын

    Good job! Really helpful!!! Thanks for sharing!

  • @drunken400
    @drunken4003 жыл бұрын

    Thank you for your Videos, I'm excited for the next one! Great work what will help me on my project :-)

  • @manojtiwari7754
    @manojtiwari77543 жыл бұрын

    You're awesome brother.. keep the good work!! Love your videos

  • @SpacebarWarrior
    @SpacebarWarrior3 жыл бұрын

    Really appreciate all the effort you've invested in your videos. I am learning a lot from watching your videos and you've somewhat boosted my motivation to better myself with python. Thanks a lot, hope to see more of your videos!

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Thanks man, happy coding!

  • @doctorglock1691
    @doctorglock16913 жыл бұрын

    Thank you so much! This is a wonderful series. This is my foundation for my opencv projects. I like the way you code and document. It's always complete and easy to understand.

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Glad you like it! Complete and easy to understand is what I'm going for.

  • @giovannicriscwcc4249
    @giovannicriscwcc42492 жыл бұрын

    Compliments ! this is very clear and well done , thanks for your job !

  • @ronanleviennesse3752
    @ronanleviennesse37523 жыл бұрын

    Thanks for this super useful and quality video !

  • @JayJay-ki4mi
    @JayJay-ki4mi2 жыл бұрын

    This opened my eyes as to what opencv can do. I am learning this stuff for gaming too.

  • @thenewsnetwork-pressytchan2637
    @thenewsnetwork-pressytchan26373 жыл бұрын

    Super helpful tutorial, thanks!

  • @narcisopascoal4498
    @narcisopascoal44983 жыл бұрын

    I watched tutorials in my own language but I didn't understand it as well as I do now. Thank you very much

  • @BlinkPls
    @BlinkPls3 жыл бұрын

    Thanks for the new video!

  • @joelweber3473
    @joelweber34732 жыл бұрын

    Bro this was such and awesome tutorial that helped me get started on my own project.

  • @sanyamkarnavat9745
    @sanyamkarnavat97452 жыл бұрын

    Man I am in love with this channel plz keep uploading vids❤👌

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

    Thank you very much! It worked and im very proud of doing this project. You're a very good teacher :-)

  • @syzygy8060
    @syzygy80603 жыл бұрын

    Thank you man. You are an excellent teacher. :)

  • @once6833
    @once68333 жыл бұрын

    thank you for this. doing this series making easier for people like me to learn these. thankss

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Happy to make these. Thanks for watching!

  • @evawyf
    @evawyf3 жыл бұрын

    This series is really awesome!

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Glad you like it!

  • @anonymoustree2182
    @anonymoustree21822 жыл бұрын

    This was really nice, thank you!

  • @SoulEaterZika
    @SoulEaterZika3 жыл бұрын

    Guy, this video help me too much. Congratulations for the clarity in the explanation. Was very hard to discover that i need to download the 3.4 version to use this features of cascade classifier. The info in the opencv site and forum are very disconnected. Your video sintetize all with clarity. Thank you so mutch!

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

    Helped me to add a new feature on my self driving car project! Cows gonna thank you!

  • @ravenebonscale9687
    @ravenebonscale96873 жыл бұрын

    You have helped me a lot when it comes to learning to program. Your channel alone has taught me from a single video then playlist from other channels

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Awesome, glad my style is working for you!

  • @Currychicken5
    @Currychicken53 жыл бұрын

    I will definitly try this :) I really like the videos

  • @BillEngwall
    @BillEngwall2 жыл бұрын

    Hey Ben! Thanks for doing this, much appreciated :)

  • @turner7777
    @turner77772 жыл бұрын

    Wow i really like your video format, you explain well! Subbed!

  • @footdle
    @footdle3 жыл бұрын

    This is the video with most likes that doesn't have a dislike that i've ever seen! Good job :p

  • @austingulotta9817
    @austingulotta98173 жыл бұрын

    I've struggled with looking through and understanding the OpenCV docs for Haar Classifiers. Thank you so much.

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Happy to help!

  • @FedericoCipriani27
    @FedericoCipriani273 жыл бұрын

    This video series has been one of the most concise I've seen so far... If it were possible to upload subtitles, I would like to upload subtitles in Spanish. Also it would be nice to see a comparison or review of YOLO algorithm.

  • @emperorpenguinv7193
    @emperorpenguinv71933 жыл бұрын

    This is the first series I am actually following on KZread, where I am not 3 years too late. Great content! I enjoyed every video so far, even though I know alot of the stuff in this one as I am currently writing my bachelor thesis in the field of machine learning.

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Cool glad you like it! Good luck with your thesis.

  • @0zero560

    @0zero560

    Жыл бұрын

    I’m 2 years late

  • @elcharlsen

    @elcharlsen

    7 ай бұрын

    and I'm 3 years late now, how ironic@@0zero560

  • @OnceUponATimeTeller

    @OnceUponATimeTeller

    6 ай бұрын

    3 years late here

  • @masfergi
    @masfergi3 жыл бұрын

    I really love and enjou your concent. I haven't meet much youtuber who make programming and ai video like you did. I hope this channel grow more and become great.

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Thank you, I enjoy making these. Hope I can do it full-time someday!

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

    Nice video and a great explanation.

  • @ashaytelang
    @ashaytelang3 жыл бұрын

    Man, you are awesome! love from India

  • @rabbitfeet6740
    @rabbitfeet67403 жыл бұрын

    this is best tutorial ever !

  • @nishitmachinewala296
    @nishitmachinewala2965 ай бұрын

    Ben i like that hand wave, "confuse no more". :)

  • @davidgladwell6283
    @davidgladwell62833 жыл бұрын

    Hey great videos, it's helped me out a bunch for my university project where I have to detect car wheels in a video feed!

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Awesome glad to help!

  • @randywilson595
    @randywilson5959 ай бұрын

    I want to see more machine learning content thanks!!

  • @CyberZyro
    @CyberZyro3 жыл бұрын

    bro u made me pro from noob u are god!!! luv from India brother.

  • @ssembeds522
    @ssembeds5222 жыл бұрын

    That was superb !

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

    Superb Bro..

  • @IdeeFixeGamer
    @IdeeFixeGamer3 жыл бұрын

    your so good at these

  • @fabioprudencio
    @fabioprudencio3 жыл бұрын

    thank you, helped me a lot

  • @cristianmerono
    @cristianmerono3 жыл бұрын

    Thanks a lot, really !! Nice content

  • @anasali5202
    @anasali52023 жыл бұрын

    Thank you bhaiya! This helped me a lot

  • @marsparera
    @marsparera3 жыл бұрын

    Bro, you're the best!

  • @lbproductions6215
    @lbproductions62153 жыл бұрын

    Very cool!!!!!!!

  • @user-uq2pd8zk3x
    @user-uq2pd8zk3x28 күн бұрын

    this is amazing!

  • @thewekendd
    @thewekendd3 жыл бұрын

    you are doing really good job, you get my attencion and i want to learn more about machine learning, i appreciated that you are making videos about programing :>>

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Thanks!

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

    Looking forward to this

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Hey, glad you're still with me!

  • @archamondearchenwold8084
    @archamondearchenwold80842 жыл бұрын

    I find your voice very soothing.

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

    good data good data thanks man

  • @BHPFestival
    @BHPFestival2 жыл бұрын

    Finally, a great and straight-forward tutorial on training haar cascades. Thank you. A couple of questions for you. I have the google imageset with all the annotations. Can I convert those and combine them into one file for use here? And secondly, I am assuming none of the negative images are marked. Is that correct.

  • @mrsDjame
    @mrsDjame3 жыл бұрын

    very great content thank you so much

  • @rakeshmulik1658
    @rakeshmulik16583 жыл бұрын

    Another awesome video. No one on KZread i have seen teaching the def. You should start teaching python basic courses. Loved ur work..keep it up

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Thanks man. Yeah I've thought about doing some more formal courses eventually.

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

    A shame you are making no more of these. I really like them.

  • @dhruvagrawal3856
    @dhruvagrawal38563 жыл бұрын

    Thanks man , For this whole series I had an experience making bots with pyautogui but it was very slow and weakk also inaccurate a bit, I found about opencv but I never ever thought I could do cool stufff but by watching your vids and reading blogs I learnt everything about opencv I made 5 bots with opencv , all very nice, game were :- 1 - Burrito Bison Launcha Libre 2 - stickman fight game 3 - archery game 4 - fruit ninja 5 - Air wars 3 Note : - You may also need to know a little bit of pyautogui for these for option like for clicking, double press, pressing key, etc, detection with opencv and of course basic python and also math

  • @tahirdevlog2047
    @tahirdevlog20472 жыл бұрын

    GREAT CONTENT

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

    You are creating monsters dude....

  • @huang3161
    @huang31613 жыл бұрын

    Hi, finally it comes to ML method. But could you make a new video on how to use YOLOv4 weights to detect object in the screen? I have followed some other tutorials and learned how to train a custom object detection model, but still have no idea how to deploy it into a real-time scenario. That would be very nice of you and thanks a ton!

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

    This is so cool

  • @muhammadayyub5943
    @muhammadayyub59432 жыл бұрын

    great bro!!!

  • @Alex_Aly
    @Alex_Aly3 жыл бұрын

    if you apply affine transformations to all of the images you could expand the dataset significantly

  • @giraffe1877
    @giraffe18773 жыл бұрын

    good vid

  • @froozynoobfan
    @froozynoobfan3 жыл бұрын

    if you can get a confidence score for each rectangle then you could extend your training data semi automatically

  • @wildpants9347
    @wildpants93473 жыл бұрын

    Please do more bot stuff in python, automating stuff with it etc etc, found your channel a week back been checkng out game botting luved it...SUbbed!!

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Thanks! I've got more Python automation stuff planned for the near future.

  • @amirhosein.najafy9913
    @amirhosein.najafy99133 жыл бұрын

    amazing thank you

  • @espvolt4833
    @espvolt48333 жыл бұрын

    You should really be bigger your content is better than most

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Thanks!

  • @luisroma7138
    @luisroma71383 жыл бұрын

    Thanks for this series. I am learning a lot. Im trying to create a model that detects my character in Binding of Isaac. It is challenging to obtain negative examples (in a optimal way) because Isaac is always in the frame. What I am doing is just getting some screencaps and cropping (manually) Isaac out of it. So I get the screencaps with a white rectangle. Do you have any suggestion about how can I do this in a better way?

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

    Subscribed!

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

    Great video it would have been great if you added CUDA support for detection I have already added it with OpenCV Cuda Support

  • @davistong
    @davistong3 жыл бұрын

    yo this is dope

  • @Ungido75
    @Ungido753 жыл бұрын

    Thanks. If my target is to train a model with monotonous events, objects of interest only matter if they have some same relative position, may the detection will work faster or accurately?

  • @abdallahazme3737
    @abdallahazme37372 жыл бұрын

    Great work, thank you for this amazing play list! I'm very new in programing, and want to know can i program an AI to run sepcific quest in a game? and if yes then how?!

  • @atsimourtos
    @atsimourtos6 ай бұрын

    Crazy tutorial and thank you for sharing. Just a thought, manually marking the rectangles for positive images seems a very consuming proccess - especially if you have a big data set or multiple elements that you would like to create a cascade classifier. Is there an alternative such as: if we just had limestone(or related element) cropped image(s) in the positives folder and not a whole screenshot/specific coords that must be given manually.

  • @mon76ph
    @mon76ph3 жыл бұрын

    Thanks ben.. I think it will be applicable in autosolving verification system of rise of kingdom game..

  • @SohanSharad
    @SohanSharad3 жыл бұрын

    Been waiting for this video for a long time And as for moving towards those limestones, do we use pathfinding Ai?

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    Thanks for sticking with me!

  • @dieweltentdecker5878

    @dieweltentdecker5878

    3 жыл бұрын

    Learn Code By Gaming Do the youtube app has a new black white design?

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    @@dieweltentdecker5878 Does it? I just got a new phone and I can't remember what it looked like on my old one already. I've got the dark theme enabled on desktop.

  • @dieweltentdecker5878

    @dieweltentdecker5878

    3 жыл бұрын

    Learn Code By Gaming Just for 1h it comes.

  • @LearnCodeByGaming

    @LearnCodeByGaming

    3 жыл бұрын

    I plan on just clicking the detected objects, but some sort of path finding would be much slicker. I've been getting a lot of questions about paths and routing a lot lately, but it's a pretty big topic so I'll have to make a separate video about it.

  • @matthewhoey3058
    @matthewhoey30582 жыл бұрын

    i have followed the series so far, as i am extremely new to python and barely have no experience, but ill learn what i need to to get what i want done, but this is the bit im unsure of, because idk how to execute it. >How would i get it so whenever the 'object' is detected on the screen it executes some commands for me, more specifically when the object is detected, it sends a signal to an Arduino that then powers some LED's or something?? - so far this has been great help, and thank you for your series!

  • @Dr.AhmedQusaySabri
    @Dr.AhmedQusaySabri2 жыл бұрын

    Than you