Mastering the Fundamentals of Computer Vision With Python
Prerequisites
Before you begin, ensure you have the following:- Python installed: This article uses Python 3.12.4. You can check your Python version by running the command:
python --version
If you encounter an error, ensure Python is installed correctly. You can download Python from the official website.
- Text editor: This tutorial uses Visual Studio Code (VS Code) as the text editor. You can download VS Code here. However, feel free to use any text editor you choose.
- Create a project folder: First, choose a location for your project folder. For this tutorial, we will create it on the desktop.
- Navigate to your desktop.
- Create a new folder named, for example, “facial-recognition.”
- Open the terminal in this folder by clicking on it and pressing `Ctrl + Cmd + C`.
- Navigate to your desktop.
- Create a new folder, for example, “facial-recognition.”
- Right-click the folder and select “Open in Terminal” or “Open PowerShell window here.”
- Create and activate a virtual environment: This helps keep project dependencies isolated from the global Python installation.
- Create a virtual environment: In your terminal, run the following command to create a virtual environment named `venv` inside the project folder:
python -m venv venv
- Activate the virtual environment: To activate the virtual environment, use the following commands based on your operating system:
source venv/bin/activate //activate virtual environment on mac
.\venv\Scripts\activate //activate virtual environment on windows
Once the virtual environment is activated, your terminal will appear like this.

Figure 1: Illustration of an activated virtual environment
pip install opencv-python
#import open cv
import cv2
import time
#here, we load the pre-built Haar Cascade model
face_classifier = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
def detect_faces():
# Access the webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
return
# Allow the webcam some time to initialize
time.sleep(2)
while True:
# Read the frame from the webcam
ret, frame = cap.read()
# Check if the frame was read successfully
if not ret:
print("Error: Failed to capture image from webcam")
break
# Check if the frame is empty
if frame is None:
print("Error: Received empty frame from webcam")
continue
try:
# Convert the frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_classifier.detectMultiScale(gray, 1.1, 5)
# Draw rectangles around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Display the frame with face detections
cv2.imshow("Face Detection", frame)
except cv2.error as e:
print(f"OpenCV Error: {e}")
break
# Exit loop if 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()
# Call the function to start the webcam feed
detect_faces()

Figure 2: Illustration of my face being detected
pip install face_recognition
pip install git+https://github.com/ageitgey/face_recognition_models
pip install setuptools //to solve the bug of face_recognition_models not being recognized as installed
import cv2
import os
import face_recognition
import time
# Path to the reference image in the images folder
reference_image_path = 'images/profile-headshot.jpg'
# Load the reference image
reference_image = face_recognition.load_image_file(reference_image_path)
reference_encoding = face_recognition.face_encodings(reference_image)[0]
# Load the pre-built Haar Cascade model for face detection
face_classifier = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
def accessWebcam():
# Access the webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
return
# Allow the webcam some time to initialize
time.sleep(2)
while True:
# Read the frame from the webcam
ret, frame = cap.read()
# Check if the frame was read successfully
if not ret:
print("Error: Failed to capture image from webcam")
break
# Convert the frame to grayscale for face detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_classifier.detectMultiScale(gray, 1.1, 5)
# Process each detected face
for (x, y, w, h) in faces:
# Extract the face region from the frame
face_region = frame[y:y+h, x:x+w]
# Resize the face region for face recognition (optional but recommended)
face_region_resized = cv2.resize(face_region, (128, 128))
# Encode the detected face
face_encoding = face_recognition.face_encodings(face_region_resized)
# Compare the detected face encoding with the reference face encoding
if len(face_encoding) > 0:
match = face_recognition.compare_faces([reference_encoding], face_encoding[0], tolerance=0.5)
# If match is found, display the name on the bounding box
if match[0]:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(frame, "Raymond", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)
else:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
else:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Display the frame with face detections
cv2.imshow("Face Detection", frame)
# Break the loop on 'q' key press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the webcam and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()
# Call the function to start the webcam feed
accessWebcam()
RuntimeError: Unsupported image type, must be 8bit gray or RGB image., you can resolve it by downgrading NumPy to version 1.26.4. This issue is associated with NumPy 2.0. To downgrade, simply run the following command:
pip install numpy==1.26.4
Now, let me demonstrate the results of our work. You’ll notice that when my face is recognized, the bounding box turns green and my name is displayed next to it. If the face is not recognized, the bounding box remains blue. Additionally, I tested the system by showing a second face from my phone. While the model detected the face, it didn’t recognize it, so the bounding box for that face stayed blue, whereas my face was highlighted with a green bounding box due to its familiarity with the model.

Figure 3: Illustration showing the detection and identification of my face

Figure 4: Illustration showing the detected and unrecognized face due to a head tilt

Figure 5: Illustration showing the detection of both faces and only identifying mine