|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from PIL import Image |
|
|
import numpy as np |
|
|
import cv2 |
|
|
import json |
|
|
import glob |
|
|
from collections import defaultdict |
|
|
|
|
|
def main(): |
|
|
|
|
|
date = '2021-10-22' |
|
|
hour = 12 |
|
|
frame_idx = 50 |
|
|
|
|
|
|
|
|
with open(f'calib.json', 'r') as fp: |
|
|
meta = json.load(fp) |
|
|
|
|
|
h, w = meta['h'], meta['w'] |
|
|
right_intrinsic = np.array(meta['right_intrinsic']) |
|
|
lidar_to_right = np.array(meta['lidar_to_right_cam']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lidar_files = glob.glob(f'lidar/{date}/{hour:0>2}*.hpl') |
|
|
lidar_data = load_lidar_data(date, hour, right_intrinsic, lidar_to_right) |
|
|
|
|
|
|
|
|
left_video = cv2.VideoCapture(f'left_images/{date}/{date}_{hour:0>2}.mp4') |
|
|
right_video = cv2.VideoCapture(f'right_images/{date}/{date}_{hour:0>2}.mp4') |
|
|
|
|
|
for _ in range(frame_idx): |
|
|
left_video.read() |
|
|
right_video.read() |
|
|
|
|
|
_, left_image = left_video.read() |
|
|
_, right_image = right_video.read() |
|
|
|
|
|
|
|
|
left_image = cv2.cvtColor(left_image, cv2.COLOR_BGR2RGB).astype('uint8') |
|
|
right_image = cv2.cvtColor(right_image, cv2.COLOR_BGR2RGB).astype('uint8') |
|
|
|
|
|
|
|
|
left_image = cv2.resize(left_image, (w, h)) |
|
|
right_image = cv2.resize(left_image, (w, h)) |
|
|
|
|
|
|
|
|
lidar_points = lidar_data[frame_idx] |
|
|
|
|
|
return left_image, right_image, lidar_points |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_lidar_data(date, hour, right_intrinsic, lidar_to_right): |
|
|
def flattenList(xss): |
|
|
return [x for xs in xss for x in xs] |
|
|
|
|
|
decimal_time = [] |
|
|
azi = [] |
|
|
elev = [] |
|
|
distance = [] |
|
|
backscatter = [] |
|
|
|
|
|
lidar_files = glob.glob(f'lidar/{date}/{date}_{hour:0>2}*.hpl') |
|
|
|
|
|
for lidar_file in lidar_files: |
|
|
ld = LidarData.fromfile(lidar_file) |
|
|
|
|
|
data_locs = ld.data_locs |
|
|
decimal_time.append(list(data_locs[:,0])) |
|
|
azi.append(list(data_locs[:,1])) |
|
|
elev.append(list(data_locs[:,2])) |
|
|
distance.append(list(ld.getDistance())) |
|
|
backscatter.append(list(ld.getBackscatter())) |
|
|
|
|
|
decimal_time = np.array(flattenList(decimal_time), ndmin=1) |
|
|
azi = np.array(flattenList(azi), ndmin=1) |
|
|
elev = np.array(flattenList(elev), ndmin=1) |
|
|
distance = np.array(flattenList(distance), ndmin=1) |
|
|
backscatter = np.array(flattenList(backscatter), ndmin=1) |
|
|
|
|
|
|
|
|
camera_time = hour + 10/3600 |
|
|
|
|
|
lidar_output = defaultdict(list) |
|
|
|
|
|
|
|
|
for frame_idx in range(717): |
|
|
|
|
|
for time in decimal_time: |
|
|
|
|
|
if np.abs(time-camera_time) < 2.5/3600: |
|
|
i = list(decimal_time).index(time) |
|
|
|
|
|
|
|
|
_, cloud_depth = findCloud_in_backscatter(backscatter[i], int(500 / 3)) |
|
|
|
|
|
if cloud_depth is not None: |
|
|
|
|
|
lidar_right_xy, lidar_right_depth = project_lidar_to_right(lidar_to_right, right_intrinsic, azi[i], |
|
|
elev[i], |
|
|
cloud_depth) |
|
|
|
|
|
|
|
|
lidar_output[frame_idx].append({'lidar_depth': cloud_depth, 'azi': azi[i], 'elev': elev[i], |
|
|
'right_cam_xy': lidar_right_xy, 'right_cam_depth': lidar_right_depth, |
|
|
'backscatter': backscatter[i]}) |
|
|
else: |
|
|
lidar_output[frame_idx].append({'azi': azi[i], 'elev': elev[i], 'backscatter': backscatter[i]}) |
|
|
|
|
|
camera_time += 5/3600 |
|
|
|
|
|
return lidar_output |
|
|
|
|
|
class LidarData(): |
|
|
def __init__(self, |
|
|
fname=None, |
|
|
system_id=None, |
|
|
num_gates=0, |
|
|
gate_length=0, |
|
|
gate_pulse_length=0, |
|
|
pulses_per_ray=0, |
|
|
start_time=None, |
|
|
data=None, |
|
|
data_locs=None): |
|
|
self.fname = fname |
|
|
self.system_id = system_id |
|
|
self.num_gates = num_gates |
|
|
self.gate_length = gate_length |
|
|
self.gate_pulse_length = gate_pulse_length |
|
|
self.pulses_per_ray = pulses_per_ray |
|
|
self.start_time = start_time |
|
|
self.data = data |
|
|
self.data_locs = data_locs |
|
|
|
|
|
@classmethod |
|
|
def fromfile(cls, filename): |
|
|
with open(filename) as f: |
|
|
header = [f.readline().split(':', maxsplit=1) for i in range(17)] |
|
|
|
|
|
fname = header[0][1].strip() |
|
|
system_id = header[1][1].strip() |
|
|
num_gates = int(header[2][1].strip()) |
|
|
gate_length = header[3][1].strip() |
|
|
gate_pulse_length = header[4][1].strip() |
|
|
pulses_per_ray = header[5][1].strip() |
|
|
start_time = header[9][1].strip() |
|
|
data_locs_format = header[13][0].split(' ')[0].strip() |
|
|
data_format = header[15][0].split(' ')[0].strip() |
|
|
|
|
|
data = [] |
|
|
data_locs = [] |
|
|
while True: |
|
|
try: |
|
|
data_locs_in = np.array(f.readline().split()).astype('float') |
|
|
if len(data_locs_in) == 0: |
|
|
break |
|
|
data_locs.append(data_locs_in) |
|
|
data.append(np.array( |
|
|
[f.readline().split() for i in range(num_gates)]).astype('float')) |
|
|
except: |
|
|
break |
|
|
data = np.array(data) |
|
|
data_locs = np.array(data_locs) |
|
|
|
|
|
return cls( |
|
|
fname=fname, |
|
|
system_id=system_id, |
|
|
num_gates=num_gates, |
|
|
gate_length=gate_length, |
|
|
gate_pulse_length=gate_pulse_length, |
|
|
pulses_per_ray=pulses_per_ray, |
|
|
start_time=start_time, |
|
|
data=data, |
|
|
data_locs=data_locs) |
|
|
|
|
|
|
|
|
def getDistance(self): |
|
|
return self.data[:,20:,0]*3 |
|
|
|
|
|
def getDoppler(self): |
|
|
return self.data[:,20:,1] |
|
|
|
|
|
def getBackscatter(self): |
|
|
return self.data[:,20:,2] |
|
|
|
|
|
def getBeta(self): |
|
|
return self.data[:,20:,3] |
|
|
|
|
|
|
|
|
def project_lidar_to_right(lidar_to_right_cam, right_intrinsic, azi, elev, depth): |
|
|
|
|
|
azi = -azi + 270 |
|
|
x = depth * np.cos(elev * np.pi / 180) * np.cos(azi * np.pi / 180) |
|
|
z = depth * np.cos(elev * np.pi / 180) * np.sin(azi * np.pi / 180) |
|
|
y = -depth * np.sin(elev * np.pi / 180) |
|
|
|
|
|
lidar_xyz = np.stack((x, y, z), axis=0) |
|
|
|
|
|
|
|
|
desired_shape = list(lidar_xyz.shape) |
|
|
desired_shape[0] = 1 |
|
|
cam_xyz = lidar_to_right_cam @ np.concatenate((lidar_xyz, np.ones(desired_shape)), axis=0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
projected_lidar = cam_xyz[:3] / cam_xyz[2] |
|
|
projected_lidar = right_intrinsic @ projected_lidar |
|
|
projected_lidar = projected_lidar[:2] |
|
|
|
|
|
|
|
|
right_cam_depth = np.sqrt(np.sum(cam_xyz ** 2, axis=0)) |
|
|
|
|
|
return projected_lidar, right_cam_depth |
|
|
|
|
|
def findCloud_in_backscatter(backscatter, dist_thresh=300): |
|
|
if np.max(backscatter) > 10: |
|
|
|
|
|
return (None, None) |
|
|
|
|
|
cloud = np.argmax(backscatter[dist_thresh:]) |
|
|
if backscatter[cloud+dist_thresh] - np.median(backscatter[dist_thresh:]) > 0.03: |
|
|
|
|
|
return cloud+dist_thresh, (cloud+dist_thresh+20)*3 |
|
|
else: |
|
|
|
|
|
return (None, None) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
left_image, right_image, lidar_points = main() |