Share
Visit

Tuesday, February 26, 2019

Deep learning by Yolo Darknet vs HAAR and LBP cascades in people detection

Deep learning vs HAAR, LBP cascades people detection

I just performed people detection by Yolo model in Opencv 4 DNN module. I analyzed my old dataset, which was used for the same task performed by Haar, LBP as well. 

I will let the evaluation on your visual feeling. Let me know if there is an application out there building on HAAR, LBP. I think that even the cameras head focus/tracking systems are no more based on HAARs. Your comments are welcomed on facebook. 


YOLO DARKNET (CPU performance) people detection in opencv 4 

300 ms per image- width=316 --height=510 --scale=0.00392


LBP based detectMultiscale 


HAAR based detectMultiscale 


HAAR based detectMultiscale



Sunday, February 3, 2019

Opencv 4 Linux application in visual studio 2017- Ubuntu Linux subsystem on windows 10

Opencv Linux application in windows tutorial - Visual studio and linux subsystem

This tutorial is step by step guide for the development of Opencv 4 Linux application in Visual Studio 2017 under windows. The motivation is simple. I would like to work in Visual Studio 2017, which is simply great. I personally love it. It is not the only motivation. I am a windows user, but the market is full of Linux based cloud machines and docker. So, I need to target Linux. The opencv running on Linux is faster from my 5-year experience(consider any possible optimization options). Another motivating point is a windows file system. Write an application for Linux that accesses the windows file system.
linux development in visual studio










Again, I am windows person with lots of Linux experience, lots of datasets, images, and video stored on windows machines. I would like to use this data without any transfer or copy of my datasets. Just directly open c:/myPositiveImages image directory in Linux app as /mnt/c/myPositiveImages. Yes, it is possible.  I am doing this to prepare my datasets stored in the windows file system and prepare the proper inputs for Yolo darknet. Yolo is not so great on windows and hard to compile. Data, images prepared in windows are not compatible with most of the Linux apps and mainly Yolo input. You can say some argument like. Prepare your dataset on windows and transfer to the Linux and read by Yolo, learn your detector and enjoy deep neural networks. No this doesn't work. Yolo has a problem to read dataset prepared on windows. This is why Visual Studio 2017 and Linux subsystem, where data from windows are processed by a Linux app. This is only one motivation from the many possible. I simply like Visual Studio, but my target is linux cloud, linux container or IoT devices like rasbery pi.

What will I learn in this opencv tutorial? (win-Linux tutorial)

  1. Prepare environment, where Visual Studio communicates with remote or local Linux
  2. Install opencv from the GIT
  3. Build Linux Opencv application from windows, linker and compiler settings
  4. Use windows file system in Linux Opencv app

Skills needed for visual studio connected to Linux

Hopefully, you can follow this tutorial without too many problems. Anyway, this is advance staff and some knowledge is beneficial for the smooth process of configuration the development environment.

-Windows
-Do not afraid of Linux and command line
-Linux basics
-Do not afraid to read step by step this text
-Try to figure out what went wrong if something fails
-Certificates, ssh
-Visu Studio
-Opencv

Environment on windows

1)Windows 10, just 10. This is not working in windows 7. Linux subsystem can be turned on and Linux easily installed from windows store, but just on W10. Open PowerShell as Administrator and run following command

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

The Windows subsystem Linux can be turned on as well in control panels (It is harder and harder to find control panels in W10). Just google it if you do not have PowerShell.

2)The second part of preparation installs Linux from the windows store. Just find and install Ubuntu 18.04.
Linux under windows

3)Install Visual studio 2017 from scratch or modify. Insclude all C++ packages and make sure to install Linux development with C++. Mark the option on the following menu in red circle.
Cross platform development in Visual Studio


Summary, 1) The windows subsystem linux is enabled. 2) Ubuntu is installed from windows store. 3) Visual Studio is installed with Linux development C++. 

Prepare Linux Environment to communicate with Visual Studio

Now, it is the time to start your Linux. Find the Ubuntu icon and start. This will open the Ubuntu command line for you.  Let's start with the upgrade the package database and install updates by following two commands.

sudo apt-get update                  update your package database
sudo apt-get upgrade                upgrade packages in your linux

The system is prepared for installation of necessary additional packages. The process is visible on the following image and all commands listed below the image. The image is attached to see the correct response for your commands.

Visual studio linux development

sudo apt install -y build-essential             Install essential packages to build Linux apps.
sudo apt install -y gdb                               Install GDB debugger program
sudo apt install -y gdbserver                     Install GDB server

Build-essential package contains following programs. make, libc6, gcc, g++, dpkg-dev. Basic package to build(C++ compiler g++, C compiler gcc, c library and header files, etc) and link your program. The gdb is debugger and gdbserver is way how to communicate with this debugger and see the information directly in Visual Studio. 
The visual studio communicates with Linux through SSH and shared file system. The same way how Putty communicates with your Linux instance in cloud machine. We need OpenSSH-server and configure ssh for PasswordAuthentification. 

sudo apt install -y openssh-server             Install openssh server
sudo nano /etc/ssh/sshd_config                 sshd configuration file and modify as follows:
Set PasswordAuthentication to Yes   “PasswordAuthentication” setting and make sure it’s set to “yes”:

sudo ssh-keygen -A                                      Generate new host keys for SSH
You can expect this as the response "ssh-keygen: generating new host keys: DSA"

sudo service ssh start                                    Start ssh service
You can expect this as the response "Starting OpenBSD Secure Shell server sshd".
The ssh configuration, key generation is the most problematic part. The problem's solution is not trivial for many people. Hopefully, now everything is prepared to communicate with Linux from Visual Studio.

Install and build opencv 4 in Linux from GIT

This part is easy for little bit experienced person. Infollowing text is described process of download latest GIT repository with Opencv 4. Build the opencv library by CMAKE and install Opencv shared library into the system.
Install opencv on ubuntu linux

I have one /home/opencv directory. Here just issue following commands:
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

This will create two folders opencv and opencv_contrib with download source code from github repository.  By following commands is installed everything what you need to build opencv. Even the packages already installed. 

[we have this]             sudo apt-get install build-essential
[additional required]   sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional]                    sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev


Next to opencv and opencv_contrib create the new directory build by mkdir and enter the directory.

mkdir build
cd build

The next step runs the configuration by cmake. Cmake creates your make file, all the parameters for compiling opencv project and link with installed packages. 

cmake -D CMAKE_BUILD_TYPE=Release -D -D CMAKE_INSTALL_PREFIX=/usr/local ..

Do not forgot the .. double dots. to compile with opencv_contrib specify the path to contrib modules by parameter -D OPENCV_EXTRA_MODULES_PATH="PATH/opencv_contrib/modules/"
The cmake finish with all configuration you need to build opencv by make. This takes a bit of time. The final command that install your library under system directory /usr/local/lib as on picture below commands. 

make -j7
sudo make install


Connect Visual Studio with Linux by cross-platform connection manager

To perform this configuration successfully the Prepare Linux Environment to communicate with Visual Studio chapter needs to be set up correctly. The first this is to create new project in Visual Studio 2017. I pick up Console Application (Linux).

Visual Studio console linux application

Now is the time to connect my project with a remote system. This means with my localhost Linux subsystem under windows 10. In main Visual studio, menu go to Tools-Options-Cross Platform. You will perform the operation under Add- and fille Connect to Remote System menu. 
Host name                   - probably localhost or ip
Port                              - You are running OpenSSH with most probably port 22
user name                    - Your user name to Linux
Authentification type  -Password
Password                     -Your password for the specified user (This option is enabled by sshd_config “PasswordAuthentication” setting and make sure it’s set to “yes” described in prepare Linux environment section ).

Tools/options/crossplatform
Visual studio connect to linux subsystem

The Remote IntelliSence is a very useful feature. Update button for selected already defined remote machine download header file into the local cache. When you are writing the code for Linux and using Linux or opencv headers visual studio knows the input parameters, format and mark them as known. This is a huge feature. Otherwise, most of your code will be marked as unknown. 


Headers from remote machine visual studio
Now you can add some source code and just verify your connection by print something to console. Maybe some header like #include <cstdio> or #incude <iostream> are required.

#include <cstdio>
#include <iostream>
int main()
{
    printf("hello from OpencvLinux!\n");
    return 0;
}




Configure Linux Opencv 4 projects in Visual Studio

This is not an easy step. The project configuration is complex and usually done by CMAKE. Cmake prepares all the compiler options, C++ language standard and linking dependencies options for you. In visual studio is used  g++compiler and linker. I configure a simple project in pure Linux, run cmake configuration and transfer all the compiler and linker option into Visual Studio. First, Look at full compiler and linker Configuration in the following sections. The explanation follows under compiler and linker full options. 


Opencv visual studio g++ compiler  configuration

This is the full compiler options 

"g++" -W"switch" -W"no-deprecated-declarations" -W"empty-body" -W"conversion" -W"return-type" -W"parentheses" -W"no-pointer-sign" -W"no-format" -W"uninitialized" -W"unreachable-code" -W"unused-function" -W"unused-value" -W"unused-variable" -std=c++11 -w -fno-strict-aliasing -I "/usr/local/include/opencv4/opnecv2" -g0 "g++" -O2 "3600000" -fno-threadsafe-statics -D "NDEBUG" -W"switch" -W"no-deprecated-declarations" -W"empty-body" -W"conversion" -W"return-type" -W"parentheses" -W"no-format" -W"uninitialized" -W"unreachable-code" -W"unused-function" -W"unused-value" -W"unused-variable" -fno-rtti -fno-omit-frame-pointer -std=c11 -fno-exceptions "1" -o  "C:\Users\Vlada\source\repos\OpencvLinux\OpencvLinux\obj\x64\Release\%(filename).o" 

Opencv visual studio g++ linker configuration 

This  is the full linker options

-o"C:\Users\Vlada\source\repos\OpencvLinux\OpencvLinux\bin\x64\Release\OpencvLinux.out" -Wl,-rpath-link="/lib/x86_64-linux-gnu/" -Wl,-rpath-link="/usr/local/lib" -Wl,-rpath-link="/usr/lib/x86_64-linux-gnu/" -Wl,-rpath-link="/usr/local/lib/" -Wl,-rpath-link="/usr/lib/gcc/x86_64-linux-gnu/7" "3600000" -Wl,-z,norelro -Wl,--no-undefined "g++" -l"opencv_core" -l"opencv_imgcodecs" -l"opencv_imgproc" -l"opencv_video" -l"opencv_videoio" -l"pthread" -l"jpeg" -l"gcc_s" -l"stdc++" -l"tbb" -l"rt" -l"dl" -l"m" -l"z" -l"png16" -l"c" 


Opencv 4 compiler and linker options

The first thing is to add in property page of C/C++ project all g++, include directory, C++11 standard in case of opencv 4 and other things from the picture below. The windows below include all the option and you can mimic this all configuration in a same window other than in separate tabs.

Linker opencv 4 configuration part is more tricky

Library dependencies -l"opencv_core" -l"opencv_imgcodecs" -l"opencv_imgproc" -l"opencv_video" -l"opencv_videoio" -l"pthread" -l"jpeg" -l"gcc_s" -l"stdc++" -l"tbb" -l"rt" -l"dl" -l"m" -l"z" -l"png16" -l"c"  

You are just filling following values into Library dependencies option without -l 
opencv_core
opencv_imgcodecs
opencv_imgproc
opencv_video
opencv_videoio
pthread
jpeg
gcc_s
stdc++
tbb
rt
dl
m
z
png16
c
opencv 4  linker

The shared library search path option is important to locate library specified in previous step. My shared libraries like c,z,tbb,stdc++ .... are located in this shared lib locations:
/lib/x86_64-linux-gnu/
/usr/local/lib
/usr/lib/x86_64-linux-gnu/
/usr/local/lib/
/usr/lib/gcc/x86_64-linux-gnu/7

(you can have different hard to say), You can compile your c++ project directly in Linux by Cmake and look what shared libraries are used by your program. By command, LDD OpencvLinux is possible list your shared dependencies library with version and location. Build a simple opencv 4 project by cmake and include all in visual studio.

Opencv 4 shared library

All linker configuration can be mimic by follow all the setting in this table.

Opencv Yolo darknet program loading image from windows

The following program is stupid, not parametric externally, but reads images from windows file system and perform some actions. I will rewrite this program and explain in more details in following tutorials how to prepare data for yolo darknet. The scope of this article is little bit different.

Yolo darknet dataset preparation program description 

Program reads images from positive image library /mnt/c/npos" (C:/npos) and negative image library /mnt/c/neg (C:/neg). The positive image is inserted into a negative one on random location. You can see an inserted person in the center of the image. This result is stored as input of yolo darknet. 1.jpg sample with 1.txt file 1 0.490885 0.811523 0.031684 0.0651042 with class, and person normalized location in the image.


One last output is txt file of all the input image for train.txt and test.txt.
/mnt/c/result/0.jpg
/mnt/c/result/1.jpg
/mnt/c/result/2.jpg
/mnt/c/result/3.jpg
/mnt/c/result/4.jpg
/mnt/c/result/5.jpg
/mnt/c/result/6.jpg
/mnt/c/result/7.jpg
/mnt/c/result/8.jpg
/mnt/c/result/9.jpg

Sample code in progress

#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/video/video.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio/videoio.hpp>
#include <opencv2/imgcodecs/imgcodecs.hpp>
#include <vector>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>


#include <time.h>
#include <ctime>
#include <random>
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
using namespace std;
using namespace cv;

void read_directory(const std::string& name, vector<String> &v)
{
cout << "read dir" << endl;
DIR* dirp = opendir(name.c_str());
struct dirent * dp;
while ((dp = readdir(dirp)) != NULL) {
v.push_back(dp->d_name);
cout << dp->d_name << endl;
}
closedir(dirp);
cout << "closing" << endl;
}


int main()
{
 
vector<String> neg;
read_directory("/mnt/c/neg", neg);
vector<String>pos;
read_directory("/mnt/c/npos", pos);
cout << "dir read ends" << endl;
long lowest = 0;
if (pos.size() > neg.size()) {
lowest = neg.size();
}
else {
lowest = pos.size();
}
cout << lowest << endl;
std::random_device rd; 
std::mt19937 gen(rd()); 
std::ofstream outtest("/mnt/c/test.txt");
std::ofstream outtrain("/mnt/c/train.txt");
cout << "step 2" << endl;
for (int i = 2; i < lowest; i++) {
cout << pos.at(i)<< endl;
cout << neg.at(i) << endl;
}
long iter = 2;
for (int i = 3; i < lowest;i++) {
cout << "xxxxxxxxxxxxxxxxx" << endl;
cout << pos.at(i) << endl;
string pom = "/mnt/c/npos/" + pos.at(i);
Mat po = imread(pom, IMREAD_COLOR);
string nem = "/mnt/c/neg/" + neg.at(i);
cout << nem << endl;
Mat ne = imread(nem, IMREAD_COLOR);
cout << "xxxxxxxxxxxxxxxxx" << endl;
cout << "after reading" << endl;
std::uniform_int_distribution<> dis(1, ne.cols - po.cols + 1);
std::uniform_int_distribution<> dis2(1, ne.rows - po.rows + 1);
int a = dis(gen);
int b = dis2(gen);
cout << a << endl;
cout << b << endl;
cout << po.cols << endl;
cout << po.rows << endl;
po.copyTo(ne(Rect(a, b, po.cols, po.rows))); cout << "writing result" << endl;
string base = "/mnt/c/result/";
stringstream builder;
builder << base << iter << ".jpg";
imwrite(builder.str(), ne);
stringstream builder2;
string base2 = "/mnt/c/result/";
builder2 << base << iter << ".txt";
std::ofstream outfile(builder2.str());
stringstream builder4;
builder4 << base2 << iter << ".jpg" << endl;
outtest << builder4.str();
outtrain << builder4.str();
//outfile << base2 << " " << a << " " << b << " " << a+po.cols << " "<< b+po.rows  << std::endl;
outfile << 1 << " " << (float)a / ne.cols << " " << (float)b / ne.rows << " " << (float)po.cols / ne.cols << " " << (float)po.rows / ne.rows << std::endl;
outfile.close();
iter = iter + 1;
}
outtest.close();
outtrain.close();
cout << "all is done" << endl;
return 0;
}

Future tutorial

In future work, I will rewrite the program into a more useful utility. The purpose is to prepare yolo darknet dataset with all inputs from images located in the windows filesystem. Are you interested?

Common problems with opencv like an undefined reference to cv::imwrite


1) Restart SSH if Visual studio stop communicates with linux subsystem:sudo service ssh restart


2)Problems with a linker, by cmake all is configured ok, by make file and own g++ setting like in case of Visual Studio the problem can be C++11 standard in opnecv 4 (std::__cxx11), include files but most probably the settings of the shared libraries. 

Symptoms:
undefined reference to cv::imwrite(std::basic_string

undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)' undefined reference to 

cv::imwrite(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)

Monday, December 10, 2018

Opencv 4 DNN module- People detection CPU performance with yolo tiny 2 model

Opencv 4 DNN, People detection CPU performance with yolo 2 tiny model

I am still working on tutorial how to simply run yolo and others model in opencv 4. I try to do as simple as possible. Not like a general sample in Opencv. Differently, simply, and described as much as possible. 

yolo tiny people detection cpu performance

Some technical specifications video

Testing opencv 4.0 DNN with yolo tiny 2 model on people detection in a mall. Pure CPU, I7 (4 cores), running by the following command under windows 10. Performance for CPU without 2 much optimization effort is 500 ms per image approximately on my configuration.  Let me know if there is some problem with the parameters. 
testOpecv.exe --config=C:\darknet-master\cfg\yolov2-tiny.cfg --model=C:\darknet-master\weights\yolov2-tiny.weights --classes=C:\opencv32\darknet-master\data\coco.names --width=616 --height=616 --scale=0.00192 --rgb 






Sources: 
Thank you for a great job

Tuesday, November 27, 2018

trailer Testing opencv 4 Deep neural network module

trailer-Testing opencv 4 deep neural network

I am working on tutorial and notes about DNN module. Step by step from installation to people detection like on video.  Here is DNN module with YOLO 2 tiny model apply on my stupid Charles Bridge sample. I take this sample while running over the Charles Bridge in Prague. See you soon with code, Tutorial and more ideas. 

Sunday, November 25, 2018

People head detection code and performance of Opencv 4 compared to opencv 3.2

Opencv 4.0 is released, people head detection compared to opencv 3.2

I am working with opencv for almost a 6 years. It is 3.5 years since the 3.0. The journey driven by DNN, new ideas, algorithms and optimization is breathtaking.  Thanks to all contributors. Here is my point of view to opencv 4.0.  The video comparison of opencv 4 and 3.2 with head and shoulders detection performance at the end of the article with sample code. 

opencv 4 performance detection
opencv 4 - opencv 3.2 performace comparison

Opencv 4.0 main changes

C++11 is one of the main changes that will programmer realize at the first. This brings a lot of advantages to write safer and stable code. Smart pointers in C++11 and many features of the standard library helps to write code safer and with better performance. The reinventing the wheel is a weakness of all programmers. Better is know what is already available in various libraries and use it. The 30 years of sorting, algorithm and memory handling do not wait for application programmers reinvent.  

Opencv 4.0 C API removed

It is mentioned removed C API from 1.x. I need to dive deeper into this. I used this API in FFmpeg combination with opencv long long time ago. Sometimes, I like old C more than C++. I get the point. I can take it. The maintain the C API there is no point.

OPENCV 4.0 XML YAML and JSON loading and storing

The configuration parsing of DNN and various other algorithm is key for automation and basically most of the annoying parts of the program. If you ever write your own parser for CSV, XML or JSON you know what is the most boring experience in programming :).

Opencv 4.0 G-API

New module G-API  is an engine for very efficient graph-based image processing pipelines. This looks interesting and can speed up performance and as well development. An example can be to prepare image into some specific stage.  Basically, G api is input-output pipeline and between input and output, you can perform very fast transformation of your image. It is as well very efficient from the point of memory management and access. Following example define pipeline from an input image to resized image to grayscale image,. If you want to use this pipeline in the code just write cv::GComputation ac(in, out);. The all the pipeline from input and output will be performed.

cv::GMat in;
cv::GMat vga = cv::gapi::resize(in, cv::Size(), 0.5, 0.5);
cv::GMat out = cv::gapi::BGR2Gray(vga);
cv::GComputation ac(in, out);

This can be very cool features. I will learn more about and prepare some of my thoughts about G-API.

Opencv 4.0 DNN module

Dnn module now includes experimental Vulkan backend. Vulkan is cross-platform 3D graphics computation API. New alternative against OpenGL and Direct3D. This can speed up processing of some layers in DNN as in case of 3D graphics. The representations and models are pretty much the same from the internal point of view. 

Opencv 4 vs 3.2 Opencv performance 

Both are optimized as much as possible for I7 HW without GPU. The development does not focus exactly on LBP cascade detectMultiscale. This both opencv tasks performing really similar on this concrete problem.


Opencv 4 cmake build configuration

This is just my example of cmake configuration. I am not using any special magic right now to build opencv 4.0.0.

