shin0624 commited on
Commit
c83e4e8
ยท
verified ยท
1 Parent(s): d57f8ea

Update gaussian_pipeline.py

Browse files
Files changed (1) hide show
  1. gaussian_pipeline.py +19 -24
gaussian_pipeline.py CHANGED
@@ -1,43 +1,38 @@
1
  from spaces import GPU # Hugging Face Spaces์—์„œ GPU ์‚ฌ์šฉ
2
 
3
  @GPU()
4
- def reconstruct_3d_gaussian(keyframes, output_dir, device="cpu"):
5
- import os
6
- import numpy as np
7
- import cv2
8
- import trimesh
9
- from sklearn.decomposition import PCA
 
 
10
 
11
  points_3d = []
12
 
13
- for frame_path in keyframes:
14
- image = cv2.imread(frame_path, cv2.IMREAD_GRAYSCALE)
15
  if image is None:
16
  continue
17
 
18
- ys, xs = np.where(image > 128)
19
- pixels = np.stack([xs, ys], axis=-1)
20
 
21
- if pixels.shape[0] < 3: # ์ตœ์†Œ 3๊ฐœ ํฌ์ธํŠธ ์—†์œผ๋ฉด PCA ์‹คํŒจ
22
- continue
23
-
24
- # 2D โ†’ 3D PCA
25
- pca = PCA(n_components=3)
26
- try:
27
- points_3d_frame = pca.fit_transform(pixels)
28
- points_3d.append(points_3d_frame)
29
- except Exception as e:
30
- print(f"PCA failed on {frame_path}: {e}")
31
- continue
32
 
33
  if not points_3d:
34
- raise ValueError("No valid 3D points were reconstructed.")
35
 
36
- all_points = np.concatenate(points_3d, axis=0)
37
  mesh = trimesh.points.PointCloud(all_points)
38
 
39
  os.makedirs(output_dir, exist_ok=True)
40
- output_model_path = os.path.join(output_dir, "reconstructed_model.obj")
41
  mesh.export(output_model_path)
42
 
43
  return output_model_path
 
 
1
  from spaces import GPU # Hugging Face Spaces์—์„œ GPU ์‚ฌ์šฉ
2
 
3
  @GPU()
4
+ from ultralytics import YOLO
5
+ import numpy as np
6
+ import cv2
7
+ import os
8
+ import trimesh
9
+
10
+ def reconstruct_3d_from_yolo(frames, output_dir, device="cuda"):
11
+ model = YOLO("yolov8n.pt") # ๊ฒฝ๋Ÿ‰ ๋ชจ๋ธ ์‚ฌ์šฉ
12
 
13
  points_3d = []
14
 
15
+ for idx, frame_path in enumerate(frames):
16
+ image = cv2.imread(frame_path)
17
  if image is None:
18
  continue
19
 
20
+ results = model(image)[0]
 
21
 
22
+ for box in results.boxes.xywh: # ์ค‘์‹ฌ ์ขŒํ‘œ (x_center, y_center, width, height)
23
+ x, y, w, h = box[:4].cpu().numpy()
24
+ z = idx * 10 # ํ”„๋ ˆ์ž„ ์ˆœ์„œ์— ๋”ฐ๋ผ z๊ฐ’ ์ฆ๊ฐ€์‹œ์ผœ ๊ฐ€์ƒ์˜ ๊นŠ์ด ๋ถ€์—ฌ
25
+ points_3d.append([x, y, z])
 
 
 
 
 
 
 
26
 
27
  if not points_3d:
28
+ raise ValueError("No valid YOLO 3D points were reconstructed.")
29
 
30
+ all_points = np.array(points_3d)
31
  mesh = trimesh.points.PointCloud(all_points)
32
 
33
  os.makedirs(output_dir, exist_ok=True)
34
+ output_model_path = os.path.join(output_dir, "yolo_3d_model.obj")
35
  mesh.export(output_model_path)
36
 
37
  return output_model_path
38
+