-
Notifications
You must be signed in to change notification settings - Fork 12
/
convert_zdf.py
143 lines (100 loc) · 4.28 KB
/
convert_zdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
Convert point cloud data from a ZDF file to your preferred format (PLY, CSV, TXT, PNG, JPG, BMP, TIFF).
Example: $ python convert_zdf.py --ply Zivid3D.zdf
Available formats:
PLY - Polygon File Format
CSV, TXT - [X, Y, Z, r, g, b, SNR]
PNG, JPG, BMP, TIFF - 2D RGB image
"""
import argparse
from pathlib import Path
import cv2
import numpy as np
import zivid
def _options() -> argparse.Namespace:
"""Function for taking in arguments from user.
Returns:
Argument from user
"""
parser = argparse.ArgumentParser(
description="Convert from a ZDF to your preferred format\
\nExample:\n\t $ python convert_zdf.py --ply Zivid3D.zdf",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument("filename", help="File to convert", default="Zivid3D.zdf")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--ply", action="store_true", help="Convert to PLY")
group.add_argument("--csv", action="store_true", help="Convert to CSV")
group.add_argument("--txt", action="store_true", help="Convert to text")
group.add_argument("--jpg", action="store_true", help="Convert to JPEG (3D->2D)")
group.add_argument("--tiff", action="store_true", help="Convert to TIFF (3D->2D)")
group.add_argument(
"--png",
action="store_true",
help="Convert to Portable Network Graphics (3D->2D)",
)
group.add_argument("--bmp", action="store_true", help="Convert to Windows bitmap (3D->2D)")
return parser.parse_args()
def _flatten_point_cloud(point_cloud: zivid.PointCloud) -> np.ndarray:
"""Convert from point cloud to flattened point cloud (with numpy).
Args:
point_cloud: A handle to point cloud in the GPU memory
Returns:
A 2D numpy array, with 8 columns and npixels rows
"""
# Convert to numpy 3D array
point_cloud = np.dstack([point_cloud.copy_data("xyz"), point_cloud.copy_data("rgba"), point_cloud.copy_data("snr")])
# Flattening the point cloud
flattened_point_cloud = point_cloud.reshape(-1, 8)
# Removing nans
return flattened_point_cloud[~np.isnan(flattened_point_cloud[:, 0]), :]
def _convert_2_ply(frame: zivid.Frame, file_name: str) -> None:
"""Convert from frame to PLY.
Args:
frame: A frame captured by a Zivid camera
file_name: File name without extension
"""
print(f"Saving the frame to {file_name}.ply")
frame.save(f"{file_name}.ply")
def _convert_2_csv(point_cloud: zivid.PointCloud, file_name: str) -> None:
"""Convert from point cloud to CSV or TXT.
Args:
point_cloud: A handle to point cloud in the GPU memory
file_name: File name with extension
"""
print(f"Saving the frame to {file_name}")
np.savetxt(file_name, _flatten_point_cloud(point_cloud), delimiter=",", fmt="%.3f")
def _convert_2_2d(point_cloud: zivid.PointCloud, file_name: str) -> None:
"""Convert from point cloud to 2D image.
Args:
point_cloud: A handle to point cloud in the GPU memory
file_name: File name without extension
"""
print(f"Saving the frame to {file_name}")
bgra = point_cloud.copy_data("bgra")
cv2.imwrite(file_name, bgra[:, :, :3])
def _main() -> None:
user_options = _options()
file_path = Path(user_options.filename)
if not file_path.exists():
raise FileNotFoundError(f"{user_options.filename} does not exist")
with zivid.Application():
print(f"Reading point cloud from file: {user_options.filename}")
frame = zivid.Frame(user_options.filename)
point_cloud = frame.point_cloud()
if user_options.ply:
_convert_2_ply(frame, file_path.stem)
elif user_options.csv:
_convert_2_csv(point_cloud, file_path.stem + ".csv")
elif user_options.txt:
_convert_2_csv(point_cloud, file_path.stem + ".txt")
elif user_options.png:
_convert_2_2d(point_cloud, file_path.stem + ".png")
elif user_options.jpg:
_convert_2_2d(point_cloud, file_path.stem + ".jpg")
elif user_options.bmp:
_convert_2_2d(point_cloud, file_path.stem + ".bmp")
elif user_options.tiff:
_convert_2_2d(point_cloud, file_path.stem + ".tiff")
if __name__ == "__main__":
_main()