Detection Stabilization
First things first, we needed to keep the face detector from doing some sort of action every time the loop runs. So to keep the detector from continuing to detect faces I used two tricks: 1.) Use an array to look at previous frames and see if it detected a face. If we see that the last 40 or so frames detected a face then we can determine that there is in fact a face there. The Cascade Classifier that comes with OpenCV can be a bit buggy and I didn't want a new event happening should the detection not detect a person in one frame. 2.) If we detected a face in the last pass of the frame loop then we don't want to do a new event because we know a face is already there. Super neat stuff!
Web Socket Server
I knew for this project I would need to pickup web sockets so I started up on that. I have never used web sockets so I used a Tech With Tim video Still learning and processing it!
def handle_cleint(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
connected = True
while connected == True:
message_length = conn.recv(HEADER).decode(FORMAT) # Receive message
if message_length:
message_length = int(message_length)
msg = conn.recv(message_length).decode(FORMAT) # Receive message
# Print message
if msg == DISCONNECT_MESSAGE:
connected = False
print(f"[{addr}] {msg}")
conn.send(f"Message received: | {msg} |".encode(FORMAT))
print(f"[{addr}] ** Has left the chat.**")
conn.close()
Here is the handle client function in the server.py script
def start():
server.listen()
print(f"SEVER IS LISTENING ON {SERVER}") # Listen for incoming connections
while True:
conn, addr = server.accept() # Accept connection
thread = threading.Thread(target=handle_cleint, args=(conn, addr))
print("[ACTIVE CONNECTIONS] ", threading.activeCount() - 1 ) # Show active connections
thread.start()
Here is the start function, also using threading (YAY!)
if len(array_cache) >= 39:
array_cache.pop(0)
if face_detected == False:
if check_array_cache_sum(array_cache) >= 38:
print("Face Detected")
face_detected = True
web_hook.send("Face Detected")
array_cache = []
if check_array_cache_sum(array_cache) <= 38 and (len(array_cache) != 0):
print("Face Not Detected")
face_detected = False
array_cache = []
Here is some of the code for smoothing out the detection.
Top comments (0)