Example #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)
Example #2
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)
Example #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)
Example #4
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)
Example #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)
Example #6
0
File: fr.py Project: 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
Example #7
0
    def findFaces(self, img):
        faces = []

        self.detect_time = time.time()
        rects = self.face_detector.detect(img)
        self.detect_time = time.time() - self.detect_time

        cvtile = opencv.cvCreateMat(128, 128, opencv.CV_8UC3)
        bwtile = opencv.cvCreateMat(128, 128, opencv.CV_8U)

        cvimg = img.asOpenCV()

        self.eye_time = time.time()

        for rect in rects:
            faceim = opencv.cvGetSubRect(cvimg, rect.asOpenCV())
            opencv.cvResize(faceim, cvtile)

            affine = pv.AffineFromRect(rect, (128, 128))

            opencv.cvCvtColor(cvtile, bwtile, cv.CV_BGR2GRAY)

            leye, reye, lcp, rcp = self.fel.locateEyes(bwtile)
            leye = pv.Point(leye)
            reye = pv.Point(reye)

            leye = affine.invertPoint(leye)
            reye = affine.invertPoint(reye)

            faces.append([rect, leye, reye])

        self.eye_time = time.time() - self.eye_time
        self.current_faces = faces

        return faces
Example #8
0
def eigen_texture(cv_image, blocksize=8, filtersize=3):
    gray_image = cv.cvCreateImage(cv.cvSize(cv_image.width, cv_image.height), cv.IPL_DEPTH_8U, 1)
    eig_tex = cv.cvCreateImage(cv.cvSize(cv_image.width*6, cv_image.height), cv.IPL_DEPTH_32F, 1)
    
    cv.cvCvtColor(cv_image, gray_image, cv.CV_BGR2GRAY)    
    cv.cvCornerEigenValsAndVecs(gray_image, eig_tex, blocksize, filtersize)
    eig_tex_np = ut.cv2np(eig_tex)
            
    eig_tex_np = np.reshape(eig_tex_np, [cv_image.height, cv_image.width, 6])            
    return eig_tex_np[:,:,0:2]
Example #9
0
def eigen_texture(cv_image, blocksize=8, filtersize=3):
    gray_image = cv.cvCreateImage(cv.cvSize(cv_image.width, cv_image.height), cv.IPL_DEPTH_8U, 1)
    eig_tex = cv.cvCreateImage(cv.cvSize(cv_image.width * 6, cv_image.height), cv.IPL_DEPTH_32F, 1)

    cv.cvCvtColor(cv_image, gray_image, cv.CV_BGR2GRAY)
    cv.cvCornerEigenValsAndVecs(gray_image, eig_tex, blocksize, filtersize)
    eig_tex_np = ut.cv2np(eig_tex)

    eig_tex_np = np.reshape(eig_tex_np, [cv_image.height, cv_image.width, 6])
    return eig_tex_np[:, :, 0:2]
Example #10
0
def ipl2cairo(iplimage):
    srcimage = opencv.cvCloneImage(iplimage)
    width = srcimage.width
    height = srcimage.height 
    image = opencv.cvCreateImage(opencv.cvGetSize(srcimage),8, 4)
    opencv.cvCvtColor(srcimage,image,opencv.CV_BGR2BGRA)
    buffer = numpy.fromstring(image.imageData, dtype=numpy.uint32).astype(numpy.uint32)
    buffer.shape = (image.width, image.height)
    opencv.cvReleaseImage(srcimage)
    opencv.cvReleaseImage(image)    
    return cairo.ImageSurface.create_for_data(buffer, cairo.FORMAT_RGB24, width, height, width*4)
Example #11
0
def ipl2cairo(iplimage):
    srcimage = opencv.cvCloneImage(iplimage)
    width = srcimage.width
    height = srcimage.height
    image = opencv.cvCreateImage(opencv.cvGetSize(srcimage), 8, 4)
    opencv.cvCvtColor(srcimage, image, opencv.CV_BGR2BGRA)
    buffer = numpy.fromstring(image.imageData, dtype=numpy.uint32).astype(numpy.uint32)
    buffer.shape = (image.width, image.height)
    opencv.cvReleaseImage(srcimage)
    opencv.cvReleaseImage(image)
    return cairo.ImageSurface.create_for_data(buffer, cairo.FORMAT_RGB24, width, height, width * 4)
