Fred Miller
Fred Miller

Reputation: 11

Python OpenCV Sliding Window Object Detection

I'm implementing a sliding window in python 2.7, openCV version 3, using sklearn, skimage to apply a HOG detector to localise an object.

The HOG set-up works fine. If I do not apply a sliding window everything works okay.

The problem is the sliding window has a size of 128x128, giving a feature vector length of 15876. Whereas the training set has a size of 579474, as it was trained on 800x600 images.

I haven't seen any question that directly address this in a clear way, but it really does have me baffled. I also don't see many papers addressing this issue.

My code is this:

clf = joblib.load(model_path) 
# load the image and define the window width and height
image = imread(args["image"], flatten=True)
(winW, winH) = (128, 128)

# loop over the image pyramid
for resized in pyramid(image, scale=1.5):
    # loop over the sliding window for each layer of the pyramid
    for (x, y, window) in sliding_window(resized, stepSize=32, windowSize=(winW, winH)):
        # if the window does not meet our desired window size, ignore it
        if window.shape[0] != winH or window.shape[1] != winW:
            continue

        fd = hog(window, orientations, pixels_per_cell, cells_per_block, visualize, normalize)
        pred = clf.predict(fd)
        if pred == 1:
            print("found, found, found, found, found")

The sliding window visualises fine if I draw it, it's just the prediction function. How to compare window features to the training features vectors of larger lengths?

Thanks a lot for your time!

Kind regards,

Fred

Upvotes: 0

Views: 3616

Answers (1)

Fred Miller
Fred Miller

Reputation: 11

I think I have the answer to this:

Simply train images of the same dimensions as the window size. Might seem like you are losing data, but then test on a larger image. In order for this to work well said target object should fit in the window size.

So I'm training on 270x200, then scan a 270x200 window over say a 2.7K X 2K ( same aspect ratio).

It works like this, for anyone else who is confused :)

Fred

Upvotes: 1

Related Questions