{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import cv2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "face_cascade_path = '/usr/local/opt/opencv/share/'\\\n", " 'OpenCV/haarcascades/haarcascade_frontalface_default.xml'\n", "eye_cascade_path = '/usr/local/opt/opencv/share/'\\\n", " 'OpenCV/haarcascades/haarcascade_eye.xml'\n", "\n", "face_cascade = cv2.CascadeClassifier(face_cascade_path)\n", "eye_cascade = cv2.CascadeClassifier(eye_cascade_path)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "src = cv2.imread('data/src/lena_square.png')\n", "src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "faces = face_cascade.detectMultiScale(src_gray)\n", "\n", "for x, y, w, h in faces:\n", " cv2.rectangle(src, (x, y), (x + w, y + h), (255, 0, 0), 2)\n", " face = src[y: y + h, x: x + w]\n", " face_gray = src_gray[y: y + h, x: x + w]\n", " eyes = eye_cascade.detectMultiScale(face_gray)\n", " for (ex, ey, ew, eh) in eyes:\n", " cv2.rectangle(face, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cv2.imwrite('data/dst/opencv_face_detect_rectangle.jpg', src)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "src = cv2.imread('data/src/lena_square.png')\n", "src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "faces = face_cascade.detectMultiScale(src_gray)\n", "\n", "for x, y, w, h in faces:\n", " src[y: y + h, x: x + w] = [0, 128, 255]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cv2.imwrite('data/dst/opencv_face_detect_fill.jpg', src)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "src = cv2.imread('data/src/lena_square.png')\n", "src_gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "faces = face_cascade.detectMultiScale(src_gray)\n", "\n", "ratio = 0.05\n", "\n", "for x, y, w, h in faces:\n", " small = cv2.resize(src[y: y + h, x: x + w], None, fx=ratio, fy=ratio, interpolation=cv2.INTER_NEAREST)\n", " src[y: y + h, x: x + w] = cv2.resize(small, (w, h), interpolation=cv2.INTER_NEAREST)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cv2.imwrite('data/dst/opencv_face_detect_mosaic.jpg', src)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }