def onIdle(self, event): img = cv.QueryFrame(self.cap) self.displayImage(img) event.RequestMore() # timing stuff self.frames += 1 interval = time.clock() - self.starttime if interval > 5: print 'rate = %.1f frames/second' % (self.frames / interval) self.frames = 0 self.starttime = time.clock()
def fetch_and_detect(self): frame = cv.QueryFrame(self.capture) if frame is None: print 'Could not get frame' return cv.Flip(frame, None, 1) # detection tpl = self.detect(frame) # = (detected something?) # update webcam image if self.show_cam: cv.ShowImage('Camera', frame) return tpl
from CVtypes import cv win = 'Show Cam' cv.NamedWindow(win) cap = cv.CreateCameraCapture(0) while cv.WaitKey(1) != 27: img = cv.QueryFrame(cap) cv.ShowImage(win, img)
print "OpenCV in Python using CVtypes" print "OpenCV version: %s (%d, %d, %d)" % (cv.VERSION,cv.MAJOR_VERSION,cv.MINOR_VERSION,cv.SUBMINOR_VERSION) #create window and move to screen position cv.NamedWindow ('Camera', cv.WINDOW_AUTOSIZE) if len (sys.argv) == 1: # no argument on the command line, try to use the camera capture = cv.CreateCameraCapture (0) # ### check that capture device is OK if not capture: print "Error opening capture device" sys.exit (1) # ### capture the 1st frame to get some propertie on it frame = cv.QueryFrame (capture) # ### get size of the frame frame_size = cv.GetSize (frame) gray = cv.CreateImage( frame_size, 8, 1 ) small_img = cv.CreateImage( cv.Size( int(frame_size.width/image_scale),int(frame_size.height/image_scale)), 8, 1 ) cascade = cv.LoadHaarClassifierCascade( cascade_name, cv.Size(1,1) ) # while 1: # do forever # capture the current image frame = cv.QueryFrame (capture) if frame is None: # no image captured... end the processing break # ### check OS
def detect(image): image_size = cv.GetSize(image) # create grayscale version grayscale = cv.CreateImage(image_size, 8, 1) cv.CvtColor(image, grayscale, cv.BGR2GRAY) # create storage storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect objects cascade = cv.LoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', cv.Size(1, 1)) faces = cv.HaarDetectObjects(grayscale, cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, cv.Size(50, 50)) if faces: print 'face detected!' for i in faces: cv.Rectangle(image, cv.Point(int(i.x), int(i.y)), cv.Point(int(i.x + i.width), int(i.y + i.height)), cv.RGB(0, 255, 0), 3, 8, 0) # create windows cv.NamedWindow('Camera', cv.WINDOW_AUTOSIZE) # create capture device device = 0 # assume we want first device capture = cv.CreateCameraCapture(0) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_WIDTH, 640) cv.SetCaptureProperty(capture, cv.CAP_PROP_FRAME_HEIGHT, 480) # check if capture device is OK if not capture: print "Error opening capture device" sys.exit(1) while 1: # do forever # capture the current frame frame = cv.QueryFrame(capture) if frame is None: break # mirror cv.Flip(frame, None, 1) # face detection detect(frame) # display webcam image cv.ShowImage('Camera', frame) # handle events k = cv.WaitKey(10) if k == 0x1b: # ESC print 'ESC pressed. Exiting ...' break
def frameiter(cap=None): if cap is None: cap = cv.CreateCameraCapture(0) while True: img = cv.QueryFrame(cap) yield img