Advertisement
AliAbdulKareem

Untitled

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