util.py 15.7 KB
Newer Older
S
shuluoshu 已提交
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
"""
This code is based on https://github.com/garrickbrazil/M3D-RPN/blob/master/lib/util.py

This file is meant to contain generic utility functions
which can be easily re-used in any project, and are not
specific to any project or framework (except python!).
"""

import os
import sys
from glob import glob
from time import time
import matplotlib.pyplot as plt
import numpy as np
import importlib
import pickle
import logging
import datetime
import pprint
import shutil
import math
import copy
import cv2
from mpl_toolkits.mplot3d import Axes3D
from PIL import Image


def copyfile(src, dst):
    """
	copyfile
	"""
    shutil.copyfile(src, dst)


def pretty_print(name, input, val_width=40, key_width=0):
    """
	This function creates a formatted string from a given dictionary input.
	It may not support all data types, but can probably be extended.

	Args:
		name (str): name of the variable root
		input (dict): dictionary to print
		val_width (int): the width of the right hand side values
		key_width (int): the minimum key width, (always auto-defaults to the longest key!)

	Example:
		pretty_str = pretty_print('conf', conf.__dict__)
		pretty_str = pretty_print('conf', {'key1': 'example', 'key2': [1,2,3,4,5], 'key3': np.random.rand(4,4)})

		print(pretty_str)
		or
		logging.info(pretty_str)
	"""

    # root
    pretty_str = name + ': {\n'

    # determine key width
    for key in input.keys():
        key_width = max(key_width, len(str(key)) + 4)

    # cycle keys
    for key in input.keys():

        val = input[key]

        # round values to 3 decimals..
        if type(val) == np.ndarray: val = np.round(val, 3).tolist()

        # difficult formatting
        val_str = str(val)
        if len(val_str) > val_width:
            val_str = pprint.pformat(val, width=val_width)
            val_str = val_str.replace('\n', '\n{tab}')
            tab = ('{0:' + str(4 + key_width) + '}').format('')
            val_str = val_str.replace('{tab}', tab)

        # more difficult formatting
        format_str = '{0:' + str(4) + '}{1:' + str(key_width) + '} {2:' + str(
            val_width) + '}\n'
        pretty_str += format_str.format('', key + ':', val_str)

    # close root object
    pretty_str += '}'

    return pretty_str


def absolute_import(file_path):
    """
	Imports a python module / file given its ABSOLUTE path.

	Args:
		 file_path (str): absolute path to a python file to attempt to import
	"""

    # module name
    _, name, _ = file_parts(file_path)

    # load the spec and module
    spec = importlib.util.spec_from_file_location(name, file_path)
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)

    return module


def init_log_file(folder_path, suffix=None, log_level=logging.INFO):
    """
	This function inits a log file given a folder to write the log to.
	it automatically adds a timestamp and optional suffix to the log.
	Anything written to the log will automatically write to console too.

	Example:
		import logging

		init_log_file('output/logs/')
		logging.info('this will show up in both the log AND console!')
	"""

    timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
    log_format = '[%(levelname)s]: %(asctime)s %(message)s'

    if suffix is not None:
        file_name = timestamp + '_' + suffix
    else:
        file_name = timestamp

    file_path = os.path.join(folder_path, file_name)
    logging.basicConfig(filename=file_path, level=log_level, format=log_format)
    logging.getLogger().addHandler(logging.StreamHandler(sys.stdout))

    return file_path


def denorm_image(im, image_means, image_stds):
    """
	:param im:
	:param image_means:
	:param image_stds:
	:return:
	"""
    im = copy.deepcopy(im)
    im[:, :, 0] *= image_stds[0]
    im[:, :, 1] *= image_stds[1]
    im[:, :, 2] *= image_stds[2]

    im[:, :, 0] += image_means[0]
    im[:, :, 1] += image_means[1]
    im[:, :, 2] += image_means[2]

    return im


def compute_eta(start_time, idx, total):
    """
	Computes the estimated time as a formatted string as well
	as the change in delta time dt.

	Example:
		from time import time

		start_time = time()

		for i in range(0, total):
			<lengthly computation>
			time_str, dt = compute_eta(start_time, i, total)
	"""

    dt = (time() - start_time) / idx
    timeleft = np.max([dt * (total - idx), 0])
    if timeleft > 3600:
        time_str = '{:.1f}h'.format(timeleft / 3600)
    elif timeleft > 60:
        time_str = '{:.1f}m'.format(timeleft / 60)
    else:
        time_str = '{:.1f}s'.format(timeleft)

    return time_str, dt


