Last Updated: February 25, 2016
·
24.67K
· eeadc

Sliding window in Python

Python provides an excellent infrastructure for iterators, and there are usecases, where you could need a windowed iterator, for example parsers with lookahead or lookbehind.

This sliding window implementation is optimized for speed (There are a dozen of implementations that are slower than this, at least the best solution on Stack Overflow):

def window(iterable, size=2):
    i = iter(iterable)
    win = []
    for e in range(0, size):
        win.append(next(i))
    yield win
    for e in i:
        win = win[1:] + [e]
        yield win

1 Response
Add your response

I'm looking for a sliding window implementation for images, which iterates over the whole image and changes scale and does it again, etc. In particular, I need this for object detection. The same kind of sliding window that was implemented by Viola and Jones, http://cs.nyu.edu/courses/fall12/CSCI-GA.2560-001/FaceRecognitionBoosting.pdf.
Do you know of any?

over 1 year ago ·