-
Notifications
You must be signed in to change notification settings - Fork 0
/
welcome.py
188 lines (156 loc) · 8.51 KB
/
welcome.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2024 SICK AG, Waldkirch
#
# SPDX-License-Identifier: Unlicense
import argparse
import os
import time
from time import sleep
from base.python.Control import Control
from base.python.PointCloud.PointCloud import (convertToPointCloud,
convertToPointCloudOptimized,
writePointCloudToPCD,
writePointCloudToPLY)
from base.python.Stream import Streaming
from base.python.Streaming import Data
from base.python.Streaming.BlobServerConfiguration import BlobClientConfig
from shared.python.framewrite import writeFrame
from shared.python.devices_config import get_device_config
def runWelcomeDemo(ip_address: str, cola_protocol: str, control_port: int, device_type: str):
# Directories to save the output in
pcl_dir = 'VisionaryToPointCloud'
img_dir = 'VisionaryImages'
os.makedirs(pcl_dir, exist_ok=True)
os.makedirs(img_dir, exist_ok=True)
# Connect to control channel
device_control = Control(ip_address, cola_protocol, control_port)
device_control.open()
# Stop image acquisition
device_control.stopStream()
# Wait for stop command to be complete
sleep(0.1)
# Login to the device for access rights to certain methods
device_control.login(Control.USERLEVEL_SERVICE, 'CUST_SERV')
# Streaming settings
streaming_settings = BlobClientConfig(device_control)
streaming_device = None
streaming_settings.setTransportProtocol(
streaming_settings.PROTOCOL_TCP)
streaming_settings.setBlobTcpPort(2114)
# Open streaming channel
streaming_device = Streaming(ip_address, 2114)
streaming_device.openStream()
# Logout after configuration
device_control.logout()
# Take a snapshot
device_control.singleStep()
sensor_data = Data.Data()
streaming_device.getFrame()
whole_frame = streaming_device.frame
sensor_data.read(whole_frame, convertToMM=False)
print("Data Timestamp [YYYY-MM-DD HH:MM:SS.mm] = %04u-%02u-%02u %02u:%02u:%02u.%03u" % (
sensor_data.getDecodedTimestamp()))
if sensor_data.hasDepthMap:
frame_number = sensor_data.depthmap.frameNumber
print("Data contains depth map data:")
print("=== Write PNG file: Frame number: {}".format(frame_number))
writeFrame(device_type, sensor_data,
os.path.join(img_dir, ""))
print("=== Converting image to pointcloud")
# Non optimized
start_time = time.time()
world_coordinates, dist_data = convertToPointCloud(sensor_data.depthmap.distance,
sensor_data.depthmap.intensity,
sensor_data.depthmap.confidence,
sensor_data.cameraParams, sensor_data.xmlParser.stereo)
end_time = time.time()
execution_time = end_time - start_time
print(f"convertToPointCloud took: {execution_time:.3}s")
# Optimized
is_stereo = True if device_type == "Visionary-S" else False
start_time = time.time()
point_cloud = convertToPointCloudOptimized(sensor_data.depthmap.distance,
sensor_data.depthmap.confidence,
sensor_data.cameraParams, is_stereo)
end_time = time.time()
execution_time = end_time - start_time
print(f"convertToPointCloudOptimized took: {execution_time:.3}s")
# Write output of the non optimized function to PLY
writePointCloudToPLY(os.path.join(
pcl_dir, "world_coordinates{}.ply".format(frame_number)), world_coordinates)
# Write output of the optimized function to PCD
writePointCloudToPCD(os.path.join(
pcl_dir, "world_coordinates{}.pcd".format(frame_number)), point_cloud.reshape(-1, point_cloud.shape[-1]))
# Stop image aqcuisition
device_control.stopStream()
# Login to the device for access rights to certain methods
device_control.login(Control.USERLEVEL_SERVICE, 'CUST_SERV')
# change one frontend setting and take another image
if device_type == "Visionary-T Mini":
device_control.setFramePeriodUs(60000)
print("\nSet frame period to 60000 μs.\n")
elif device_type == "Visionary-S":
device_control.setIntegrationTimeUs(3000)
print("\nSet integration time to 3000 μs.\n")
# Logout after configuration
device_control.logout()
# Take a snapshot
device_control.singleStep()
sensor_data = Data.Data()
streaming_device.getFrame()
whole_frame = streaming_device.frame
sensor_data.read(whole_frame, convertToMM=False)
print("Data Timestamp [YYYY-MM-DD HH:MM:SS.mm] = %04u-%02u-%02u %02u:%02u:%02u.%03u" % (
sensor_data.getDecodedTimestamp()))
if sensor_data.hasDepthMap:
frame_number = sensor_data.depthmap.frameNumber
print("Data contains depth map data:")
print("=== Write PNG file: Frame number: {}".format(frame_number))
writeFrame(device_type, sensor_data,
os.path.join(img_dir, ""))
print("=== Converting image to pointcloud")
# Non optimized
start_time = time.time()
world_coordinates, dist_data = convertToPointCloud(sensor_data.depthmap.distance,
sensor_data.depthmap.intensity,
sensor_data.depthmap.confidence,
sensor_data.cameraParams, sensor_data.xmlParser.stereo)
end_time = time.time()
execution_time = end_time - start_time
print(f"convertToPointCloud took: {execution_time:.3}s")
# Optimized
is_stereo = True if device_type == "Visionary-S" else False
start_time = time.time()
point_cloud = convertToPointCloudOptimized(sensor_data.depthmap.distance,
sensor_data.depthmap.confidence,
sensor_data.cameraParams, is_stereo)
end_time = time.time()
execution_time = end_time - start_time
print(f"convertToPointCloudOptimized took: {execution_time:.3}s")
# Write output of the non optimized function to PLY
writePointCloudToPLY(os.path.join(
pcl_dir, "world_coordinates{}.ply".format(frame_number)), world_coordinates)
# Write output of the optimized function to PCD
writePointCloudToPCD(os.path.join(
pcl_dir, "world_coordinates{}.pcd".format(frame_number)), point_cloud.reshape(-1, point_cloud.shape[-1]))
# Stop image aqcuisition
device_control.stopStream()
# Close streaming channel
streaming_device.closeStream()
# Logout and close
device_control.close()
print("Logout and close.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="This welcome demo shows what is possible with the API.")
parser.add_argument('-i', '--ipAddress', required=False, type=str,
default="192.168.1.10", help="The ip address of the device.")
parser.add_argument('-d', '--device_type', required=False, type=str,
default="Visionary-T Mini", choices=["Visionary-S", "Visionary-T Mini"],
help="Visionary product type.")
args = parser.parse_args()
cola_protocol, control_port, _ = get_device_config(args.device_type)
runWelcomeDemo(args.ipAddress, cola_protocol,
control_port, args.device_type)