1- # ©2023 PyJs
2-
3- import os
4- try :
5- import cv2
6- except :
7- os .system ("pip3 install opencv-python" )
8- import cv2
9- print ("THIS USES CV2. RUN \" pip uninstall opencv-python\" TO UNINSTALL." )
10- # Initialize a list to store enrolled faces
11- enrolled_faces = []
12-
13- face_cascade = cv2 .CascadeClassifier (cv2 .data .haarcascades + "haarcascade_frontalface_default.xml" )
14-
15- def scan_face ():
16- cap = cv2 .VideoCapture (0 )
17- ret , frame = cap .read ()
18- return frame
19-
20- def enroll_face ():
21- frame = scan_face ()
22- global enrolled_faces
23-
24- # Convert the frame to grayscale for face detection
25- gray = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
26-
27- # Detect faces in the frame
28- faces = face_cascade .detectMultiScale (gray , scaleFactor = 1.1 , minNeighbors = 5 , minSize = (30 , 30 ))
29-
30- if len (faces ) > 0 :
31- for (x , y , w , h ) in faces :
32- # Crop and store each enrolled face
33- enrolled_faces .append (frame [y :y + h , x :x + w ])
34-
35- return True
36- else :
37- return False
38-
39- # Function to match a face
40- def is_match ():
41- global enrolled_faces
42- frame = scan_face ()
43- # Convert the frame to grayscale for face detection
44- gray = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
45-
46- # Detect faces in the frame
47- faces = face_cascade .detectMultiScale (gray , scaleFactor = 1.1 , minNeighbors = 5 , minSize = (30 , 30 ))
48-
49- if len (faces ) > 0 :
50- for (x , y , w , h ) in faces :
51- # Crop the detected face for comparison
52- detected_face = frame [y :y + h , x :x + w ]
53-
54- # Check if the detected face matches any of the enrolled faces
55- for enrolled_face in enrolled_faces :
56- if reuowg_w (enrolled_face , detected_face ):
57- return True
58-
59- return False
60-
61- # Function to compare faces (a basic image comparison)
62- def reuowg_w (face1 , face2 ):
63- # Resize the images to the same size
64- face1 = cv2 .resize (face1 , (200 , 200 ))
65- face2 = cv2 .resize (face2 , (200 , 200 ))
66- diff = cv2 .absdiff (face1 , face2 )
67- mean_diff = diff .mean ()
1+ # ©2023 PyJs
2+
3+ import os
4+ try :
5+ import cv2
6+ except :
7+ os .system ("pip3 install opencv-python" )
8+ import cv2
9+ print ("THIS USES CV2. RUN \" pip uninstall opencv-python\" TO UNINSTALL." )
10+ # Initialize a list to store enrolled faces
11+ enrolled_faces = []
12+
13+ face_cascade = cv2 .CascadeClassifier (cv2 .data .haarcascades + "haarcascade_frontalface_default.xml" )
14+
15+ def scan_face ():
16+ cap = cv2 .VideoCapture (0 )
17+ ret , frame = cap .read ()
18+ return frame
19+
20+ def enroll_face ():
21+ frame = scan_face ()
22+ global enrolled_faces
23+
24+ # Convert the frame to grayscale for face detection
25+ gray = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
26+
27+ # Detect faces in the frame
28+ faces = face_cascade .detectMultiScale (gray , scaleFactor = 1.1 , minNeighbors = 5 , minSize = (30 , 30 ))
29+
30+ if len (faces ) > 0 :
31+ for (x , y , w , h ) in faces :
32+ # Crop and store each enrolled face
33+ enrolled_faces .append (frame [y :y + h , x :x + w ])
34+
35+ return True
36+ else :
37+ return False
38+
39+ # Function to match a face
40+ def is_match ():
41+ global enrolled_faces
42+ frame = scan_face ()
43+ # Convert the frame to grayscale for face detection
44+ gray = cv2 .cvtColor (frame , cv2 .COLOR_BGR2GRAY )
45+
46+ # Detect faces in the frame
47+ faces = face_cascade .detectMultiScale (gray , scaleFactor = 1.1 , minNeighbors = 5 , minSize = (30 , 30 ))
48+
49+ if len (faces ) > 0 :
50+ for (x , y , w , h ) in faces :
51+ # Crop the detected face for comparison
52+ detected_face = frame [y :y + h , x :x + w ]
53+
54+ # Check if the detected face matches any of the enrolled faces
55+ for enrolled_face in enrolled_faces :
56+ if reuowg_w (enrolled_face , detected_face ):
57+ return True
58+
59+ return False
60+
61+ # Function to compare faces (a basic image comparison)
62+ def reuowg_w (face1 , face2 ):
63+ # Resize the images to the same size
64+ face1 = cv2 .resize (face1 , (200 , 200 ))
65+ face2 = cv2 .resize (face2 , (200 , 200 ))
66+ diff = cv2 .absdiff (face1 , face2 )
67+ mean_diff = diff .mean ()
6868 return mean_diff < 50
0 commit comments