yolov8_region_counter.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. import argparse
  3. from collections import defaultdict
  4. from pathlib import Path
  5. import cv2
  6. import numpy as np
  7. from shapely.geometry import Polygon
  8. from shapely.geometry.point import Point
  9. from ultralytics import YOLO
  10. from ultralytics.utils.files import increment_path
  11. from ultralytics.utils.plotting import Annotator, colors
  12. track_history = defaultdict(list)
  13. current_region = None
  14. counting_regions = [
  15. {
  16. "name": "YOLOv8 Polygon Region",
  17. "polygon": Polygon([(50, 80), (250, 20), (450, 80), (400, 350), (100, 350)]), # Polygon points
  18. "counts": 0,
  19. "dragging": False,
  20. "region_color": (255, 42, 4), # BGR Value
  21. "text_color": (255, 255, 255), # Region Text Color
  22. },
  23. {
  24. "name": "YOLOv8 Rectangle Region",
  25. "polygon": Polygon([(200, 250), (440, 250), (440, 550), (200, 550)]), # Polygon points
  26. "counts": 0,
  27. "dragging": False,
  28. "region_color": (37, 255, 225), # BGR Value
  29. "text_color": (0, 0, 0), # Region Text Color
  30. },
  31. ]
  32. def mouse_callback(event, x, y, flags, param):
  33. """
  34. Handles mouse events for region manipulation.
  35. Args:
  36. event (int): The mouse event type (e.g., cv2.EVENT_LBUTTONDOWN).
  37. x (int): The x-coordinate of the mouse pointer.
  38. y (int): The y-coordinate of the mouse pointer.
  39. flags (int): Additional flags passed by OpenCV.
  40. param: Additional parameters passed to the callback (not used in this function).
  41. Global Variables:
  42. current_region (dict): A dictionary representing the current selected region.
  43. Mouse Events:
  44. - LBUTTONDOWN: Initiates dragging for the region containing the clicked point.
  45. - MOUSEMOVE: Moves the selected region if dragging is active.
  46. - LBUTTONUP: Ends dragging for the selected region.
  47. Notes:
  48. - This function is intended to be used as a callback for OpenCV mouse events.
  49. - Requires the existence of the 'counting_regions' list and the 'Polygon' class.
  50. Example:
  51. >>> cv2.setMouseCallback(window_name, mouse_callback)
  52. """
  53. global current_region
  54. # Mouse left button down event
  55. if event == cv2.EVENT_LBUTTONDOWN:
  56. for region in counting_regions:
  57. if region["polygon"].contains(Point((x, y))):
  58. current_region = region
  59. current_region["dragging"] = True
  60. current_region["offset_x"] = x
  61. current_region["offset_y"] = y
  62. # Mouse move event
  63. elif event == cv2.EVENT_MOUSEMOVE:
  64. if current_region is not None and current_region["dragging"]:
  65. dx = x - current_region["offset_x"]
  66. dy = y - current_region["offset_y"]
  67. current_region["polygon"] = Polygon(
  68. [(p[0] + dx, p[1] + dy) for p in current_region["polygon"].exterior.coords]
  69. )
  70. current_region["offset_x"] = x
  71. current_region["offset_y"] = y
  72. # Mouse left button up event
  73. elif event == cv2.EVENT_LBUTTONUP:
  74. if current_region is not None and current_region["dragging"]:
  75. current_region["dragging"] = False
  76. def run(
  77. weights="yolov8n.pt",
  78. source=None,
  79. device="cpu",
  80. view_img=False,
  81. save_img=False,
  82. exist_ok=False,
  83. classes=None,
  84. line_thickness=2,
  85. track_thickness=2,
  86. region_thickness=2,
  87. ):
  88. """
  89. Run Region counting on a video using YOLOv8 and ByteTrack.
  90. Supports movable region for real time counting inside specific area.
  91. Supports multiple regions counting.
  92. Regions can be Polygons or rectangle in shape
  93. Args:
  94. weights (str): Model weights path.
  95. source (str): Video file path.
  96. device (str): processing device cpu, 0, 1
  97. view_img (bool): Show results.
  98. save_img (bool): Save results.
  99. exist_ok (bool): Overwrite existing files.
  100. classes (list): classes to detect and track
  101. line_thickness (int): Bounding box thickness.
  102. track_thickness (int): Tracking line thickness
  103. region_thickness (int): Region thickness.
  104. """
  105. vid_frame_count = 0
  106. # Check source path
  107. if not Path(source).exists():
  108. raise FileNotFoundError(f"Source path '{source}' does not exist.")
  109. # Setup Model
  110. model = YOLO(f"{weights}")
  111. model.to("cuda") if device == "0" else model.to("cpu")
  112. # Extract classes names
  113. names = model.names
  114. # Video setup
  115. videocapture = cv2.VideoCapture(source)
  116. frame_width = int(videocapture.get(3))
  117. frame_height = int(videocapture.get(4))
  118. fps = int(videocapture.get(5))
  119. fourcc = cv2.VideoWriter_fourcc(*"mp4v")
  120. # Output setup
  121. save_dir = increment_path(Path("ultralytics_rc_output") / "exp", exist_ok)
  122. save_dir.mkdir(parents=True, exist_ok=True)
  123. video_writer = cv2.VideoWriter(str(save_dir / f"{Path(source).stem}.avi"), fourcc, fps, (frame_width, frame_height))
  124. # Iterate over video frames
  125. while videocapture.isOpened():
  126. success, frame = videocapture.read()
  127. if not success:
  128. break
  129. vid_frame_count += 1
  130. # Extract the results
  131. results = model.track(frame, persist=True, classes=classes)
  132. if results[0].boxes.id is not None:
  133. boxes = results[0].boxes.xyxy.cpu()
  134. track_ids = results[0].boxes.id.int().cpu().tolist()
  135. clss = results[0].boxes.cls.cpu().tolist()
  136. annotator = Annotator(frame, line_width=line_thickness, example=str(names))
  137. for box, track_id, cls in zip(boxes, track_ids, clss):
  138. annotator.box_label(box, str(names[cls]), color=colors(cls, True))
  139. bbox_center = (box[0] + box[2]) / 2, (box[1] + box[3]) / 2 # Bbox center
  140. track = track_history[track_id] # Tracking Lines plot
  141. track.append((float(bbox_center[0]), float(bbox_center[1])))
  142. if len(track) > 30:
  143. track.pop(0)
  144. points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
  145. cv2.polylines(frame, [points], isClosed=False, color=colors(cls, True), thickness=track_thickness)
  146. # Check if detection inside region
  147. for region in counting_regions:
  148. if region["polygon"].contains(Point((bbox_center[0], bbox_center[1]))):
  149. region["counts"] += 1
  150. # Draw regions (Polygons/Rectangles)
  151. for region in counting_regions:
  152. region_label = str(region["counts"])
  153. region_color = region["region_color"]
  154. region_text_color = region["text_color"]
  155. polygon_coordinates = np.array(region["polygon"].exterior.coords, dtype=np.int32)
  156. centroid_x, centroid_y = int(region["polygon"].centroid.x), int(region["polygon"].centroid.y)
  157. text_size, _ = cv2.getTextSize(
  158. region_label, cv2.FONT_HERSHEY_SIMPLEX, fontScale=0.7, thickness=line_thickness
  159. )
  160. text_x = centroid_x - text_size[0] // 2
  161. text_y = centroid_y + text_size[1] // 2
  162. cv2.rectangle(
  163. frame,
  164. (text_x - 5, text_y - text_size[1] - 5),
  165. (text_x + text_size[0] + 5, text_y + 5),
  166. region_color,
  167. -1,
  168. )
  169. cv2.putText(
  170. frame, region_label, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, region_text_color, line_thickness
  171. )
  172. cv2.polylines(frame, [polygon_coordinates], isClosed=True, color=region_color, thickness=region_thickness)
  173. if view_img:
  174. if vid_frame_count == 1:
  175. cv2.namedWindow("Ultralytics YOLOv8 Region Counter Movable")
  176. cv2.setMouseCallback("Ultralytics YOLOv8 Region Counter Movable", mouse_callback)
  177. cv2.imshow("Ultralytics YOLOv8 Region Counter Movable", frame)
  178. if save_img:
  179. video_writer.write(frame)
  180. for region in counting_regions: # Reinitialize count for each region
  181. region["counts"] = 0
  182. if cv2.waitKey(1) & 0xFF == ord("q"):
  183. break
  184. del vid_frame_count
  185. video_writer.release()
  186. videocapture.release()
  187. cv2.destroyAllWindows()
  188. def parse_opt():
  189. """Parse command line arguments."""
  190. parser = argparse.ArgumentParser()
  191. parser.add_argument("--weights", type=str, default="yolov8n.pt", help="initial weights path")
  192. parser.add_argument("--device", default="", help="cuda device, i.e. 0 or 0,1,2,3 or cpu")
  193. parser.add_argument("--source", type=str, required=True, help="video file path")
  194. parser.add_argument("--view-img", action="store_true", help="show results")
  195. parser.add_argument("--save-img", action="store_true", help="save results")
  196. parser.add_argument("--exist-ok", action="store_true", help="existing project/name ok, do not increment")
  197. parser.add_argument("--classes", nargs="+", type=int, help="filter by class: --classes 0, or --classes 0 2 3")
  198. parser.add_argument("--line-thickness", type=int, default=2, help="bounding box thickness")
  199. parser.add_argument("--track-thickness", type=int, default=2, help="Tracking line thickness")
  200. parser.add_argument("--region-thickness", type=int, default=4, help="Region thickness")
  201. return parser.parse_args()
  202. def main(options):
  203. """Main function."""
  204. run(**vars(options))
  205. if __name__ == "__main__":
  206. opt = parse_opt()
  207. main(opt)