object_counter.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. # Ultralytics YOLO 🚀, AGPL-3.0 license
  2. from collections import defaultdict
  3. import cv2
  4. from ultralytics.utils.checks import check_imshow, check_requirements
  5. from ultralytics.utils.plotting import Annotator, colors
  6. check_requirements("shapely>=2.0.0")
  7. from shapely.geometry import LineString, Point, Polygon
  8. class ObjectCounter:
  9. """A class to manage the counting of objects in a real-time video stream based on their tracks."""
  10. def __init__(
  11. self,
  12. classes_names,
  13. reg_pts=None,
  14. count_reg_color=(255, 0, 255),
  15. count_txt_color=(0, 0, 0),
  16. count_bg_color=(255, 255, 255),
  17. line_thickness=2,
  18. track_thickness=2,
  19. view_img=False,
  20. view_in_counts=True,
  21. view_out_counts=True,
  22. draw_tracks=False,
  23. track_color=None,
  24. region_thickness=5,
  25. line_dist_thresh=15,
  26. cls_txtdisplay_gap=50,
  27. ):
  28. """
  29. Initializes the ObjectCounter with various tracking and counting parameters.
  30. Args:
  31. classes_names (dict): Dictionary of class names.
  32. reg_pts (list): List of points defining the counting region.
  33. count_reg_color (tuple): RGB color of the counting region.
  34. count_txt_color (tuple): RGB color of the count text.
  35. count_bg_color (tuple): RGB color of the count text background.
  36. line_thickness (int): Line thickness for bounding boxes.
  37. track_thickness (int): Thickness of the track lines.
  38. view_img (bool): Flag to control whether to display the video stream.
  39. view_in_counts (bool): Flag to control whether to display the in counts on the video stream.
  40. view_out_counts (bool): Flag to control whether to display the out counts on the video stream.
  41. draw_tracks (bool): Flag to control whether to draw the object tracks.
  42. track_color (tuple): RGB color of the tracks.
  43. region_thickness (int): Thickness of the object counting region.
  44. line_dist_thresh (int): Euclidean distance threshold for line counter.
  45. cls_txtdisplay_gap (int): Display gap between each class count.
  46. """
  47. # Mouse events
  48. self.is_drawing = False
  49. self.selected_point = None
  50. # Region & Line Information
  51. self.reg_pts = [(20, 400), (1260, 400)] if reg_pts is None else reg_pts
  52. self.line_dist_thresh = line_dist_thresh
  53. self.counting_region = None
  54. self.region_color = count_reg_color
  55. self.region_thickness = region_thickness
  56. # Image and annotation Information
  57. self.im0 = None
  58. self.tf = line_thickness
  59. self.view_img = view_img
  60. self.view_in_counts = view_in_counts
  61. self.view_out_counts = view_out_counts
  62. self.names = classes_names # Classes names
  63. self.annotator = None # Annotator
  64. self.window_name = "Ultralytics YOLOv8 Object Counter"
  65. # Object counting Information
  66. self.in_counts = 0
  67. self.out_counts = 0
  68. self.count_ids = []
  69. self.class_wise_count = {}
  70. self.count_txt_thickness = 0
  71. self.count_txt_color = count_txt_color
  72. self.count_bg_color = count_bg_color
  73. self.cls_txtdisplay_gap = cls_txtdisplay_gap
  74. self.fontsize = 0.6
  75. # Tracks info
  76. self.track_history = defaultdict(list)
  77. self.track_thickness = track_thickness
  78. self.draw_tracks = draw_tracks
  79. self.track_color = track_color
  80. # Check if environment supports imshow
  81. self.env_check = check_imshow(warn=True)
  82. # Initialize counting region
  83. if len(self.reg_pts) == 2:
  84. print("Line Counter Initiated.")
  85. self.counting_region = LineString(self.reg_pts)
  86. elif len(self.reg_pts) >= 3:
  87. print("Polygon Counter Initiated.")
  88. self.counting_region = Polygon(self.reg_pts)
  89. else:
  90. print("Invalid Region points provided, region_points must be 2 for lines or >= 3 for polygons.")
  91. print("Using Line Counter Now")
  92. self.counting_region = LineString(self.reg_pts)
  93. def mouse_event_for_region(self, event, x, y, flags, params):
  94. """
  95. Handles mouse events for defining and moving the counting region in a real-time video stream.
  96. Args:
  97. event (int): The type of mouse event (e.g., cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN, etc.).
  98. x (int): The x-coordinate of the mouse pointer.
  99. y (int): The y-coordinate of the mouse pointer.
  100. flags (int): Any associated event flags (e.g., cv2.EVENT_FLAG_CTRLKEY, cv2.EVENT_FLAG_SHIFTKEY, etc.).
  101. params (dict): Additional parameters for the function.
  102. """
  103. if event == cv2.EVENT_LBUTTONDOWN:
  104. for i, point in enumerate(self.reg_pts):
  105. if (
  106. isinstance(point, (tuple, list))
  107. and len(point) >= 2
  108. and (abs(x - point[0]) < 10 and abs(y - point[1]) < 10)
  109. ):
  110. self.selected_point = i
  111. self.is_drawing = True
  112. break
  113. elif event == cv2.EVENT_MOUSEMOVE:
  114. if self.is_drawing and self.selected_point is not None:
  115. self.reg_pts[self.selected_point] = (x, y)
  116. self.counting_region = Polygon(self.reg_pts)
  117. elif event == cv2.EVENT_LBUTTONUP:
  118. self.is_drawing = False
  119. self.selected_point = None
  120. def extract_and_process_tracks(self, tracks):
  121. """Extracts and processes tracks for object counting in a video stream."""
  122. # Annotator Init and region drawing
  123. self.annotator = Annotator(self.im0, self.tf, self.names)
  124. # Draw region or line
  125. self.annotator.draw_region(reg_pts=self.reg_pts, color=self.region_color, thickness=self.region_thickness)
  126. if tracks[0].boxes.id is not None:
  127. boxes = tracks[0].boxes.xyxy.cpu()
  128. clss = tracks[0].boxes.cls.cpu().tolist()
  129. track_ids = tracks[0].boxes.id.int().cpu().tolist()
  130. # Extract tracks
  131. for box, track_id, cls in zip(boxes, track_ids, clss):
  132. # Draw bounding box
  133. self.annotator.box_label(box, label=f"{self.names[cls]}#{track_id}", color=colors(int(track_id), True))
  134. # Store class info
  135. if self.names[cls] not in self.class_wise_count:
  136. self.class_wise_count[self.names[cls]] = {"IN": 0, "OUT": 0}
  137. # Draw Tracks
  138. track_line = self.track_history[track_id]
  139. track_line.append((float((box[0] + box[2]) / 2), float((box[1] + box[3]) / 2)))
  140. if len(track_line) > 30:
  141. track_line.pop(0)
  142. # Draw track trails
  143. if self.draw_tracks:
  144. self.annotator.draw_centroid_and_tracks(
  145. track_line,
  146. color=self.track_color or colors(int(track_id), True),
  147. track_thickness=self.track_thickness,
  148. )
  149. prev_position = self.track_history[track_id][-2] if len(self.track_history[track_id]) > 1 else None
  150. # Count objects in any polygon
  151. if len(self.reg_pts) >= 3:
  152. is_inside = self.counting_region.contains(Point(track_line[-1]))
  153. if prev_position is not None and is_inside and track_id not in self.count_ids:
  154. self.count_ids.append(track_id)
  155. if (box[0] - prev_position[0]) * (self.counting_region.centroid.x - prev_position[0]) > 0:
  156. self.in_counts += 1
  157. self.class_wise_count[self.names[cls]]["IN"] += 1
  158. else:
  159. self.out_counts += 1
  160. self.class_wise_count[self.names[cls]]["OUT"] += 1
  161. # Count objects using line
  162. elif len(self.reg_pts) == 2:
  163. if prev_position is not None and track_id not in self.count_ids:
  164. distance = Point(track_line[-1]).distance(self.counting_region)
  165. if distance < self.line_dist_thresh and track_id not in self.count_ids:
  166. self.count_ids.append(track_id)
  167. if (box[0] - prev_position[0]) * (self.counting_region.centroid.x - prev_position[0]) > 0:
  168. self.in_counts += 1
  169. self.class_wise_count[self.names[cls]]["IN"] += 1
  170. else:
  171. self.out_counts += 1
  172. self.class_wise_count[self.names[cls]]["OUT"] += 1
  173. labels_dict = {}
  174. for key, value in self.class_wise_count.items():
  175. if value["IN"] != 0 or value["OUT"] != 0:
  176. if not self.view_in_counts and not self.view_out_counts:
  177. continue
  178. elif not self.view_in_counts:
  179. labels_dict[str.capitalize(key)] = f"OUT {value['OUT']}"
  180. elif not self.view_out_counts:
  181. labels_dict[str.capitalize(key)] = f"IN {value['IN']}"
  182. else:
  183. labels_dict[str.capitalize(key)] = f"IN {value['IN']} OUT {value['OUT']}"
  184. if labels_dict:
  185. self.annotator.display_analytics(self.im0, labels_dict, self.count_txt_color, self.count_bg_color, 10)
  186. def display_frames(self):
  187. """Displays the current frame with annotations and regions in a window."""
  188. if self.env_check:
  189. cv2.namedWindow(self.window_name)
  190. if len(self.reg_pts) == 4: # only add mouse event If user drawn region
  191. cv2.setMouseCallback(self.window_name, self.mouse_event_for_region, {"region_points": self.reg_pts})
  192. cv2.imshow(self.window_name, self.im0)
  193. # Break Window
  194. if cv2.waitKey(1) & 0xFF == ord("q"):
  195. return
  196. def start_counting(self, im0, tracks):
  197. """
  198. Main function to start the object counting process.
  199. Args:
  200. im0 (ndarray): Current frame from the video stream.
  201. tracks (list): List of tracks obtained from the object tracking process.
  202. """
  203. self.im0 = im0 # store image
  204. self.extract_and_process_tracks(tracks) # draw region even if no objects
  205. if self.view_img:
  206. self.display_frames()
  207. return self.im0
  208. if __name__ == "__main__":
  209. classes_names = {0: "person", 1: "car"} # example class names
  210. ObjectCounter(classes_names)