def findEyes(img_gray): eye_cascade = cv2.CascadeClassifier( 'C:\\OpenCV\\data\\haarcascades\\haarcascade_eye.xml') eyes = eye_cascade.detectMultiScale(img_gray) im_toshow = copy.deepcopy(img_gray) eyeArr = [] for (ex, ey, ew, eh) in eyes: cv2.rectangle(im_toshow, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2) eyeArr.append((ex, ey, ew, eh)) # filter by area eyeAreas = map(areaRect, eyeArr) ix = np.argmax(eyeAreas) eye1 = eyeArr[ix] eyeAreas.pop(ix) eyeArr.pop(ix) ix = np.argmax(eyeAreas) eye2 = eyeArr[ix] ex1, ey1, ew1, eh1 = eye1 eye1loc = Point(ex1 + ew1 / 2, ey1 + eh1 / 2) ex2, ey2, ew2, eh2 = eye2 eye2loc = Point(ex2 + ew2 / 2, ey2 + eh2 / 2) #cv2.circle( im_toshow, ( int(eye1loc.x), int(eye1loc.y) ), 1, (255,255,255) ) #cv2.circle( im_toshow, ( int(eye2loc.x), int(eye2loc.y) ), 1, (255,255,255) ) #showImg( im_toshow ) return eye1loc, eye2loc
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 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 calcShift(point, img_gray): ## get point p (ix 48) p = point # Get gradient ( unitV already ) f, m = dasm.getGradient(p, img_gray) # print "x %f, y %f" % ( p.x, p.y ) cX = p.x cY = p.y #print f, m maxM = 1 cnt = 0 while cX < np.shape(img_gray)[1] and cY < np.shape(img_gray)[0]: if cnt > 30: return pt.x + f[0] * float(m) / float(maxM), pt.y + f[1] * float( m) / float(maxM) cnt += 1 cX = cX + f[0] cY = cY + f[1] if cX < np.shape(img_gray)[1] and cY < np.shape(img_gray)[0]: _, cM = dasm.getGradient(Point(cX, cY), img_gray) if cM > maxM: maxM = cM # print "cX %f cY %f" % (cX, cY) return pt.x + f[0] * float(m) / float(maxM), pt.y + f[1] * float( m) / float(maxM)
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 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 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 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()
def singlePoint( ptIx, evIx, asm, vals, vecs ): return Point( asm.meanShape.xs[ptIx] + MUL * math.sqrt( vals[evIx] ) * vecs[ptIx][evIx], asm.meanShape.ys[ptIx] + MUL * math.sqrt(vals[evIx]) * vecs[ptIx+1][evIx] )
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