def _readEyesFile(self): ''' Private: Do not call directly. Reads the eye file. ''' if self.filename[-4:] == '.csv': f = open(self.filename, 'r') for line in f: #print line line = line.split(',') if len(line) < 5: continue for i in range(1, len(line), 4): fname = self._parseName(line[0]) if len(line) < i + 4: print "Warning in %s image %s: Count of numbers is not a multiple of four." % ( self.filename, fname) break eye1 = Point(float(line[i + 0]), float(line[i + 1])) eye2 = Point(float(line[i + 2]), float(line[i + 3])) truth_rect = BoundingRect(eye1, eye2) truth_rect.w = 2.0 * truth_rect.w truth_rect.h = truth_rect.w truth_rect.x = truth_rect.x - 0.25 * truth_rect.w truth_rect.y = truth_rect.y - 0.3 * truth_rect.w #print fname,eye1,eye2,truth_rect if not self.images.has_key(fname): self.images[fname] = [] self.images[fname].append([fname, eye1, eye2, truth_rect]) else: f = open(self.filename, 'r') for line in f: #print line, line = line.split() fname = self._parseName(line[0]) eye1 = Point(float(line[1]), float(line[2])) eye2 = Point(float(line[3]), float(line[4])) truth_rect = BoundingRect(eye1, eye2) truth_rect.w = 2.0 * truth_rect.w truth_rect.h = truth_rect.w truth_rect.x = truth_rect.x - 0.25 * truth_rect.w truth_rect.y = truth_rect.y - 0.3 * truth_rect.w #print fname,eye1,eye2,truth_rect if not self.images.has_key(fname): self.images[fname] = [] self.images[fname].append([fname, eye1, eye2, truth_rect])
def test_translate(self): transform = AffineTranslate(10.,15.,(640,480)) im = transform.transformImage(self.test_image) #im.show() pt = transform.transformPoint(Point(320,240)) self.assertAlmostEqual(pt.X(),330.) self.assertAlmostEqual(pt.Y(),255.) pt = transform.invertPoint(Point(320,240)) self.assertAlmostEqual(pt.X(),310.) self.assertAlmostEqual(pt.Y(),225.)
def test_scale(self): transform = AffineScale(1.5,(640,480)) im = transform.transformImage(self.test_image) #im.show() pt = transform.transformPoint(Point(320,240)) self.assertAlmostEqual(pt.X(),480.) self.assertAlmostEqual(pt.Y(),360.) pt = transform.invertPoint(Point(320,240)) self.assertAlmostEqual(pt.X(),213.33333333333331) self.assertAlmostEqual(pt.Y(),160.)
def test_rotation(self): transform = AffineRotate(3.14/8,(640,480)) im = transform.transformImage(self.test_image) # im.show() pt = transform.transformPoint(Point(320,240)) self.assertAlmostEqual(pt.X(),203.86594448424472) self.assertAlmostEqual(pt.Y(),344.14920700118842) pt = transform.invertPoint(Point(320,240)) self.assertAlmostEqual(pt.X(),387.46570317672939) self.assertAlmostEqual(pt.Y(),99.349528744542198)
def test_from_rect(self): transform = AffineFromRect(Rect(100,100,300,300),(100,100)) im = transform.transformImage(self.test_image) #im.show() pt = transform.transformPoint(Point(320,240)) self.assertAlmostEqual(pt.X(),73.333333333333329) self.assertAlmostEqual(pt.Y(),46.666666666666671) pt = transform.invertPoint(Point(50.,50.)) self.assertAlmostEqual(pt.X(),250.0) self.assertAlmostEqual(pt.Y(),250.0)
def _readEyesFile(self): ''' Private: Do not call directly. Reads the eye file. ''' if self.filename[-4:] == '.csv': f = open(self.filename, 'r') for line in f: #print line, line = line.split(',') fname = self._parseName(line[0]) eye1 = Point(float(line[1]), float(line[2])) eye2 = Point(float(line[3]), float(line[4])) truth_rect = BoundingRect(eye1, eye2) truth_rect.w = 2.0 * truth_rect.w truth_rect.h = truth_rect.w truth_rect.x = truth_rect.x - 0.25 * truth_rect.w truth_rect.y = truth_rect.y - 0.3 * truth_rect.w #print fname,eye1,eye2,truth_rect if not self.images.has_key(fname): self.images[fname] = [] self.images[fname].append([fname, eye1, eye2, truth_rect]) else: f = open(self.filename, 'r') for line in f: #print line, line = line.split() fname = self._parseName(line[0]) eye1 = Point(float(line[1]), float(line[2])) eye2 = Point(float(line[3]), float(line[4])) truth_rect = BoundingRect(eye1, eye2) truth_rect.w = 2.0 * truth_rect.w truth_rect.h = truth_rect.w truth_rect.x = truth_rect.x - 0.25 * truth_rect.w truth_rect.y = truth_rect.y - 0.3 * truth_rect.w #print fname,eye1,eye2,truth_rect if not self.images.has_key(fname): self.images[fname] = [] self.images[fname].append([fname, eye1, eye2, truth_rect])
def transformPoint(self,pt): ''' Transform a point from the old image to the new image. @param pt: the point @returns: the new point ''' vec = dot(self.matrix,pt.asVector2H()) return Point(x=vec[0,0],y=vec[1,0],w=vec[2,0])
def train(self,**kwargs): # compute the mean location self.x_svm.train(**kwargs) self.y_svm.train(**kwargs) cx = self.x_sum/self.point_count cy = self.y_sum/self.point_count self.mean = Point(cx,cy)
def __call__(self, im): tmp = im.asPIL() tmp = tmp.resize((160, 120)) tmp = Image(tmp) points = self.dog.detect(tmp) for score, pt, radius in points: pt = Point(pt.X() * 4, pt.Y() * 4) im.annotateCircle(pt, radius * 4) return im
def invertPoint(self,pt): ''' Transforms a Point from the new coordinate system to the old coordinate system. @param pt: a single point @returns: the transformed point ''' vec = dot(self.inverse,pt.asVector2H()) return Point(x=vec[0,0],y=vec[1,0],w=vec[2,0])
def detect(self,image,**kwargs): ''' Returns a list of region of interest. Each element in the list is a tuple of (score,centerpoint,radius). Radius of "None" is used for point detectors. Higher scores are better and scores of "None" indicate no score is avalible. ''' # TODO: Call subclass A = None if isinstance(image,Image): A = image.asMatrix2D() elif isinstance(image,array) and len(image.shape)==2: A = image else: raise TypeError("ERROR Unknown Type (%s) - Only arrays and pyvision images supported."%type(image)) L = self._detect(image,**kwargs) L.sort() L.reverse() if self.selector == 'best': L=L[:self.n] elif self.selector == 'bins': nbins = A.shape[0]/self.bin_size*A.shape[1]/self.bin_size npts = self.n / nbins + 1 corners = [] for xmin in range(0,A.shape[0],self.bin_size): xmax = xmin + self.bin_size for ymin in range(0,A.shape[1],self.bin_size): bin = [] ymax = ymin + self.bin_size for each in L: #print each if xmin <= each[1] and each[1] < xmax and ymin <= each[2] and each[2] < ymax: bin.append(each) if len(bin) >= npts: break corners += bin L = corners else: # TODO: assume all pass roi = [] for each in L: roi.append([each[0],Point(each[1],each[2]),each[3]]) #L = concatenate((L.transpose,ones((1,L.shape[0])))) return roi
def __call__(self, im): tmp = im.asPIL() tmp = tmp.resize((160, 120)) tmp = Image(tmp) points = self.surf.detect(tmp) for score, pt, radius in points: score = score - 500.0 if score > 500.0: score = 500.0 score = int(255 * score / 500.0) color = "#%02x0000" % score pt = Point(pt.X() * 4, pt.Y() * 4) im.annotateCircle(pt, radius, color=color) return im
def __init__(self, face_size=(128, 128), left_eye=Point(32, 52), right_eye=Point(96, 52), normalize=PCA_MEAN_STD_NORM, measure=PCA_COS, whiten=True, drop_front=2, basis_vectors=100): '''Crate a PCA classifier''' FaceRecognizer.__init__(self) self.face_size = face_size self.pca = pyvision.vector.PCA.PCA() self.norm = normalize self.trained = False self.whiten = whiten self.drop_front = drop_front self.basis_vectors = basis_vectors self.measure = measure self.left_eye = left_eye self.right_eye = right_eye
def AffineNormalizePoints(points): ''' Create a transform that centers a set of points such that there mean is (0,0) and then scale such that there average distance from (0,0) is 1.0 @param points: list of link.Point to normalize @returns: an AffineTransform object ''' # compute the center mean = Point(0,0) count = 0 for point in points: mean += point count += 1 mean = (1.0/count)*mean # mean center the points center = AffineTranslate(-mean.X(),-mean.Y(),(0,0)) points = center.transformPoints(points) # Compute the mean distance mean_dist = 0.0 count = 0 for point in points: x,y = point.X(),point.Y() dist = sqrt(x*x+y*y) mean_dist += dist count += 1 mean_dist = (1.0/count)*mean_dist # Rescale the points scale = AffineScale(1.0/mean_dist,(0,0)) points = scale.transformPoints(points) # compute the composite transform norm = scale*center return norm
def predict(self,image): x = self.x_svm.predict(image) y = self.y_svm.predict(image) return Point(x,y)