Example #12
0
def run():
    filename = 'C:\\Documents and Settings\\rmccormack\\Desktop\\work_projects\\openCV\\test\\test1.jpg'
    im = hg.cvLoadImage(filename)
    if not im:
        print "Error opening %s" % filename
        sys.exit(-1)
    im2 = opencv.cvCreateImage(opencv.cvGetSize(im), 8, 4)
    opencv.cvCvtColor(im, im2, opencv.CV_BGR2BGRA)
    buffer = numpy.fromstring(im2.imageData,
                              dtype=numpy.uint32).astype(numpy.float32)
    buffer.shape = (im2.width, im2.height)
    return buffer
Example #13
0
    def __init__(self, src="", time=None):

        self.src = src
        self.time = time
        if self.time:
            hg.cvSetCaptureProperty(self.src, hg.CV_CAP_PROP_POS_FRAMES, self.time)    
        self.iplimage = hg.cvQueryFrame(self.src)
        self.width = self.iplimage.width
        self.height = self.iplimage.height
        self.image = opencv.cvCreateImage(opencv.cvGetSize(self.iplimage),8, 4)
        opencv.cvCvtColor(self.iplimage,self.image,opencv.CV_BGR2BGRA)
        self.buffer = numpy.fromstring(self.image.imageData, dtype=numpy.uint32).astype(numpy.uint32)
        self.buffer.shape = (self.image.width, self.image.height)
        self.time = hg.cvGetCaptureProperty(self.src, hg.CV_CAP_PROP_POS_MSEC)
Example #14
0
    def __init__(self, src="", time=None):

        self.src = src
        self.time = time
        if self.time:
            hg.cvSetCaptureProperty(self.src, hg.CV_CAP_PROP_POS_FRAMES, self.time)
        self.iplimage = hg.cvQueryFrame(self.src)
        self.width = self.iplimage.width
        self.height = self.iplimage.height
        self.image = opencv.cvCreateImage(opencv.cvGetSize(self.iplimage), 8, 4)
        opencv.cvCvtColor(self.iplimage, self.image, opencv.CV_BGR2BGRA)
        self.buffer = numpy.fromstring(self.image.imageData, dtype=numpy.uint32).astype(numpy.uint32)
        self.buffer.shape = (self.image.width, self.image.height)
        self.time = hg.cvGetCaptureProperty(self.src, hg.CV_CAP_PROP_POS_MSEC)
Example #15
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        
Example #16
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
Example #17
0
    def Ipl2NumPy(input):
        """Converts an OpenCV/IPL image to a numpy array.
  
      Supported input image formats are
         IPL_DEPTH_8U  x 1 channel
         IPL_DEPTH_8U  x 3 channels
         IPL_DEPTH_32F x 1 channel
         IPL_DEPTH_32F x 2 channels
         IPL_DEPTH_32S x 1 channel
         IPL_DEPTH_64F x 1 channel
         IPL_DEPTH_64F x 2 channels
      """

        if not isinstance(input, cv.CvMat):
            raise TypeError, 'must be called with a cv.CvMat!'

        # data type dictionary:
        # (channels, depth) : numpy dtype
        ipl2dtype = {
            (1, cv.IPL_DEPTH_8U): numpy.uint8,
            (3, cv.IPL_DEPTH_8U): numpy.uint8,
            (1, cv.IPL_DEPTH_32F): numpy.float32,
            (2, cv.IPL_DEPTH_32F): numpy.float32,
            (1, cv.IPL_DEPTH_32S): numpy.int32,
            (1, cv.IPL_DEPTH_64F): numpy.float64,
            (2, cv.IPL_DEPTH_64F): numpy.float64
        }

        key = (input.nChannels, input.depth)
        if not ipl2dtype.has_key(key):
            raise ValueError, 'unknown or unsupported input mode'

        # Get the numpy array and reshape it correctly
        # ATTENTION: flipped dimensions width/height on 2007-11-15
        if input.nChannels == 1:
            array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key])
            return numpy.reshape(array_1d, (input.height, input.width))
        elif input.nChannels == 2:
            array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key])
            return numpy.reshape(array_1d, (input.height, input.width, 2))
        elif input.nChannels == 3:
            # Change the order of channels from BGR to RGB
            rgb = cv.cvCreateImage(cv.cvSize(input.width, input.height),
                                   input.depth, 3)
            cv.cvCvtColor(input, rgb, cv.CV_BGR2RGB)
            array_1d = numpy.fromstring(rgb.imageData, dtype=ipl2dtype[key])
            return numpy.reshape(array_1d, (input.height, input.width, 3))
Example #18
0
 def __init__(self, frame):
     self.blob_image = opencv.cvCloneImage(frame.iplimage)
     self.blob_gray = opencv.cvCreateImage(opencv.cvGetSize(self.blob_image), 8, 1)
     self.blob_mask = opencv.cvCreateImage(opencv.cvGetSize(self.blob_image), 8, 1)
     opencv.cvSet(self.blob_mask, 1)
     opencv.cvCvtColor(self.blob_image, self.blob_gray, opencv.CV_BGR2GRAY)
     # opencv.cvEqualizeHist(self.blob_gray, self.blob_gray)
     # opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.thresh, 255, opencv.CV_THRESH_BINARY)
     # opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.thresh, 255, opencv.CV_THRESH_TOZERO)
     opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.bthresh, 255, frame.bthreshmode)
     # opencv.cvAdaptiveThreshold(self.blob_gray, self.blob_gray, 255, opencv.CV_ADAPTIVE_THRESH_MEAN_C, opencv.CV_THRESH_BINARY_INV)
     self._frame_blobs = CBlobResult(self.blob_gray, self.blob_mask, 100, True)
     self._frame_blobs.filter_blobs(10, 10000)
     self.count = self._frame_blobs.GetNumBlobs()
     self.items = []
     for i in range(self.count):
         self.items.append(self._frame_blobs.GetBlob(i))
Example #19
0
 def __init__(self, frame):
     self.blob_image = opencv.cvCloneImage(frame.iplimage)
     self.blob_gray = opencv.cvCreateImage(opencv.cvGetSize(self.blob_image), 8, 1)
     self.blob_mask = opencv.cvCreateImage(opencv.cvGetSize(self.blob_image), 8, 1)
     opencv.cvSet(self.blob_mask, 1)        
     opencv.cvCvtColor(self.blob_image, self.blob_gray, opencv.CV_BGR2GRAY)
     #opencv.cvEqualizeHist(self.blob_gray, self.blob_gray)
     #opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.thresh, 255, opencv.CV_THRESH_BINARY)	
     #opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.thresh, 255, opencv.CV_THRESH_TOZERO)
     opencv.cvThreshold(self.blob_gray, self.blob_gray, frame.bthresh, 255, frame.bthreshmode)
     #opencv.cvAdaptiveThreshold(self.blob_gray, self.blob_gray, 255, opencv.CV_ADAPTIVE_THRESH_MEAN_C, opencv.CV_THRESH_BINARY_INV)
     self._frame_blobs = CBlobResult(self.blob_gray, self.blob_mask, 100, True)
     self._frame_blobs.filter_blobs(10, 10000)
     self.count = self._frame_blobs.GetNumBlobs()
     self.items = []
     for i in range(self.count):
         self.items.append(self._frame_blobs.GetBlob(i)) 
