车位角点检测代码
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.

63 lines
2.4KB

  1. """Class for logging."""
  2. import math
  3. import numpy as np
  4. from visdom import Visdom
  5. from PIL import ImageDraw
  6. class Logger():
  7. """Logger for training."""
  8. def __init__(self, enable_visdom=False, curve_names=None):
  9. self.curve_names = curve_names
  10. if enable_visdom:
  11. self.vis = Visdom()
  12. assert self.vis.check_connection()
  13. self.curve_x = np.array([0])
  14. else:
  15. self.curve_names = None
  16. def log(self, xval=None, win_name='loss', **kwargs):
  17. """Log and print the information."""
  18. print("##############################################################")
  19. for key, value in kwargs.items():
  20. print(key, value, sep='\t')
  21. if self.curve_names:
  22. if not xval:
  23. xval = self.curve_x
  24. for i in range(len(self.curve_names)):
  25. name = self.curve_names[i]
  26. if name not in kwargs:
  27. continue
  28. yval = np.array([kwargs[name]])
  29. self.vis.line(Y=yval, X=xval, win=win_name, update='append',
  30. name=name, opts=dict(showlegend=True))
  31. self.curve_x += 1
  32. def plot_curve(self, yvals, xvals, win_name='pr_curves'):
  33. """Plot curve."""
  34. self.vis.line(Y=np.array(yvals), X=np.array(xvals), win=win_name)
  35. def plot_marking_points(self, image, marking_points, win_name='mk_points'):
  36. """Plot marking points on visdom."""
  37. width, height = image.size
  38. draw = ImageDraw.Draw(image)
  39. for point in marking_points:
  40. p0_x = width * point.x
  41. p0_y = height * point.y
  42. p1_x = p0_x + 50*math.cos(point.direction)
  43. p1_y = p0_y + 50*math.sin(point.direction)
  44. draw.line((p0_x, p0_y, p1_x, p1_y), fill=(255, 0, 0))
  45. p2_x = p0_x - 50*math.sin(point.direction)
  46. p2_y = p0_y + 50*math.cos(point.direction)
  47. if point.shape > 0.5:
  48. draw.line((p2_x, p2_y, p0_x, p0_y), fill=(255, 0, 0))
  49. else:
  50. p3_x = p0_x + 50*math.sin(point.direction)
  51. p3_y = p0_y - 50*math.cos(point.direction)
  52. draw.line((p2_x, p2_y, p3_x, p3_y), fill=(255, 0, 0))
  53. image = np.asarray(image, dtype="uint8")
  54. image = np.transpose(image, (2, 0, 1))
  55. self.vis.image(image, win=win_name)