Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
Arquivo: fr.py Projeto: alien9/cam
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
Exemplo n.º 3
0
def findcontours(iplimage, threshold=100):
    srcimage = opencv.cvCloneImage(iplimage)    
    # create the storage area and bw image
    grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1)
    opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY)
    #threshold
    opencv.cvThreshold(grayscale, grayscale, threshold, 255, opencv.CV_THRESH_BINARY)
    storage = opencv.cvCreateMemStorage(0)
    opencv.cvClearMemStorage(storage)   
    # find the contours
    nb_contours, contours = opencv.cvFindContours (grayscale, storage)
    # comment this out if you do not want approximation
    contours = opencv.cvApproxPoly (contours, opencv.sizeof_CvContour, storage, opencv.CV_POLY_APPROX_DP, 3, 1)
    # next line is for ctypes-opencv
    #contours = opencv.cvApproxPoly (contours, opencv.sizeof(opencv.CvContour), storage, opencv.CV_POLY_APPROX_DP, 3, 1)
    conts = []
    for cont in contours.hrange():
        points=[]
        for pt in cont:
            points.append((pt.x,pt.y))                
        conts.append(points)
    opencv.cvReleaseMemStorage(storage)    
    opencv.cvReleaseImage(srcimage)
    opencv.cvReleaseImage(grayscale)
    return (nb_contours, conts)
Exemplo n.º 4
0
    def determine_next_position(self, image):

        self.image = image
        self.im = array2image(image)
        self.ipl_im = opencv.adaptors.PIL2Ipl(self.im)
        self.storage = opencv.cvCreateMemStorage(0)
        opencv.cvClearMemStorage(self.storage)
        self.cascade = opencv(
            '/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml',
            opencv.cvSize(1, 1))
        self.faces = opencv.cvHaarDetectObjects(
            self.ipl_im, self.cascade, self.storage, 1.2, 2,
            opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50, 50))

        if self.faces.total < 1:
            return None

        for f in self.faces:
            print "face detected: %s" % f
            #row and column are inverted in Opencv
            self.pos = (f.y, f.x)
            self.coormin_bbox = (self.pos[0], self.pos[1])
            self.coormax_bbox = (self.pos[0] + f.width, self.pos[1] + f.height)
            self.brightpixel = False
            return (self.pos, self.coormin_bbox, self.coormax_bbox,
                    self.brightpixel)
Exemplo n.º 5
0
	def get_frame(self, face_rec = False):
		
		image = highgui.cvQueryFrame(self.device)
		face_matches = False
		
		if face_rec:
			
			grayscale = cv.cvCreateImage(cv.cvSize(640, 480), 8, 1)
			cv.cvCvtColor(image, grayscale, cv.CV_BGR2GRAY)
			storage = cv.cvCreateMemStorage(0)
			cv.cvClearMemStorage(storage)
			cv.cvEqualizeHist(grayscale, grayscale)
			
			for cascade in self.haarfiles:
				matches = cv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, 
										  cv.CV_HAAR_DO_CANNY_PRUNING, cv.cvSize(100,100))
			  
				if matches:
					face_matches = True
					for i in matches:
						cv.cvRectangle(image, cv.cvPoint( int(i.x), int(i.y)),
							cv.cvPoint(int(i.x+i.width), int(i.y+i.height)),
							cv.CV_RGB(0,0,255), 2, 5, 0)
			
			image = cv.cvGetMat(image)
			
		return (image, face_matches)
Exemplo n.º 6
0
def findcontours(iplimage, threshold=100):
    srcimage = opencv.cvCloneImage(iplimage)
    # create the storage area and bw image
    grayscale = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 1)
    opencv.cvCvtColor(srcimage, grayscale, opencv.CV_BGR2GRAY)
    # threshold
    opencv.cvThreshold(grayscale, grayscale, threshold, 255, opencv.CV_THRESH_BINARY)
    storage = opencv.cvCreateMemStorage(0)
    opencv.cvClearMemStorage(storage)
    # find the contours
    nb_contours, contours = opencv.cvFindContours(grayscale, storage)
    # comment this out if you do not want approximation
    contours = opencv.cvApproxPoly(contours, opencv.sizeof_CvContour, storage, opencv.CV_POLY_APPROX_DP, 3, 1)
    # next line is for ctypes-opencv
    # contours = opencv.cvApproxPoly (contours, opencv.sizeof(opencv.CvContour), storage, opencv.CV_POLY_APPROX_DP, 3, 1)
    conts = []
    for cont in contours.hrange():
        points = []
        for pt in cont:
            points.append((pt.x, pt.y))
        conts.append(points)
    opencv.cvReleaseMemStorage(storage)
    opencv.cvReleaseImage(srcimage)
    opencv.cvReleaseImage(grayscale)
    return (nb_contours, conts)
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
    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
Exemplo n.º 9
0
 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        
Exemplo n.º 10
0
    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)
Exemplo n.º 11
0
    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 )
Exemplo n.º 12
0
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
Exemplo n.º 13
0
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 
Exemplo n.º 14
0
    def determine_next_position(self,image):

        self.image=image
        self.im=array2image(image)
        self.ipl_im = opencv.adaptors.PIL2Ipl(self.im)
        self.storage = opencv.cvCreateMemStorage(0)
        opencv.cvClearMemStorage(self.storage)
        self.cascade = opencv('/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml',opencv.cvSize(1,1))
        self.faces = opencv.cvHaarDetectObjects(self.ipl_im, self.cascade, self.storage, 1.2, 2,opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(50,50))

        if self.faces.total < 1:
            return None

        for f in self.faces:
            print "face detected: %s" %f
            #row and column are inverted in Opencv
            self.pos=(f.y,f.x)
            self.coormin_bbox=(self.pos[0],self.pos[1])
            self.coormax_bbox=(self.pos[0]+f.width,self.pos[1]+f.height)
            self.brightpixel=False
            return (self.pos,self.coormin_bbox,self.coormax_bbox,self.brightpixel)
Exemplo n.º 15
0
  def detect(self, pil_image, cascade_name, recogn_w = 50, recogn_h = 50):
    # Get cascade:
    cascade = self.get_cascade(cascade_name)

    image = opencv.PIL2Ipl(pil_image) 
    image_size = opencv.cvGetSize(image)
    grayscale = image
    if pil_image.mode == "RGB": 
      # create grayscale version
      grayscale = opencv.cvCreateImage(image_size, 8, 1)
      # Change to RGB2Gray - I dont think itll affect the conversion
      opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY)
 
    # create storage
    storage = opencv.cvCreateMemStorage(0)
    opencv.cvClearMemStorage(storage)
 
    # equalize histogram
    opencv.cvEqualizeHist(grayscale, grayscale)
 
    # detect objects
    return opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2, opencv.CV_HAAR_DO_CANNY_PRUNING, opencv.cvSize(recogn_w, recogn_h))
Exemplo n.º 16
0
    def detect(self, pil_image, cascade_name, recogn_w=50, recogn_h=50):
        # Get cascade:
        cascade = self.get_cascade(cascade_name)

        image = opencv.PIL2Ipl(pil_image)
        image_size = opencv.cvGetSize(image)
        grayscale = image
        if pil_image.mode == "RGB":
            # create grayscale version
            grayscale = opencv.cvCreateImage(image_size, 8, 1)
            # Change to RGB2Gray - I dont think itll affect the conversion
            opencv.cvCvtColor(image, grayscale, opencv.CV_BGR2GRAY)

        # create storage
        storage = opencv.cvCreateMemStorage(0)
        opencv.cvClearMemStorage(storage)

        # equalize histogram
        opencv.cvEqualizeHist(grayscale, grayscale)

        # detect objects
        return opencv.cvHaarDetectObjects(grayscale, cascade, storage, 1.2, 2,
                                          opencv.CV_HAAR_DO_CANNY_PRUNING,
                                          opencv.cvSize(recogn_w, recogn_h))
Exemplo n.º 17
0
 def __init__(self,haarcascade="haarcascade_frontalface_alt.xml"):
   self.cascade = opencv.cvLoadHaarClassifierCascade(haarcascade,opencv.CvSize())
   self.storage = opencv.cvCreateMemStorage(0)
   opencv.cvClearMemStorage(self.storage)