File size: 1,763 Bytes
72b443b
 
 
 
50c7113
72b443b
 
b66bc16
 
 
50c7113
b66bc16
72b443b
50c7113
72b443b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b66bc16
 
 
 
72b443b
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pyvista as pv
import numpy as np
import cv2
from glibvision.cv2_utils  import pil_to_bgr_image
from gradio_utils import clear_old_files,get_image_id
from mp_utils import get_pixel_cordinate_list,extract_landmark,get_pixel_cordinate,get_normalized_xyz
import mp_triangles
import io
import hashlib
import os


def process_image3d(image,smooth_mesh,depto_ratio,inner_eyes,inner_mouth):
    clear_old_files()

    mp_image,face_landmarker_result = extract_landmark(image)
    landmark_points = [get_normalized_xyz(face_landmarker_result.face_landmarks,i) for i in range(0,468)]#468 0478 is iris
     
    aspect_ratio = image.width/image.height
    yup = [#I'm not sure
           # I'm not sure 
           ( point[2]*aspect_ratio*depto_ratio,1.0-point[0]*aspect_ratio,1.0 - point[1]) for point in landmark_points
    ]

    uv = [
           ( point[0],1.0-point[1]) for point in landmark_points
    ]
    # 頂点座標 (x, y, z)
    vertices = np.array(
        yup
    )

    # 三角形インデックス
    
    def flatten_and_interleave(list_of_lists):
        return [([len(item)] + list(item) )for item in list_of_lists]

    faces = np.array(
        flatten_and_interleave(mp_triangles.get_triangles_copy(True,inner_eyes,inner_eyes,inner_mouth))
    )

    # PolyDataオブジェクトの作成
    mesh = pv.PolyData(vertices, faces)

    id = get_image_id(image)
    os.makedirs("files",exist_ok=True)
    path = f"files/{id}.gltf"#TODO uniq file
    
    texture = pv.Texture(np.array(image, dtype=np.uint8))
    uv_coords = np.array(uv,dtype="float32")    
    mesh.active_texture_coordinates =  uv_coords

    pl = pv.Plotter()
   
    pl.add_mesh(mesh,texture=texture,smooth_shading=smooth_mesh)
    pl.export_gltf(path)  

    return path