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 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 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 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