what's going on everybody welcome to
another open CV with Python tutorial in
this tutorial we're going to be talking
about how you can go about making your
own hard Cascades for object detection
so the idea of hard Cascades as you were
shown in the previous tutorial was for
detecting specific types of objects and
we showed how to detect a face and some
eyes now we did this with Harr cascade
files that were already created for us
and there are a lot of hard cascade
files that are already created things
like faces and bodies and eyes and
license plates and cars and stuff like
this but for anything beyond really
popular horror cascade types or object
recognition types of things that you're
going to be looking for you just simply
will not find them and the reason why
for one is they're just they're hard to
create and generate and then also I mean
there might be some located in some
obscure places in the web but it's going
to just be really difficult to find them
so it's useful if you can create your
own horror cascade files so to do that
you you can create your own hard
Cascades on a Windows machine but it's a
lot it's a lot different the steps are
going to be a little different as far as
setup is concerned as opposed to on Mac
or Linux now you can get a mat or while
you can't get a Mac user well you
probably could but you can get a Linux
server for really cheap so what we're
going to do is we're going to follow
we're going to go through the steps on a
linux server you can even get a Linux
server for free on something like AWS is
free tier ideally you want a bigger
server the amount of time that you're
going to need this server is probably
two to four hours at most and most hosts
nowadays will charge you by the hour for
your server anyway so we're talking
about it's going to cost you a dime or
something so so if you Windows users you
have a dime to spare go for it you can
use the free tier AWS server you just
won't be able to do as much training on
it as large of a data set as would be
ideal so I really do suggest you get a
server something like digitalocean you
can get a pretty cheap server and you
can just pay by the hour I would
recommend getting a two gigabyte so
ever again just use it for the four
hours that you might need it and then
get rid of it okay so it won't cost you
very much I'll put a link to
digitalocean in the description so the
first thing we want to do is we want to
talk about you know what is the actual
process for hora cascades so the HAR
cascade the way that we're going to make
them is you first you have to have
negative or background images and these
are images that do not contain whatever
your object is that you're tracking and
then you're going to have positive
images now these are images that do
contain the object that you're tracking
ideally you're going to have thousands
of these you'd like to have double the
amount of positive images that you have
for negative or background images when
it comes to time to Train so just keep
that in mind the for the positive images
you can either collect them yourself or
you can create them and I'll show you
guys in a little bit but we'll be using
a website called image net which is like
wordnet if you're familiar with word net
but bottom line you can search for
images and you can download them or at
least find links to them very quick and
easily so that's what we'll be using so
you need negative and positive images
you can either pull the positives or you
can create them with OpenCV we'll just
actually create them dynamically with
OpenCV so be really easy so you'll just
need one positive image the image that I
will be using is this image here it's of
it's just of the face of my watch I
recommend you use an image of your own
just try to make it the same size as my
image so this is a 50 by 50 image so
just fine it finds an object you want to
track make it fill up 50 by 50 pixels
and then follow along with whatever
image you have if at any point you're
failing or whatever you can always go
back and grab one of my images and then
train and see what where you maybe went
wrong so once you have all of that you
for the will return back to how you're
going to organize these images but then
you at one end
happening is once you've collected those
images you create a positive vector file
which basically is stitching together
all of your positive images and then
with that you train your Cascade with
the positives versus the negatives and
then that has a bunch of parameters that
we'll be talking about in the vector
file does too but we'll get there when
we get there but these are the main
steps you have to get to images you got
to get thousands of negatives thousands
of positives you train the vector you
train the Cascade and then you can use
it like we did in the previous tutorial
now
with your your negative and your
positive images you have to have
description files for the negative
images this is a file that just contains
byline the path to each image so you
need to have this so when you run the
training and stuff like that
it knows where to find all the images so
you just need a like generally this is
going to be called BG text then for the
positive images it's a lot like the
negatives only it has a few more
parameters so sometimes this will be
called like info or pause dot text or
something like that okay this is going
to contain the path to each image byline
along with how many objects are in that
image so that's the number one in our
example line and then and then where
they are located so that's the rectangle
coordinates for that image so ideally if
you can you want to find images that are
just of the object and then you can just
automatically say it starts at zero zero
and it spans the full size of the image
and then you don't have to go through
manually a saying where these images are
because that's that's a real pain but
luckily we're going to create them based
off a single image which will be this
watch image and so it won't actually be
an issue really but it will only detect
that watch right so if you wanted to
detect in general analog watches or
something you would need to go back and
do the manual option so finally just
some notes to make before we jump in
you're going to want negative images
that are larger than your positive
images in general especially if you're
going to be creating samples rather than
collecting the labeling positives
because what's going to happen when we
just create samples based on that one
watch image is it's going to take that
watch
image and just kind of superimpose it on
the negative images and that creates
your positive image so that's how that
works so and then generally you're going
to have like maybe 100 by 100 for the
negative images and 50 by 50 for the
positives and then obviously when you
impose the positive on the negative it
will become a 100 by 100 therefore the
images will be the same size so if
you're training and you've got your
image within so for your positives let's
say if you're positive or if your object
doesn't take up the whole image then you
would probably have the same size as
your negatives and your positives but
because we're going to create samples
we're going to automatically place our
50 by 50 watch on a 100 by 100 negative
image and that creates our positive
files so if that's still confusing feel
free to ask below and I'll further kind
of break it down or you can check out
the text-based version of the tutorial
when it comes time to Train generally
you'll even decrease the size even more
generally like a 20 by 20 or something
like that
and finally you generally want to have
double the positives as you do negatives
if you're using create samples it
doesn't matter just get as many
negatives as possible and then when it
comes time to Train you just specify
that hey the number of positives that I
want to use is going to be double the
negatives so that's that okay so once
you're ready you've got an idea of how
we need to go through this entire
process it's a pretty big process so
there's a lot of steps here and we'll
try to break it down as simple as
possible so now that you've got the idea
what we want to do is actually begin to
set up the server that we have so I have
this the server I just basically booted
it up spun it up and we're ready to go I
am currently located in the root
directory I just logged in so I'm in my
root user basically so just slash root
so you might be in slash you know
whatever your user name is so go ahead
and get there and then anytime you have
a new server you always want to just go
ahead and run
apt-get update you might need to do sudo
apt-get update or sudo apt-get and then
the next thing will be upgrade okay so
we need to get those and then we'll end
up creating a little workspace and then
once we're all done setting all this up
that's when we're going to we'll
probably cut there and then go to the
next tutorial
where we start grabbing our images so
that was a update go ahead and run an
upgrade now and yes and after this we'll
end up we're in our username but then
we're going to kind of create a little
workspace for where we're going to
actually you know put our code or images
and all that kind of stuff and then
while we're waiting I'll just kind of
bounce back and forth since this kind of
takes a while to do stuff well the place
that we're going to go to get images is
going to be this website here it's
imagenet so it's image - net dot org and
what's cool about image net is first of
all it works with wordnet identically
it's the same structure so if you're
familiar with word net it's just a way
to figure out what words mean get
synonyms and antonyms and stuff like
that it's really cool it's pretty it's
pretty popular if you've done like the
NL TK stuff okay we're going to pause
from that and continue on over here and
our server now this is done the next
thing we're going to do is we're going
to make directory open CV underscore
workspace or whatever you want to call
it and then change directory into that
directory and if you don't already have
it
sudo apt-get install git this is for
github so we can grab the open CV this
will just let us get the latest version
of open CV basically and then we're
going to want to clone that so we're
going to go get clone HTTP colon slash
slash github.com slash capital IT SE z
slash open CV dot git for with these
lines that we're typing you can actually
the the text-based version of this
tutorial is on Python program net so you
can actually head there and get all
these lines and probably the next few
lines I'm going to just say
you guys there because they're kind of
long it's a lot of packages that we have
to install but I'll explain what you're
doing as you're installing it so right
now we're grabbing the open CV files
here so we'll let that Rumble so anyways
back to image net you you can you get
access to a bunch of images but you can
also just search manually for these
images we're done okay so now we're
going to go back to our server and let's
go ahead and I'm just going to start
copying and pasting these commands so
this is for the compiler so if you if
you go to the link in the description
just do ctrl f4 find compiler : and
you'll be on the line that we're on
right now run that we'll grab that sure
enough so we can search for something
like for example we can search for a
watch okay and sure enough we get all
these sin sets for watches and the one
that we want might be analog watch right
that's the type of watch so we'll click
that and we'll let that load here and
we'll come back over and now we're going
to install the library so again ctrl F
libraries : would get you to where you
want to be install all these libraries
well we're kind of double installing get
there but that's okay yes back to image
net well that's going I've lost my mouse
there it is so here we have just bunch
images of watches so you can click them
and then you can even go to the URL that
is this you know it's on these flicker
calm so you could you know you can
manually download these but let's be
real so you can come to downloads tab
and they have all the URLs so you can
click on this and it's just a list of
all the URLs that are of analog watches
so this is we can view the source come
down we got about yeah one thousand two
hundred and seventy rows of watch URLs
okay so we downloaded the libraries back
to our server
hopefully this bouncing around isn't
killing you guys and now we're going to
do the python bindings
let's get those we're almost done we got
really I think just one more thing that
we got to grab okay so this is that list
so so if you wanted to create your own
positives you
use this list and then for example most
of these images actually are almost 100
s no longer available we'll actually be
talking about this because if you notice
on Flickr what we're going to do is
we're going to automatically go through
and pull all these pictures with a
program and this is a picture I can't
believe they did this to us it's
horrible so we'll talk about how we can
handle for when this nonsense occurs but
anyways most of the time the image is
like a hundred percent the thing that we
search for especially like here and here
so you could get away with saying all
these images after you resize them start
at zero zero span for this thing and
then you can get a general search going
like that accuracy like you're gonna
have images like this where that might
cause a lot of trouble so keep that in
mind so that was all the Python bindings
now we're going to get the opencv
development library so app get installed
lib OpenCV - dev do that yes sir
and um let that download and and we're
good to go but like I said we're
actually not going to use these it would
take twice as long and actually this
won't be that accurate because again
you've got a lot of images like this
you'd really need to scale them down
manually so I don't really want to go
through that process because that's
that's a really tedious process but if
you wanted to you can do that rather
than use create samples which if you're
not familiar you're still fuzzy about
that don't worry you'll be clear on all
of these commands by the time we get
done but instead of creating samples you
can actually create your own images from
these images rescale them or whatever
and you could even go through by hand
and just rescale them because these are
all much larger than 50 by 50 or 100 by
100 even so scale them yourself crop
them there are programs that you can run
through and crop them pretty quick but
you still gotta do it manually and
that's a pain for watch you could do
edge detection find the circle there are
algorithms to find circles with OpenCV
actually find the circle crop to that
circle
save go that would obviously only work
for watches but it is it's possible and
you can try to do algorithms like that
each time but we're gonna leave that
alone for now so so anyways so that's
that so so so so that's how you would
maybe do the positives was that enough
shows I'm not sure that's how you do the
positive so what about negatives well we
can use the exact same website for
negatives we could search for people ok
click on the sin set for people awesome
we'll start exploring and sure enough we
get a bunch of people yeah these people
could be wearing watches but by the time
we scale these images down you're not
even going to see it one of the ones
that I'm going to use actually one of
the links we use this people link but
we're also going to use sports and
athletics these are the two links we're
going to use so you can get the
downloads for the people you can get the
downloads for sports and athletics my my
opinion there is especially with like
sports and athletics people might be
wearing a watch but they're probably not
wearing an analog watch while they're
doing sports so anyways that's what
we're going to do the next tutorial
you're all set up at this point you've
got your server if you're on Windows and
you don't know how to set up servers and
stuff like that you can basically use
this tutorial here the introduction of
practical flash for getting everything
set up I think at this point this is a
little bit more of an advanced tutorial
I hope that you guys just kind of know
what you're going to use to connect to
your server but if you don't I'm still
here to help no matter what so anyways
get to this point get all the things you
need installed and then in the next
tutorial we're going to be doing is
we're going to run through and we're
going to run through imagenet basically
those URL lists and all we're going to
write a Python program that will
automatically download resize convert to
grayscale and get rid of those nasty
images like that Flickr image do all
that save the images and go from there
so anyways if you guys have questions
comments whatever feel free to leave
them below otherwise as always thanks
for watching thanks for all the support
subscriptions and until next time