making this function work for time interval

April 4, 2024, 17:31

11person11

So here the thing, this code only works only once after I run the program, how can i make this thing work again for like 3 minutes or so. anyways here is the code
py
#If someone in your dataset is identified, print their name on the screen
if currentname != name:
currentname = name
print(currentname)
#Take a picture to send in the email
img_name = "image.jpg"
cv2.imwrite(img_name, frame)
print('Taking a picture.')
                
#Now send me an email to let me know who is at the door
request = send_message(name)
print ('Status Code: '+format(request.status_code)) #200 status code means email sent successfully

k9t33n

You want to make it run several times automatically?

k9t33n

Will a for loop do?

11person11

only when a person activates it

11person11

its a face recognition thing

k9t33n

So why can't you just run python [script]? Or will you need a button?

11person11

i have this issue when the camera sees a registered person, it only sends an email once

11person11

not like i want it to spam the email

k9t33n

Oh okay. So do you have a specific amount of times you want it to run?

11person11

for example if the person enters the house and goes out again

11person11

maybe for like after an hour he enters again, it will no longer email

11person11

yea something like that

k9t33n

Or you want it to run continually but it only activates and sends a email if a button or something is pressed?

11person11


11person11

i want it to run continually

11person11

like this

11person11

that status code thing is the email indication

k9t33n

Ah ok

11person11

it only works once

k9t33n

So why not just use a while loop?

11person11

i dont know exactly how to do it

k9t33n

Okay

11person11

that door lock indicates that i am no longer in the camera

k9t33n

I'm on mobile so I can't use your code easily but simply make this change:
py
while True: 
  try:
    [Put code here]
  except:
    print("code raised an error. Most likely timed out for whatever reason.")
    pass

11person11

and after i show my face again after 5 mins or so, status code 200 wont work anymore

11person11

this thing?

11person11

i tried the time.sleep thing but it only pauses the camera live feed

k9t33n

Yes

k9t33n

That's the point of time.sleep(). It's purposefullly not asynchronous

11person11

yea i thought it kinda works like a loop as if the program is executed again from the start

11person11

can you explain what does
while true:
do?

k9t33n

Yes

k9t33n

Once you code is over it just runs it again

11person11

oooh

k9t33n

And try: and except: just is a fail safe because I think your code is timing out at some point for whatever reason

k9t33n

Just some small code I could conjour up

k9t33n

Wait let me change it a third time

k9t33n

Their updated

11person11

alright il try it

k9t33n

Great

11person11

ok,, one issue though

11person11

it spams print

k9t33n

Can you show me?

k9t33n

Copy and paste the output

k9t33n

Oh yeah it would

11person11

the command line interface is jsut black

11person11

but i saw in a second that it spams

11person11

then it went black

k9t33n

Press ctrl + c

k9t33n

Should cancel and you can see output properly

11person11

so heres what i did

11person11


11person11

the print(door unlock) keeps spamming

k9t33n

Yes

k9t33n

That's because it's in a while loop ofc

k9t33n

One second

11person11

can i slow it down or something

k9t33n

Yes you can but I'm trying to get past that and only let it work once you are their and only work once for that time

k9t33n

This is only a snippet of the code tho isn't it

k9t33n

Can you show me the full code? Because right now it's only running the first bit once and then running the last bit over and over which is useless

11person11

yea

11person11

i can show you the entire code if you want

k9t33n

Yes

k9t33n

That'd be preferable from the start

11person11

can you see it?

k9t33n

After a glance over it. You should just put the whole thing apart form the importing of the modules in a while loop

11person11

sorry, i am kinda confused

k9t33n

Np

k9t33n

Do this but for all your code. It's a bad practice and I will improve it once I'm on desktop but for now it works

11person11


11person11

so here i show my face in the camera for like 3 seconds

11person11

and now im gone and looking back at it

11person11

it no longer says
taking a picture
status code: 200

11person11


k9t33n

One sec

k9t33n

Yeah this is exactly what I expected to happen

11person11

yeayea

11person11

so i tried this entire thing, and it wont run

11person11


11person11


k9t33n

1. You missed it out try:

k9t33n

2. Your supposed to put the entire code in that

k9t33n

Otherwise it won't work like expected

11person11

which code

k9t33n

Put all this where I put [code here]

11person11

so i put the while true at the very top?

k9t33n

Yes

k9t33n

And you need to set the entire code one indent in

11person11

doesnt it affect other feature like my lock thing?

11person11


11person11

like this you say?

k9t33n

Should be

11person11

and where should i put the exept and pass

11person11

it doesnt work

11person11


k9t33n

That's because you haven't indented probably

k9t33n

Highlight all of your code and press tab twice

11person11

is it not possible just for this block?
py
#If someone in your dataset is identified, print their name on the screen
if currentname != name:
currentname = name
print(currentname)
#Take a picture to send in the email
img_name = "image.jpg"
cv2.imwrite(img_name, frame)
print('Taking a picture.')
                
#Now send me an email to let me know who is at the door
request = send_message(name)
print ('Status Code: '+format(request.status_code)) #200 status code means email sent successfully

11person11

py
  while True:
    try:
#If someone in your dataset is identified, print their name on the screen
if currentname != name:
currentname = name
print(currentname)
#Take a picture to send in the email
img_name = "image.jpg"
cv2.imwrite(img_name, frame)
print('Taking a picture.')
                
#Now send me an email to let me know who is at the door
request = send_message(name)
print ('Status Code: '+format(request.status_code)) #200 status code means email sent successfully

11person11

i am turning off my raspberry pi since its been operating for hours now.. thanks for your help :))

k9t33n

Sorry for late reply

k9t33n

No because then itd see your face and just spam open even when your gone

11person11

i see...

11person11

its fine bro.. i really appreciate your help, i understand a lot

11person11

so if i apply
while true
to the entire code, will it also do the same thing?

11person11

im kinda scared to try it cause it already causes lag a lot when i tried this

k9t33n

Yeah that makes sense. But with the whole code it will be slower

k9t33n

And we can work out a cooldown

11person11

can a while loop be slowed down?

11person11

what does except and pass exactly do?

k9t33n

They are just fail safes catching your code incase it does fail

k9t33n

The way I did it is technically a bad practice but ignore that

k9t33n

Then we'd use time.sleep()