def interp_color(dist,
                 bounds=[0, 1],
                 color_lo=(0, 0, 250),
                 color_hi=(0, 250, 250)):
    """
	:param dist:
	:param bounds:
	:param color_lo:
	:param color_hi:
	:return:
	"""
    percent = (dist - bounds[0]) / (bounds[1] - bounds[0])
    b = color_lo[0] * (1 - percent) + color_hi[0] * percent
    g = color_lo[1] * (1 - percent) + color_hi[1] * percent
    r = color_lo[2] * (1 - percent) + color_hi[2] * percent

    return (b, g, r)


def create_colorbar(height, width, color_lo=(0, 0, 250),
                    color_hi=(0, 250, 250)):
    """
	:param height:
	:param width:
	:param color_lo:
	:param color_hi:
	:return:
	"""
    im = np.zeros([height, width, 3])

    for h in range(0, height):
        color = interp_color(h + 0.5, [0, height], color_hi, color_lo)
        im[h, :, 0] = (color[0])
        im[h, :, 1] = (color[1])
        im[h, :, 2] = (color[2])

    return im.astype(np.uint8)


def mkdir_if_missing(directory, delete_if_exist=False):
    """
	Recursively make a directory structure even if missing.

	if delete_if_exist=True then we will delete it first
	which can be useful when better control over initialization is needed.
	"""

    if delete_if_exist and os.path.exists(directory): shutil.rmtree(directory)

    # check if not exist, then make
    if not os.path.exists(directory):
        os.makedirs(directory)


def list_files(base_dir, file_pattern):
    """
	Returns a list of files given a directory and pattern
	The results are sorted alphabetically

	Example:
		files = list_files('path/to/images/', '*.jpg')
	"""

    return sorted(glob(os.path.join(base_dir) + file_pattern))


def file_parts(file_path):
    """
	Lists a files parts such as base_path, file name and extension

	Example
		base, name, ext = file_parts('path/to/file/dog.jpg')
		print(base, name, ext) --> ('path/to/file/', 'dog', '.jpg')
	"""

    base_path, tail = os.path.split(file_path)
    name, ext = os.path.splitext(tail)

    return base_path, name, ext


def pickle_write(file_path, obj):
    """
	Serialize an object to a provided file_path
	"""

    with open(file_path, 'wb') as file:
        pickle.dump(obj, file)


def pickle_read(file_path):
    """
	De-serialize an object from a provided file_path
	"""

    with open(file_path, 'rb') as file:
        return pickle.load(file)


def get_color(ind, hex=False):
    """
	:param ind:
	:param hex:
	:return:
	"""
    colors = [(111, 74, 0), (81, 0, 81), (128, 64, 128), (244, 35, 232),
              (250, 170, 160), (230, 150, 140), (70, 70, 70), (102, 102, 156),
              (190, 153, 153), (180, 165, 180), (150, 100, 100), (150, 120, 90),
              (153, 153, 153), (250, 170, 30), (220, 220, 0), (107, 142, 35),
              (152, 251, 152), (70, 130, 180), (220, 20, 60), (255, 0, 0),
              (0, 0, 142), (0, 0, 70), (0, 60, 100), (0, 0, 90), (0, 0, 110),
              (0, 80, 100), (0, 0, 230), (119, 11, 32), (0, 0, 142)]

    color = colors[ind % len(colors)]

    if hex:
        return '#%02x%02x%02x' % (color[0], color[1], color[2])
    else:
        return color


def draw_3d_box(im, verts, color=(0, 200, 200), thickness=1):
    """
	:param im:
	:param verts:
	:param color:
	:param thickness:
	:return:
	"""
    for lind in range(0, verts.shape[0] - 1):
        v1 = verts[lind]
        v2 = verts[lind + 1]
        cv2.line(im, (int(v1[0]), int(v1[1])), (int(v2[0]), int(v2[1])), color,
                 thickness)


def draw_bev(canvas_bev,
             z3d,
             l3d,
             w3d,
             x3d,
             ry3d,
             color=(0, 200, 200),
             scale=1,
             thickness=2):
    """
	:param canvas_bev:
	:param z3d:
	:param l3d:
	:param w3d:
	:param x3d:
	:param ry3d:
	:param color:
	:param scale:
	:param thickness:
	:return:
	"""
    w = l3d * scale
    l = w3d * scale
    x = x3d * scale
    z = z3d * scale
    r = ry3d * -1

    corners1 = np.array([[-w / 2, -l / 2, 1], [+w / 2, -l / 2, 1],
                         [+w / 2, +l / 2, 1], [-w / 2, +l / 2, 1]])

    ry = np.array([
        [+math.cos(r), -math.sin(r), 0],
        [+math.sin(r), math.cos(r), 0],
        [0, 0, 1],
    ])

    corners2 = ry.dot(corners1.T).T

    corners2[:, 0] += w / 2 + x + canvas_bev.shape[1] / 2
    corners2[:, 1] += l / 2 + z

    draw_line(
        canvas_bev, corners2[0], corners2[1], color=color, thickness=thickness)
    draw_line(
        canvas_bev, corners2[1], corners2[2], color=color, thickness=thickness)
    draw_line(
        canvas_bev, corners2[2], corners2[3], color=color, thickness=thickness)
    draw_line(
        canvas_bev, corners2[3], corners2[0], color=color, thickness=thickness)


