Spaces:
Running
Running
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 |