def readInASM(self, asm): allLines = None #f = "C:\\Users\\Valerie\\Documents\\Visual Studio 2013\\Projects\\ActiveShapeModels\\ActiveShapeModels\\outputs\\outfile-ASM-100iters-500tr.txt" with open( os.path.join( self.output, 'outfile-ASM-%diters-%dtr.txt' % (self.nIters, self.nTrain)), "r") as infile: #with open( f, "r") as infile: allLines = infile.readlines() cleanLines = map(lambda x: x.strip().split(), allLines) s = [] for tuple in cleanLines: if tuple[0] == '!!!': if s != []: asm.meanShape = ActiveShape(s) s = [] else: pass elif tuple[0] == '@@@': if s != []: asm.addShape(ActiveShape(s)) s = [] else: pass else: s.append(Vector(float(tuple[0]), float(tuple[1]))) return asm
def calcNormScale(self, shape): if self.n == 68: d = Point.dist(shape.shapePoints[self.leftEyeIx], shape.shapePoints[self.rightEyeIx]) else: rc = ActiveShape.centroid(ActiveShape(shape.shapePoints[31:35])) lc = ActiveShape.centroid(ActiveShape(shape.shapePoints[27:31])) d = Point.dist(rc, lc) s = float(1) / float(d) return s
def readInPoints(self, asm): m = 0 for f in self.pointFiles: ptList = self.readInOneDude(f) if self.doExclImgs: if m in (set(range(self.nTrain)) - set(self.exclImgs)): asm.addShape(ActiveShape(ptList)) else: asm.addShape(ActiveShape(ptList)) m += 1 if m > self.nTrain - 1: return asm
def calcMeanShape(self): xList = [el.xs for el in self.allShapes] yList = [el.ys for el in self.allShapes] meanPointsList = map(lambda x, y: Vector(x, y), np.mean(xList, 0), np.mean(yList, 0)) # meanPointsList = zip( np.mean(xList, 0), np.mean(yList, 0) ) return ActiveShape(meanPointsList)
def alignEyes(self, eye1, eye2): x = ActiveShape.createShape(self.x) f, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2) # distance between eyes: d1 = Point.dist(eye1, eye2) rc = ActiveShape.centroid(ActiveShape(x.shapePoints[31:35])) lc = ActiveShape.centroid(ActiveShape(x.shapePoints[27:31])) if self.asm.n == 68: d2 = Point.dist(x.shapePoints[self.asm.rightEyeIx], x.shapePoints[self.asm.leftEyeIx]) else: d2 = Point.dist(rc, lc) s = float(d1 / d2) shape = copy.deepcopy(x) DrawFace(shape, ax1).drawBold() shape = shape.scale(s) DrawFace(shape, ax2).drawBold() rot, thetaRot = self.asm.calcNormRotateImg(shape) shape = shape.rotate(rot) DrawFace(shape, ax3).drawBold() ax1.invert_yaxis() ax2.invert_yaxis() ax3.invert_yaxis() rc = ActiveShape.centroid(ActiveShape(shape.shapePoints[31:35])) lc = ActiveShape.centroid(ActiveShape(shape.shapePoints[27:31])) if self.asm.n == 68: t = [[(eye2.x - shape.shapePoints[self.asm.leftEyeIx].x)], [(eye2.y - shape.shapePoints[self.asm.leftEyeIx].y)]] else: t = [[(eye2.x - rc.x)], [(eye2.y - rc.y)]] shape = shape.translate(t) ### Check that initial shape is within image frame tempS = 0 nr, nc = np.shape(self.img) for pt in shape.shapePoints: if pt.y > nr: # print "y big" tempS += (pt.y - nr + 10) / nr if pt.x > nc: # print "x big" tempS += (pt.x - nc + 10) / nc if tempS != 0: shape = shape.scale(1 - tempS) if self.asm.n == 68: t = [[(eye2.x - shape.shapePoints[self.asm.leftEyeIx].x)], [(eye2.y - shape.shapePoints[self.asm.leftEyeIx].y)]] else: rc = ActiveShape.centroid(ActiveShape( shape.shapePoints[31:35])) lc = ActiveShape.centroid(ActiveShape( shape.shapePoints[27:31])) t = [[(eye2.x - rc.x)], [(eye2.y - rc.y)]] shape = shape.translate(t) #print "row: %d\tcol:%d" % ( pt.y, pt.x ) # print np.shape(self.img ) DrawFace(shape, ax4).drawBold() ax4.scatter(eye1.x, eye1.y, c='r') ax4.scatter(eye2.x, eye2.y, c='g') ax4.imshow(self.img, cmap='gray') f.show() plt.savefig(os.path.join(self.out, "deform-init.png")) plt.gca().invert_yaxis() plt.close() srot = np.dot(s, rot) transDict = { 't': t, 's': s, 'rot': rot, 'srot': srot, 'theta': thetaRot } return shape, transDict
def coordOffset( pt, n ): y = pt.y #row x = pt.x #col return x - ( n - 1)/2, y - (n-1)/1 #def run( ): i = 20 tr = 500 out = "C:\\Users\\Valerie\\Desktop\\output\\20-500-1" fh = FileHelper( i, tr, out ) ebenFace = fh.readInImage() ebenPoints = fh.readInOneDude( '000_1_1.pts') ebenShape = ActiveShape( ebenPoints ) #### MATCHING PROCESS # Read in image I = cv2.imread( "C:\Users\Valerie\Desktop\MicroExpress\CASME2\CASME2_RAW\CASME2-RAW\sub01\EP02_01f\img1.jpg") I = cv2.cvtColor( I, cv2.COLOR_BGR2GRAY) # Align shape asm = ActiveShapeModel( [36,31] ) asm = fh.readInASM( asm ) asm.PCA() appASM = ApplyASM( asm, i, tr, out, I ) m, tdict = appASM.initialPosition( ) def genTemplateArr( ):
s = 1.2 t = [[-2],[4] ] rMat = [ [ s * math.cos( r ), - s* math.sin( r ) ] , [ s * math.sin( r ), s * math.cos( r ) ]] def p( x, y ): return [[x],[y]] p1 = p( 1,1 ) p2 = p( 3,4 ) p3 = p( 4,2 ) def trans( p, rMat, t ): return np.dot( rMat, p ) + t n1 = trans( p1, rMat, t) n2 = trans( p2, rMat, t) n3 = trans( p3, rMat, t) AS1 = ActiveShape( [ (1,1), (3,4), (4,2) ] ) AS2 = ActiveShape( [(-2, 5.697), (-2.849, 9.940), (-0.303, 9.091 ) ]) ASM = ActiveShapeModel( [0,1] ) SA = ShapeAligner( ASM, 100, "" ) tdict = SA.calcAlignTransBtwn( AS2, AS1, np.ones( 3 ) )