跳至内容

参考资料 ultralytics/solutions/distance_calculation.py

备注

该文件可在https://github.com/ultralytics/ultralytics/blob/main/ ultralytics/solutions/distance_calculation .py 下找到。如果您发现问题,请通过提交 Pull Request🛠️ 帮助修复。谢谢🙏!



ultralytics.solutions.distance_calculation.DistanceCalculation

根据实时视频流中两个物体的轨迹计算它们之间距离的类。

源代码 ultralytics/solutions/distance_calculation.py
class DistanceCalculation:
    """A class to calculate distance between two objects in a real-time video stream based on their tracks."""

    def __init__(
        self,
        names,
        pixels_per_meter=10,
        view_img=False,
        line_thickness=2,
        line_color=(255, 255, 0),
        centroid_color=(255, 0, 255),
    ):
        """
        Initializes the DistanceCalculation class with the given parameters.

        Args:
            names (dict): Dictionary mapping class indices to class names.
            pixels_per_meter (int, optional): Conversion factor from pixels to meters. Defaults to 10.
            view_img (bool, optional): Flag to indicate if the video stream should be displayed. Defaults to False.
            line_thickness (int, optional): Thickness of the lines drawn on the image. Defaults to 2.
            line_color (tuple, optional): Color of the lines drawn on the image (BGR format). Defaults to (255, 255, 0).
            centroid_color (tuple, optional): Color of the centroids drawn (BGR format). Defaults to (255, 0, 255).
        """
        # Visual & image information
        self.im0 = None
        self.annotator = None
        self.view_img = view_img
        self.line_color = line_color
        self.centroid_color = centroid_color

        # Prediction & tracking information
        self.clss = None
        self.names = names
        self.boxes = None
        self.line_thickness = line_thickness
        self.trk_ids = None

        # Distance calculation information
        self.centroids = []
        self.pixel_per_meter = pixels_per_meter

        # Mouse event information
        self.left_mouse_count = 0
        self.selected_boxes = {}

        # Check if environment supports imshow
        self.env_check = check_imshow(warn=True)

    def mouse_event_for_distance(self, event, x, y, flags, param):
        """
        Handles mouse events to select regions in a real-time video stream.

        Args:
            event (int): Type of mouse event (e.g., cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN, etc.).
            x (int): X-coordinate of the mouse pointer.
            y (int): Y-coordinate of the mouse pointer.
            flags (int): Flags associated with the event (e.g., cv2.EVENT_FLAG_CTRLKEY, cv2.EVENT_FLAG_SHIFTKEY, etc.).
            param (dict): Additional parameters passed to the function.
        """
        if event == cv2.EVENT_LBUTTONDOWN:
            self.left_mouse_count += 1
            if self.left_mouse_count <= 2:
                for box, track_id in zip(self.boxes, self.trk_ids):
                    if box[0] < x < box[2] and box[1] < y < box[3] and track_id not in self.selected_boxes:
                        self.selected_boxes[track_id] = box

        elif event == cv2.EVENT_RBUTTONDOWN:
            self.selected_boxes = {}
            self.left_mouse_count = 0

    def extract_tracks(self, tracks):
        """
        Extracts tracking results from the provided data.

        Args:
            tracks (list): List of tracks obtained from the object tracking process.
        """
        self.boxes = tracks[0].boxes.xyxy.cpu()
        self.clss = tracks[0].boxes.cls.cpu().tolist()
        self.trk_ids = tracks[0].boxes.id.int().cpu().tolist()

    @staticmethod
    def calculate_centroid(box):
        """
        Calculates the centroid of a bounding box.

        Args:
            box (list): Bounding box coordinates [x1, y1, x2, y2].

        Returns:
            (tuple): Centroid coordinates (x, y).
        """
        return int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)

    def calculate_distance(self, centroid1, centroid2):
        """
        Calculates the distance between two centroids.

        Args:
            centroid1 (tuple): Coordinates of the first centroid (x, y).
            centroid2 (tuple): Coordinates of the second centroid (x, y).

        Returns:
            (tuple): Distance in meters and millimeters.
        """
        pixel_distance = math.sqrt((centroid1[0] - centroid2[0]) ** 2 + (centroid1[1] - centroid2[1]) ** 2)
        distance_m = pixel_distance / self.pixel_per_meter
        distance_mm = distance_m * 1000
        return distance_m, distance_mm

    def start_process(self, im0, tracks):
        """
        Processes the video frame and calculates the distance between two bounding boxes.

        Args:
            im0 (ndarray): The image frame.
            tracks (list): List of tracks obtained from the object tracking process.

        Returns:
            (ndarray): The processed image frame.
        """
        self.im0 = im0
        if tracks[0].boxes.id is None:
            if self.view_img:
                self.display_frames()
            return im0

        self.extract_tracks(tracks)
        self.annotator = Annotator(self.im0, line_width=self.line_thickness)

        for box, cls, track_id in zip(self.boxes, self.clss, self.trk_ids):
            self.annotator.box_label(box, color=colors(int(cls), True), label=self.names[int(cls)])

            if len(self.selected_boxes) == 2:
                for trk_id in self.selected_boxes.keys():
                    if trk_id == track_id:
                        self.selected_boxes[track_id] = box

        if len(self.selected_boxes) == 2:
            self.centroids = [self.calculate_centroid(self.selected_boxes[trk_id]) for trk_id in self.selected_boxes]

            distance_m, distance_mm = self.calculate_distance(self.centroids[0], self.centroids[1])
            self.annotator.plot_distance_and_line(
                distance_m, distance_mm, self.centroids, self.line_color, self.centroid_color
            )

        self.centroids = []

        if self.view_img and self.env_check:
            self.display_frames()

        return im0

    def display_frames(self):
        """Displays the current frame with annotations."""
        cv2.namedWindow("Ultralytics Distance Estimation")
        cv2.setMouseCallback("Ultralytics Distance Estimation", self.mouse_event_for_distance)
        cv2.imshow("Ultralytics Distance Estimation", self.im0)

        if cv2.waitKey(1) & 0xFF == ord("q"):
            return

__init__(names, pixels_per_meter=10, view_img=False, line_thickness=2, line_color=(255, 255, 0), centroid_color=(255, 0, 255))

使用给定的参数初始化 DistanceCalculation 类。

参数

名称 类型 说明 默认值
names dict

将类索引映射到类名称的字典。

所需
pixels_per_meter int

从像素到米的转换系数。默认为 10。

10
view_img bool

指示是否显示视频流的标志。默认为 "假"。

False
line_thickness int

在图像上绘制线条的厚度。默认为 2。

2
line_color tuple

图像上绘制线条的颜色(BGR 格式)。默认值为 (255,255,0)。

(255, 255, 0)
centroid_color tuple

绘制中心点的颜色(BGR 格式)。默认值为 (255,0,255)。

(255, 0, 255)
源代码 ultralytics/solutions/distance_calculation.py
def __init__(
    self,
    names,
    pixels_per_meter=10,
    view_img=False,
    line_thickness=2,
    line_color=(255, 255, 0),
    centroid_color=(255, 0, 255),
):
    """
    Initializes the DistanceCalculation class with the given parameters.

    Args:
        names (dict): Dictionary mapping class indices to class names.
        pixels_per_meter (int, optional): Conversion factor from pixels to meters. Defaults to 10.
        view_img (bool, optional): Flag to indicate if the video stream should be displayed. Defaults to False.
        line_thickness (int, optional): Thickness of the lines drawn on the image. Defaults to 2.
        line_color (tuple, optional): Color of the lines drawn on the image (BGR format). Defaults to (255, 255, 0).
        centroid_color (tuple, optional): Color of the centroids drawn (BGR format). Defaults to (255, 0, 255).
    """
    # Visual & image information
    self.im0 = None
    self.annotator = None
    self.view_img = view_img
    self.line_color = line_color
    self.centroid_color = centroid_color

    # Prediction & tracking information
    self.clss = None
    self.names = names
    self.boxes = None
    self.line_thickness = line_thickness
    self.trk_ids = None

    # Distance calculation information
    self.centroids = []
    self.pixel_per_meter = pixels_per_meter

    # Mouse event information
    self.left_mouse_count = 0
    self.selected_boxes = {}

    # Check if environment supports imshow
    self.env_check = check_imshow(warn=True)

calculate_centroid(box) staticmethod

计算包围盒的中心点。

参数

名称 类型 说明 默认值
box list

边界框坐标 [x1, y1, x2, y2]。

所需

返回:

类型 说明
tuple

中心点坐标(x、y)。

源代码 ultralytics/solutions/distance_calculation.py
@staticmethod
def calculate_centroid(box):
    """
    Calculates the centroid of a bounding box.

    Args:
        box (list): Bounding box coordinates [x1, y1, x2, y2].

    Returns:
        (tuple): Centroid coordinates (x, y).
    """
    return int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)

calculate_distance(centroid1, centroid2)

计算两个中心点之间的距离。

参数

名称 类型 说明 默认值
centroid1 tuple

第一个中心点的坐标(x、y)。

所需
centroid2 tuple

第二个中心点的坐标(x、y)。

所需

返回:

类型 说明
tuple

以米和毫米为单位的距离。

源代码 ultralytics/solutions/distance_calculation.py
def calculate_distance(self, centroid1, centroid2):
    """
    Calculates the distance between two centroids.

    Args:
        centroid1 (tuple): Coordinates of the first centroid (x, y).
        centroid2 (tuple): Coordinates of the second centroid (x, y).

    Returns:
        (tuple): Distance in meters and millimeters.
    """
    pixel_distance = math.sqrt((centroid1[0] - centroid2[0]) ** 2 + (centroid1[1] - centroid2[1]) ** 2)
    distance_m = pixel_distance / self.pixel_per_meter
    distance_mm = distance_m * 1000
    return distance_m, distance_mm

display_frames()

显示带有注释的当前帧。

源代码 ultralytics/solutions/distance_calculation.py
def display_frames(self):
    """Displays the current frame with annotations."""
    cv2.namedWindow("Ultralytics Distance Estimation")
    cv2.setMouseCallback("Ultralytics Distance Estimation", self.mouse_event_for_distance)
    cv2.imshow("Ultralytics Distance Estimation", self.im0)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        return

extract_tracks(tracks)

从提供的数据中提取跟踪结果。

参数

名称 类型 说明 默认值
tracks list

物体追踪过程中获得的轨迹列表。

所需
源代码 ultralytics/solutions/distance_calculation.py
def extract_tracks(self, tracks):
    """
    Extracts tracking results from the provided data.

    Args:
        tracks (list): List of tracks obtained from the object tracking process.
    """
    self.boxes = tracks[0].boxes.xyxy.cpu()
    self.clss = tracks[0].boxes.cls.cpu().tolist()
    self.trk_ids = tracks[0].boxes.id.int().cpu().tolist()

mouse_event_for_distance(event, x, y, flags, param)

处理鼠标事件,以便在实时视频流中选择区域。

参数

名称 类型 说明 默认值
event int

鼠标事件类型(如 cv2.EVENT_MOUSEMOVE、cv2.EVENT_LBUTTONDOWN 等)。

所需
x int

鼠标指针的 X 坐标。

所需
y int

鼠标指针的 Y 坐标。

所需
flags int

与事件相关的标志(如 cv2.EVENT_FLAG_CTRLKEY、cv2.EVENT_FLAG_SHIFTKEY 等)。

所需
param dict

传递给函数的附加参数。

所需
源代码 ultralytics/solutions/distance_calculation.py
def mouse_event_for_distance(self, event, x, y, flags, param):
    """
    Handles mouse events to select regions in a real-time video stream.

    Args:
        event (int): Type of mouse event (e.g., cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN, etc.).
        x (int): X-coordinate of the mouse pointer.
        y (int): Y-coordinate of the mouse pointer.
        flags (int): Flags associated with the event (e.g., cv2.EVENT_FLAG_CTRLKEY, cv2.EVENT_FLAG_SHIFTKEY, etc.).
        param (dict): Additional parameters passed to the function.
    """
    if event == cv2.EVENT_LBUTTONDOWN:
        self.left_mouse_count += 1
        if self.left_mouse_count <= 2:
            for box, track_id in zip(self.boxes, self.trk_ids):
                if box[0] < x < box[2] and box[1] < y < box[3] and track_id not in self.selected_boxes:
                    self.selected_boxes[track_id] = box

    elif event == cv2.EVENT_RBUTTONDOWN:
        self.selected_boxes = {}
        self.left_mouse_count = 0

start_process(im0, tracks)

处理视频帧并计算两个边界框之间的距离。

参数

名称 类型 说明 默认值
im0 ndarray

图像框架

所需
tracks list

物体追踪过程中获得的轨迹列表。

所需

返回:

类型 说明
ndarray

处理后的图像帧。

源代码 ultralytics/solutions/distance_calculation.py
def start_process(self, im0, tracks):
    """
    Processes the video frame and calculates the distance between two bounding boxes.

    Args:
        im0 (ndarray): The image frame.
        tracks (list): List of tracks obtained from the object tracking process.

    Returns:
        (ndarray): The processed image frame.
    """
    self.im0 = im0
    if tracks[0].boxes.id is None:
        if self.view_img:
            self.display_frames()
        return im0

    self.extract_tracks(tracks)
    self.annotator = Annotator(self.im0, line_width=self.line_thickness)

    for box, cls, track_id in zip(self.boxes, self.clss, self.trk_ids):
        self.annotator.box_label(box, color=colors(int(cls), True), label=self.names[int(cls)])

        if len(self.selected_boxes) == 2:
            for trk_id in self.selected_boxes.keys():
                if trk_id == track_id:
                    self.selected_boxes[track_id] = box

    if len(self.selected_boxes) == 2:
        self.centroids = [self.calculate_centroid(self.selected_boxes[trk_id]) for trk_id in self.selected_boxes]

        distance_m, distance_mm = self.calculate_distance(self.centroids[0], self.centroids[1])
        self.annotator.plot_distance_and_line(
            distance_m, distance_mm, self.centroids, self.line_color, self.centroid_color
        )

    self.centroids = []

    if self.view_img and self.env_check:
        self.display_frames()

    return im0





Created 2024-01-05, Updated 2024-06-02
Authors: glenn-jocher (2), Burhan-Q (1), AyushExel (1), RizwanMunawar (1)