General configuration for OpenCV 4.0.0 =====================================
Version control: unknown
Timestamp: 2018-11-25T10:16:09Z
Host: Windows 10.0.17134 AMD64
CMake: 3.13.0-rc2
CMake generator: Visual Studio 15 2017 Win64
CMake build tool: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/MSBuild/15.0/Bin/MSBuild.exe
MSVC: 1915
CPU/HW features:
Baseline: SSE SSE2 SSE3
requested: SSE3
Dispatched code generation: SSE4_1 SSE4_2 FP16 AVX AVX2
requested: SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
SSE4_1 (7 files): + SSSE3 SSE4_1
SSE4_2 (2 files): + SSSE3 SSE4_1 POPCNT SSE4_2
FP16 (1 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
AVX (5 files): + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
AVX2 (13 files): + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
FFMPEG: YES (prebuilt binaries)
avcodec: YES (ver 58.35.100)
avformat: YES (ver 58.20.100)
avutil: YES (ver 56.22.100)
swscale: YES (ver 5.3.100)
avresample: YES (ver 4.0.0)
DirectShow: YES
Media Foundation: YES
Parallel framework: Concurrency
Trace: YES (with Intel ITT)
Other third-party libraries:
Intel IPP: 2019.0.0 Gold [2019.0.0]
Intel IPP IW: sources (2019.0.0)

Install to: C:/opencv4build/install

-----------------------------------------------------------------

Code of opencv 4 performance comparison sample 

If you want to run the code. The problem is to use own detection cascade instead of cascade25.xml. This cascade is my own train to detect head and shoulders mainly from top view. This is not even top view but still there are some detections. Parameters (detectorBody.detectMultiScale(img, human, 1.4, 3, 0 | 1, Size(30,30), Size(120, 120));. ) are also not optimal but perform very fast on CPU and for comparison purpuse only. 

Opencv 3 vs Opencv 4 diferrence 

Warning focus on difference between opencv 3 and opencv 4

CV_CAP_OPENNI_BGR_IMAGE as CAP_OPENNI_BGR_IMAGE
CV_BGR2GRAY   as    COLOR_BGR2GRAY
opencv 3
outputVideo.open("video3.wmv", CV_FOURCC('W', 'M', 'V', '2'), cap.get(CV_CAP_PROP_FPS), Size(640, 480), true);


opencv 4
outputVideo.open("video3.wmv", VideoWriter::fourcc('W', 'M', 'V', '2'), cap.get(CAP_PROP_FPS), Size(640, 480), true);


Opencv 4.0 head detection, tutorial code example 

Almost everithink is the same. This is the reason why there is only function det(). Focus on parts described in Opencv 3 vs Opencv 4 diferrence chapter.



#include "pch.h"
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <fstream>
#include <sstream>
#include "opencv2/objdetect\objdetect.hpp"



using namespace cv;
using namespace std;


string to_strin(double x) {
 ostringstream x_convert;
 x_convert << x;
 return x_convert.str();
}

int det()
{


 // prepare video input
 VideoCapture cap("mo.mov");

 // prepare video output
 VideoWriter outputVideo;

 outputVideo.open("video3.wmv", VideoWriter::fourcc('W', 'M', 'V', '2'),
        cap.get(CAP_PROP_FPS), Size(640, 480), true);

 // prepare cascadeClassifier
 CascadeClassifier detectorBody;

 // !! Put your cascade or opencv cascede into project folder !!
 string cascadeName1 = "cascade25.xml";


 // Load cascade into CascadeClassifier
 bool loaded1 = detectorBody.load(cascadeName1);
 

 // Basic video input loop
 for (;;)
 {

  bool Is = cap.grab();
  if (Is == false) {

   cout << "Video Capture Fail" << endl;
   break;
  }
  else {
   // Just for measure time   
   const clock_t begin_time = clock();

   // Store results in these 2 vectors
   vector human;
   vector upperBody;

   // prepare 2 Mat container
   Mat img;
   Mat original;

   // capture frame from video file
   cap.retrieve(img, CAP_OPENNI_BGR_IMAGE);
   // Resize image if you want with same size as your VideoWriter
   resize(img, img, Size(640, 480));
   // Store original colored image
   img.copyTo(original);
   // color to gray image
   cvtColor(img, img, COLOR_BGR2GRAY);

   // detect people, more remarks in performace section  
   detectorBody.detectMultiScale(img, human, 1.4, 3,
                                    0 | 1, Size(30, 30), Size(120, 120));

   // Draw results from detectorBody into original colored image
   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);
    }
   }
          
   // measure time as current - begin_time
   clock_t diff = clock() - begin_time;
   // convert time into string
   //char buffer[126];
   //sprintf(buffer, "%d", diff);
   // display TIME ms on original image
   double ms = (double)diff;
   
   string mmm = to_strin(ms);
   putText(original, mmm, Point(100, 20), 1, 2, 
                               Scalar(255, 255, 255), 2, 8, 0);
   putText(original, "ms", Point(150, 20), 1, 2,
                               Scalar(255, 255, 255), 2, 8, 0);
   // draw results
   namedWindow("prew", WINDOW_AUTOSIZE);
   imshow("prew", original);
   // make video output
   outputVideo << original;

   int key1 = waitKey(20);

  }
 }
}


int main(int argc, char** argv)
{
 det();
 return 0;
}

ad