Creating a full script for a CCTV system with facial recognition involves several steps and requires the installation of certain libraries. For simplicity, I'll demonstrate a basic example using the face_recognition library, which is built on top of Dlib, and OpenCV for handling video capture. This script will recognize faces from the webcam feed against pre-known faces.
Given the complexity of such a system, this example will be simplified to focus on the core concepts. Before running the script, you'll need to install OpenCV and face_recognition. You can do this using pip:
pip install opencv-python-headless face_recognition
Here's a basic Python script to capture video from your webcam and identify known faces:
import face_recognition
import cv2
import numpy as np
# Load a sample picture and learn how to recognize it.
known_person_image = face_recognition.load_image_file("your_known_person.jpg")
known_person_face_encoding = face_recognition.face_encodings(known_person_image)[0]
# Create arrays of known face encodings and their names
known_face_encodings = [known_person_face_encoding]
known_face_names = ["Known Person's Name"]
# Initialize variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
# Capture video from the webcam
video_capture = cv2.VideoCapture(0)
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(small_frame)
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# Use the known face with the smallest distance to the new face
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
process_this_frame = not process_this_frame
# Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
This script will open your webcam, capture video, and attempt to recognize faces against the known ones.
Things to Note:
- Change
"your_known_person.jpg" to the path of an image file of the person you want to recognize. Also, change "Known Person's Name" to the person's actual name.
- This script processes every other frame to reduce computation; this might affect the real-time performance. Adjust according to your needs.
- Many details, like handling multiple known faces or improving the efficiency and accuracy of face recognition, are not covered due to the simplicity of the script. You're encouraged to explore the
face_recognition and OpenCV documentation for more advanced features.
- Always consider privacy and legal implications when implementing such systems.