日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人工智能 > pytorch >内容正文

pytorch

三维人脸重建:精读3dmm.py

發(fā)布時(shí)間:2023/12/20 pytorch 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 三维人脸重建:精读3dmm.py 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

      • 代碼解析
        • 1. 載入模型
        • 2. 生成mesh, 也就是一些點(diǎn),三角形的集合
        • 3. 坐標(biāo)變換
        • 4. 使用68個(gè)關(guān)鍵點(diǎn)做fit

  • 轉(zhuǎn)載請(qǐng)注明出處

這個(gè)代碼的主要內(nèi)容就是根據(jù)一些隨機(jī)的參數(shù)從3d模型渲染到2d圖片
然后從圖片再變換回去

代碼解析

1. 載入模型

bfm = MorphabelModel('Data/BFM/Out/BFM.mat')# 這里面初始化的參數(shù)有: def __init__(self, model_path, model_type = 'BFM'):super( MorphabelModel, self).__init__()if model_type=='BFM':self.model = load.load_BFM(model_path)# fixed attributesself.nver = self.model['shapePC'].shape[0]/3 # 頂點(diǎn)的個(gè)數(shù)self.ntri = self.model['tri'].shape[0] # 三角形的個(gè)數(shù)self.n_shape_para = self.model['shapePC'].shape[1] # 199self.n_exp_para = self.model['expPC'].shape[1] # 29self.n_tex_para = self.model['texMU'].shape[1] # 1self.kpt_ind = self.model['kpt_ind'] # 關(guān)鍵點(diǎn)索引self.triangles = self.model['tri']self.full_triangles = np.vstack((self.model['tri'], self.model['tri_mouth'])) # 按行堆疊, 例如兩個(gè)(1, 3)成為(2, 3)

2. 生成mesh, 也就是一些點(diǎn),三角形的集合

sp = bfm.get_shape_para('random') ep = bfm.get_exp_para('random') # 上面就是生成一些隨機(jī)的形狀與表情參數(shù)vertices = bfm.generate_vertices(sp, ep) # 模型所有不包含顏色后加和的點(diǎn)tp = bfm.get_tex_para('random') colors = bfm.generate_colors(tp) colors = np.minimum(np.maximum(colors, 0), 1)
  • 看看這個(gè)generate_vertices, 輸入形狀系數(shù)和表情系數(shù),返回頂點(diǎn), 大小是(nver, 3)
def generate_vertices(self, shape_para, exp_para):'''Args:shape_para: (n_shape_para, 1)exp_para: (n_exp_para, 1) Returns:vertices: (nver, 3)'''vertices = self.model['shapeMU'] + self.model['shapePC'].dot(shape_para) +self.model['expPC'].dot(exp_para)# 這個(gè)求解的過(guò)程就是均值加上系數(shù)乘PCA降維后的基向量vertices = np.reshape(vertices, [int(3), int(len(vertices)/3)], 'F').Treturn vertices
  • 這個(gè)generated_colors是根據(jù)紋理系數(shù)生成顏色, 其實(shí)這里并沒有對(duì)
    紋理也就是顏色做后續(xù)的fit, 只不過(guò)是用一個(gè)隨機(jī)數(shù)乘了原來(lái)的系數(shù)
# 輸入紋理系數(shù), 返回顏色def generate_colors(self, tex_para):'''Args:tex_para: (n_tex_para, 1) 這個(gè)其實(shí)是1,1Returns:colors: (nver, 3)'''colors = self.model['texMU'] + self.model['texPC'].dot(tex_para*self.model['texEV']) # 這里是個(gè)隨機(jī)的數(shù)乘原來(lái)的texEV ''' texEV是紋理正交空間的系數(shù), 大小是199*1 '''colors = np.reshape(colors, [int(3), int(len(colors)/3)], 'F').T/255. return colors

3. 坐標(biāo)變換

  • 指定了s, R, t等縮放旋轉(zhuǎn)位移參數(shù), 然后把3D的點(diǎn)變到2D
s = 8e-04 angles = [0, 0, 0] t = [0, 0, 0] transformed_vertices = bfm.transform(vertices, s, angles, t) projected_vertices = transformed_vertices.copy() ''' 這里應(yīng)該就對(duì)應(yīng)相機(jī)坐標(biāo)系的變換, 相機(jī)坐標(biāo)系和兩個(gè)投影變換不要混了 ''' # using stantard camera & orth projection h = w = 256; c = 3# 這個(gè)是變換到圖片坐標(biāo)系上,x,y加上圖片的一半, y軸反轉(zhuǎn) image_vertices = mesh.transform.to_image(projected_vertices, h, w)# 這個(gè)在之前的精度中講過(guò), 就是利用重心坐標(biāo)對(duì)顏色進(jìn)行插值 ''' 這個(gè)的返回值是image: [h, w, 3] , 三個(gè)通道是一樣的 ''' image = mesh.render.render_colors(image_vertices, bfm.triangles, colors, h, w)

4. 使用68個(gè)關(guān)鍵點(diǎn)做fit

x = projected_vertices[bfm.kpt_ind, :2] # 這個(gè)是相機(jī)坐標(biāo)系下的人臉關(guān)鍵點(diǎn)X_ind = bfm.kpt_ind # index of keypoints in 3DMM. fixed. # fit 根據(jù)二維點(diǎn)來(lái)解出對(duì)應(yīng)的3d參數(shù)叫fit fitted_sp, fitted_ep, fitted_s, fitted_angles, fitted_t = bfm.fit(x, X_ind, max_iter = 3)
  • 這里的fit算法里面有個(gè)黃金標(biāo)準(zhǔn)算法, 網(wǎng)上沒有講的很深入的, 同時(shí)現(xiàn)在大多數(shù)是用深度學(xué)習(xí)的方法fit了, 這個(gè)先占坑

這里的fit結(jié)果有5個(gè)參數(shù), 分別是形狀, 表情, s, R, t

總結(jié)

以上是生活随笔為你收集整理的三维人脸重建:精读3dmm.py的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。