{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import cv2\n",
    "from scipy.misc import lena\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import dautil as dl\n",
    "import os\n",
    "from IPython.display import HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def plot_with_rect(ax, img):\n",
    "    img2 = img.copy()\n",
    "\n",
    "    for x, y, w, h in face_cascade.detectMultiScale(img2, 1.3, 5):\n",
    "        cv2.rectangle(img2, (x, y), (x + w, y + h), (255, 0, 0), 2)\n",
    "\n",
    "    dl.plotting.img_show(ax, img2, cmap=plt.cm.gray)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# dir = '/usr/local/share/OpenCV/haarcascades/'\n",
    "base = 'https://raw.githubusercontent.com/Itseez/opencv/master/data/'\n",
    "url = base + 'haarcascades/haarcascade_frontalface_default.xml'\n",
    "path = os.path.join(dl.data.get_data_dir(),\n",
    "                    'haarcascade_frontalface_default.xml')\n",
    "\n",
    "if not dl.conf.file_exists(path):\n",
    "    dl.data.download(url, path)\n",
    "\n",
    "face_cascade = cv2.CascadeClassifier(path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "context = dl.nb.Context('detecting_faces')\n",
    "dl.nb.RcWidget(context)\n",
    "dl.nb.LabelWidget(2, 2, context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sp = dl.plotting.Subplotter(2, 2, context)\n",
    "img = lena().astype(np.uint8)\n",
    "plot_with_rect(sp.ax, img)\n",
    "sp.label()\n",
    "\n",
    "rows, cols = img.shape\n",
    "mat = cv2.getRotationMatrix2D((cols/2, rows/2), 21, 1)\n",
    "rot = cv2.warpAffine(img, mat, (cols, rows))\n",
    "plot_with_rect(sp.next_ax(), rot)\n",
    "sp.label()\n",
    "\n",
    "np.random.seed(36)\n",
    "noisy = img * (np.random.random(img.shape) < 0.6)\n",
    "plot_with_rect(sp.next_ax(), noisy)\n",
    "sp.label()\n",
    "\n",
    "blur = cv2.blur(img, (9, 9))\n",
    "plot_with_rect(sp.next_ax(), blur)\n",
    "sp.label()\n",
    "\n",
    "HTML(sp.exit())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "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.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
