Spaces:
Runtime error
Runtime error
initial 3d-re files
Browse files- app.py +14 -0
- depth_detection.py +18 -0
- image_resize.py +10 -0
- mesh_network.py +18 -0
- point_cloud.py +20 -0
app.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from depth_detection import depth_detection
|
3 |
+
from mesh_network import mesh_network
|
4 |
+
from point_cloud import point_cloud
|
5 |
+
|
6 |
+
def reconstruction3d(input_image):
|
7 |
+
|
8 |
+
new_image, output = depth_detection(input_image,pad=16)
|
9 |
+
pcd_img = point_cloud(new_image, output)
|
10 |
+
output_mesh = mesh_network(pcd_img)
|
11 |
+
return output_mesh
|
12 |
+
|
13 |
+
iface = gr.Interface(fn=reconstruction3d, inputs="image", outputs="image")
|
14 |
+
iface.launch()
|
depth_detection.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import GLPNImageProcessor, GLPNForDepthEstimation
|
3 |
+
from image_resize import resize_img
|
4 |
+
|
5 |
+
def depth_detection(image, pad=16):
|
6 |
+
|
7 |
+
feature_extractor = GLPNImageProcessor.from_pretrained("vinvino02/glpn-nyu")
|
8 |
+
model = GLPNForDepthEstimation.from_pretrained("vinvino02/glpn-nyu")
|
9 |
+
new_img = resize_img(image)
|
10 |
+
inputs = feature_extractor(images=new_img, return_tensors="pt")
|
11 |
+
|
12 |
+
with torch.no_grad():
|
13 |
+
outputs = model(**inputs)
|
14 |
+
predicted_depth = outputs.predicted_depth
|
15 |
+
|
16 |
+
output = predicted_depth.squeeze().cpu().numpy() * 1000.0
|
17 |
+
output = output[pad:-pad, pad:-pad]
|
18 |
+
return new_img,output
|
image_resize.py
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def resize_img(image):
|
2 |
+
new_height = 480 if image.height > 480 else image.height
|
3 |
+
new_height -= (new_height % 32)
|
4 |
+
new_width = int(new_height * image.width / image.height)
|
5 |
+
diff = new_width % 32
|
6 |
+
new_width = new_width - diff if diff < 16 else new_width + 32 - diff
|
7 |
+
new_size = (new_width, new_height)
|
8 |
+
image = image.resize(new_size)
|
9 |
+
|
10 |
+
return image
|
mesh_network.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import open3d as o3d
|
3 |
+
|
4 |
+
def mesh_network(point_cloud):
|
5 |
+
|
6 |
+
pcd = point_cloud
|
7 |
+
cl, ind = pcd.remove_statistical_outlier(nb_neighbors=20, std_ratio=20.0)
|
8 |
+
pcd = pcd.select_by_index(ind)
|
9 |
+
|
10 |
+
pcd.estimate_normals()
|
11 |
+
pcd.orient_normals_to_align_with_direction()
|
12 |
+
|
13 |
+
mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd, depth=12, n_threads=1)[0]
|
14 |
+
rotation = mesh.get_rotation_matrix_from_xyz((np.pi, 0, 0))
|
15 |
+
mesh.rotate(rotation, center=(0, 0, 0))
|
16 |
+
o3d.io.write_triangle_mesh(f'./mesh_depth12.obj', mesh)
|
17 |
+
|
18 |
+
return mesh
|
point_cloud.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import open3d as o3d
|
3 |
+
|
4 |
+
def point_cloud(image, dp_image):
|
5 |
+
|
6 |
+
width, height = image.size
|
7 |
+
|
8 |
+
depth_image = (dp_image * 255 / np.max(dp_image)).astype('uint8')
|
9 |
+
image = np.array(image)
|
10 |
+
|
11 |
+
depth_o3d = o3d.geometry.Image(depth_image)
|
12 |
+
image_o3d = o3d.geometry.Image(image)
|
13 |
+
rgbd_image = o3d.geometry.RGBDImage.create_from_color_and_depth(image_o3d, depth_o3d, convert_rgb_to_intensity=False)
|
14 |
+
|
15 |
+
camera_intrinsic = o3d.camera.PinholeCameraIntrinsic()
|
16 |
+
camera_intrinsic.set_intrinsics(width, height, 500, 500, width/2, height/2)
|
17 |
+
|
18 |
+
pcd = o3d.geometry.PointCloud.create_from_rgbd_image(rgbd_image, camera_intrinsic)
|
19 |
+
o3d.io.write_point_cloud("output_donut.ply", pcd)
|
20 |
+
return pcd
|