# Head and people detection in opencv

> Free LBP Cascade download links with code example specifically designed for people and head detection in OpenCV

- Canonical URL: https://www.funvisiontutorials.com/blog/head-people-detection-opencv/
- Author: Vladimir Kucera
- Published: 2017-01-09
- Updated: 2024-05-03
- Topics: Object detection
- Videos: https://www.youtube.com/watch?v=tBwUbQ0pyUw

You can find free LBP Cascade download links specifically designed for people and head detection in OpenCV. It’s trained on my own people and head datasets.

The cascades can be used by using the CascadeClassifier in OpenCV, which allows you to detect objects (such as people and heads) in images or video streams.

Keep in mind that no cascade is perfect, but in some cases, this custom LBP cascade might perform better than the default OpenCV cascades. The differences arise from the shape and feature space used for detection. For instance, the head detector may have more false detections compared to the people detector due to the common features shared by various head shapes.

[Image: detect peole head opencv haar cascade]

### Video example of expected detection results by detectMultiScale

Video: https://www.youtube.com/watch?v=tBwUbQ0pyUw

### Head cascade and people cascade download link

[Head detection download link
](https://drive.google.com/file/d/0B_kNUWF69Zs2TERsLUNxYVZrbmc/view?usp=sharing&resourcekey=0-TTAVSFFd6-vpwKWgZmqQjA)[People detection download link](https://drive.google.com/file/d/0B_kNUWF69Zs2N2JpZWkyLXNfSnc/view?usp=sharing&resourcekey=0-r2nqxlqhIZWX-qC5VhUFbw)

### Head and people detection tutorial code

**Update from 2024  opencv installation**

-   I would recommend using the VCPKG installation described on my blog [here](/blog/opencv-installed-vcpkg-package-manager-cmake-project/) for the CMake project.
-   For the Visual Studio project [here](/blog/opencv-setup-visual-studio-2022-vcpkg/).

You can simply prepare the project inside the Visual Studio 2015 by Nuget Packages. This approach is easy for beginners and better than standard installation with all the environmental variables problems. Just follow the installation steps [here](/blog/opencv-31-opencv-2413-instalation-visual-studio/)

### LBP detectMultiScale cascade Opencv code

This code is based on my previous tutorial with more details [Fast people detection](/blog/fast-pedestrian-detection-opencv-cpp-tutorial/).

```cpp
// 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.04, 4, 0 | 1, Size(30, 80), Size(80,200));
 detectorHead.detectMultiScale(img, head, 1.1, 4, 0 | 1, Size(40, 40), Size(100, 100));
 if (human.size() > 0) {
  for (int gg = 0; gg < human.size(); gg++) {
      rectangle(original, human[gg].tl(), human[gg].br(), Scalar(0, 0, 255), 2, 8, 0);
  }
}
 if (head.size() > 0) {
  for (int gg = 0; gg < head.size(); gg++) {
      rectangle(original, head[gg].tl(), head[gg].br(), Scalar(0, 0, 255), 2, 8, 0);
  }
 }
```
