From 49fee8e048d6534cc94ea21bb1a14a94f44cddb1 Mon Sep 17 00:00:00 2001 From: Real Python Bot <42617967+realpython-bot@users.noreply.github.com> Date: Fri, 4 Dec 2026 21:51:45 +0000 Subject: [PATCH] Add traditional-face-detection-python materials folder Sample code for the updated tutorial Traditional Face Detection With Python. Co-Authored-By: Claude Opus 5.5 --- traditional-face-detection-python/README.md | 31 ++++++++++++ .../detect_faces.py | 50 +++++++++++++++++++ .../requirements.txt | 1 + 3 files changed, 82 insertions(+) create mode 100644 traditional-face-detection-python/README.md create mode 100644 traditional-face-detection-python/detect_faces.py create mode 100644 traditional-face-detection-python/requirements.txt diff --git a/traditional-face-detection-python/README.md b/traditional-face-detection-python/README.md new file mode 100644 index 0000000000..a5b8c346e6 --- /dev/null +++ b/traditional-face-detection-python/README.md @@ -0,0 +1,31 @@ +# Traditional Face Detection With Python + +This folder contains the sample code for the Real Python tutorial [Traditional Face Detection With Python](https://realpython.com/traditional-face-detection-python/). + +## Setup + +You need Python 3.12 or later. Create and activate a virtual environment, then install the dependencies: + +```sh +$ python -m venv venv/ +$ source venv/bin/activate +(venv) $ python -m pip install -r requirements.txt +``` + +Make sure you install `opencv-contrib-python` and not `opencv-python`. Starting with OpenCV 5, `CascadeClassifier` lives in the contrib `xobjdetect` module. + +## Usage + +Pass the path to an image that contains faces: + +```sh +(venv) $ python detect_faces.py path/to/your-image.jpg +``` + +The script draws a green rectangle around each detected face and shows the result in a window. Press any key to close it. + +On a machine without a graphical display, pass a second path to save the result to a file instead: + +```sh +(venv) $ python detect_faces.py path/to/your-image.jpg detected-faces.jpg +``` diff --git a/traditional-face-detection-python/detect_faces.py b/traditional-face-detection-python/detect_faces.py new file mode 100644 index 0000000000..6acf002016 --- /dev/null +++ b/traditional-face-detection-python/detect_faces.py @@ -0,0 +1,50 @@ +"""Detect faces in an image with OpenCV's Viola-Jones classifier. + +Usage: + python detect_faces.py path/to/your-image.jpg + python detect_faces.py path/to/your-image.jpg detected-faces.jpg + +Pass a second path to save the result instead of opening a window, +for example on a machine without a graphical display. +""" + +import sys + +import cv2 as cv + +if len(sys.argv) < 2: + sys.exit(__doc__) + +# Read image from your local file system +original_image = cv.imread(sys.argv[1]) +if original_image is None: + sys.exit(f"Couldn't read the image {sys.argv[1]!r}") + +# Convert color image to grayscale for Viola-Jones +grayscale_image = cv.cvtColor(original_image, cv.COLOR_BGR2GRAY) + +# Load the classifier and create a cascade object for face detection +classifier_path = cv.data.haarcascades + "haarcascade_frontalface_alt.xml" +face_cascade = cv.CascadeClassifier(classifier_path) +if face_cascade.empty(): + sys.exit(f"Couldn't load the classifier from {classifier_path!r}") + +detected_faces = face_cascade.detectMultiScale(grayscale_image) +print(f"Detected {len(detected_faces)} face(s)") + +for column, row, width, height in detected_faces: + cv.rectangle( + original_image, + (column, row), + (column + width, row + height), + (0, 255, 0), + 2, + ) + +if len(sys.argv) > 2: + cv.imwrite(sys.argv[2], original_image) + print(f"Saved the result to {sys.argv[2]}") +else: + cv.imshow("Image", original_image) + cv.waitKey(0) + cv.destroyAllWindows() diff --git a/traditional-face-detection-python/requirements.txt b/traditional-face-detection-python/requirements.txt new file mode 100644 index 0000000000..7d78de141c --- /dev/null +++ b/traditional-face-detection-python/requirements.txt @@ -0,0 +1 @@ +opencv-contrib-python==5.1.0.95