File size: 10,387 Bytes
bac55b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import io
import numpy as np
from PIL import Image as PIL_Image # gltflibのImageと被るので別名にする。
import cv2
import struct
import triangle
import uuid

from gltflib import (
    GLTF, GLTFModel, Asset, Scene, Node, Mesh, Primitive, Attributes, Buffer, BufferView, Image, Texture, TextureInfo, Material, Sampler, Accessor, AccessorType,
    BufferTarget, ComponentType, GLBResource, PBRMetallicRoughness)

# 表面及び裏面の頂点リストの作成
def make_front_and_back_vertex_list(coordinate_list, img):

    # 表面の頂点
    front_vertex_list = []
    # 裏面の頂点
    back_vertex_list = []
    for coordinates in coordinate_list:
        front_vertices = []
        back_vertices = []
        # Y軸方向は画像とGLBで上下が逆になるので注意
        for coordinate in coordinates:
            front_vertices.append((coordinate[0] * 2 / img.size[0] - 1.0, -(coordinate[1] * 2 / img.size[1] - 1.0), 0.2))
            back_vertices.append((coordinate[0] * 2 / img.size[0] - 1.0, -(coordinate[1] * 2 / img.size[1] - 1.0), -0.2))
    
        front_vertex_list.append(front_vertices)
        back_vertex_list.append(back_vertices)

    return front_vertex_list, back_vertex_list
    
# メッシュの各種情報の作成
def make_mesh_data(coordinate_list, img):
    front_vertex_list, back_vertex_list = make_front_and_back_vertex_list(coordinate_list, img)

    # 頂点データ(POSITION)
    vertices = []
    # 頂点インデックス決定時に使うオフセット値のリスト
    front_offset = 0
    front_offset_list = []
    back_offset_list = []
    for front_vertices, back_vertices in zip(front_vertex_list, back_vertex_list):
        vertices.extend(front_vertices)
        vertices.extend(back_vertices)

        back_offset = front_offset + len(front_vertices)
        front_offset_list.append(front_offset)
        back_offset_list.append(back_offset)
        front_offset += len(front_vertices) + len(back_vertices)        

    # 法線データ(NORMAL)
    normals = []
    for front_vertices, back_vertices in zip(front_vertex_list, back_vertex_list):
        normals.extend([( 0.0,  0.0,  1.0)] * len(front_vertices))
        normals.extend([( 0.0,  0.0, -1.0)] * len(back_vertices))

    # テクスチャ座標(TEXCOORD_0)
    # 画像は左上原点になり、Y軸の上下を変える必要がある。
    texcoord_0s = [((vertex[0] + 1.0) / 2.0, 1.0 - ((vertex[1] + 1.0) / 2.0) ) for vertex in vertices]

    # 頂点インデックス
    vertex_indices = []
    for front_vertices, back_vertices, front_offset, back_offset \
        in zip(front_vertex_list, back_vertex_list, front_offset_list, back_offset_list):
        polygon = {
            'vertices': np.array(front_vertices)[:, :2],
            'segments': np.array([( i, (i + 1) % (len(front_vertices)) ) for i in range(len(front_vertices))])  # 各辺を定義
        }
        triangulate_result = triangle.triangulate(polygon, 'p')
        vertex_indices.extend(list(np.array(triangulate_result['triangles']+front_offset).flatten())) # 表面
        vertex_indices.extend(list((np.array(triangulate_result['triangles'])+back_offset).flatten())) # 裏面
        vertex_indices.extend(list(np.array([[front_offset + i, 
                                              front_offset + (i + 1) % len(front_vertices), 
                                              back_offset + i] 
                                               for i in range(len(front_vertices))]).flatten())) # 側面1
        vertex_indices.extend(list(np.array([[back_offset + i,
                                              back_offset + (i + 1) % len(back_vertices), 
                                              front_offset+ (i + 1) % len(front_vertices)] for i in range(len(front_vertices))]).flatten())) # 側面2
            
    return vertices, normals, texcoord_0s, vertex_indices

