3

I am doing object recognition in opencv and python. I have done the training using 51 positive samples and 100 negative samples, and the stages of training is 7. using the command opencv_traincascade -data samples -vec phones.vec -bg bg.txt -numPos 51 -numNeg 100 -w 40 -h 70 -featureType LBP

So the training is completed and cascade.xml is created. Now I am trying to do object recognition, Problem I am facing is, It took a long time around 12 - 13 minutes for the detection. How can I fix it.

import cv2
import sys


cascpath = ('/home/aquib/opencv/opencv-3.0.0/data/haarcascades/cascade.xml')

Cascade = cv2.CascadeClassifier(cascpath)

img = cv2.imread('c2.jpg')    #saved in desktop

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #converting it_to grey 


object_detect = Cascade.detectMultiScale(
    gray,
    scaleFactor=1.3,
    minNeighbors=5,
    minSize=(15,15),
    flags=cv2.CASCADE_SCALE_IMAGE
)

for (x,y,w,h) in object_detect:
    img = cv2.rectangle(img,(x,y),((x+w),(y+h)),(255,0,0),2)
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]


cv2.namedWindow("Object_detect", cv2.WINDOW_NORMAL)
cv2.imshow("object",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Andrea Corbellini
  • 17,339
  • 3
  • 53
  • 69
Aquib
  • 320
  • 6
  • 20
  • Something is wrong with your code. Maybe the code is taking too much memory. Can you check your memory??? – hoaphumanoid Dec 26 '15 at 10:23
  • I have used the TOP command in the terminal. and I run the python code in the other terminal so it's showing me %CPU 98.3 and %MEM 2.2 for the Python code. – Aquib Dec 26 '15 at 17:08
  • This 98% is the problem. It shouldn't happen. Can you post the exact function that is causing the problem? – hoaphumanoid Dec 26 '15 at 17:10
  • Can you just be clear 'I am not getting the exact function' I am new to Ubuntu.... Or Should I paste my code ? – Aquib Dec 26 '15 at 17:16
  • Yes please, post your code. Also it would be good if you could identify the exact line of code that is causing the problem. – hoaphumanoid Dec 26 '15 at 17:17
  • I have posted the code.... Thank you so much for the help, thanks a lot – Aquib Dec 26 '15 at 17:23
  • Could you please send me the XML file and the jpg file? My email is hoaphumanoid at gmail dot com. If you could upload those files to the web that would be great. Maybe others can give you an answer – hoaphumanoid Dec 26 '15 at 17:29
  • Yes, I am try to upload it but it's not taking it in format... have sent the mail please find it ... – Aquib Dec 26 '15 at 17:35
  • I'll take a look. Cheers!! – hoaphumanoid Dec 26 '15 at 17:38
  • I had the same problem with my face detection code, down sampling the image helped me out. – danishansari Dec 27 '15 at 18:11

1 Answers1

2

The problem is here:

object_detect = Cascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=5,
minSize=(15,15),
flags=cv2.CASCADE_SCALE_IMAGE)

you are iterating a haar cascade with min size 15x15 in an image of 1280x720. This is a very small window in a very large image. The haar cascade looks for patterns in a square of 15x15 inside your image, and after that it starts to increase the size of the square. There are many squares of 15x15 in your image. You have to resize your image to a smaller size or increase the size of the haar cascade.

Haar cascades works well when trying to identify highly marked patterns, like faces. A face, in grey scale, has a horizontal line corresponding to the eyes, then a vertical line corresponding to the nose and then a horizontal line corresponding to the mouth. This kind of patterns are well detected by haar. If you want to identify other objects like a mobile phone, a book or similar, it is best to use other techniques like SIFT features.

hoaphumanoid
  • 977
  • 1
  • 9
  • 25