<jupyter_start><jupyter_text>OpenCV example. Show webcam image and detect face.It uses Lena's face and add random noise to it if the video capture doesn't work for some reason.https://gist.github.com/astanin/3097851      ![Lena's picture + random noise](./images/Screenshot_2017-06-15_12-47-50.png)    ![Patrick's face captured without noise](./images/Screenshot_2017-06-15_12-50-42.png)    OpenCV Haar Cascade classiferhttp://docs.opencv.org/trunk/d7/d8b/tutorial_py_face_detection.htmlhttps://github.com/opencv/opencv/tree/master/data/haarcascadesExit by pressing `ESC`<jupyter_code>from __future__ import print_function

import numpy as np
import cv2

# local modules
from video import create_capture
from common import clock, draw_str


def detect(img, cascade):
    rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=4, minSize=(30, 30),
                                     flags=cv2.CASCADE_SCALE_IMAGE)
    if len(rects) == 0:
        return []
    rects[:,2:] += rects[:,:2]
    return rects

def draw_rects(img, rects, color):
    for x1, y1, x2, y2 in rects:
        cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)


import sys, getopt
print(__doc__)

args, video_src = getopt.getopt(sys.argv[2:], '', ['cascade=', 'nested-cascade='])
try:
    video_src = video_src[0]
except:
    video_src = 0
args = dict(args)
fullpath_openCV = '/home/patechoc/anaconda2/share/OpenCV/haarcascades/'
cascade_fn = args.get('--cascade', fullpath_openCV + "haarcascade_frontalface_alt.xml")
nested_fn  = args.get('--nested-cascade', fullpath_openCV + "haarcascade_eye.xml")

cascade = cv2.CascadeClassifier(cascade_fn)
nested = cv2.CascadeClassifier(nested_fn)

# cam = create_capture(video_src, fallback='synth:bg=./data/lena.jpg:noise=0.05')
cam = create_capture(video_src, fallback='synth:bg=./data/lena.jpg:noise=0.05')

while True:
    ret, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = cv2.equalizeHist(gray)

    t = clock()
    rects = detect(gray, cascade)
    vis = img.copy()
    draw_rects(vis, rects, (0, 255, 0))
    if not nested.empty():
        for x1, y1, x2, y2 in rects:
            roi = gray[y1:y2, x1:x2]
            vis_roi = vis[y1:y2, x1:x2]
            subrects = detect(roi.copy(), nested)
            draw_rects(vis_roi, subrects, (255, 0, 0))
    dt = clock() - t

    draw_str(vis, (20, 20), 'time: %.1f ms' % (dt*1000))
    cv2.imshow('facedetect', vis)

    if cv2.waitKey(5) == 27:
        break
cv2.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1405940594059406, 'fraction_numerical': 0.044356435643564354, 'mean_word_length': 3.66913123844732, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '12187', 'license': 'MIT', 'n_tokens_mistral': 1031, 'n_tokens_neox': 958, 'n_words': 198, 'path': 'test_openCV/face_eyes_detection/OpenCV2 Face Detection Lena+noise.ipynb', 'repo_name': 'Patechoc/face-detection-demo-with-microsoft-api', 'size': '2525'}