Skip to main content

Download My custom trained Opencv Cascade Classifier for detectMultiScale

This tutorial contains a list of custom trained LBP and HAAR cascade trained for Opencv CascadeClassifier detect multiscale method. You can download and test these cascades to detect people, heads, and cars in OpenCV. They are of mixed quality and mainly tuned for performance. I preferred LBP exactly for the performance reason. The cascades are not perfect but available for free. The cascades are useful in many projects.  The training data are originally mine as well. Please cite the blog if you are using these cascades. Thanks
Download LBP and HAAR cascade for head and people detection

Opencv Car Cascade Classifier

Download car cascade HERE

I created the car dataset from the video from my prone. The base set of positive car samples was very poor. Basically, I used background subtraction to extract moving bounding boxes over the highway. Then the dataset was clean manually. The duplicates and very similar pictures were removed for better robustness of the detector. 
Cascade classifier car detector

Opencv car Cascade Classifier properties

<!--
This is just basic 5 stage car detector develop by 
V.Kfrom https://funvision.blogspot.com
-->
<opencv_storage>
<cascade>
  <stageType>BOOST</stageType>
  <featureType>LBP</featureType>
  <height>32</height>
  <width>32</width>
  <stageParams>
    <boostType>GAB</boostType>
    <minHitRate>9.9999499320983887e-01</minHitRate>
    <maxFalseAlarm>4.1999998688697815e-01</maxFalseAlarm>
    <weightTrimRate>9.4999999999999996e-01</weightTrimRate>
    <maxDepth>25</maxDepth>
    <maxWeakCount>80</maxWeakCount></stageParams>
  <featureParams>
    <maxCatCount>256</maxCatCount>
    <featSize>1</featSize></featureParams>
  <stageNum>5</stageNum>

Opencv car cascade classifier code


#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2\objdetect\objdetect.hpp"
#include "opencv2/video/tracking.hpp"
#include <vector>
#include <stdio.h>
#include <Windows.h>
#include <iostream>
#include <time.h>
#include <ctime>

using namespace cv;
using namespace std;

int main(int argc, const char** argv)
{
 // Output video
 VideoWriter outputVideo;
 outputVideo.open("caars.wmv"CV_FOURCC('W''M''V''2'), 30,
 Size(640480), true);
 // Input video
 VideoCapture capture("car.MOV");
 Mat fgimg;
 Mat fgmask;
 int imcount = 0;
 string cascadeName1 = "cascade.xml";
 CascadeClassifier detectorBody;
 bool loaded1 = detectorBody.load(cascadeName1);
 for (;;)
 {
  bool Is = capture.grab();
  if (Is == false) {
   cout << "Video Capture Fail" << endl;
  }
  else {
   Mat img;
   capture.retrieve(img, CV_CAP_OPENNI_BGR_IMAGE);
   resize(img, img, Size(640480));
   img = img(Rect(075639379));
   Mat original;
   img.copyTo(original);
   vector car;
   cvtColor(img, img, CV_BGR2GRAY);
   equalizeHist(img, img);
   detectorBody.detectMultiScale(img, car, 1.150 | 1Size(2020), Size(600600));
   if (car.size() > 0) {
   for (int gg = 0; gg < car.size(); gg++) {
    rectangle(original, car[gg].tl(), car[gg].br(), Scalar(00255), 280);
    }
   }
   imshow("det", original);
   int key6 = waitKey(40);
   resize(original, original, Size(640480));
   outputVideo << original;
  }
 }
}

Opencv Head Cascade Classifier

Head cascade download HERE
To create this dataset was very tricky. The problem was to isolate the heads. I have a dataset for the upper body as a result of the crop the upper 30 percent of pedestrian and people detection. I want to train the detector more focused just to head detection. It can achieve some benefit in multi-target tracking. I perform the collection of the head purely manually. I dataset contains more than 2000 heads as positive samples and some random pictures as negatives.

Opencv head Cascade Classifier properties

<?xml version="1.0"?>
<!--
This is just basic 16 stage lbp cascade head detector develop by 
V.Kfrom https://funvision.blogspot.com
-->
<opencv_storage>
<cascade>
  <stageType>BOOST</stageType>
  <featureType>LBP</featureType>
  <height>38</height>
  <width>38</width>
  <stageParams>
    <boostType>GAB</boostType>
    <minHitRate>9.9999994039535522e-01</minHitRate>
    <maxFalseAlarm>6.0000002384185791e-01</maxFalseAlarm>
    <weightTrimRate>9.4999999999999996e-01</weightTrimRate>
    <maxDepth>25</maxDepth>
    <maxWeakCount>60</maxWeakCount></stageParams>
  <featureParams>
    <maxCatCount>256</maxCatCount>
    <featSize>1</featSize></featureParams>
  <stageNum>16</stageNum>

Opencv People Cascade Classifier

People cascade download link
People cascade 2 download
This cascades was trained on the people positive samples extracted mostly from security cameras view. The process of dataset collection was the same as well in car case. The background substraction collect very messy collection of moving people in foreground. This was clear manually to extract dataset of various situation, where the cuplicates are removed for better robustness.

Opencv people Cascade Classifier properties 1

<?xml version="1.0"?>
<!--
This is just basic 16 stage lbp cascade head detector develop by 
V.Kfrom https://funvision.blogspot.com
-->
<opencv_storage>
<cascade>
  <stageType>BOOST</stageType>
  <featureType>LBP</featureType>
  <height>64</height>
  <width>32</width>
  <stageParams>
    <boostType>GAB</boostType>
    <minHitRate>9.9999499320983887e-01</minHitRate>
    <maxFalseAlarm>3.0000001192092896e-01</maxFalseAlarm>
    <weightTrimRate>9.4999999999999996e-01</weightTrimRate>
    <maxDepth>20</maxDepth>
    <maxWeakCount>80</maxWeakCount></stageParams>
  <featureParams>
    <maxCatCount>256</maxCatCount>
    <featSize>1</featSize></featureParams>
  <stageNum>10</stageNum>

The second people cascade Cascade Classifier properties

<?xml version="1.0"?>
<!--
This is just basic 12 stage haar cascade pedestrian detector develop by 
V.Kfrom https://funvision.blogspot.com
-->
<opencv_storage>
<cascade>
  <stageType>BOOST</stageType>
  <featureType>LBP</featureType>
  <height>64</height>
  <width>32</width>
  <stageParams>
    <boostType>GAB</boostType>
    <minHitRate>9.9500000476837158e-01</minHitRate>
    <maxFalseAlarm>4.1999998688697815e-01</maxFalseAlarm>
    <weightTrimRate>9.4999999999999996e-01</weightTrimRate>
    <maxDepth>1</maxDepth>
    <maxWeakCount>100</maxWeakCount></stageParams>
  <featureParams>
    <maxCatCount>256</maxCatCount>
    <featSize>1</featSize></featureParams>
  <stageNum>13</stageNum>

Opencv Cascade classifier performance and detection considerations

minNeighbors – This mainly influences false positive and true positive detection. Take a time to tune this parameter.

scaleFactor –This mainly influences performance as well as false positive and true positive detection. Take a time to tune this parameter.

minSize – Minimum possible object size. This dramatically influence performance
maxSize – Maximum possible object size.This dramatically influence performance

ignored.CascadeClassifier::detectMultiScale(const Mat& image, vector<Rect>& objects, double scaleFactor=1.1, int minNeighbors=3, int flags=0, Size minSize=Size(), Size maxSize=Size()

Code how to use opencv Cascade Classifier for detectMultiScale

//V.Kfrom https://funvision.blogspot.com
// Name of the downloaded my cascades.. 
 string cascadeHead = "cascadeH5.xml";
 string cascadeName = "cascadG.xml";
// Load the cascade
 CascadeClassifier detectorBody;
 bool loaded1 = detectorBody.load(cascadeName);
 CascadeClassifier detectorHead;
 bool loaded2 = detectorHead.load(cascadeHead);
        
// save original make img gray
// draw rectangle back to the original colored sample
 Mat original;
 img.copyTo(original);
// Prepare vector for results 
vector<Rect> human;
vector<Rect> head;
// Prepare gray image
 cvtColor(img, img, CV_BGR2GRAY);
// equalize Histogram  
        equalizeHist(img, img);
// detect body and head in the img 
// Set the proper min and max size for your case
 detectorBody.detectMultiScale(img, human, 1.0440 | 1Size(3080), Size(80,200));
 detectorHead.detectMultiScale(img, head, 1.140 | 1Size(4040), Size(100100));
 if (human.size() > 0) {
  for (int gg = 0; gg < human.size(); gg++) {
   rectangle(original, human[gg].tl(), human[gg].br(), Scalar(00255), 280);
     }
  }
 if (head.size() > 0) {
  for (int gg = 0; gg < head.size(); gg++) {
   rectangle(original, head[gg].tl(), head[gg].br(), Scalar(00255), 280);
     }
 }

Opencv LBP HAAR Cascade Classifier for detectMultiScale in action



Comments