1#!/usr/bin/python 2# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt 3# 4# This example program shows how to find frontal human faces in a webcam stream using OpenCV. 5# It is also meant to demonstrate that rgb images from Dlib can be used with opencv by just 6# swapping the Red and Blue channels. 7# 8# You can run this program and see the detections from your webcam by executing the 9# following command: 10# ./opencv_face_detection.py 11# 12# This face detector is made using the now classic Histogram of Oriented 13# Gradients (HOG) feature combined with a linear classifier, an image 14# pyramid, and sliding window detection scheme. This type of object detector 15# is fairly general and capable of detecting many types of semi-rigid objects 16# in addition to human faces. Therefore, if you are interested in making 17# your own object detectors then read the train_object_detector.py example 18# program. 19# 20# 21# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE 22# You can install dlib using the command: 23# pip install dlib 24# 25# Alternatively, if you want to compile dlib yourself then go into the dlib 26# root folder and run: 27# python setup.py install 28# 29# Compiling dlib should work on any operating system so long as you have 30# CMake installed. On Ubuntu, this can be done easily by running the 31# command: 32# sudo apt-get install cmake 33# 34# Also note that this example requires Numpy which can be installed 35# via the command: 36# pip install numpy 37 38import sys 39import dlib 40import cv2 41 42detector = dlib.get_frontal_face_detector() 43cam = cv2.VideoCapture(0) 44color_green = (0,255,0) 45line_width = 3 46while True: 47 ret_val, img = cam.read() 48 rgb_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 49 dets = detector(rgb_image) 50 for det in dets: 51 cv2.rectangle(img,(det.left(), det.top()), (det.right(), det.bottom()), color_green, line_width) 52 cv2.imshow('my webcam', img) 53 if cv2.waitKey(1) == 27: 54 break # esc to quit 55cv2.destroyAllWindows() 56