车位角点检测代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.4KB

  1. """Utility classes and functions."""
  2. import time
  3. import cv2 as cv
  4. import torch
  5. import numpy as np
  6. from PIL import Image
  7. class Timer(object):
  8. """Timer."""
  9. def __init__(self):
  10. self.start_ticking = False
  11. self.start = 0.
  12. self.count = 0
  13. self.total_time = 0.
  14. def tic(self):
  15. """Start timer."""
  16. self.start = time.time()
  17. self.start_ticking = True
  18. def toc(self):
  19. """End timer."""
  20. duration = time.time() - self.start
  21. self.start_ticking = False
  22. print("Time elapsed:", duration, "s.")
  23. self.count += 1
  24. self.total_time += duration
  25. def calc_average_time(self):
  26. """Calculate average elapsed time of timer."""
  27. if self.count == 0:
  28. return 0.
  29. return self.total_time / self.count
  30. def tensor2array(image_tensor, imtype=np.uint8):
  31. """
  32. Convert float CxHxW image tensor between [0, 1] to HxWxC numpy ndarray
  33. between [0, 255]
  34. """
  35. assert isinstance(image_tensor, torch.Tensor)
  36. image_numpy = (image_tensor.detach().cpu().numpy()) * 255.0
  37. image_numpy = np.transpose(image_numpy, (1, 2, 0)).astype(imtype)
  38. return image_numpy
  39. def tensor2im(image_tensor, imtype=np.uint8):
  40. """Convert float CxHxW BGR image tensor to RGB PIL Image"""
  41. image_numpy = tensor2array(image_tensor, imtype)
  42. image_numpy = cv.cvtColor(image_numpy, cv.COLOR_BGR2RGB)
  43. return Image.fromarray(image_numpy)