Libraries
pip install opencv-python
pip install winsound
pip install plyer
pip install pygame
Sentry code
Make sure to replace C:\Users\User\Downloads\Example.mp3 with the path to the mp3 file you want to play. (or use beep)
import cv2
import winsound
from plyer import notification
import time
import pygame
# Load the pre-trained face detection model
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Capture video from the default camera (camera index 0)
cap = cv2.VideoCapture(0)
def beep():
# Show a system notification
notification.notify(
title="Face Detected",
message="A face has been detected for more than 1 second.",
timeout=10
)
# Sound an alert
x = 15
while x > 0:
winsound.Beep(1500, 50) # Beep at 1500 Hz for 50 ms 15 *
time.sleep(.1)
x -= 1
def play_sound():
# Show a system notification
notification.notify(
title="Face Detected",
message="A face has been detected for more than 1 second.",
timeout=10
)
# Initialize pygame mixer
pygame.mixer.init()
# Load the MP3 file
try:
pygame.mixer.music.load(r"C:\Users\User\Downloads\Example.mp3") # Replace this with the correct path to your .mp3 file
except pygame.error as _ :
print(f"Error loading sound file: {_} ")
return
# Play the MP3 file
pygame.mixer.music.play()
alert_choice = input("Beeping(1) or Music(2): ")
if alert_choice == "1":
alert = beep
elif alert_choice == "2":
alert = play_sound
else:
print("Invalid choice. Using default beep alert.")
alert = beep
face_detected_time = 0
face_detected = False
while True:
ret, frame = cap.read()
# Convert the frame to grayscale for face detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=12, minSize=(30, 30)) # Change the minNeighbors value up for lower sensitivity and a lower value for more face sensitivity
if len(faces) > 0:
if not face_detected:
face_detected = True
face_detected_time = time.time()
else:
current_time = time.time()
if current_time - face_detected_time >= 1:
alert() # Call the selected alert function
face_detected = False
# Draw Red rectangles around detected faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 5)
# Display the frame with detected faces
cv2.imshow('Face Detection', frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()
Face or Brightest spot
My original script which simply looks for faces or just the brightest spot in the frame
import cv2
# Load the pre-trained face detection model
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Capture video from the default camera (camera index 0)
cap = cv2.VideoCapture(0)
# Prompt the user to choose between face detection or finding the brightest spot
choice = input("Enter 'f' for face detection, 'b' for finding the brightest spot, or nothing for a blank camera : ")
while True:
# Read a frame from the video capture
ret, frame = cap.read()
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if choice == 'f':
# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=12, minSize=(30, 30)) # Change the minNeighbors value up for lower sensitivity and a lower value for more face sensitivity
# Draw Red rectangles around detected faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
elif choice == 'b':
# Find the brightest spot in the frame
(minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(gray)
cv2.circle(frame, maxLoc, 30, (0, 0, 255), 2)
# Display the processed frame
cv2.imshow('Video', frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()
0 Comments