Not able to identify the images correctly
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()
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.
Thankyou very much for your input,will try to get rid of the false positives.
The steps I followed:
createsamples.exe -info positive/info.txt -vec vector/vector.vec -num 100000 -w 48 -h 24
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
haarconv.exe cascades myhaar.xml 48 24
I have few questions:
Should the -w & -h values be same as the dimension of the object to be detected?
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).
Ow your approach is sow flawed and for the following reasons
Could you get back to me with the info?
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.
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.