Not able to identify the images correctly

asked 2019-08-20 07:22:15 -0600

Srikanth gravatar image

updated 2019-08-20 07:37:10 -0600

LBerger gravatar image

Hi team,

I have created haar cascade xml file with some training images. But when I use that to detect the object in a video, it's identifying a black portion in the video as the object where as the object is located at some other place in the video frame.

Could you please help me resolve this issue.

Here is my program:

from __future__ import print_function

import cv2 as cv

import argparse

def detectAndDisplay(frame):

    frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    frame_gray = cv.equalizeHist(frame_gray)

    #-- Detect faces

    faces = face_cascade.detectMultiScale(frame_gray)

    for (x,y,w,h) in faces:

        print('car detected')

        center = (x + w//2, y + h//2)

        frame = cv.ellipse(frame, center, (w//2, h//2), 0, 0, 360, (255, 0, 255), 4)

        faceROI = frame_gray[y:y+h,x:x+w]

    cv.imshow('Capture - Face detection', frame)

parser = argparse.ArgumentParser(description='Code for Cascade Classifier tutorial.')

parser.add_argument('--face_cascade', help='Path to face cascade.', default='C:/AdrsTrain/dasar_haartrain/myhaar.xml')

parser.add_argument('--camera', help='Camera devide number.', type=int, default=0)

args = parser.parse_args()

face_cascade_name = args.face_cascade

face_cascade = cv.CascadeClassifier()

#-- 1. Load the cascades

if not face_cascade.load('C:/AdrsTrain/dasar_haartrain/myhaar.xml'):

    print('--(!)Error loading face cascade')

    exit(0)


camera_device = args.camera

#-- 2. Read the video stream

cap = cv.VideoCapture('C:/Accident-detection-Project--master/DemoRun/videos/Detectionbk3.mp4')

if not cap.isOpened:

    print('--(!)Error opening video capture')

    exit(0)

while True:

    ret, frame = cap.read()

    if frame is None:

        print('--(!) No captured frame -- Break!')

        break

    detectAndDisplay(frame)

    if cv.waitKey(50) == 27:

        break

cap.release()

cv.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

Without any more details (number of images, examples, dataset link, training parameters, model, ...) it is almost impossible to say what goes wrong. However, my guess is your model hasn't seen enough no-object items to be sure of black areas. In most common approaches one uses hard negative mining. Collect those bad detections and add them as explicit negatives to the training set. Then run the training again.

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-20 07:50:57 -0600 )edit

Thankyou very much for your input,will try to get rid of the false positives.

The steps I followed:

  1. createsamples.exe -info positive/info.txt -vec vector/vector.vec -num 100000 -w 48 -h 24

  2. haartraining.exe -data cascades -vec vector/vector.vec -bg negative/bg.txt -npos 8 -nneg 200 -nstages 15 -mem 1024 -mode ALL -w 48 -h 24

  3. haarconv.exe cascades myhaar.xml 48 24

I have few questions:

  1. Should the -w & -h values be same as the dimension of the object to be detected?

  2. I trained my classifier with '-w 24 -h 24', it wasn't detecting the object (car) then I tried different dimensions: 100100, 5050 etc. But I used to get 'out of memory' error. When I finally tried -w 48 -h 24 to create the cascade file, it started detecting the object (with some false positives).

Srikanth gravatar imageSrikanth ( 2019-08-20 22:27:55 -0600 )edit

Ow your approach is sow flawed and for the following reasons

  1. haartraining is an old tool, it was replaced by traincascade tool which is updated. What OpenCV you are using?
  2. the dimensions of your object should have the same ratio as your w and h. Absolute size doesn't matter due to the pyramid scaling.

Could you get back to me with the info?

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-21 04:38:38 -0600 )edit

Hi Steven,

https://www.youtube.com/watch?v=Dg-4M... That is the link I used as reference for my work. I am using cascade (haar cascade) training only not haartraining. Thank you for mentioning about 'same ratio', let me follow that and check.

Srikanth gravatar imageSrikanth ( 2019-08-21 09:05:57 -0600 )edit

On top of that, there is this book opencv 3 blueprints, which has a very detailed discussion on the process as well as an official tutorial, that works better than youtube videos.

StevenPuttemans gravatar imageStevenPuttemans ( 2019-08-22 09:28:11 -0600 )edit