-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFace Recognition.py
64 lines (49 loc) · 2.04 KB
/
Face Recognition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import cv2
import numpy as np
# Initialize OpenCV's pre-trained Haar Cascade classifier for face detection
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Initialize variables
face_id = 0
face_data = {}
def add_new_face(face_image):
global face_id
if face_id is None:
face_id = 0
# Increment face_id for new face
face_id += 1
# Store face in the database (for now, just store the face image)
face_data[face_id] = face_image
return face_id
def recognize_face(face_image):
# Placeholder function for face recognition
# In a real implementation, you would compare face_image with existing face data
# and return the ID if recognized, or None if not recognized.
return None
# Initialize the video capture
cap = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Convert frame to grayscale for face detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5, minSize=(30, 30))
# Draw rectangles around the faces and identify
for (x, y, w, h) in faces:
face_image = gray[y:y+h, x:x+w] # Extract the face ROI
# Try to recognize the face
face_id = recognize_face(face_image)
if face_id is None:
# If face is not recognized, add it as a new face
face_id = add_new_face(face_image)
# Draw a rectangle around the face
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.putText(frame, f'Face ID: {face_id}', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
# Display the resulting frame
cv2.imshow('Face Recognition System', frame)
# Exit the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture and destroy all windows
cap.release()
cv2.destroyAllWindows()