{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Pedestrian Detection (Default)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2016/03/28 ysmrnbt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import cv2 # use opencv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The HOG detector returns slightly larger rectangles than the real objects.\n", "So we slightly shrink the rectangles to get a nicer output." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def draw_detections(img, rects, thickness = 1):\n", " for x, y, w, h in rects:\n", " pad_w, pad_h = int(0.15*w), int(0.05*h)\n", " cv2.rectangle(img, (x+pad_w, y+pad_h), (x+w-pad_w, y+h-pad_h), (0, 255, 0), thickness)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Default detector: INRIA Person Dataset is used for training" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "HOG(Histogram of Oriented Gradients) and SVM(Support Vector Machine) are used to detect pedestrian in 1 picture.\n", "- hitThreshold: Threshold for the distance between features and SVM classifying plane. Usually it is 0 and should be specfied in the detector coefficients (as the last free coefficient). But if the free coefficient is omitted (which is allowed), you can specify it manually here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- finalThreshold: Coefficient to regulate the similarity threshold. When detected, some objects can be covered by many rectangles. 0 means not to perform grouping. " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def PD_default(filename):\n", " image = cv2.imread(filename) # read image\n", " hog = cv2.HOGDescriptor() # derive HOG features\n", " hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) # SVM\n", " \n", " # pedestrian detection\n", " found, w = hog.detectMultiScale(image, hitThreshold=0, winStride=(8,8), padding=(0,0), scale=1.05, finalThreshold=2)\n", " draw_detections(image, found) # draw rectangles\n", " \n", " # write & save image\n", " cv2.imshow('original',image) # write image\n", " cv2.waitKey() # for keyboard binding\n", " cv2.imwrite('./img/inria.jpg',image) # save image\n", " #cv2.destroyAllWindows() # clean up" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "scrolled": false }, "outputs": [], "source": [ "PD_default(\"./img/kids.jpg\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Daimler detector: Daimler Pedestrian Detection Benchmark Dataset is used for training" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def PD_daimler(filename):\n", " image = cv2.imread(filename) # read image\n", " hog = cv2.HOGDescriptor((48,96), (16,16), (8,8), (8,8), 9) # derive HOG features\n", " hog.setSVMDetector(cv2.HOGDescriptor_getDaimlerPeopleDetector()) # SVM\n", " \n", " # pedestrian detection\n", " found, w = hog.detectMultiScale(image, hitThreshold=0, winStride=(8,8), padding=(32,32), scale=1.05, finalThreshold=2)\n", " draw_detections(image, found) # draw rectangles\n", " \n", " # write & save image\n", " cv2.imshow('original',image) # write image\n", " cv2.waitKey() # for keyboard binding\n", " cv2.imwrite('./img/daimler.jpg',image) # save image\n", " #cv2.destroyAllWindows() # clean up" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "PD_daimler(\"./img/kids.jpg\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.5.2" }, "widgets": { "state": {}, "version": "1.1.2" } }, "nbformat": 4, "nbformat_minor": 1 }