Skip to Content
OpenCV with Python By Example
book

OpenCV with Python By Example

by Prateek Joshi
September 2015
Beginner to intermediate
296 pages
5h 57m
English
Packt Publishing
Content preview from OpenCV with Python By Example

Detecting and tracking faces

OpenCV provides a nice face detection framework. We just need to load the cascade file and use it to detect the faces in an image. Let's see how to do it:

import cv2 import numpy as np face_cascade = cv2.CascadeClassifier('./cascade_files/haarcascade_frontalface_alt.xml') cap = cv2.VideoCapture(0) scaling_factor = 0.5 while True: ret, frame = cap.read() frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) face_rects = face_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in face_rects: cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3) cv2.imshow('Face Detector', frame) c = cv2.waitKey(1) if c == 27: break cap.release() ...
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

OpenCV 4 with Python Blueprints - Second Edition

OpenCV 4 with Python Blueprints - Second Edition

Dr. Menua Gevorgyan, Michael Beyeler (USD), Arsen Mamikonyan, Michael Beyeler
OpenCV By Example

OpenCV By Example

Prateek Joshi, David Millán Escrivá, Vinícius G. Mendonça
OpenCV 3 Computer Vision with Python Cookbook

OpenCV 3 Computer Vision with Python Cookbook

Aleksei Spizhevoi, Aleksandr Rybnikov

Publisher Resources

ISBN: 9781785283932Supplemental Content