{ "nbformat": 4, "nbformat_minor": 0, "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.0" }, "colab": { "name": "4-Cascade_classification.ipynb", "provenance": [], "collapsed_sections": [] } }, "cells": [ { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true, "id": "rOG12Rey7g9C" }, "source": [ "# Cascade Classification\n", "\n", "One of the key things we can do with vision is object detect.\n", "\n", "Object Detection using Haar feature-based cascade classifiers is an effective object detection method proposed by Paul Viola and Michael Jones in their [paper](https://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf) and improved later by [Rainer Lienhart](http://wiki.opencv.org.cn/images/5/52/MRL-TR-May02-revised-Dec02.pdf).\n", "\n", "It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images.\n", "\n", "OpenCV provides a training method ( see [Cascade Classifier Training](https://docs.opencv.org/3.4/dc/d88/tutorial_traincascade.html)) or pretrained models, that can be read using the cv2.CascadeClassifierload method. The pre-*trained* models are located in the data folder in the OpenCV installation or can be found [here](https://github.com/opencv/opencv/tree/3.4/data).\n", "\n", "In this notebook we will play with some of the provided pre-trained haarcascades models. More on this topic can be found [here](https://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html).\n", "\n", "
\n", " Estimated time needed: 20 min\n", "
" ] }, { "cell_type": "code", "metadata": { "deletable": true, "editable": true, "id": "dvzQbnbX7g9D" }, "source": [ "# Download the test image and utils files\n", "!wget --no-check-certificate \\\n", " https://raw.githubusercontent.com/computationalcore/introduction-to-opencv/master/assets/test.jpg \\\n", " -O test.jpg\n", "!wget --no-check-certificate \\\n", " https://raw.githubusercontent.com/computationalcore/introduction-to-opencv/master/assets/haarcascade_frontalface_default.xml \\\n", " -O haarcascade_frontalface_default.xml\n", "!wget --no-check-certificate \\\n", " https://raw.githubusercontent.com/computationalcore/introduction-to-opencv/master/assets/haarcascade_smile.xml \\\n", " -O haarcascade_smile.xml\n", "!wget --no-check-certificate \\\n", " https://raw.githubusercontent.com/computationalcore/introduction-to-opencv/master/assets/haarcascade_eye.xml \\\n", " -O haarcascade_eye.xml\n", "!wget --no-check-certificate \\\n", " https://raw.githubusercontent.com/computationalcore/introduction-to-opencv/master/utils/common.py \\\n", " -O common.py\n", "\n", "# these imports let you use opencv\n", "import cv2 #opencv itself\n", "import common #some useful opencv functions\n", "import numpy as np # matrix manipulations\n", "\n", "#the following are to do with this interactive notebook code\n", "%matplotlib inline \n", "from matplotlib import pyplot as plt # this lets you draw inline pictures in the notebooks\n", "import pylab # this allows you to control figure size \n", "pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook" ], "execution_count": null, "outputs": [] }, { "cell_type": "code", "metadata": { "deletable": true, "editable": true, "id": "E254mGVV7g9F", "outputId": "71064603-9156-4f02-8d4a-3c7a9d0ca3d5", "colab": { "base_uri": "https://localhost:8080/", "height": 487 } }, "source": [ "# Load the test image and create a greyscale copy of it to be used in the classifiers\n", "\n", "base_image = cv2.imread('test.jpg')\n", "grey = cv2.cvtColor(base_image, cv2.COLOR_BGR2GRAY)\n", "plt.imshow(cv2.cvtColor(base_image, cv2.COLOR_BGR2RGB))" ], "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "