Introduction
Face recognition helps computers find and identify people by looking at their faces, just like how we recognize friends and family.
face_recognizer = FaceRecognitionModel() face_recognizer.train(training_images, labels) predictions = face_recognizer.predict(test_images)
face_recognizer = FaceRecognitionModel() face_recognizer.train(images_of_friends, friend_names) predictions = face_recognizer.predict(new_photos)
face_recognizer = FaceRecognitionModel() face_recognizer.train(employee_photos, employee_ids) recognized = face_recognizer.predict(camera_feed)
import numpy as np from sklearn.neighbors import KNeighborsClassifier # Sample face data: each face is a simple 2D point (for example only) face_features = np.array([[1, 2], [2, 3], [3, 4], [10, 10], [11, 11], [12, 12]]) labels = ['Alice', 'Alice', 'Alice', 'Bob', 'Bob', 'Bob'] # Create and train a simple KNN model model = KNeighborsClassifier(n_neighbors=1) model.fit(face_features, labels) # New face to recognize new_face = np.array([[2, 2]]) prediction = model.predict(new_face) print(f"Recognized person: {prediction[0]}")
known_faces = {"Alice": [0.1, 0.2, 0.3], "Bob": [0.4, 0.5, 0.6]}
new_face = [0.1, 0.2, 0.31]
# Simple distance check
def recognize(face):
for name, features in known_faces.items():
dist = sum((f - nf) ** 2 for f, nf in zip(features, face)) ** 0.5
if dist < 0.05:
return name
return "Unknown"
print(recognize(new_face))known_faces = {"Alice": [0.1, 0.2, 0.3]}
new_face = [0.1, 0.2, 0.3]
def recognize(face):
for name, features in known_faces.items():
dist = sum((f - nf) ** 2 for f, nf in zip(features, face)) ** 0.5
if dist > 0.05:
return name
return "Unknown"
print(recognize(new_face))Pass the test and this pattern moves unknown