Files
3Dscan/main.py
T

109 lines
2.6 KiB
Python
Raw Normal View History

2024-05-29 10:11:05 -06:00
from freenect2 import Device, FrameType
2024-06-02 00:24:56 -06:00
import psutil
import cv2
2024-05-29 10:11:05 -06:00
import open3d as o3d
import numpy as np
from threading import Thread
from copy import deepcopy
voxel_size = 0.005
vis = o3d.visualization.Visualizer()
vis.create_window(height=480, width=640)
reconstruction = o3d.geometry.PointCloud()
reconstruction.points = o3d.utility.Vector3dVector(np.random.random((5, 3)))
vis.add_geometry(reconstruction)
2024-06-02 00:24:56 -06:00
threads = []
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
def parse_frame(depth_frame, color_frame):
2024-05-29 10:11:05 -06:00
global reconstruction
2024-06-02 00:24:56 -06:00
undistorted, registered, big_depth = device.registration.apply(depth_frame,
color_frame,
with_big_depth=True)
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
cv2.imshow("1", np.array(registered.to_image()))
# points = device.registration.get_points_xyz_array(undistorted)
# points = points[~np.isnan(points).any(axis=2)]
# points = points.reshape(-1, 3) / 5
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
# print(points.shape)
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
# if np.isnan(points).any():
# print("nan")
# return
2024-05-29 10:11:05 -06:00
#
2024-06-02 00:24:56 -06:00
# reconstruction.points = o3d.utility.Vector3dVector(points)
# vis.update_geometry(reconstruction)
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
print("123")
# global threads
# threads.pop(thread_index)
2024-05-29 10:11:05 -06:00
running = True
2024-06-02 00:24:56 -06:00
device = Device()
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
depth_frame = None
color_frame = None
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
n_cpus = psutil.cpu_count()
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
with device.running():
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
for type_, frame in device:
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
if type_ == FrameType.Depth:
depth_frame = frame
elif type_ == FrameType.Color:
color_frame = frame
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
if depth_frame is not None and \
color_frame is not None:
#
if len(threads) > 100:
depth_frame = None
color_frame = None
continue
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
undistorted, registered, big_depth = device.registration.apply(depth_frame,
color_frame,
with_big_depth=True)
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
cv2.imshow('Grayscale', np.array(registered.to_image()))
2024-05-29 10:11:05 -06:00
2024-06-02 00:24:56 -06:00
# cv2.imshow("1", np.array(registered.to_image()))
# thread = Thread(target=parse_frame, args=(depth_frame, color_frame))
# threads.append(thread)
# thread.start()
#
# print(len(threads))
#
# depth_frame = None
# color_frame = None
2024-05-29 10:11:05 -06:00
if not vis.poll_events():
break
vis.update_renderer()
vis.close()
running = False
2024-06-02 00:24:56 -06:00
print("joining threads...")
2024-05-29 10:11:05 -06:00
for thread in threads:
2024-06-02 00:24:56 -06:00
thread.join()