Timilehin commited on
Commit
1a353bb
·
verified ·
1 Parent(s): 6b63590

Upload recon1.py

Browse files
Files changed (1) hide show
  1. pifuhd/apps/recon1.py +182 -0
pifuhd/apps/recon1.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import sys
3
+ import os
4
+
5
+ sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
6
+ ROOT_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
7
+
8
+ import time
9
+ import json
10
+ import numpy as np
11
+ import cv2
12
+ import random
13
+ import torch
14
+ import torch.nn as nn
15
+ from tqdm import tqdm
16
+ from torch.utils.data import DataLoader
17
+ import matplotlib.pyplot as plt
18
+ import matplotlib.cm as cm
19
+ import matplotlib
20
+ from numpy.linalg import inv
21
+
22
+ from lib.options import BaseOptions
23
+ from lib.mesh_util import reconstruction # save_obj_mesh_with_color
24
+ from lib.data import EvalWPoseDataset, EvalDataset
25
+ from lib.model import HGPIFuNetwNML, HGPIFuMRNet
26
+ from lib.geometry import index
27
+
28
+ from PIL import Image
29
+ from io import BytesIO
30
+
31
+ parser = BaseOptions()
32
+
33
+
34
+ def save_obj_mesh_with_color(output, verts, faces, colors=None):
35
+ # Ensure we're writing in binary mode for BytesIO
36
+ def write_data(f, data):
37
+ if isinstance(output, BytesIO):
38
+ f.write(data.encode('utf-8'))
39
+ else:
40
+ f.write(data)
41
+
42
+ if isinstance(output, str):
43
+ f = open(output, 'w')
44
+ elif isinstance(output, BytesIO):
45
+ f = output
46
+ else:
47
+ raise ValueError("output must be a filepath or BytesIO")
48
+
49
+ for i in range(verts.shape[0]):
50
+ if colors is not None:
51
+ line = f'v {verts[i, 0]} {verts[i, 1]} {verts[i, 2]} {colors[i, 0]} {colors[i, 1]} {colors[i, 2]}\n'
52
+ else:
53
+ line = f'v {verts[i, 0]} {verts[i, 1]} {verts[i, 2]}\n'
54
+ write_data(f, line)
55
+
56
+ for i in range(faces.shape[0]):
57
+ line = f'f {faces[i, 0] + 1} {faces[i, 1] + 1} {faces[i, 2] + 1}\n'
58
+ write_data(f, line)
59
+
60
+ if isinstance(output, str):
61
+ f.close()
62
+
63
+
64
+
65
+ def gen_mesh(res, net, cuda, data, thresh=0.5, use_octree=True, components=False):
66
+ image_tensor_global = data['img_512'].to(device=cuda)
67
+ image_tensor = data['img'].to(device=cuda)
68
+ calib_tensor = data['calib'].to(device=cuda)
69
+
70
+ net.filter_global(image_tensor_global)
71
+ net.filter_local(image_tensor[:, None])
72
+
73
+ try:
74
+ if net.netG.netF is not None:
75
+ image_tensor_global = torch.cat([image_tensor_global, net.netG.nmlF], 0)
76
+ if net.netG.netB is not None:
77
+ image_tensor_global = torch.cat([image_tensor_global, net.netG.nmlB], 0)
78
+ except Exception as e:
79
+ print(f"Error in netG filter: {e}")
80
+
81
+ b_min = data['b_min']
82
+ b_max = data['b_max']
83
+
84
+ try:
85
+ verts, faces, _, _ = reconstruction(
86
+ net, cuda, calib_tensor, res, b_min, b_max, thresh, use_octree=use_octree, num_samples=50000
87
+ )
88
+ verts_tensor = torch.from_numpy(verts.T).unsqueeze(0).to(device=cuda).float()
89
+
90
+ color = np.zeros(verts.shape)
91
+ interval = 50000
92
+ for i in range(len(color) // interval + 1):
93
+ left = i * interval
94
+ right = -1 if i == len(color) // interval else (i + 1) * interval
95
+ net.calc_normal(verts_tensor[:, None, :, left:right], calib_tensor[:, None], calib_tensor)
96
+ nml = net.nmls.detach().cpu().numpy()[0] * 0.5 + 0.5
97
+ color[left:right] = nml.T
98
+
99
+ obj_buffer = BytesIO()
100
+ save_obj_mesh_with_color(obj_buffer, verts, faces, color)
101
+ obj_buffer.seek(0) # Reset buffer pointer to the beginning
102
+
103
+ return obj_buffer
104
+
105
+ except Exception as e:
106
+ print(f"Error generating mesh: {e}")
107
+ return None
108
+
109
+
110
+ def recon(opt, use_rect=False):
111
+ state_dict_path = None
112
+ if opt.load_netMR_checkpoint_path is not None:
113
+ state_dict_path = opt.load_netMR_checkpoint_path
114
+ elif opt.resume_epoch < 0:
115
+ state_dict_path = f'{opt.checkpoints_path}/{opt.name}_train_latest'
116
+ opt.resume_epoch = 0
117
+ else:
118
+ state_dict_path = f'{opt.checkpoints_path}/{opt.name}_train_epoch_{opt.resume_epoch}'
119
+
120
+ start_id = opt.start_id
121
+ end_id = opt.end_id
122
+
123
+ cuda = torch.device(f'cuda:{opt.gpu_id}' if torch.cuda.is_available() else 'cpu')
124
+
125
+ if state_dict_path and os.path.exists(state_dict_path):
126
+ print(f'Resuming from {state_dict_path}')
127
+ state_dict = torch.load(state_dict_path, map_location=cuda)
128
+ print('Warning: opt is overwritten.')
129
+ dataroot = opt.dataroot
130
+ resolution = opt.resolution
131
+ results_path = opt.results_path
132
+ loadSize = opt.loadSize
133
+
134
+ opt = state_dict['opt']
135
+ opt.dataroot = dataroot
136
+ opt.resolution = resolution
137
+ opt.results_path = results_path
138
+ opt.loadSize = loadSize
139
+ else:
140
+ raise Exception(f'Failed to load state dict: {state_dict_path}')
141
+
142
+ test_dataset = EvalWPoseDataset(opt) if not use_rect else EvalDataset(opt)
143
+ print(f'Test data size: {len(test_dataset)}')
144
+ projection_mode = test_dataset.projection_mode
145
+
146
+ opt_netG = state_dict['opt_netG']
147
+ netG = HGPIFuNetwNML(opt_netG, projection_mode).to(device=cuda)
148
+ netMR = HGPIFuMRNet(opt, netG, projection_mode).to(device=cuda)
149
+
150
+ netG.eval()
151
+ netMR.load_state_dict(state_dict['model_state_dict'])
152
+
153
+ os.makedirs(opt.checkpoints_path, exist_ok=True)
154
+ os.makedirs(opt.results_path, exist_ok=True)
155
+
156
+ if start_id < 0:
157
+ start_id = 0
158
+ if end_id < 0:
159
+ end_id = len(test_dataset)
160
+
161
+ obj_buffers = []
162
+ with torch.no_grad():
163
+ print('Generating mesh (test) ...')
164
+ for i in tqdm(range(start_id, end_id)):
165
+ if i >= len(test_dataset):
166
+ break
167
+
168
+ test_data = test_dataset[i]
169
+ obj_buffer = gen_mesh(opt.resolution, netMR, cuda, test_data, components=opt.use_compose)
170
+ if obj_buffer:
171
+ obj_buffers.append(obj_buffer)
172
+
173
+ return obj_buffers
174
+
175
+ def reconWrapper(args=None, use_rect=False):
176
+ opt = parser.parse(args)
177
+ return recon(opt, use_rect)
178
+
179
+ if __name__ == '__main__':
180
+ buffers = reconWrapper()
181
+ if buffers:
182
+ print(f"Generated {len(buffers)} OBJ files in memory.")