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)
def preRender(self): frame = self.getFrame() capture = self.getCapture() #print _AR.GetMultiMV('data/19_20_21_22_23_24.cfg') #_AR.GetMultiMV('data/13_14_15_16_17_18.cfg') #if 'darwin' in os.uname()[0].lower(): if 0: settings = [ cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_FOURCC), cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_BRIGHTNESS), cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_CONTRAST), cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_SATURATION), cv.GetCaptureProperty(capture, CVtypes.CV_CAP_PROP_HUE), ] print settings size = cv.GetSize(frame) fsize = size.width, size.height size = cv.GetSize(self.image) isize = size.width, size.height if fsize == isize: cv.Copy(frame, self.image) else: cv.Resize( frame, self.image, CVtypes.CV_INTER_LINEAR, #CVtypes.CV_INTER_CUBIC, ) cv.CvtColor(self.image, self.hsvImage, CVtypes.CV_RGB2HSV) cv.Split(self.hsvImage, self.hsvPlanes[0], self.hsvPlanes[1], self.hsvPlanes[2], 0) cv.CvtColor(self.image, self.grayImage, CVtypes.CV_RGB2GRAY) if 1: #cv.EqualizeHist(self.hsvPlanes[0],self.hsvPlanes[0]) #cv.EqualizeHist(self.hsvPlanes[2],self.hsvPlanes[2]) #cv.Merge(self.hsvPlanes[0],self.hsvPlanes[1], # self.hsvPlanes[2],0, self.hsvImage) #cv.CvtColor(self.hsvImage,self.image,CVtypes.CV_HSV2RGB) cv.EqualizeHist(self.grayImage, self.grayImage) cv.Split(self.image, self.rgbPlanes[0], self.rgbPlanes[1], self.rgbPlanes[2], 0) cv.Copy(self.image, self.modifiedImage)
def detect(self, image): size = cv.GetSize(image) # create grayscale version grayscale = cv.CreateImage(size, 8, 1) cv.CvtColor(image, grayscale, cv.BGR2GRAY) # create and clear storage storage = cv.CreateMemStorage(0) cv.ClearMemStorage(storage) # equalize histogram cv.EqualizeHist(grayscale, grayscale) # detect faces faces = cv.HaarDetectObjects(grayscale, self.face_cascade, storage, 1.2, 2, cv.HAAR_DO_CANNY_PRUNING, self.face_size) if faces: # faces 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) detected = True is_face = True else: # detect body bodies = cv.HaarDetectObjects(grayscale, self.body_cascade, storage, 1.1, 3, 0, self.body_size) if bodies: # body detected for i in bodies: 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) detected = True is_face = False else: detected = False is_face = False # release resources we don't need any more cv.ReleaseImage(grayscale) cv.ReleaseMemStorage(storage) return (detected, is_face)
#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 if (osName == "nt"): cv.Flip(frame, frame, 0) else:
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