Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HOGDescriptor with videos to recognize objects

Unfortunately I am both a python and a openCV beginner, so wish to excuse me if the question is stupid.

I am trying to use a cv2.HOGDescriptor to recognize objects in a video. I am concerned with a frame-by-frame recognition (i.e. no tracking or so).


Here is what I am doing:

  1. I read the video (currently a .mpg) by using

    capture = cv.CreateFileCapture(video_path) #some path in which I have my video
    #capturing frames
    frame = cv.QueryFrame(capture) #returns cv2.cv.iplimage
    
  2. In order to ultimately use the detector on the frames (which I would do by using

    found, w = hog.detectMultiScale(frame, winStride, padding, scale)
    

    ) I figured that I need to convert frame from cv2.cv.iplimage to numpy.ndarray which I did by

    tmp = cv.CreateImage(cv.GetSize(frame),8,3)
    cv.CvtColor(frame,tmp,cv.CV_BGR2RGB)
    
    ararr = np.asarray(cv.GetMat(tmp)).
    

Now I have the following error:

    found, w = hog.detectMultiScale(ararr, winStride, padding, scale)
 TypeError: a float is required

where

    winStride=(8,8)
    padding=(32,32)
    scale=1.05

I really can't understand which element is the real problem here. I.e. which number should be the float?

Any help appreciated

like image 289
Acorbe Avatar asked Feb 18 '23 19:02

Acorbe


2 Answers

There is no need to perform that extra conversion yourself, that problem is related to the mixing of the new and old OpenCV bindings for Python. The other problem regarding hog.detectMultiScale is simply due to incorrect parameter ordering.

The second problem can be directly seen by checking help(cv2.HOGDescriptor().detectMultiScale):

detectMultiScale(img[, hitThreshold[, winStride[, padding[, 
           scale[, finalThreshold[, useMeanshiftGrouping]]]]]])

as you can see, every parameter is optional but the first (the image). The ordering is also important, since you are effectively using winStride as the first, while it is expected to be the second, and so on. You can used named arguments to pass it. (All this has been observed in the earlier answer.)

The other problem is the code mix, here is a sample code that you should consider using:

import sys
import cv2

hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
hogParams = {'winStride': (8, 8), 'padding': (32, 32), 'scale': 1.05}

video = cv2.VideoCapture(sys.argv[1])
while True:
    ret, frame = video.read()
    if not ret:
        break

    result = hog.detectMultiScale(frame, **hogParams)
    print result
like image 102
mmgp Avatar answered Feb 20 '23 17:02

mmgp


The documentation for the C++ version of HOGDescriptor::detectMultiScale shows a hit_threshold parameter (of type double) prior to the win_stride argument. So it appears you are missing an argument to the function. To accept the default argument for win_stride, you should pass your addition arguments used in your question as keywords.

like image 36
bogatron Avatar answered Feb 20 '23 16:02

bogatron