Example #20
0
 def Ipl2NumPy(input):
     """Converts an OpenCV/IPL image to a numpy array.
 
     Supported input image formats are
        IPL_DEPTH_8U  x 1 channel
        IPL_DEPTH_8U  x 3 channels
        IPL_DEPTH_32F x 1 channel
        IPL_DEPTH_32F x 2 channels
        IPL_DEPTH_32S x 1 channel
        IPL_DEPTH_64F x 1 channel
        IPL_DEPTH_64F x 2 channels
     """
     
     if not isinstance(input, cv.CvMat):
         raise TypeError, 'must be called with a cv.CvMat!'
           
     # data type dictionary:
     # (channels, depth) : numpy dtype
     ipl2dtype = {
         (1, cv.IPL_DEPTH_8U)  : numpy.uint8,
         (3, cv.IPL_DEPTH_8U)  : numpy.uint8,
         (1, cv.IPL_DEPTH_32F) : numpy.float32,
         (2, cv.IPL_DEPTH_32F) : numpy.float32,
         (1, cv.IPL_DEPTH_32S) : numpy.int32,
         (1, cv.IPL_DEPTH_64F) : numpy.float64,
         (2, cv.IPL_DEPTH_64F) : numpy.float64
         }
     
     key = (input.nChannels, input.depth)
     if not ipl2dtype.has_key(key):
         raise ValueError, 'unknown or unsupported input mode'
     
     # Get the numpy array and reshape it correctly
     # ATTENTION: flipped dimensions width/height on 2007-11-15
     if input.nChannels == 1:
         array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key])
         return numpy.reshape(array_1d, (input.height, input.width))
     elif input.nChannels == 2:
         array_1d = numpy.fromstring(input.imageData, dtype=ipl2dtype[key])
         return numpy.reshape(array_1d, (input.height, input.width, 2))
     elif input.nChannels == 3:
         # Change the order of channels from BGR to RGB
         rgb = cv.cvCreateImage(cv.cvSize(input.width, input.height), input.depth, 3)
         cv.cvCvtColor(input, rgb, cv.CV_BGR2RGB)
         array_1d = numpy.fromstring(rgb.imageData, dtype=ipl2dtype[key])
         return numpy.reshape(array_1d, (input.height, input.width, 3))
Example #21
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
Example #22
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 
Example #23
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))
    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))
    def prepare(self, features_k_nearest_neighbors, nonzero_indices = None, all_save_load = False, regenerate_neightborhood_indices = False):
        #print np.shape(self.processor.pts3d_bound), 'shape pts3d_bound'

        imgTmp = cv.cvCloneImage(self.processor.img)
        self.imNP = ut.cv2np(imgTmp,format='BGR')
        ###self.processor.map2d = np.asarray(self.processor.camPts_bound) #copied from laser to image mapping
        
        if features_k_nearest_neighbors == None or features_k_nearest_neighbors == False: #use range
            self.kdtree2d = kdtree.KDTree(self.processor.pts3d_bound.T)
            
            #print len(nonzero_indices)
            #print np.shape(np.asarray((self.processor.pts3d_bound.T)[nonzero_indices]))
            
            if nonzero_indices != None:
                print ut.getTime(), 'query ball tree for ', len(nonzero_indices), 'points'
                kdtree_query = kdtree.KDTree((self.processor.pts3d_bound.T)[nonzero_indices])
            else:
                print ut.getTime(), 'query ball tree'
                kdtree_query = kdtree.KDTree(self.processor.pts3d_bound.T)
            
            filename = self.processor.config.path+'/data/'+self.processor.scan_dataset.id+'_sphere_neighborhood_indices_'+str(self.processor.feature_radius)+'.pkl'
            if all_save_load == True and os.path.exists(filename) and regenerate_neightborhood_indices == False:
                #if its already there, load it:
                print ut.getTime(), 'loading',filename
                self.kdtree_queried_indices = ut.load_pickle(filename)    
            else:
                self.kdtree_queried_indices = kdtree_query.query_ball_tree(self.kdtree2d, self.processor.feature_radius, 2.0, 0.2) #approximate
                print ut.getTime(), 'queried kdtree: ',len(self.kdtree_queried_indices),'points, radius:',self.processor.feature_radius
                if all_save_load == True:
                    ut.save_pickle(self.kdtree_queried_indices, filename)
                    
            #make dict out of list for faster operations? (doesn't seem to change speed significantly):
            #self.kdtree_queried_indices = dict(zip(xrange(len(self.kdtree_queried_indices)), self.kdtree_queried_indices))
        
        else: #experiemental: use_20_nearest_neighbors == True
            #TODO: exclude invalid values in get_featurevector (uncomment code there)
           
            self.kdtree2d = kdtree.KDTree(self.processor.pts3d_bound.T)
            self.kdtree_queried_indices = []
            print ut.getTime(), 'kdtree single queries for kNN start, k=', features_k_nearest_neighbors
            count = 0
            for point in ((self.processor.pts3d_bound.T)[nonzero_indices]):
                count = count + 1
                result = self.kdtree2d.query(point, features_k_nearest_neighbors,0.2,2,self.processor.feature_radius)
                #existing = result[0][0] != np.Inf
                #print existing
                #print result[1]
                self.kdtree_queried_indices += [result[1]] #[existing]
                if count % 4096 == 0:
                    print ut.getTime(),count
            print ut.getTime(), 'kdtree singe queries end'
            
            #convert to numpy array -> faster access
            self.kdtree_queried_indices = np.asarray(self.kdtree_queried_indices)
        
        #print self.kdtree_queried_indices
        #takes long to compute:
        #avg_len = 0
        #minlen = 999999
        #maxlen = 0
        #for x in self.kdtree_queried_indices:
        #    avg_len += len(x)
        #    minlen = min(minlen, len(x))
        #    maxlen = max(maxlen, len(x))
        #avg_len = avg_len / len(self.kdtree_queried_indices)
        #print ut.getTime(), "range neighbors: avg_len", avg_len, 'minlen', minlen, 'maxlen', maxlen
        
        
        #create HSV numpy images:
        # compute the hsv version of the image 
        image_size = cv.cvGetSize(self.processor.img)
        img_h = cv.cvCreateImage (image_size, 8, 1)
        img_s = cv.cvCreateImage (image_size, 8, 1)
        img_v = cv.cvCreateImage (image_size, 8, 1)
        img_hsv = cv.cvCreateImage (image_size, 8, 3)
        
        cv.cvCvtColor (self.processor.img, img_hsv, cv.CV_BGR2HSV)
        
        cv.cvSplit (img_hsv, img_h, img_s, img_v, None)
        self.imNP_h = ut.cv2np(img_h)
        self.imNP_s = ut.cv2np(img_s)
        self.imNP_v = ut.cv2np(img_v)
        
        textures = texture_features.eigen_texture(self.processor.img)
        self.imNP_tex1 = textures[:,:,0]
        self.imNP_tex2 = textures[:,:,1]
        
        self.debug_before_first_featurevector = True
        
        self.generate_voi_histogram(self.processor.point_of_interest,self.processor.voi_width)
Example #26
0
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
## @author Hai Nguyen/[email protected]
##@package image_broadcaster
# Broadcast image given on command line to topic 'image'.

import rostools
rostools.updatePath('gmmseg')
import rospy
import std_msgs.msg.Image as RImage 
import opencv.highgui as hg
import opencv as cv
import sys
import time

rospy.ready(sys.argv[0])
pub_channel = rospy.TopicPub('image', RImage)
cv_image    = hg.cvLoadImage(sys.argv[1])
cv.cvCvtColor(cv_image, cv_image, cv.CV_BGR2RGB )

