import cv2
import numpy as np
import os
import datetime
تحميل نموذج Haarcascade للكشف عن الوجه والجسم
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
body_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_fullbody.xml')
مسار حفظ الصور للتعرف على الوجه
face_dataset_path = 'face_dataset'
مسار حفظ الصور للتعرف على الجسم
body_dataset_path = 'body_dataset'
تحميل أسماء الموظفين وقوائم الحضور
employee_names = []
attendance_records = []
تحميل أسماء الموظفين من ملف نصي (افترض أن كل اسم في سطر جديد)
with open('employee_names.txt', 'r') as file:
employee_names = file.readlines()
إزالة علامات نهاية السطر من الأسماء
employee_names = [name.strip() for name in employee_names]
دالة للكشف عن الوجه وتسجيل الحضور
def face_recognition_attendance(frame, employee_id):
global employee_names, attendance_records
# تحويل الإطار إلى تدرج الرمادي
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# الكشف عن الوجوه في الإطار
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# فحص كل وجه تم اكتشافه
for (x, y, w, h) in faces:
# رسم مستطيل حول الوجه المكتشف
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# قص الوجه المكتشف من الإطار
face_image = frame[y:y+h, x:x+w]
# حفظ صورة الوجه في مجموعة البيانات
cv2.imwrite(os.path.join(face_dataset_path, f'employee_{employee_id}.jpg'), face_image)
# تسجيل وقت الحضور
attendance_records.append((employee_id, datetime.datetime.now()))
# عرض اسم الموظف ووقت الحضور
cv2.putText(frame, f'Employee: {employee_names[employee_id]}', (x, y-10), cv2.FONT_HERO, 1, (0, 255, 0), 2)
cv2.putText(frame, f'Attendance Time: {datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")}', (x, y+h+20), cv2.FONT_HERO, 1, (0, 255, 0), 2)
دالة للكشف عن الجسم وتسجيل وقت المغادرة
def body_detection_leaving_time(frame, employee_id):
global employee_names, attendance_records
# تحويل الإطار إلى تدرج الرمادي
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# الكشف عن الأجسام في الإطار
bodies = body_cascade.detectMultiScale(gray, 1.1, 4)
# فحص كل جسم تم اكتشافه
for (x, y, w, h) in bodies:
# رسم مستطيل حول الجسم المكتشف
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
# قص الجسم المكتشف من الإطار
body_image = frame[y:y+h, x:x+w]
# مطابقة الجسم مع صورة الوجه المخزنة
face_image = cv2.imread(os.path.join(face_dataset_path, f'employee_{employee_id}.jpg'), cv2.IMREAD_GRAYSCALE)
# حساب معامل الارتباط بين صورتي الوجه والجسم
correlation = np.corrcoef(face_image.flatten(), body_image.flatten())[0, 1]
# إذا كان معامل الارتباط أعلى من عتبة معينة، فافترض أنه نفس الشخص
if correlation > 0.75:
# تسجيل وقت المغادرة
leaving_time = datetime.datetime.now()
# العثور على سجل الحضور المقابل لهذا الموظف
for record in attendance_records:
if record[0] == employee_id:
attendance_time = record[1]
break
# حساب مدة بقاء الموظف في العمل
duration = leaving_time - attendance_time
# عرض اسم الموظف ووقت المغادرة ومدة البقاء
cv2.putText(frame, f'Employee: {employee_names[employee_id]}', (x, y-10), cv2.FONT_HERO, 1, (0, 255, 0), 2)
cv2.putText(frame, f'Leaving Time: {leaving_time.strftime("%Y-%m-%d %H:%M:%S")}', (x, y+h+20), cv2.FONT_HERO, 1, (0, 255, 0), 2)
cv2.putText(frame, f'Duration: {duration}', (x, y+h+40), cv2.FONT_HERO, 1, (0, 255, 0), 2)
دالة لاستدعاء كاميرا الويب والبدء في معالجة الفيديو
def start_video_processing():
global employee_names, attendance_records
# فتح كاميرا الويب
cap = cv2.VideoCapture(0)
# تهيئة متغيرات لتتبع حالة الموظف
employee_id = -1
employee_detected = False
while True:
# قراءة إطار من كاميرا الويب
ret, frame = cap.read()
# تحويل الإطار إلى تدرج الرمادي
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# الكشف عن الوجوه في الإطار
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# فحص كل وجه تم اكتشافه
for (x, y, w, h) in faces:
# إذا تم اكتشاف وجه جديد، قم بتسجيل الحضور
if not employee_detected:
employee_id += 1
face_recognition_attendance(frame, employee_id)
employee_detected = True
# إذا تم اكتشاف وجه موجود مسبقاً، قم بتسجيل وقت المغادرة
else:
body_detection_leaving_time(frame, employee_id)
employee_detected = False
# عرض الإطار مع النتائج
cv2.imshow('Employee Attendance System', frame)
# إذا تم الضغط على مفتاح 'q'، قم بإنهاء البرنامج
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# تحرير موارد الكاميرا
cap.release()
cv2.destroyAllWindows()
بدء معالجة الفيديو
start_video_processing()