def draw_line(im, v1, v2, color=(0, 200, 200), thickness=1):
    """
	:param im:
	:param v1:
	:param v2:
	:param color:
	:param thickness:
	:return:
	"""
    cv2.line(im, (int(v1[0]), int(v1[1])), (int(v2[0]), int(v2[1])), color,
             thickness)


def draw_circle(im,
                pos,
                radius=5,
                thickness=1,
                color=(250, 100, 100),
                fill=True):
    """
	:param im:
	:param pos:
	:param radius:
	:param thickness:
	:param color:
	:param fill:
	:return:
	"""
    if fill: thickness = -1

    cv2.circle(
        im, (int(pos[0]), int(pos[1])),
        radius,
        color=color,
        thickness=thickness)


def draw_2d_box(im, box, color=(0, 200, 200), thickness=1):
    """
	:param im:
	:param box:
	:param color:
	:param thickness:
	:return:
	"""
    x = box[0]
    y = box[1]
    w = box[2]
    h = box[3]
    x2 = (x + w) - 1
    y2 = (y + h) - 1

    cv2.rectangle(im, (int(x), int(y)), (int(x2), int(y2)), color, thickness)


def imshow(im, fig_num=None):
    """
	:param im:
	:param fig_num:
	:return:
	"""
    if fig_num is not None: plt.figure(fig_num)

    if len(im.shape) == 2:
        im = np.tile(im, [3, 1, 1]).transpose([1, 2, 0])

    plt.imshow(cv2.cvtColor(im.astype(np.uint8), cv2.COLOR_RGB2BGR))
    plt.show(block=False)


def imwrite(im, path):
    """
	:param im:
	:param path:
	:return:
	"""
    cv2.imwrite(path, im)


def imread(path):
    """
	:param path:
	:return:
	"""
    return cv2.imread(path)


def draw_tick_marks(im, ticks):
    """
	:param im:
	:param ticks:
	:return:
	"""
    ticks_loc = list(
        range(0, im.shape[0] + 1, int((im.shape[0]) / (len(ticks) - 1))))

    for tind, tick in enumerate(ticks):
        y = min(max(ticks_loc[tind], 50), im.shape[0] - 10)
        x = im.shape[1] - 115

        draw_text(
            im,
            '-{}m'.format(tick), (x, y),
            lineType=2,
            scale=1.1,
            bg_color=None)


def draw_text(im,
              text,
              pos,
              scale=0.4,
              color=(0, 0, 0),
              font=cv2.FONT_HERSHEY_SIMPLEX,
              bg_color=(0, 255, 255),
              blend=0.33,
              lineType=1):
    """
	:param im:
	:param text:
	:param pos:
	:param scale:
	:param color:
	:param font:
	:param bg_color:
	:param blend:
	:param lineType:
	:return:
	"""
    pos = [int(pos[0]), int(pos[1])]

    if bg_color is not None:
        text_size, _ = cv2.getTextSize(text, font, scale, lineType)
        x_s = int(np.clip(pos[0], a_min=0, a_max=im.shape[1]))
        x_e = int(
            np.clip(
                pos[0] + text_size[0] - 1 + 4, a_min=0, a_max=im.shape[1]))
        y_s = int(
            np.clip(
                pos[1] - text_size[1] - 2, a_min=0, a_max=im.shape[0]))
        y_e = int(np.clip(pos[1] + 1 - 2, a_min=0, a_max=im.shape[0]))

        im[y_s:y_e + 1, x_s:x_e + 1, 0] = im[
            y_s:y_e + 1, x_s:x_e + 1, 0] * blend + bg_color[0] * (1 - blend)
        im[y_s:y_e + 1, x_s:x_e + 1, 1] = im[
            y_s:y_e + 1, x_s:x_e + 1, 1] * blend + bg_color[1] * (1 - blend)
        im[y_s:y_e + 1, x_s:x_e + 1, 2] = im[
            y_s:y_e + 1, x_s:x_e + 1, 2] * blend + bg_color[2] * (1 - blend)

        pos[0] = int(np.clip(pos[0] + 2, a_min=0, a_max=im.shape[1]))
        pos[1] = int(np.clip(pos[1] - 2, a_min=0, a_max=im.shape[0]))

    cv2.putText(im, text, tuple(pos), font, scale, color, lineType)


# Calculates rotation matrix to euler angles
# The result is the same as MATLAB except the order
# of the euler angles ( x and z are swapped ).
# adopted from https://www.learnopencv.com/rotation-matrix-to-euler-angles/
def mat2euler(R):
    """
	:param R:
	:return:
	"""
    sy = math.sqrt(R[0, 0] * R[0, 0] + R[1, 0] * R[1, 0])

    singular = sy < 1e-6

    if not singular:
        x = math.atan2(R[2, 1], R[2, 2])
        y = math.atan2(-R[2, 0], sy)
        z = math.atan2(R[1, 0], R[0, 0])

    else:
        raise ValueError('singular matrix found in mat2euler')

    return np.array([x, y, z])


def fig_to_im(fig):
    """
	:param fig:
	:return:
	"""
    fig.canvas.draw()

    # Get the RGBA buffer from the figure
    w, h = fig.canvas.get_width_height()
    buf = np.fromstring(fig.canvas.tostring_argb(), dtype=np.uint8)
    buf.shape = (w, h, 4)

    # canvas.tostring_argb give pixmap in ARGB mode. Roll the ALPHA channel to have it in RGBA mode
    buf = np.roll(buf, 3, axis=2)

    w, h, d = buf.shape

    im_pil = Image.frombytes("RGBA", (w, h), buf.tostring())
    im_np = np.array(im_pil)[:, :, :3]

    return im_np


def imzoom(im, zoom=0):
    """
	:param im:
	:param zoom:
	:return:
	"""
    # single value passed in for both axis?
    # extend same val for w, h
    zoom = np.array(zoom)
    if zoom.size == 1: zoom = np.array([zoom, zoom])

    zoom = np.clip(zoom, a_min=0, a_max=0.99)

    cx = im.shape[1] / 2
    cy = im.shape[0] / 2

    w = im.shape[1] * (1 - zoom[0])
    h = im.shape[0] * (1 - zoom[-1])

    x1 = int(np.clip(cx - w / 2, a_min=0, a_max=im.shape[1] - 1))
    x2 = int(np.clip(cx + w / 2, a_min=0, a_max=im.shape[1] - 1))
    y1 = int(np.clip(cy - h / 2, a_min=0, a_max=im.shape[0] - 1))
    y2 = int(np.clip(cy + h / 2, a_min=0, a_max=im.shape[0] - 1))

    im = im[y1:y2 + 1, x1:x2 + 1, :]

    return im


def imhstack(im1, im2):
    """
	:param im1:
	:param im2:
	:return:
	"""
    sf = im1.shape[0] / im2.shape[0]

    if sf > 1:
        im2 = cv2.resize(im2, (int(im2.shape[1] / sf), im1.shape[0]))
    else:
        im1 = cv2.resize(im1, (int(im1.shape[1] / sf), im2.shape[0]))

    im_concat = np.hstack((im1, im2))

    return im_concat


# Calculates Rotation Matrix given euler angles.
# adopted from https://www.learnopencv.com/rotation-matrix-to-euler-angles/
def euler2mat(x, y, z):
    """
	:param x:
	:param y:
	:param z:
	:return:
	"""
    R_x = np.array([[1, 0, 0], [0, math.cos(x), -math.sin(x)],
                    [0, math.sin(x), math.cos(x)]])

    R_y = np.array([[math.cos(y), 0, math.sin(y)], [0, 1, 0],
                    [-math.sin(y), 0, math.cos(y)]])

    R_z = np.array([[math.cos(z), -math.sin(z), 0],
                    [math.sin(z), math.cos(z), 0], [0, 0, 1]])

    R = np.dot(R_z, np.dot(R_y, R_x))

    return R


def convertAlpha2Rot(alpha, z3d, x3d):
    """
	:param alpha:
	:param z3d:
	:param x3d:
	:return:
	"""

    ry3d = alpha + math.atan2(-z3d, x3d) + 0.5 * math.pi

    while ry3d > math.pi:
        ry3d -= math.pi * 2
    while ry3d < (-math.pi):
        ry3d += math.pi * 2

    return ry3d


def convertRot2Alpha(ry3d, z3d, x3d):
    """
	convertRot2Alpha
	"""

    alpha = ry3d - math.atan2(-z3d, x3d) - 0.5 * math.pi

    while alpha > math.pi:
        alpha -= math.pi * 2
    while alpha < (-math.pi):
        alpha += math.pi * 2

    return alpha