Created
November 8, 2024 16:34
-
-
Save NL647/2b06aeacb061a583addfabedf180b4a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import face_recognition | |
import os | |
import time | |
import ctypes | |
from datetime import datetime | |
#pip install opencv-python face-recognition | |
def enroll_owner(): | |
""" | |
Enrolls the owner's face by capturing a single image from the webcam. | |
""" | |
print("Please look directly at the camera for enrollment.") | |
cap = cv2.VideoCapture(0) | |
ret, frame = cap.read() | |
cap.release() | |
if ret: | |
face_locations = face_recognition.face_locations(frame) | |
if face_locations: | |
face_encoding = face_recognition.face_encodings(frame, face_locations)[0] | |
# Save the owner's face encoding to a file | |
with open("owner_face_encoding.npy", "wb") as f: | |
np.save(f, face_encoding) | |
print("Enrollment successful. Owner's face is now saved.") | |
else: | |
print("No face detected. Please try again.") | |
else: | |
print("Failed to capture image from webcam.") | |
def check_face(): | |
""" | |
Checks if the face in the current webcam frame matches the owner's face. | |
""" | |
# Load owner's face encoding | |
if not os.path.exists("owner_face_encoding.npy"): | |
print("Owner's face encoding not found. Please enroll the owner's face first.") | |
return | |
owner_face_encoding = np.load("owner_face_encoding.npy") | |
cap = cv2.VideoCapture(0) | |
ret, frame = cap.read() | |
cap.release() | |
if ret: | |
face_locations = face_recognition.face_locations(frame) | |
if face_locations: | |
current_face_encoding = face_recognition.face_encodings(frame, face_locations)[0] | |
match = face_recognition.compare_faces([owner_face_encoding], current_face_encoding)[0] | |
if not match: | |
save_image_and_notify(frame) | |
lock_computer() | |
else: | |
print("No face detected.") | |
def save_image_and_notify(frame): | |
""" | |
Saves the captured frame to a file and opens an HTML notification. | |
""" | |
# Save the captured image | |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
captured_image_path = f"unauthorized_face_{timestamp}.jpg" | |
cv2.imwrite(captured_image_path, frame) | |
# Create an HTML file with the message | |
html_content = f""" | |
<html> | |
<body> | |
<h1>Unauthorized Access Detected</h1> | |
<p>A face not recognized as the owner's has been detected.</p> | |
<img src="{captured_image_path}" alt="Captured Face"> | |
</body> | |
</html> | |
""" | |
html_path = "unauthorized_access.html" | |
with open(html_path, "w") as f: | |
f.write(html_content) | |
# Open the HTML file in the default web browser | |
os.startfile(html_path) | |
def lock_computer(): | |
""" | |
Locks the computer screen. | |
""" | |
ctypes.windll.user32.LockWorkStation() | |
def main(): | |
# If the owner is not enrolled, enroll them | |
if not os.path.exists("owner_face_encoding.npy"): | |
enroll_owner() | |
print("Starting face check every 5 seconds. Press Ctrl+C to stop.") | |
try: | |
while True: | |
check_face() | |
time.sleep(5) | |
except KeyboardInterrupt: | |
print("Face check stopped.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment