Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- import os
- known_faces = []
- known_names = []
- image = cv2.imread(f"C:\\Users\\HP\\Desktop\\steve.jpg")
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- face = np.reshape(gray, (gray.shape[0], gray.shape[1]))
- known_faces.append(face)
- known_names.append(filename.split(".")[0])
- # تشغيل الكاميرا
- video_capture = cv2.VideoCapture(0)
- while True:
- # التقاط صورة من الكاميرا
- ret, frame = video_capture.read()
- # RGB تحويل الصورة إلى صيغة
- rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
- # تحويل الصورة إلى تدرج رمادي
- gray_frame = cv2.cvtColor(rgb_frame, cv2.COLOR_BGR2GRAY)
- # PCA تطبيق خوارزمية
- eigen_faces, eigen_values = cv2.PCACompute(gray_frame.reshape(-1, 1), maxComponents=100)
- # مطابقة الوجوه مع صور الطلاب
- for face in known_faces:
- projection = np.dot(eigen_faces, face)
- distance = np.linalg.norm(projection - face)
- if distance < 0.6:
- name = known_names[known_faces.index(face)]
- break
- else:
- name = "غير معروف"
- # تحديد اسم الطالب
- for face_distance, name in zip(matches, known_names):
- if face_distance < 0.6:
- break
- else:
- name = "غير معروف"
- # رسم مربع حول وجه الطالب وكتابة اسمه
- cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
- cv2.putText(frame, name, (left + 6, bottom - 6), cv2.FONT_HERSHEY_SIMPLEX,
- 1, (0, 0, 255), 2)
- # عرض الصورة
- cv2.imshow("Face Recognition", frame)
- # للخروج من البرنامج و الضغط على
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- video_capture.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement