def readInAlign(): allLines = None with open("outfile-ASM-100iters-500tr.txt", "r") as infile: allLines = infile.readlines() cleanLines = map(lambda x: x.strip().split(), allLines) asm = PASM([36, 31], 10) s = [] for tuple in cleanLines: if tuple[0] == '!!!': if s != []: asm.meanShape = Shape(s) s = [] else: pass elif tuple[0] == '@@@': if s != []: asm.addShape(Shape(s)) s = [] else: pass else: s.append(Point(float(tuple[0]), float(tuple[1]))) return asm
def showVary( asm, vals, vecs, numPlots, eval ): # Vary one eigenvalue f, axes = plt.subplots( 1, numPlots, sharex = True, sharey = True ) vals = np.ravel( vals ) rs = np.linspace( - MUL * math.sqrt( vals[eval] ), MUL * math.sqrt( vals[eval] ), numPlots ) for m in range( len(rs) ): reps = np.add(asm.meanShape.allPts,np.multiply( np.ravel(vecs[:,eval] ), rs[m] )) x, y = reravel( reps ) s = Shape( [ Point (pt[0], pt[1] ) for pt in zip(x,y) ]) s.draw( newpal, axes[m] ) ## diff axes[m].plot( s.xs, s.ys, lw =1, c ='k') plt.savefig( 'simple-example-PC%d.png'% eval )
def showVaryMultiplePCS( asm, vals, vecs, numPlots, numPCs, newpal): f, axes = plt.subplots( 1, numPlots, sharex = True, sharey = True ) bs = [] for p in range( numPCs ): rs = np.linspace( - MUL * math.sqrt( vals[p] ), MUL * math.sqrt( vals[p] ), numPlots ) bs.append( rs ) P = vecs[:, 0:numPCs] for pl in range(numPlots) : b = [ bs[p][pl] for p in range(len(bs) ) ] X = np.add( asm.meanShape.allPts, np.dot( P, b ) ) x, y = reravel( X ) s = Shape( [ Point (pt[0], pt[1] ) for pt in zip(x,y) ]) s.draw( newpal, axes[pl] ) ## diff axes[pl].plot( s.xs, s.ys, lw =1, c ='k') f.savefig( "simple-example-%d-at-a-time.png" % numPCs)
def setup( ): s1 = Shape( [ Point(200,340), Point( 0, 200), Point( 350,200) ] ) s2 = Shape( [ Point(210,320), Point( 5, 205), Point( 340,190) ] ) s3 = Shape( [ Point(205,300), Point( 10, 190), Point( 344,204) ] ) s4 = Shape( [ Point(199,380), Point( -5, 205), Point( 333,203) ] ) s5 = Shape( [ Point(190,290), Point( 0, 190), Point( 351,201) ] ) asm = PASM( [0,1], 10 ) asm.addShape( s1 ) asm.addShape( s2 ) asm.addShape( s3 ) asm.addShape( s4 ) asm.addShape( s5 ) return asm
def readIn(asm, files): m = 0 for f in files: if m > 499: return with open(os.path.join(folder, f), "r") as infile: ptList = [] allLines = infile.readlines() pointLine = False cleanLines = [x.strip() for x in allLines] for line in cleanLines: if line is '{': pointLine = True elif line is '}': pointLine = False pass elif pointLine: ptList.append(map(float, line.split(' '))) else: pass asm.addShape(Shape(ptList)) m += 1 print m
def reProjectCumulative(asm, numVals, vals, vecs): P = vecs[:, 0:numVals] b = [np.transpose(np.mat(vals[0:numVals]))] X = np.add(asm.meanShape.allPts, np.dot(P, b)) x, y = reravel(X) return Shape([Point(pt[0], pt[1]) for pt in zip(x, y)])
def projectOnePC(evIx, b, asm, vals, vecs): X = np.add(asm.meanShape.allPts, np.multiply(vecs[:, evIx], b)) x, y = reravel(X) sv = [] for a, b in zip(x, y): sv.append(Point(a, b)) s = Shape(sv) return s
def project( evIx, asm, vals, vecs ): X = np.add( asm.meanShape.allPts, np.multiply( vecs[:,evIx], math.sqrt( vals[evIx] ))) x,y = reravel( X ) #s = Shape( [ Point( X[0], X[1]), Point( X[2], X[3] ), Point( X[4], X[5] ) ] ) sv = [] for a, b in zip( x, y): sv.append( Point( a, b ) ) s = Shape( sv ) return s
def adjust(self, img): self.dX = np.ravel( map(lambda x: DASM.getGradient(x, img), self.meanShape.shapePoints)) nX = np.add(self.meanShape.allPts, self.dX) x, y = DASM.deravel(nX) ms = Shape([Point(pt[0], pt[1]) for pt in zip(x, y)]) self.transDict = self.alignOneShape(ms, self.appModel)
def run(): asm = PASM([0, 1], 1000) allLines = [] pts = [] for f in files: with open(os.path.join(OUTPUT, f), "r") as infile: allLines = infile.readlines() if len(allLines) > 0: cleanLines = [x.strip().split('\t') for x in allLines] ptList = [Point(x[0], x[1]) for x in cleanLines] print len(ptList) asm.addShape(Shape(ptList)) asm.iterateAlignment()
def showVaryMultiplePCS(asm, vals, vecs, numPlots, numPCs): f, axes = plt.subplots(1, numPlots) bs = [] for p in range(numPCs): rs = np.linspace(-0.25 * math.sqrt(vals[p]), 0.25 * math.sqrt(vals[p]), numPlots) bs.append(rs) P = vecs[:, 0:numPCs] for pl in range(numPlots): b = [bs[p][pl] for p in range(len(bs))] X = np.add(asm.meanShape.allPts, np.dot(P, b)) x, y = reravel(X) s = Shape([Point(pt[0], pt[1]) for pt in zip(x, y)]) FaceDraw(s, axes[pl]).drawBold() axes[pl].set_xlim((-2, 2)) axes[pl].set_ylim((-3, 3)) f.savefig('faces-%d-PCs-at-once.png' % numPCs) plt.close()
dmodel = copy.deepcopy(dasm.model) i = 0 while i < 100: # print i allpt = [] pts = [] for pt in dmodel.shapePoints: dx, dy = calcShift(pt, img_gray) allpt.append(dx) allpt.append(dy) pts.append([dx, dy]) #print pts dmodel = Shape(pts) FaceDraw(dasm.model, plt).drawBold() FaceDraw(dmodel, plt).drawContrast() plt.imshow(img_gray) ### i += 1 plt.show() FaceDraw(dasm.model, plt).drawBold() FaceDraw(dmodel, plt).drawContrast() plt.imshow(img_gray) plt.show()