count = 1
while not rospy.isShutdown():
    image = RImage(None, 'static', cv_image.width, cv_image.height, 
            '', 'rgb24', cv_image.imageData)
    pub_channel.publish(image)
    print 'published', count
    count = count + 1
    time.sleep(.3)
    def prepare(self,
                features_k_nearest_neighbors,
                nonzero_indices=None,
                all_save_load=False,
                regenerate_neightborhood_indices=False):
        #print np.shape(self.processor.pts3d_bound), 'shape pts3d_bound'

        imgTmp = cv.cvCloneImage(self.processor.img)
        self.imNP = ut.cv2np(imgTmp, format='BGR')
        ###self.processor.map2d = np.asarray(self.processor.camPts_bound) #copied from laser to image mapping

        if features_k_nearest_neighbors == None or features_k_nearest_neighbors == False:  #use range
            self.kdtree2d = kdtree.KDTree(self.processor.pts3d_bound.T)

            #print len(nonzero_indices)
            #print np.shape(np.asarray((self.processor.pts3d_bound.T)[nonzero_indices]))

            if nonzero_indices != None:
                print ut.getTime(), 'query ball tree for ', len(
                    nonzero_indices), 'points'
                kdtree_query = kdtree.KDTree(
                    (self.processor.pts3d_bound.T)[nonzero_indices])
            else:
                print ut.getTime(), 'query ball tree'
                kdtree_query = kdtree.KDTree(self.processor.pts3d_bound.T)

            filename = self.processor.config.path + '/data/' + self.processor.scan_dataset.id + '_sphere_neighborhood_indices_' + str(
                self.processor.feature_radius) + '.pkl'
            if all_save_load == True and os.path.exists(
                    filename) and regenerate_neightborhood_indices == False:
                #if its already there, load it:
                print ut.getTime(), 'loading', filename
                self.kdtree_queried_indices = ut.load_pickle(filename)
            else:
                self.kdtree_queried_indices = kdtree_query.query_ball_tree(
                    self.kdtree2d, self.processor.feature_radius, 2.0,
                    0.2)  #approximate
                print ut.getTime(), 'queried kdtree: ', len(
                    self.kdtree_queried_indices
                ), 'points, radius:', self.processor.feature_radius
                if all_save_load == True:
                    ut.save_pickle(self.kdtree_queried_indices, filename)

            #make dict out of list for faster operations? (doesn't seem to change speed significantly):
            #self.kdtree_queried_indices = dict(zip(xrange(len(self.kdtree_queried_indices)), self.kdtree_queried_indices))

        else:  #experiemental: use_20_nearest_neighbors == True
            #TODO: exclude invalid values in get_featurevector (uncomment code there)

            self.kdtree2d = kdtree.KDTree(self.processor.pts3d_bound.T)
            self.kdtree_queried_indices = []
            print ut.getTime(
            ), 'kdtree single queries for kNN start, k=', features_k_nearest_neighbors
            count = 0
            for point in ((self.processor.pts3d_bound.T)[nonzero_indices]):
                count = count + 1
                result = self.kdtree2d.query(point,
                                             features_k_nearest_neighbors, 0.2,
                                             2, self.processor.feature_radius)
                #existing = result[0][0] != np.Inf
                #print existing
                #print result[1]
                self.kdtree_queried_indices += [result[1]]  #[existing]
                if count % 4096 == 0:
                    print ut.getTime(), count
            print ut.getTime(), 'kdtree singe queries end'

            #convert to numpy array -> faster access
            self.kdtree_queried_indices = np.asarray(
                self.kdtree_queried_indices)

        #print self.kdtree_queried_indices
        #takes long to compute:
        #avg_len = 0
        #minlen = 999999
        #maxlen = 0
        #for x in self.kdtree_queried_indices:
        #    avg_len += len(x)
        #    minlen = min(minlen, len(x))
        #    maxlen = max(maxlen, len(x))
        #avg_len = avg_len / len(self.kdtree_queried_indices)
        #print ut.getTime(), "range neighbors: avg_len", avg_len, 'minlen', minlen, 'maxlen', maxlen

        #create HSV numpy images:
        # compute the hsv version of the image
        image_size = cv.cvGetSize(self.processor.img)
        img_h = cv.cvCreateImage(image_size, 8, 1)
        img_s = cv.cvCreateImage(image_size, 8, 1)
        img_v = cv.cvCreateImage(image_size, 8, 1)
        img_hsv = cv.cvCreateImage(image_size, 8, 3)

        cv.cvCvtColor(self.processor.img, img_hsv, cv.CV_BGR2HSV)

        cv.cvSplit(img_hsv, img_h, img_s, img_v, None)
        self.imNP_h = ut.cv2np(img_h)
        self.imNP_s = ut.cv2np(img_s)
        self.imNP_v = ut.cv2np(img_v)

        textures = texture_features.eigen_texture(self.processor.img)
        self.imNP_tex1 = textures[:, :, 0]
        self.imNP_tex2 = textures[:, :, 1]

        self.debug_before_first_featurevector = True

        self.generate_voi_histogram(self.processor.point_of_interest,
                                    self.processor.voi_width)
