All right, so I've got my motion detection
program running here, and as
you see this is capturing me moving at
the moment. If I move away,
[No audio]
you can only see some shadows there,
[No audio]
and so this
is great. But as I said, you may want to
store the time, so when an object enters
the frame, so that you end up
with something like this.
[No audio]
So this is the first entrance, as the start time here
is where the object exited the frame,
and then the second entrance of the
object, and so on. So in this
lecture, I'll show you how to build
that table using this script. So we
will add some more code here, and the way to
start doing this is to first figure out
the point, the line in the script where
the status is changing from, let's say,
motion to non-motion. Hence, you, we have
non-motion in here. So as soon as
webcam triggers, let's say the status is
0. So just a symbol that I'm choosing
to denote that there's no motion in the
current frame. So then you get that
first_frame, which has a status of 0,
and then you grayed out, and then you check
if the first_frame is None and then you
continue the loop again, and then you grab
the second frame, so the status is still
0, and then you apply the difference and the
threshold, and the dilate, and then you iterate
through contours. So if you find an area
that is less than, I changed this
earlier, I put it 10000. So I wanted to
detect bigger objects, because I was
closer to the camera, so I wanted to
adjust it. So depending on what objects
you want to capture, here you want to adjust
these pixels here.
[No audio]
So 10000 is something like
100 by 100 pixels window.
So we're checking if you don't find a
window that is bigger than 10000, then
you continue to the beginning of the
loop until you find a contour with
this size bigger than this. When Python
finds that contour with that size, we
want to change the status to 1.
[No audio]
Okay.
And let's now print out that status variable.
[No audio]
So what you want to do is
print it at the end of your loop, so status.
And let me delete this NumPy arrays,
which are being printed, we don't need
those, and make some room here.
[No audio]
So this is inside the loop, and I now expect
to see the status, so 1 or 0 or 0 printed
out depending on whether there is an
object in the frame or not. So if this
is working, that means we now have
actual status of the frame, and we can
use that 1 or 0 for other things. So let's see.
[No audio]
Okay, there is nothing at the moment. Let me
[No audio]
minimize this. So we can
see the console.
[No audio]
This as well. Great. So
0 is being printed out, and if
something shows up, now you see 1s.
[No audio]
Again, 1, 0, 1, 1, 0, great.
[No audio]
We can now simply apply a datetime method,
you know, such as datetime.now,
and that would record the time of
each frame. So we don't want that
exactly. We want to use a datetime.now
method but not like that. Because
that would record times for
every frame. So what we want instead is
we want to know when the status changes
from 0 to 1, which indicates that
there is an object entering the frame.
So we want to record that time, and we
also want to record the other time when
the status changes from 1 to 0, so
the object exits the frame. Let's say
status_list, that will be equal to
empty brackets, so an empty list, and
then I want to append the status of the
object to that list.
So after this loop finishes,
which is here, so I want to be
here at the same level with the loop, so I
want to apply something outside the loop,
so status_list, I want to append the
current status of the list here in the
status_list. Great, and to illustrate,
to show you what this status_list is
about, I'd like to print that out actually,
[No audio]
and I would want to print out
a list outside the while loop. So I
don't want to print out every
time the loop iterates.
[No audio]
So no movement, movement here,
[No audio]
and quit. So these are the statuses
for each of the frames, so 0, 0, 0, no movement,
and then something entered the frame, and
then something entered, like, let's
say 30 frames and then the object exits
the frame, and that's it. So what we
want to do now is we want to capture, so
what's next. So the next thing is we
want to capture the time that we switch
from 0 to 1, and also from 1 to
0. So basically, what we're
interested about is to capture the last
two items on the status_list. So let's
say we have status 0, so we
start with a empty list, and the first item
that will be appended to the status_list
is 0, and then the second item, let's
say will be 0, or let's say will be
1, and so we want to apply a conditional
to check whether the last two items of
the lists are changing. So I know all this
may be confusing, but once I write the,
this four lines of code here, you'll
understand it, and it will make a lot of
sense. So we want to check if status_list,
so the last item of the status_list,
which has an index of -1,
remember, list indexing, if that is
equal to 1,
[No audio]
and status_list with index
of -2, so the item that was added
first, the item that was added second,
is equal to 0.
[No audio]
We want to record the
datetime of this event in the list. So
let's create a list here, say times
equals empty, and then we go here, here,
and we say times.append datetime.now,
you know about this function
already, and hence so we want to append the
current timestamp when this change occurred.
[No audio]
So that means we need to import
[Author typing]
or from datetime import
datetime. So datetime class from the datetime library.
[No audio]
So we're basically recording the
time when a status
changed to 1, and we also wants to
record the time when the status changes
from 1 to 0. So we would have a
similar conditional
but this will be 0 and this would be
1, okay.
[No audio]
So what's happening here is let's go
back to the start of the loop. So,
Python captured the first_frame and it
converts it to gray and blurred it and
apply GaussianBlur there to the first_frame,
and then we store the first_frame
in the first_frame variable and then we
go ahead and calculate the difference
between the first_frame and the current
frame, findContours, and then if this
little findContours greater than
10000, a status variable changes from
0, so it started at 0, so it changes
at 1 here, and that means this empty
status_list will get the another value. But
the first value is always 0. So the
list will start with 0, you know,
like here, so the very first value is 0,
and so on. Let's say for the next 10
frames, it will be 0, 0, 0, 0, and so on, and
then if an object enters the frame and then the
status changes to 1, so we'll have
0, 0, 0, 0, and then 1, and then what we're
doing here is, we're comparing the last
two items of the list, okay. And here, I'd like to
print out the times.list, so that you see
what's going on, and I expect to get an
error at the first execution here,
because we have something more to do.
But let's see.
[No audio]
So yeah, it says list
index is out of range. So that means
Python is having troubles accessing the
second item of the list, because the
list has no items here, so it has no
items, and then we add an item, so we
add status, and then Python tries to
find the second item, but there is no
second item. So we need to do a trick
here, we need to add two items, okay. Let's
say None and None.
Now let's execute the script again.
[No audio]
So there's no object there at the moment,
now an object appears. It goes out
again, two times, and one more time, so
three entrances,
[No audio]
and I quit it. So let
me go through this list now. This is
our times.list, and so this is the time,
so the year, the month, a day, and hour,
minutes, and seconds, and then
microseconds in here. So this is the
start time when an object entered the
frame for the first time, and then this
is the time when the object exits the
frame. So I stayed there for 2 seconds,
so 51 upto 53, and then the next object
enter the frame, so from this seconds to
this, so for 2 other seconds, and then
another third time, from 0 to 1, for
1 second. So we've got three entrances
in the frame. So I hope that makes
sense. This is good. But sometimes, see
it can happen that when you're running the script,
[No audio]
so you have background, and then
the object shows, and the object disappears,
and shows again, and then if you quit the
program at this point, so let me quit it,
[No audio]
you'll see that you have the start time
here for the first entrance, then the
end time, and then you have another start
time here for the second entrance.
But then you don't have the exit time. So
you may want to add the end time of
the exit of your last object, which
happens to be when the window was quit.
So you want to go here at the end, and
the script is waiting for a key, so if
you press quit here, the while loop will
break. But before that, you may want to
check the status. So if status is 1,
and in that case, you'd add knob with
this scenario here. So if status is 1,
when you press the Q key from the keyboard,
[No audio]
you want to add another item
to your times.list, so times.append
datetime.now. Great.
[No audio]
So that should work differently now. So
let's execute the script.
[No audio]
No object, object, no object, object in the frame,
quit, and now we should have four
datetimes here, four timestamps 1, 2, 3, and
4. So this here was recorded in this
expression here. Great. And the next thing
we want to do now is to throw this list
into a Pandas DataFrame, and then into a
csv file, so we'll have a start
and an end column, and in the start
column, we would have, for instance,
this value, and in the end column, we
would have the other values, so this one
here, and the last one, this, okay, so first
of all, we need an empty DataFrame, a
Pandas DataFrame. So we'll be using
Pandas to create DataFrames, of course.
So let me import pandas here, and let's
say, DataFrame, df equals to pandas.DataFrame,
[No audio]
with columns
[No audio]
Start and End, as easy as that. So that will
create a DataFrame structure for us,
which has no values, but it has two columns.
[No audio]
And then the next thing you want to do is
outside the loop, so once
you have created, once you have
generated your times.list, you want to
iterate through that list, and then
append those values into the Pandas
DataFrame. So you need a loop, let's
say for i in range, and so for here,
you'd have to iterate with a step of
2 , and you'd want to iterate as many
times as there are values in the list.
So you'll start from 0, and then you'd
end up at how many values you have in the
list? Well, you can find that using the
length method. So length times with a
step of 2, great, so that will be, so
for each iteration df, so the DataFrame
will be equal to itself, .attend, so
you are updating the DataFrame, and
here you'd need to pass a dictionary. So
for the Start column, you'd want to
enter times i
and for the End column,
[No audio]
so the keys of the dictionary here are
Start and End, and you'd want to append
times i+1,
and you want to ignore_index,
so equal to True. Great. So what
this is doing is, let's say for i in
range, this will be, let's say, we have a
list with 6 items. So we have 3
Start times and 3 End times. So that
would mean range 0 to 6 with a
step of 2. So for 0, we would do
this. So we will start from 0 in the
loop, and we would access the item with
the 0 index in times, which happens
to be this item for instance, so the first item
[No audio]
and then we append that item
to the first row of the Start column.
[No audio]
And then we access the item 0+1
which happens to be item with index
1, which should be this item, so the
second item, and we append that item to
the End column for the same row. So
that's it, then we go to the next item.
So for so this time, we would start with
index 3, so 3 here added to the
Start column and then 4 added to the
Start column to the add column, so we add
so on, and lastly, we would have a
DataFrame, and what you might want to
do then is df.to_csv,
[Author typing]
let's say times.csv.
So we are exporting the DataFrame
to a csv file.
Great. So let's see how this will go.
[No audio]
It's getting quite dark here. Okay, and
one more time, frame, quit,
[No audio]
and as you see times.csv was
created in my file system.
[No audio]
So these are the times.
[No audio]
I can also open it using Excel.
[No audio]
So these are the
Start and End times. if you want to get
four month then just four months sales,
here and you can see the dates now.
Great. Great.
[No audio]
So that ends this lecture.
And I hope you enjoyed it. We have quite
a good script now, and to make it more
beautiful, we would want to display
these times in a graph. So that's how
you turn your data into real
information. And we'll do that through the
next lectures. But before I'd like to
introduce you to the bokeh library,
which is a library for interactive
visualizations in the browser, so
please follow the lectures in the
correct order, and I'm sure you will
learn a lot. Talk to you later.