Skip to Content
Neural Network Projects with Python
book

Neural Network Projects with Python

by James Loy
February 2019
Beginner to intermediate
308 pages
7h 42m
English
Packt Publishing
Content preview from Neural Network Projects with Python

Face detection in Python

Face detection can be implemented by the OpenCV library in Python. OpenCV is an open source computer vision library for computer vision tasks. Let's see how we can use OpenCV for face detection.

First, we import OpenCV:

import cv2

Next, let's load a pre-trained cascade classifier for face detection. This cascade classifier can be found in the accompanying GitHub repository and should have been downloaded to your computer (refer to the Technical requirements section):

face_cascades = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

Next, we define a function that takes in an image, performs face detection on the image, and draws a bounding box around the image:

def detect_faces(img, draw_box=True): # convert ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Machine Learning with Python Cookbook

Machine Learning with Python Cookbook

Chris Albon

Publisher Resources

ISBN: 9781789138900Supplemental Content