예제 #1
0
    def loadModel(self, filename, translate, scale, normobj=False):
        model = obj(filename)
        light = [0, 0, 1]
        for face in model.faces:

            vertCount = len(face)
            if normobj:
                for vert in range(vertCount):

                    v0 = model.vertices[int(face[vert][0]) - 1]
                    v1 = model.vertices[int(face[(vert + 1) % vertCount][0]) -
                                        1]

                    x0 = int(v0[0] * scale[0] + translate[0])
                    y0 = int(v0[1] * scale[1] + translate[1])
                    x1 = int(v1[0] * scale[0] + translate[0])
                    y1 = int(v1[1] * scale[1] + translate[1])

                    self.glLineWin(x0, y0, x1, y1)
            else:
                v0 = model.vertices[face[0][0] - 1]
                v1 = model.vertices[face[1][0] - 1]
                v2 = model.vertices[face[2][0] - 1]

                v0 = self.transform1(v0, translate, scale)
                v1 = self.transform1(v1, translate, scale)
                v2 = self.transform1(v2, translate, scale)

                #polycolor = color(random.randint(0,255) / 255,
                #                  random.randint(0,255) / 255,
                #                  random.randint(0,255) / 255)

                normal = op.cross(op.subtract(v1, v0), op.subtract(v2, v0))

                normal = op.divide(normal, op.norm(normal))
                intensity = op.dot(normal, light)

                if intensity >= 0:
                    self.triangle_bc(
                        v0, v1, v2,
                        self.glColor(intensity, intensity, intensity))

                if vertCount > 3:  #asumamos que 4, un cuadrado
                    v3 = model.vertices[face[3][0] - 1]

                    if intensity >= 0:
                        self.triangle_bc(
                            v0, v2, v3, color(intensity, intensity, intensity))
예제 #2
0
"""   

    
#def main():
if __name__ == '__main__':
    lamda = 1
    time1 = time.time()
    rootDir = r'D:\WinPython-64bit-2.7.10.1\mine\Unconstrained 3D Face Reconstruction\data'
    imgSetDir = os.path.join(rootDir, 'test')
    landmarkPath = os.path.join(rootDir, 'landmark.txt')
    templatePath = os.path.join(rootDir, 'template.obj')
    neibPath = os.path.join(rootDir, 'neib')
    tempPath = os.path.join(rootDir, 'tempResult')
    landmarkAll = getAllLandmark(imgSetDir)
    landmarkIndex = loadLandmark(landmarkPath)
    template = obj(templatePath)
    template.load()
    vCount = len(np.array(template.v))
    X0 = np.array(template.v).reshape((3*vCount,1))
    #selection matrix
    row = np.array(map(lambda x:[x*3,x*3+1, x*3+2], landmarkIndex))
    row = (row.reshape((1,row.size))).squeeze()
    dataD = np.ones(row.shape)
    D = csr_matrix((dataD, (row, row)), shape=(3*vCount, 3*vCount))
    L = computeL(template)
    for i in range(2):      
        template = itera(template)
        template.save(os.path.join(tempPath, 'iter{}.obj'.format(str(i))))
        print time.time() - time1
        time1 = time.time()
"""
    newV = spsolve(sumL, sumR)
    template.v = newV.reshape((len(newV)/3, 3))
    return template, pMatrix
    

    
#def main():
if __name__ == '__main__':
    time1 = time.time()
    rootDir = r'D:\WinPython-64bit-2.7.10.1\mine\Unconstrained 3D Face Reconstruction\data'
    imgSetDir = os.path.join(rootDir, 'imgSet2')
    landmarkPath = os.path.join(rootDir, 'landmark.txt')
    templatePath = os.path.join(rootDir, 'template2.obj')
    tempPath = os.path.join(rootDir, 'tempResult')
    landmarkIndex = loadLandmark(landmarkPath)
    template = obj(templatePath)
    template.load()
    vCount = len(np.array(template.v))
    X = np.array(template.v).reshape((3*vCount,1))
    X0 = X[:]
    L = calL(template)
    L0 = L[:]
    #selection matrix
    D = np.zeros((3*vCount, 3*vCount))
    for index in landmarkIndex:
        for i in range(3):
            D[3*index+i, 3*index+i] = 1
    D = csr_matrix(D)
    costV2 = []
    costV1 = []
    lamda = 1