feat: render pokemon to Open3D
This commit is contained in:
parent
3ad49bfe20
commit
048f15cb08
20
main.py
20
main.py
@ -1,6 +1,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import open3d as o3d
|
import open3d as o3d
|
||||||
import cv2
|
import cv2
|
||||||
|
from pokemon import Pokemon
|
||||||
|
|
||||||
|
|
||||||
PAPER_WCS_POINT = np.array([[0, 0, 0], [18.5, 0, 0], [0, 26, 0], [18.5, 26, 0]], dtype=np.float32)
|
PAPER_WCS_POINT = np.array([[0, 0, 0], [18.5, 0, 0], [0, 26, 0], [18.5, 26, 0]], dtype=np.float32)
|
||||||
@ -81,7 +82,7 @@ def plot_corner_points(frame, approx):
|
|||||||
cv2.circle(frame, (x, y), 30, (255, 0, 0), -1) # 在角點位置畫紅色圓圈
|
cv2.circle(frame, (x, y), 30, (255, 0, 0), -1) # 在角點位置畫紅色圓圈
|
||||||
|
|
||||||
|
|
||||||
def visualization(inverse_matrix_M):
|
def visualization(inverse_matrix_M, pokemon):
|
||||||
box_size = 0.3
|
box_size = 0.3
|
||||||
|
|
||||||
# initialize visualizer
|
# initialize visualizer
|
||||||
@ -113,6 +114,14 @@ def visualization(inverse_matrix_M):
|
|||||||
vis.add_geometry(paper_triangle_set)
|
vis.add_geometry(paper_triangle_set)
|
||||||
|
|
||||||
|
|
||||||
|
# Pokemon model
|
||||||
|
pcd = o3d.geometry.PointCloud()
|
||||||
|
pcd.points = o3d.utility.Vector3dVector(pokemon.get_position())
|
||||||
|
pcd.colors = o3d.utility.Vector3dVector(pokemon.get_colors())
|
||||||
|
vis.add_geometry(pcd)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# 下面是畫相機
|
# 下面是畫相機
|
||||||
# 找 金字塔端點
|
# 找 金字塔端點
|
||||||
points = []
|
points = []
|
||||||
@ -182,11 +191,13 @@ def visualization(inverse_matrix_M):
|
|||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
# 設定連接到 Android 手機的相機
|
# 設定連接到 Android 手機的相機
|
||||||
cap = cv2.VideoCapture(0) # 0 表示第一個相機(通常是後置相機),若是前置相機,可以使用 1
|
# cap = cv2.VideoCapture(0) # 0 表示第一個相機(通常是後置相機),若是前置相機,可以使用 1
|
||||||
# cap = cv2.VideoCapture('demo1.mp4')
|
cap = cv2.VideoCapture('demo1.mp4')
|
||||||
|
|
||||||
previous_approx = []
|
previous_approx = []
|
||||||
|
|
||||||
|
pokemon = Pokemon('water', 'cat')
|
||||||
|
|
||||||
rotation_vectors = []
|
rotation_vectors = []
|
||||||
translation_vectors = []
|
translation_vectors = []
|
||||||
while True:
|
while True:
|
||||||
@ -274,7 +285,8 @@ if __name__ == '__main__':
|
|||||||
M = np.concatenate((rt, [[0, 0, 0, 1]]), axis=0)
|
M = np.concatenate((rt, [[0, 0, 0, 1]]), axis=0)
|
||||||
M_inv.append(np.linalg.inv(M))
|
M_inv.append(np.linalg.inv(M))
|
||||||
|
|
||||||
visualization(M_inv)
|
# load Pokemon points
|
||||||
|
visualization(M_inv, pokemon)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
46
pokemon.py
Normal file
46
pokemon.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
class Pokemon():
|
||||||
|
def __init__(self, pokemon_type, animal):
|
||||||
|
self.type = pokemon_type
|
||||||
|
self.animal = animal
|
||||||
|
|
||||||
|
self.scalar = 10
|
||||||
|
|
||||||
|
self.x = 8
|
||||||
|
self.y = 13
|
||||||
|
self.z = 8
|
||||||
|
|
||||||
|
self.cloudpoints = {
|
||||||
|
'points': None,
|
||||||
|
'colors': None,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.load_model()
|
||||||
|
|
||||||
|
def load_model(self):
|
||||||
|
self.cloudpoints['points'] = np.load('models/{}_{}/0/points.npy'.format(self.type, self.animal))
|
||||||
|
self.cloudpoints['colors'] = np.load('models/{}_{}/0/colors.npy'.format(self.type, self.animal))
|
||||||
|
|
||||||
|
self.cloudpoints['colors'] = self.cloudpoints['colors'] / 255
|
||||||
|
self.cloudpoints['colors'] = self.cloudpoints['colors'][:, :3]
|
||||||
|
|
||||||
|
def get_position(self):
|
||||||
|
transform_matrix = np.array([
|
||||||
|
[self.scalar, 0, 0, 0],
|
||||||
|
[0, 0, self.scalar, 0],
|
||||||
|
[0, self.scalar, 0, 0],
|
||||||
|
[self.x, self.y, self.z, 0]]) # shift (前後, 左右, 高度)
|
||||||
|
|
||||||
|
pokemon_points = self.cloudpoints['points']
|
||||||
|
|
||||||
|
ones = np.ones((pokemon_points.shape[0], 1))
|
||||||
|
pokemon_points = np.concatenate([pokemon_points, ones], axis=1)
|
||||||
|
pokemon_points = pokemon_points @ transform_matrix
|
||||||
|
pokemon_points = pokemon_points[:, :3]
|
||||||
|
|
||||||
|
return pokemon_points
|
||||||
|
|
||||||
|
def get_colors(self):
|
||||||
|
return self.cloudpoints['colors']
|
||||||
Loading…
Reference in New Issue
Block a user