Problem with Haar cascade

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

Hi

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 resize.py

import cv2
import os

pic_num=0
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))
    cv2.imwrite("negatif/"+str(pic_num)+".jpg",resized_image)
    pic_num=pic_num+1

[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 samplepos.sh

#!/bin/bash

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
done

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
done

# 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 train.sh

#!/bin/bash

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 verif.py

import cv2

from matplotlib import pyplot as plt

img=cv2.imread('img_mous.jpg')
imggris = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgrgb = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

FiltreMoustique=cv2.CascadeClassifier("/home/karim/perso/moustiques/haar/cascade.xml")

moustique=FiltreMoustique.detectMultiScale(imggris,scaleFactor=1.1,minNeighbors=4,minSize=(20,20))

n=len(moustique)

if n!=0:
    for (x,y,w,h) in moustique:
        cv2.rectangle(imgrgb,(x,y),(x+h,y+w),(0,255,0),5)

plt.subplot(1,1,1)
plt.imshow(imgrgb)
plt.show()

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

Karim

image description

edit retag flag offensive close merge delete

Comments

1

"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 -0600 )edit

I know now, Thanks !

karimbgmail gravatar imagekarimbgmail ( 2018-05-08 01:50:10 -0600 )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 -0600 )edit