Hey, welcome back.
Our program is working fine.
It can detect objects and draw a rectangle around them.
Now it's time to think about how we can connect.
This script with an email sending function.
Well, for that we're going to write a function in a separate file.
So in this emailing that P file, for example.
So let's define a send email function.
And we're going to write the code later on.
So for now, let's just print something like email was sent.
Right.
So this function now will have to be called from main dot P.
So when and where are we going to call the function?
At what point?
In the script.
So think again about this.
The frame is static.
No objects have entered the view event.
An object enters.
Right?
So when this happens, we draw a rectangle around the object.
But this should also be the point when we trigger that email sending function.
So when we detect that there is movement and that is in my script, that would be in.
Line 26 right here.
Well, actually, it's not exactly line 26, but it is line 28, because in here we are saying that
if the counter is less than 5000 the area, then we assume that there is no object, there is no real
object in the camera.
So in fact.
If that doesn't occur, then this occurs.
So at this point we know that there was an object.
So in line 28.
But we can also do this like, you know, we can assign a rectangle variable to CV to that rectangle.
So we store the rectangle arrays, the rectangle known by matrices in this variable.
And then we say.
If rectangle.
Send email, send emails should be imported so from email link import sent email.
So make sure that this is the name of your function.
Sends email from emailing that py sends email from emailing.
So we call that function in here if we notice a rectangle.
Notice that if rectangle is inside the for loop so one indentation level below the for loop.
So let's try this run.
So I always start with static frame.
So please don't show on camera when you are just running your program and then introduce an object there.
For example, I just introduced an object and we got an error.
It says that the true value of an array so rectangle is an array with more than one element is ambiguous.
So you use you should use that any or all.
So that's what we do here.
A rectangle that any.
As we are being advised from the error messages.
Run again.
There's a static view.
Let's introduce an object.
So there we go.
And you can see that in the terminal we have this email was sent string being printed out multiple times.
All right, so the email function.
It's being executed as many times as there are rectangles being detected.
So this would mean that the email function would be executed multiple times, and that means the email
would be sent out multiple times.
But that is not what we want because that's just too much.
So we only want to send the email once.
Now, there might might be different ways to approach this, but one way which we will use is to send
the email when the object has exited the frame.
So which means that now there is no object and then an object enters the frame.
We don't send the email yet.
But then when the object has exited the frame, then is when we send the email.
So let's see how we can do that.
Well, to do that, we can assign a value of one to this status variable.
So this variable is one when there are rectangles, in other words, when there is an object in the
view.
Otherwise status will be zero.
Status is equal to zero.
So when the loop starts.
We all list reset the status value.
So basically we re assign zero to status, but then either objects, the status variable is updated
to one.
Right.
Then we also have outside of the loop, we have a status, the list which is equal to an empty list
for now.
So status list is equal to an empty list.
Then we go in here where we are sending the email and we go at this level.
In the same level with the wild loop.
So indented under the wild loop, but at the same level with the full loop and we see status lists that
append.
I know you are lost, but I'll explain this to you what I'm doing here.
So status lists dot append status.
Why am I doing this?
Well, let me print out status list so you see what's going on here, and I'll run the program.
So make sure that these two are one indentation level you see inside the wild loop.
So run the script.
And what we're getting is the status.
If I introduce now.
This object, and then I quit this.
If you scroll to the end of this, you're going to see that the lay, the latest statuses are ones.
So the latest values are ones.
And in here this is where the object entered the view, Right.
So we switched from a value of zero to a value of one.
Let's do this again.
Let's try again.
So we have zeros.
An object entered it.
Exit the frame.
Quit.
Again, here is the switch from 0 to 1.
The object entered the frame, but then in here, 1 to 0.
This is where the object exited the frame.
Now we can modify this status list by saying that status list is equal to status list minus two.
Up to the ends.
So what this will do is basically let's suppose we have.
This amount of items in a list status list is equal to.
This list writes zeros and ones.
So what we're doing here is basically this.
So let me copy that in here.
And now status list is going to be updated by containing only, only the last two items of the list,
because that's what we're interested about.
We're only interested about the last two items if they are changing or not.
Because if then status list zero, which is the first item, if that is equal to one and status list.
One.
So this item is equal to zero.
That means the object has just exited the frame, right?
Because this was one which means that there was an object currently in the frame.
But then that changed to zero.
Right?
Sorry, this is equal to zero.
So that changed to zero.
And that means the object has just exited the frame.
In that case, we might want to send out the email so that an email call from there and paste it in
here.
And let's run the script.
Right.
So now we get only these 0000.
Let's introduce now an object we have once.
No email is being sent out just yet.
Now, when the object disappears, you saw that we got this printout.
This email sent.
Yeah, email was sent.
So this happens at the time when we switched from one 1 to 1 zero at this point is when we sent out
the email.
And the email should also contain one of the images which will show the object inside the frame.
The view like a screenshot of the video with the object inside the frame.
And that's something that we'll do in the next video.
So with this.
We are done, and I'll see you in the next one.