distance_calculation.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. import math
  3. import cv2
  4. from ultralytics.utils.checks import check_imshow
  5. from ultralytics.utils.plotting import Annotator, colors
  6. class DistanceCalculation:
  7. """A class to calculate distance between two objects in a real-time video stream based on their tracks."""
  8. def __init__(
  9. self,
  10. names,
  11. pixels_per_meter=10,
  12. view_img=False,
  13. line_thickness=2,
  14. line_color=(255, 255, 0),
  15. centroid_color=(255, 0, 255),
  16. ):
  17. """
  18. Initializes the DistanceCalculation class with the given parameters.
  19. Args:
  20. names (dict): Dictionary mapping class indices to class names.
  21. pixels_per_meter (int, optional): Conversion factor from pixels to meters. Defaults to 10.
  22. view_img (bool, optional): Flag to indicate if the video stream should be displayed. Defaults to False.
  23. line_thickness (int, optional): Thickness of the lines drawn on the image. Defaults to 2.
  24. line_color (tuple, optional): Color of the lines drawn on the image (BGR format). Defaults to (255, 255, 0).
  25. centroid_color (tuple, optional): Color of the centroids drawn (BGR format). Defaults to (255, 0, 255).
  26. """
  27. # Visual & image information
  28. self.im0 = None
  29. self.annotator = None
  30. self.view_img = view_img
  31. self.line_color = line_color
  32. self.centroid_color = centroid_color
  33. # Prediction & tracking information
  34. self.clss = None
  35. self.names = names
  36. self.boxes = None
  37. self.line_thickness = line_thickness
  38. self.trk_ids = None
  39. # Distance calculation information
  40. self.centroids = []
  41. self.pixel_per_meter = pixels_per_meter
  42. # Mouse event information
  43. self.left_mouse_count = 0
  44. self.selected_boxes = {}
  45. # Check if environment supports imshow
  46. self.env_check = check_imshow(warn=True)
  47. def mouse_event_for_distance(self, event, x, y, flags, param):
  48. """
  49. Handles mouse events to select regions in a real-time video stream.
  50. Args:
  51. event (int): Type of mouse event (e.g., cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN, etc.).
  52. x (int): X-coordinate of the mouse pointer.
  53. y (int): Y-coordinate of the mouse pointer.
  54. flags (int): Flags associated with the event (e.g., cv2.EVENT_FLAG_CTRLKEY, cv2.EVENT_FLAG_SHIFTKEY, etc.).
  55. param (dict): Additional parameters passed to the function.
  56. """
  57. if event == cv2.EVENT_LBUTTONDOWN:
  58. self.left_mouse_count += 1
  59. if self.left_mouse_count <= 2:
  60. for box, track_id in zip(self.boxes, self.trk_ids):
  61. if box[0] < x < box[2] and box[1] < y < box[3] and track_id not in self.selected_boxes:
  62. self.selected_boxes[track_id] = box
  63. elif event == cv2.EVENT_RBUTTONDOWN:
  64. self.selected_boxes = {}
  65. self.left_mouse_count = 0
  66. def extract_tracks(self, tracks):
  67. """
  68. Extracts tracking results from the provided data.
  69. Args:
  70. tracks (list): List of tracks obtained from the object tracking process.
  71. """
  72. self.boxes = tracks[0].boxes.xyxy.cpu()
  73. self.clss = tracks[0].boxes.cls.cpu().tolist()
  74. self.trk_ids = tracks[0].boxes.id.int().cpu().tolist()
  75. @staticmethod
  76. def calculate_centroid(box):
  77. """
  78. Calculates the centroid of a bounding box.
  79. Args:
  80. box (list): Bounding box coordinates [x1, y1, x2, y2].
  81. Returns:
  82. (tuple): Centroid coordinates (x, y).
  83. """
  84. return int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)
  85. def calculate_distance(self, centroid1, centroid2):
  86. """
  87. Calculates the distance between two centroids.
  88. Args:
  89. centroid1 (tuple): Coordinates of the first centroid (x, y).
  90. centroid2 (tuple): Coordinates of the second centroid (x, y).
  91. Returns:
  92. (tuple): Distance in meters and millimeters.
  93. """
  94. pixel_distance = math.sqrt((centroid1[0] - centroid2[0]) ** 2 + (centroid1[1] - centroid2[1]) ** 2)
  95. distance_m = pixel_distance / self.pixel_per_meter
  96. distance_mm = distance_m * 1000
  97. return distance_m, distance_mm
  98. def start_process(self, im0, tracks):
  99. """
  100. Processes the video frame and calculates the distance between two bounding boxes.
  101. Args:
  102. im0 (ndarray): The image frame.
  103. tracks (list): List of tracks obtained from the object tracking process.
  104. Returns:
  105. (ndarray): The processed image frame.
  106. """
  107. self.im0 = im0
  108. if tracks[0].boxes.id is None:
  109. if self.view_img:
  110. self.display_frames()
  111. return im0
  112. self.extract_tracks(tracks)
  113. self.annotator = Annotator(self.im0, line_width=self.line_thickness)
  114. for box, cls, track_id in zip(self.boxes, self.clss, self.trk_ids):
  115. self.annotator.box_label(box, color=colors(int(cls), True), label=self.names[int(cls)])
  116. if len(self.selected_boxes) == 2:
  117. for trk_id in self.selected_boxes.keys():
  118. if trk_id == track_id:
  119. self.selected_boxes[track_id] = box
  120. if len(self.selected_boxes) == 2:
  121. self.centroids = [self.calculate_centroid(self.selected_boxes[trk_id]) for trk_id in self.selected_boxes]
  122. distance_m, distance_mm = self.calculate_distance(self.centroids[0], self.centroids[1])
  123. self.annotator.plot_distance_and_line(
  124. distance_m, distance_mm, self.centroids, self.line_color, self.centroid_color
  125. )
  126. self.centroids = []
  127. if self.view_img and self.env_check:
  128. self.display_frames()
  129. return im0
  130. def display_frames(self):
  131. """Displays the current frame with annotations."""
  132. cv2.namedWindow("Ultralytics Distance Estimation")
  133. cv2.setMouseCallback("Ultralytics Distance Estimation", self.mouse_event_for_distance)
  134. cv2.imshow("Ultralytics Distance Estimation", self.im0)
  135. if cv2.waitKey(1) & 0xFF == ord("q"):
  136. return
  137. if __name__ == "__main__":
  138. names = {0: "person", 1: "car"} # example class names
  139. distance_calculation = DistanceCalculation(names)