def create_extracted_objects_model(img_bytearray):

    # 画像の取得
    img = PIL_Image.open(img_bytearray).convert('RGB')
    img_bytearray = io.BytesIO()
    img.save(img_bytearray, format="JPEG", quality=95)
    img_bytearray = img_bytearray.getvalue()
    img_bytelen = len(img_bytearray)

    # 3Dモデルのスケールの計算
    scale_factor = np.power(img.size[0] * img.size[1], 0.5)
    scale = (img.size[0] / scale_factor, img.size[1] / scale_factor, 0.4)

    # 画像主要部分の頂点の取得
    base_color = img.getpixel((0, 0))
    mask = PIL_Image.new('RGB', img.size)
    for i in range(img.size[0]):
        for j in range(img.size[1]):
            if base_color == img.getpixel((i, j)):
                mask.putpixel((i, j), (0, 0, 0))
            else:
                mask.putpixel((i, j), (255, 255, 255))

    opening = cv2.morphologyEx(np.array(mask), cv2.MORPH_OPEN, kernel=np.ones((15, 15),np.uint8))
    contours, _ = cv2.findContours(cv2.cvtColor(np.array(opening), cv2.COLOR_RGB2GRAY), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    coordinate_list = []
    for contour in contours:
        coordinates = []
        for [[x, y]] in contour:
            coordinates.append((x, y))
        coordinate_list.append(coordinates)
    
    # 各種データの作成
    vertices, normals, texcoord_0s, vertex_indices = make_mesh_data(coordinate_list, img)
    
    # 頂点データ(POSITION)
    vertex_bytearray = bytearray()
    for vertex in vertices:
        for value in vertex:
            vertex_bytearray.extend(struct.pack('f', value))
    vertex_bytelen = len(vertex_bytearray)
    mins = [min([vertex[i] for vertex in vertices]) for i in range(3)]
    maxs = [max([vertex[i] for vertex in vertices]) for i in range(3)]

    # 法線データ(NORMAL)
    normal_bytearray = bytearray()
    for normal in normals:
        for value in normal:
            normal_bytearray.extend(struct.pack('f', value))
    normal_bytelen = len(normal_bytearray)

    # テクスチャ座標(TEXCOORD_0)
    texcoord_0s = [
    ((vertex[0] + 1.0) / 2.0, 1.0 - ((vertex[1] + 1.0) / 2.0) ) for vertex in vertices
    ]
    texcoord_0_bytearray = bytearray()
    for texcoord_0 in texcoord_0s:
        for value in texcoord_0:
            texcoord_0_bytearray.extend(struct.pack('f', value))
    texcoord_0_bytelen = len(texcoord_0_bytearray)

    # 頂点インデックス
    vertex_index_bytearray = bytearray()
    for value in vertex_indices:
        vertex_index_bytearray.extend(struct.pack('H', value))
    vertex_index_bytelen = len(vertex_index_bytearray)

    # バイナリデータ部分の結合
    bytearray_list = [
        vertex_bytearray,
        normal_bytearray,
        texcoord_0_bytearray,
        vertex_index_bytearray,
        img_bytearray,
    ]
    bytelen_list = [
        vertex_bytelen,
        normal_bytelen,
        texcoord_0_bytelen,
        vertex_index_bytelen,
        img_bytelen,
    ]
    bytelen_cumsum_list = list(np.cumsum(bytelen_list))
    bytelen_cumsum_list = list(map(lambda x: int(x), bytelen_cumsum_list))

    all_bytearray = bytearray()
    for temp_bytearray in bytearray_list:
        all_bytearray.extend(temp_bytearray)
    offset_list = [0] + bytelen_cumsum_list # 最初のオフセットは0
    offset_list.pop() # 末尾を削除

    # リソースの作成
    resources = [GLBResource(data=all_bytearray)]

    # 各種設定
    # アセット
    asset=Asset()

    # バッファ
    buffers = [Buffer(byteLength=len(all_bytearray))]

    # バッファビュー
    bufferViews = [
        BufferView(buffer=0, byteOffset=offset_list[0], byteLength=bytelen_list[0], target=BufferTarget.ARRAY_BUFFER.value),
        BufferView(buffer=0, byteOffset=offset_list[1], byteLength=bytelen_list[1], target=BufferTarget.ARRAY_BUFFER.value),
        BufferView(buffer=0, byteOffset=offset_list[2], byteLength=bytelen_list[2], target=BufferTarget.ARRAY_BUFFER.value),
        BufferView(buffer=0, byteOffset=offset_list[3], byteLength=bytelen_list[3], target=BufferTarget.ELEMENT_ARRAY_BUFFER.value),
        BufferView(buffer=0, byteOffset=offset_list[4], byteLength=bytelen_list[4], target=None),
    ]

    # アクセサー
    accessors = [
        Accessor(bufferView=0, componentType=ComponentType.FLOAT.value, count=len(vertices), type=AccessorType.VEC3.value, max=maxs, min=mins),
        Accessor(bufferView=1, componentType=ComponentType.FLOAT.value, count=len(normals), type=AccessorType.VEC3.value, max=None, min=None),
        Accessor(bufferView=2, componentType=ComponentType.FLOAT.value, count=len(texcoord_0s), type=AccessorType.VEC2.value, max=None, min=None),
        Accessor(bufferView=3, componentType=ComponentType.UNSIGNED_SHORT.value, count=len(vertex_indices), type=AccessorType.SCALAR.value, max=None, min=None)
    ]

    # イメージ
    images=[
        Image(mimeType='image/jpeg', bufferView=4),
    ]

    # サンプラー
    samplers = [Sampler(magFilter=9728, minFilter=9984)] # magFilter:最近傍フィルタリング、minFilter:ミップマップ+最近傍フィルタリング

    # テクスチャ
    textures = [
        Texture(name='Main',sampler=0,source=0),
    ]

    # マテリアル
    materials = [
        Material(
            pbrMetallicRoughness=PBRMetallicRoughness(
                baseColorTexture=TextureInfo(index=0),
                metallicFactor=0,
                roughnessFactor=1
            ),
            name='Material0',
            alphaMode='OPAQUE',
            doubleSided=True
        ),
    ]

    # メッシュ
    meshes = [
        Mesh(name='Main', primitives=[Primitive(attributes=Attributes(POSITION=0, NORMAL=1,TEXCOORD_0=2),
                                                indices=3, material=0, mode=4)]),
    ]

    # ノード
    nodes = [
        Node(mesh=0,rotation=None, scale=scale),
    ]

    # シーン
    scene = 0
    scenes = [Scene(name='Scene', nodes=[0])]

    model = GLTFModel(
        asset=asset,
        buffers=buffers,
        bufferViews=bufferViews,
        accessors=accessors,
        images=images,
        samplers=samplers,
        textures=textures,
        materials=materials,
        meshes=meshes,
        nodes=nodes,
        scene=scene,
        scenes=scenes
    )

    gltf = GLTF(model=model, resources=resources)

    tmp_filename = uuid.uuid4().hex
    model_path = f'../tmp/{tmp_filename}.glb'

    gltf.export(model_path)

    return model_path