Example #28
0
    if mode == MODE_DETECT_OBSTACLES:
      screen.fill(white)
      queryWebcam()
      if time.time() > timer + 0.5:
        switchMode(MODE_DETECT_OBSTACLES2)
    elif mode == MODE_DETECT_OBSTACLES3:
      screen.fill(white)
      queryWebcam()
      if time.time() > timer + 0.5:
        action = ""
        switchMode(MODE_READY)
    elif mode == MODE_DETECT_OBSTACLES2:
      before = time.time()
      screen.fill(white)
      im = queryWebcam()
      opencv.cvCvtColor (im, igray, opencv.CV_BGR2GRAY)
      opencv.cvSmooth(igray, igray, opencv.CV_GAUSSIAN, 3, 3)
      opencv.cvAdaptiveThreshold(igray, iwhite, 255, opencv.CV_ADAPTIVE_THRESH_GAUSSIAN_C)
      num, contours = opencv.cvFindContours (iwhite, stor, opencv.sizeof_CvContour, opencv.CV_RETR_LIST)
  
      opencv.cvCvtColor(iwhite, im, opencv.CV_GRAY2BGR)
      staticImage = im

      retrieveObstacles(contours)
      switchMode(MODE_DETECT_OBSTACLES3)
      action = "Detection took: %.2f" % (time.time()-before)

    elif mode == MODE_READY:
      screen.fill(lgray)
      im  = queryWebcam()
      opencv.cvCvtColor(im, igray, opencv.CV_BGR2GRAY)
def detect_and_draw(image):
    image_size = hg.cvGetSize(image)
    grayscale = cv.cvCreateImage(image_size, 8, 1)
    cv.cvCvtColor(image, grayscale, CV_RGB2GRAY)
    return grayscale
Example #30
0
 def hsv2rgb(img,copy=True):
   res=img.copy()
   opencv.cvCvtColor (img, res,opencv.CV_HSV2RGB)
   return res
Example #31
0
 def toGrayscale(self):
   grayscale = opencv.cvCreateImage(self.details.size(), 8, 1)
   opencv.cvCvtColor(self.image, grayscale, 6)
   opencv.cvEqualizeHist(grayscale, grayscale)
   return grayscale
Example #32
0
 def rgb2hsv(img,copy=True):
   res=img.copy()
   #print img, res, opencv.CV_RGB2HSV
   opencv.cvCvtColor (img, res, opencv.CV_RGB2HSV)
   return res
Example #33
0
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
## @author Hai Nguyen/[email protected]
##@package image_broadcaster
# Broadcast image given on command line to topic 'image'.

import rostools
rostools.updatePath('gmmseg')
import rospy
import std_msgs.msg.Image as RImage
import opencv.highgui as hg
import opencv as cv
import sys
import time

rospy.ready(sys.argv[0])
pub_channel = rospy.TopicPub('image', RImage)
cv_image = hg.cvLoadImage(sys.argv[1])
cv.cvCvtColor(cv_image, cv_image, cv.CV_BGR2RGB)

count = 1
while not rospy.isShutdown():
    image = RImage(None, 'static', cv_image.width, cv_image.height, '',
                   'rgb24', cv_image.imageData)
    pub_channel.publish(image)
    print 'published', count
    count = count + 1
    time.sleep(.3)