creat his
creat his
import os
import numpy as np
import threading
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from deepface import DeepFace
from queue import Queue, Empty
import datetime
import logging
# Configuration
DATABASE_FOLDER = "database"
MODEL_NAME = "ArcFace"
DETECTOR_BACKEND = "ssd"
FRAME_SKIP = 4
MAX_QUEUE_SIZE = 2
SIMILARITY_THRESHOLD = 0.65
HISTORY_FOLDER = "historique"
# Email Configuration
EMAIL_ENABLED = True
EMAIL_FROM = "[email protected]"
EMAIL_PASSWORD = "uvfbgvkicjtzzpvp"
EMAIL_TO = "[email protected] , [email protected]"
EMAIL_SMTP_SERVER = "smtp.gmail.com"
EMAIL_SMTP_PORT = 587
EMAIL_COOLDOWN = 60
# RFID Configuration
RFID_ENABLED = True
RFID_PORT = "COM3"
RFID_BAUDRATE = 9600
RFID_EMERGENCY_CODES = ["1234567890", "0987654321"]
# Logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
filename='face_recognition.log'
)
logger = logging.getLogger(__name__)
# Global variables
face_database = []
processing_queue = Queue(maxsize=MAX_QUEUE_SIZE)
current_results = []
is_running = True
frame_count = 0
fps = 0
last_fps_update = time.time()
fps_counter = 0
last_email_time = 0
unknown_faces_detected = False
def create_directories():
directories = [DATABASE_FOLDER, "unknown_faces", HISTORY_FOLDER]
for directory in directories:
if not os.path.exists(directory):
os.makedirs(directory)
def init_database():
if not os.path.exists(DATABASE_FOLDER):
os.makedirs(DATABASE_FOLDER)
result = DeepFace.represent(
img_path=img,
model_name=MODEL_NAME,
detector_backend=DETECTOR_BACKEND,
enforce_detection=True,
align=True
)
if result:
face_database.append({
"name": os.path.splitext(filename)[0],
"embedding": result[0]["embedding"]
})
loaded += 1
except Exception as e:
logger.error(f"Error loading {filename}: {str(e)}")
current_time = datetime.datetime.now()
timestamp = current_time.strftime("%Y-%m-%d_%H-%M-%S")
date_folder = os.path.join(HISTORY_FOLDER, current_time.strftime("%Y-%m-
%d"))
if not os.path.exists(date_folder):
os.makedirs(date_folder)
x, y, w, h = face_area
margin_x, margin_y = int(w * 0.2), int(h * 0.2)
x1 = max(0, x - margin_x)
y1 = max(0, y - margin_y)
x2 = min(frame.shape[1], x + w + margin_x)
y2 = min(frame.shape[0], y + h + margin_y)
filename = f"{name}_{timestamp}.jpg"
filepath = os.path.join(date_folder, filename)
cv2.imwrite(filepath, face_img)
return True
except Exception as e:
logger.error(f"Error saving recognized face: {str(e)}")
return False
def process_frames():
global current_results, unknown_faces_detected
while is_running:
try:
frame = processing_queue.get(timeout=0.5)
faces = DeepFace.extract_faces(
img_path=frame,
detector_backend=DETECTOR_BACKEND,
enforce_detection=False,
align=True
)
results = []
unknown_detected = False
region = face["facial_area"]
x, y, w, h = region["x"], region["y"], region["w"], region["h"]
if not face_database:
results.append((x, y, w, h, "Unknown", 0, frame))
unknown_detected = True
continue
try:
embedding = DeepFace.represent(
img_path=face_img,
model_name=MODEL_NAME,
detector_backend="skip",
enforce_detection=False,
align=False
)[0]["embedding"]
if similarities.size > 0:
best_idx = np.argmax(similarities)
score = (similarities[best_idx] + 1) / 2
current_results = results
unknown_faces_detected = unknown_detected
if unknown_detected:
send_email_notification()
processing_queue.task_done()
except Empty:
pass
except Exception as e:
logger.error(f"Process frame error: {str(e)}")
filename = f"{name}_{int(time.time())}.jpg"
path = os.path.join(DATABASE_FOLDER, filename)
region = faces[0]["facial_area"]
x, y, w, h = region["x"], region["y"], region["w"], region["h"]
margin_x, margin_y = int(w * 0.2), int(h * 0.2)
x1 = max(0, x - margin_x)
y1 = max(0, y - margin_y)
x2 = min(frame.shape[1], x + w + margin_x)
y2 = min(frame.shape[0], y + h + margin_y)
embedding = DeepFace.represent(
img_path=face_img,
model_name=MODEL_NAME,
detector_backend=DETECTOR_BACKEND,
enforce_detection=True
)[0]["embedding"]
except Exception as e:
logger.error(f"Enrollment failed: {str(e)}")
return False
def send_email_notification():
global last_email_time
if not EMAIL_ENABLED:
return
current_time = time.time()
if current_time - last_email_time < EMAIL_COOLDOWN:
return
last_email_time = current_time
threading.Thread(target=_send_email_worker).start()
def _send_email_worker():
try:
unknown_face = None
for face in current_results:
if face[4] == "Unknown" and face[6] is not None:
unknown_face = face
break
if unknown_face is None:
return
x, y, w, h, _, _, frame = unknown_face
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = EMAIL_TO
msg['Subject'] = f"⚠️ SECURITY ALERT: Unknown Person Detected -
{timestamp}"
body = f"""
<html><body>
<h2>Security Alert: Unknown Person Detected</h2>
<p><b>Time:</b> {timestamp}</p>
<p>An unknown person has been detected by the security system.</p>
</body></html>
"""
msg.attach(MIMEText(body, 'html'))
alert_img = frame.copy()
cv2.rectangle(alert_img, (x, y), (x+w, y+h), (0, 0, 255), 2)
cv2.putText(alert_img, "Unknown", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(0, 0, 255), 2)
except Exception as e:
logger.error(f"Email sending failed: {str(e)}")
def main():
global is_running, frame_count, fps, last_fps_update, fps_counter,
SIMILARITY_THRESHOLD
create_directories()
init_database()
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
known_persons = set()
last_recognized_person = None
try:
while True:
ret, frame = cap.read()
if not ret:
break
fps_counter += 1
now = time.time()
if now - last_fps_update >= 1.0:
fps = fps_counter
fps_counter = 0
last_fps_update = now
current_frame_persons = set()
face_coords = {}
if label != "Unknown":
current_frame_persons.add(label)
face_coords[label] = (x, y, w, h)
# Capture logic
for person in current_frame_persons:
if person not in known_persons:
save_recognized_face(frame.copy(), person, face_coords[person])
known_persons.add(person)
last_recognized_person = person
logger.info(f"New person detected: {person}")
# UI elements
cv2.putText(frame, f"FPS: {fps}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
0.7, (0, 255, 0), 2)
cv2.putText(frame, f"Thresh: {SIMILARITY_THRESHOLD:.2f}", (10, 60),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
if last_recognized_person:
cv2.putText(frame, f"Current: {last_recognized_person}", (10, 120),
if unknown_faces_detected:
cv2.putText(frame, "ALERT: Unknown face detected", (frame.shape[1]
- 300, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)
if __name__ == "__main__":
main()