8

I've gotten OpenCV working with Python and I can even detect a face through my webcam. What I really want to do though, is see movement and find the point in the middle of the blob of movement. The camshift sample is close to what I want, but I don't want to have to select which portion of the video to track. Bonus points for being able to predict the next frame.

Here's the code I have currently:

#!/usr/bin/env python

import cv

def is_rect_nonzero(r):
    (_,_,w,h) = r
    return (w > 0) and (h > 0)

class CamShiftDemo:

    def __init__(self):
        self.capture = cv.CaptureFromCAM(0)
        cv.NamedWindow( "CamShiftDemo", 1 )
        self.storage = cv.CreateMemStorage(0)
        self.cascade = cv.Load("/usr/local/share/opencv/haarcascades/haarcascade_mcs_upperbody.xml")
        self.last_rect = ((0, 0), (0, 0))

    def run(self):
        hist = cv.CreateHist([180], cv.CV_HIST_ARRAY, [(0,180)], 1 )
        backproject_mode = False
        i = 0
        while True:
            i = (i + 1) % 12

            frame = cv.QueryFrame( self.capture )

            if i == 0:
                found = cv.HaarDetectObjects(frame, self.cascade, self.storage, 1.2, 2, 0, (20, 20))
                for p in found:
                    # print p
                    self.last_rect = (p[0][0], p[0][1]), (p[0][2], p[0][3])
                    print self.last_rect

            cv.Rectangle( frame, self.last_rect[0], self.last_rect[1], cv.CV_RGB(255,0,0), 3, cv.CV_AA, 0 )
            cv.ShowImage( "CamShiftDemo", frame )

            c = cv.WaitKey(7) % 0x100
            if c == 27:
                break

if __name__=="__main__":
    demo = CamShiftDemo()
    demo.run()
3
  • 1
    just follows its slime trail :D Commented Jul 30, 2010 at 18:44
  • Even a C++ example would be helpful, as the docs are very hard to follow. Commented Jul 30, 2010 at 19:41
  • 1
    Also see simpler'ized : stackoverflow.com/questions/3374828/… Commented Jul 30, 2010 at 19:53

1 Answer 1

4

Found a solution at How do I track motion using OpenCV in Python?

Sign up to request clarification or add additional context in comments.

1 Comment

Got some video of it over at appdelegateinc.com/blog/2010/08/02/… too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.