Reputation: 53
Hope you are doing fine.
So my problem or question is actually more complicated than the title but I will try to keep it simple.
I have this code that runs on cam ( using OpenCV ) and will detect if a person wearing a mask or not, as far as detection go it performs very well ( I got the code from YouTube : https://www.youtube.com/watch?v=Ax6P93r32KU&t=73s ).
In short I want the program to see if the person(with confidence > 95%) is wearing a mask or not, if he is wearing the mask a pop-up message will appear thanking him then the program will close.
In theory I've done all that, The problem is that when the person wears a mask the pop-up message will immediately appear before even the cam catch up ! or if the person isn't wearing a mask then starts to wear one the pop-up message will immediately appear also while he is wearing.
Ideally I would like to check if he is wearing the mask for 2 or 3 continues seconds then the pop-up will appear.
I tried using delay but it slows the camera so much and don't see what is happening, I tried append the confidence of wearing a mask into a list then take the average of the list but still didn't work.
So ideally :
confidence of person wearing mask = M
If M > 95% for 3 seconds:
print(message(thank you))
exit()
Hope you have an idea how to solve this, and I'm sorry if my explanation isn't obvious. I can show it with a live stream (discord) for anyone who is interested and didn't understand my problem.
Upvotes: 0
Views: 632
Reputation: 53
I was able to solve the issue by making an empty list and set t1 = time.time() ( before the while True loop )
mask_list=[]
t1 = time.time()
and inside the loop I would take the reading for 4 seconds
mask_list.append(mask)
if time.time() - t1 > 4: # when 4 seconds passes
mask_avg = sum(mask_list) / len(mask_list)
if mask_avg*100 > 90: # when the average of wearing a mask is > 90%
# code for wearing a mask then exit()
else :
# code for NOT wearing a mask then exit()
I'm not sure if I'm allowed but I will post the code on GitHub if anyone wants to see the full code : https://github.com/Dalsallum/Login-System-With-Mask-Detection
Upvotes: 1
Reputation: 578
So if i understand properly, something like this should work. Let me know what happends or if you dont understand something. This code uses multiple threads as to not stop the video portion of code.
import time
import threading
wearingMaskBool = False
def checkPerson3Seconds():
global wearingMaskBool
#lets check the person 6 times we will use multiple threads so the camera and the 3 second check code can run simpltaniously
for i in range(6)
if (M > 95%):
#adding one if mask is worn
wearingMask += 1
else:
#subtracting one if mask isnt worn
wearingMask -= 1
#wait half a second
time.sleep(0.5)
#checking if wearingMask == 6
if wearingMask == 6:
#execute code for person that is wearing mask
#example
# we can define the "wearingMaskBool" as a global and use it in out main script
wearingMaskBool = True
else:
#execute code for person that isnt wearing mask
#example
# we can define the "wearingMaskBool" as a global and use it in out main script
wearingMaskBool = False
#start thread aka staring the checkPerson3Seconds script
threading.Thread(target=checkPerson3Seconds(), daemon=True).start()
#after the script is ran if they are wearing a mask with a certainty of 95 percent of 3 seconds the the wearingMaskBool will be equal to true
#if it fell below 95% it will be equal to false
Upvotes: 0