def detect(self, image): # image size is needed by underlying opencv lib to allocate memory image_size = opencv.cvGetSize(image) # the algorithm works with grayscale images grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # more underlying c lib memory allocation storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect faces using haar cascade, the used file is trained to # detect frontal faces cascade = opencv.cvLoadHaarClassifierCascade( 'haarcascade_frontalface_alt.xml', opencv.cvSize(1, 1)) faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(100, 100)) # draw rectangles around faces for face in faces: opencv.cvRectangle( image, opencv.cvPoint(int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # return faces casted to list here, otherwise some obscure bug # in opencv will make it segfault if the casting happens later return image, list(faces)
def detect(self, image): # image size is needed by underlying opencv lib to allocate memory image_size = opencv.cvGetSize(image) # the algorithm works with grayscale images grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # more underlying c lib memory allocation storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect faces using haar cascade, the used file is trained to # detect frontal faces cascade = opencv.cvLoadHaarClassifierCascade( 'haarcascade_frontalface_alt.xml', opencv.cvSize(1, 1)) faces = opencv.cvHaarDetectObjects( grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(100, 100)) # draw rectangles around faces for face in faces: opencv.cvRectangle( image, opencv.cvPoint( int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # return faces casted to list here, otherwise some obscure bug # in opencv will make it segfault if the casting happens later return image, list(faces)
def get_cascade(self, cascade_name): self._cached_cascades[ cascade_name] = opencv.cvLoadHaarClassifierCascade( os.path.join(self.cascade_dir, cascade_name), opencv.cvSize(1, 1)) #cascade_name, opencv.cvSize(1,1)) return self._cached_cascades[cascade_name]
def __init__(self): conf = Configuration.get() hdir = conf['haar_classifiers_dir'] self.haarfiles = [ cv.cvLoadHaarClassifierCascade(str(hdir + h), cv.cvSize(10, 10)) for h in conf['haar_classifiers'] ] self.camera_devices = glob(conf['cameras_glob'])
def detect(image): # Find out how large the file is, as the underlying C-based code # needs to allocate memory in the following steps image_size = opencv.cvGetSize(image) # create grayscale version - this is also the point where the allegation about # facial recognition being racist might be most true. A caucasian face would have more # definition on a webcam image than an African face when greyscaled. # I would suggest that adding in a routine to overlay edge-detection enhancements may # help, but you would also need to do this to the training images as well. grayscale = opencv.cvCreateImage(image_size, 8, 1) opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY) # create storage (It is C-based so you need to do this sort of thing) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) # equalize histogram opencv.cvEqualizeHist(grayscale, grayscale) # detect objects - Haar cascade step # In this case, the code uses a frontal_face cascade - trained to spot faces that look directly # at the camera. In reality, I found that no bearded or hairy person must have been in the training # set of images, as the detection routine turned out to be beardist as well as a little racist! cascade = opencv.cvLoadHaarClassifierCascade('haarcascade_frontalface_alt.xml', opencv.cvSize(1,1)) faces = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) if faces: for face in faces: # Hmm should I do a min-size check? # Draw a Chartreuse rectangle around the face - Chartruese rocks opencv.cvRectangle(image, opencv.cvPoint( int(face.x), int(face.y)), opencv.cvPoint(int(face.x + face.width), int(face.y + face.height)), opencv.CV_RGB(127, 255, 0), 2) # RGB #7FFF00 width=2
def detectObject(self, classifier): self.grayscale = opencv.cvCreateImage(opencv.cvGetSize(self.iplimage), 8, 1) opencv.cvCvtColor(self.iplimage, self.grayscale, opencv.CV_BGR2GRAY) self.storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(self.storage) opencv.cvEqualizeHist(self.grayscale, self.grayscale) try: self.cascade = opencv.cvLoadHaarClassifierCascade(os.path.join(os.path.dirname(__file__), classifier+".xml"),opencv.cvSize(1, 1)) except: raise AttributeError("could not load classifier file") self.objects = opencv.cvHaarDetectObjects(self.grayscale, self.cascade, self.storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) return self.objects
def detectObject(self, classifier): self.grayscale = opencv.cvCreateImage(opencv.cvGetSize(self.iplimage), 8, 1) opencv.cvCvtColor(self.iplimage, self.grayscale, opencv.CV_BGR2GRAY) self.storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(self.storage) opencv.cvEqualizeHist(self.grayscale, self.grayscale) try: self.cascade = opencv.cvLoadHaarClassifierCascade(os.path.join(os.path.dirname(__file__), classifier+".xml"),opencv.cvSize(1,1)) except: raise AttributeError, "could not load classifier file" self.objects = opencv.cvHaarDetectObjects(self.grayscale, self.cascade, self.storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50,50)) return self.objects
def detectFaces(self): self._faces = [] frame = self._camera.getFrameAsIpl() storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) cascade = opencv.cvLoadHaarClassifierCascade(self._trainedHaar, opencv.cvSize(1, 1)) mugsht = opencv.cvHaarDetectObjects(frame, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(75, 75)) if mugsht: for mug in mugsht: face = [0, 0, 0, 0] face[0], face[1], face[2], face[ 3] = mug.x, mug.y, mug.width, mug.height self._faces.append(face)
def detectHaar(iplimage, classifier): srcimage = opencv.cvCloneImage(iplimage) grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1) opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) opencv.cvEqualizeHist(grayscale, grayscale) try: cascade = opencv.cvLoadHaarClassifierCascade(os.path.join(os.path.dirname(__file__), classifier + ".xml"), opencv.cvSize(1, 1)) except: raise AttributeError("could not load classifier file") objs = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50)) objects = [] for obj in objs: objects.append(Haarobj(obj)) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(grayscale) opencv.cvReleaseMemStorage(storage) return objects
def detectHaar(iplimage, classifier): srcimage = opencv.cvCloneImage(iplimage) grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1) opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY) storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(storage) opencv.cvEqualizeHist(grayscale, grayscale) try: cascade = opencv.cvLoadHaarClassifierCascade(os.path.join(os.path.dirname(__file__), classifier+".xml"),opencv.cvSize(1,1)) except: raise AttributeError, "could not load classifier file" objs = opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50,50)) objects = [] for obj in objs: objects.append(Haarobj(obj)) opencv.cvReleaseImage(srcimage) opencv.cvReleaseImage(grayscale) opencv.cvReleaseMemStorage(storage) return objects
def detectFaces( self ): self._faces = [] frame = self._camera.getFrameAsIpl() storage = opencv.cvCreateMemStorage( 0 ) opencv.cvClearMemStorage( storage ) cascade = opencv.cvLoadHaarClassifierCascade( self._trainedHaar, opencv.cvSize( 1, 1 ) ) mugsht = opencv.cvHaarDetectObjects( frame, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize( 75, 75 ) ) if mugsht: for mug in mugsht: face = [ 0, 0, 0, 0 ] face[0], face[1], face[2], face[3] = mug.x, mug.y, mug.width, mug.height self._faces.append( face )
def get_cascade(self, cascade_name): self._cached_cascades[cascade_name] = opencv.cvLoadHaarClassifierCascade( os.path.join(self.cascade_dir, cascade_name), opencv.cvSize(1,1)) #cascade_name, opencv.cvSize(1,1)) return self._cached_cascades[cascade_name]
def __init__(self,haarcascade="haarcascade_frontalface_alt.xml"): self.cascade = opencv.cvLoadHaarClassifierCascade(haarcascade,opencv.CvSize()) self.storage = opencv.cvCreateMemStorage(0) opencv.cvClearMemStorage(self.storage)