Spaces:
Runtime error
Runtime error
Upload hy3dgen/texgen/custom_rasterizer/custom_rasterizer/io_obj.py with huggingface_hub
Browse files
hy3dgen/texgen/custom_rasterizer/custom_rasterizer/io_obj.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Open Source Model Licensed under the Apache License Version 2.0
|
2 |
+
# and Other Licenses of the Third-Party Components therein:
|
3 |
+
# The below Model in this distribution may have been modified by THL A29 Limited
|
4 |
+
# ("Tencent Modifications"). All Tencent Modifications are Copyright (C) 2024 THL A29 Limited.
|
5 |
+
|
6 |
+
# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
|
7 |
+
# The below software and/or models in this distribution may have been
|
8 |
+
# modified by THL A29 Limited ("Tencent Modifications").
|
9 |
+
# All Tencent Modifications are Copyright (C) THL A29 Limited.
|
10 |
+
|
11 |
+
# Hunyuan 3D is licensed under the TENCENT HUNYUAN NON-COMMERCIAL LICENSE AGREEMENT
|
12 |
+
# except for the third-party components listed below.
|
13 |
+
# Hunyuan 3D does not impose any additional limitations beyond what is outlined
|
14 |
+
# in the repsective licenses of these third-party components.
|
15 |
+
# Users must comply with all terms and conditions of original licenses of these third-party
|
16 |
+
# components and must ensure that the usage of the third party components adheres to
|
17 |
+
# all relevant laws and regulations.
|
18 |
+
|
19 |
+
# For avoidance of doubts, Hunyuan 3D means the large language models and
|
20 |
+
# their software and algorithms, including trained model weights, parameters (including
|
21 |
+
# optimizer states), machine-learning model code, inference-enabling code, training-enabling code,
|
22 |
+
# fine-tuning enabling code and other elements of the foregoing made publicly available
|
23 |
+
# by Tencent in accordance with TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT.
|
24 |
+
|
25 |
+
import cv2
|
26 |
+
import numpy as np
|
27 |
+
|
28 |
+
|
29 |
+
def LoadObj(fn):
|
30 |
+
lines = [l.strip() for l in open(fn)]
|
31 |
+
vertices = []
|
32 |
+
faces = []
|
33 |
+
for l in lines:
|
34 |
+
words = [w for w in l.split(' ') if w != '']
|
35 |
+
if len(words) == 0:
|
36 |
+
continue
|
37 |
+
if words[0] == 'v':
|
38 |
+
v = [float(words[i]) for i in range(1, 4)]
|
39 |
+
vertices.append(v)
|
40 |
+
elif words[0] == 'f':
|
41 |
+
f = [int(words[i]) - 1 for i in range(1, 4)]
|
42 |
+
faces.append(f)
|
43 |
+
|
44 |
+
return np.array(vertices).astype('float32'), np.array(faces).astype('int32')
|
45 |
+
|
46 |
+
|
47 |
+
def LoadObjWithTexture(fn, tex_fn):
|
48 |
+
lines = [l.strip() for l in open(fn)]
|
49 |
+
vertices = []
|
50 |
+
vertex_textures = []
|
51 |
+
faces = []
|
52 |
+
face_textures = []
|
53 |
+
for l in lines:
|
54 |
+
words = [w for w in l.split(' ') if w != '']
|
55 |
+
if len(words) == 0:
|
56 |
+
continue
|
57 |
+
if words[0] == 'v':
|
58 |
+
v = [float(words[i]) for i in range(1, len(words))]
|
59 |
+
vertices.append(v)
|
60 |
+
elif words[0] == 'vt':
|
61 |
+
v = [float(words[i]) for i in range(1, len(words))]
|
62 |
+
vertex_textures.append(v)
|
63 |
+
elif words[0] == 'f':
|
64 |
+
f = []
|
65 |
+
ft = []
|
66 |
+
for i in range(1, len(words)):
|
67 |
+
t = words[i].split('/')
|
68 |
+
f.append(int(t[0]) - 1)
|
69 |
+
ft.append(int(t[1]) - 1)
|
70 |
+
for i in range(2, len(f)):
|
71 |
+
faces.append([f[0], f[i - 1], f[i]])
|
72 |
+
face_textures.append([ft[0], ft[i - 1], ft[i]])
|
73 |
+
|
74 |
+
tex_image = cv2.cvtColor(cv2.imread(tex_fn), cv2.COLOR_BGR2RGB)
|
75 |
+
return np.array(vertices).astype('float32'), np.array(vertex_textures).astype('float32'), np.array(faces).astype(
|
76 |
+
'int32'), np.array(face_textures).astype('int32'), tex_image
|