Problem with Haar cascade

asked 2018-05-07 12:17:49 -0500


I am trying to detect mosquitoes on images using Haar Cascade and it doesn't work.

I use ~200 images of walls (to use as negative images) and ~20 images of mosquitoes.

I did this :

1) I run the script below to resize the negative images (100x100 ) :

[karim@10-83-102-17 haar]$ more

import cv2
import os

for img in os.listdir('/home/karim/perso/moustiques/haar/downloads/wall'):
    print img
    timg = cv2.imread('/home/karim/perso/moustiques/haar/downloads/wall/'+img,cv2.IMREAD_GRAYSCALE)
    resized_image = cv2.resize(timg, (100, 100))

[karim@10-83-102-17 haar]$ ls negatif/* > negatif.txt

2) I run the script below to generate positive images :

[karim@10-83-102-17 haar]$ more


mkdir -p info 

# create the samples
for file in downloads/9*.jpg; do
    echo "File=" $file
    opencv_createsamples -img "$file" -bg negatif.txt -info "info/info_$file.da"t -pngoutput info -maxxangle 0.5 -maxyangle -0.5 -maxzangle 0.5 -num 125

mv info/info_downloads/* info

# generate the sample file
touch temp
for file in info/*.dat; do
    echo "File="$file
    cat temp "$file" >> ./info/info.dat

# generate the sample vector
opencv_createsamples -info info/info.dat -vec positif.vec -w 25 -h 18 -num 2000

3) Then, I train the classifier

[karim@10-83-102-17 haar]$ more


mkdir -p data

# train the classifier using haar cascade features
opencv_traincascade -data data -vec positif.vec -bg negatif.txt -numStages 10 -numPos 1800 -numNeg 900 -w 25 -h 18 -mode ALL -precalcValBuffSize 1024 -precalcIdxBuffSize 1024

mv ./data/cascade.xml .

It runs a couple of hours.

4) If I run the classifier on a mosquito image, I have the attached image

[karim@10-83-102-17 haar]$ more

import cv2

from matplotlib import pyplot as plt

imggris = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgrgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)




if n!=0:
    for (x,y,w,h) in moustique:


I got scripts from internet and I modified them for my use. I am probably doing something wrong ... Thanks for any advice.


image description

edit retag flag offensive close merge delete



"I am trying to detect mosquitoes on images using Haar Cascade and it doesn't work."

and it won't, ever.

cascades are good for static things, not for living, moving insects.

berak gravatar imageberak ( 2018-05-07 12:30:02 -0500 )edit

I know now, Thanks !

karimbgmail gravatar imagekarimbgmail ( 2018-05-08 01:50:10 -0500 )edit

Ah well thats not true, I have detected side view cows before with cascades, which simply worked, but your whole pipeline is broken. Do not use artificial sample generation, make sure your objects do not fill the complete image (that is classification, not localisation), your -w and -h parameter are too small to capture enough detail ... BTW for the last 2 years, CNNS and all there applications took over object detection, so stop wasting time here and go for SSD, YOLO, ...

StevenPuttemans gravatar imageStevenPuttemans ( 2018-05-08 07:58:59 -0500 )edit