Advertisement
AliAbdulKareem

Untitled

Mar 5th, 2024
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4.  
  5.  
  6. known_faces = []
  7. known_names = []
  8.  
  9.  
  10. image = cv2.imread(f"C:\\Users\\HP\\Desktop\\steve.jpg")
  11. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  12. face = np.reshape(gray, (gray.shape[0], gray.shape[1]))
  13. known_faces.append(face)
  14. known_names.append(filename.split(".")[0])
  15.  
  16. # تشغيل الكاميرا
  17. video_capture = cv2.VideoCapture(0)
  18.  
  19. while True:
  20.     # التقاط صورة من الكاميرا
  21.     ret, frame = video_capture.read()
  22.  
  23.     # RGB تحويل الصورة إلى صيغة
  24.     rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  25.  
  26.     # تحويل الصورة إلى تدرج رمادي
  27.     gray_frame = cv2.cvtColor(rgb_frame, cv2.COLOR_BGR2GRAY)
  28.  
  29.     # PCA تطبيق خوارزمية
  30.     eigen_faces, eigen_values = cv2.PCACompute(gray_frame.reshape(-1, 1), maxComponents=100)
  31.  
  32.     # مطابقة الوجوه مع صور الطلاب
  33.     for face in known_faces:
  34.         projection = np.dot(eigen_faces, face)
  35.         distance = np.linalg.norm(projection - face)
  36.         if distance < 0.6:
  37.             name = known_names[known_faces.index(face)]
  38.             break
  39.     else:
  40.         name = "غير معروف"
  41.  
  42.     # تحديد اسم الطالب
  43.     for face_distance, name in zip(matches, known_names):
  44.         if face_distance < 0.6:
  45.             break
  46.     else:
  47.         name = "غير معروف"
  48.  
  49.     # رسم مربع حول وجه الطالب وكتابة اسمه
  50.     cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  51.     cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_SIMPLEX,
  52.                 1, (0, 0, 255), 2)
  53.  
  54.     # عرض الصورة
  55.     cv2.imshow("Face Recognition", frame)
  56.  
  57.     # للخروج من البرنامج و الضغط على
  58.     if cv2.waitKey(1) & 0xFF == ord('q'):
  59.         break
  60.  
  61. video_capture.release()
  62. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement