Bỏ để qua phần nội dung

Tài liệu tham khảo cho ultralytics/solutions/distance_calculation.py

Ghi

Tệp này có sẵn tại https://github.com/ultralytics/ultralytics/blob/main/ultralytics/giải pháp/distance_calculation.py. Nếu bạn phát hiện ra một vấn đề, vui lòng giúp khắc phục nó bằng cách đóng góp Yêu cầu 🛠️ kéo. Cảm ơn bạn 🙏 !



ultralytics.solutions.distance_calculation.DistanceCalculation

Một lớp để tính toán khoảng cách giữa hai đối tượng trong luồng video thời gian thực dựa trên các bản nhạc của chúng.

Mã nguồn trong ultralytics/solutions/distance_calculation.py
class DistanceCalculation:
    """A class to calculate distance between two objects in real-time video stream based on their tracks."""

    def __init__(self):
        """Initializes the distance calculation class with default values for Visual, Image, track and distance
        parameters.
        """

        # Visual & im0 information
        self.im0 = None
        self.annotator = None
        self.view_img = False
        self.line_color = (255, 255, 0)
        self.centroid_color = (255, 0, 255)

        # Predict/track information
        self.clss = None
        self.names = None
        self.boxes = None
        self.line_thickness = 2
        self.trk_ids = None

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

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

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

    def set_args(
        self,
        names,
        pixels_per_meter=10,
        view_img=False,
        line_thickness=2,
        line_color=(255, 255, 0),
        centroid_color=(255, 0, 255),
    ):
        """
        Configures the distance calculation and display parameters.

        Args:
            names (dict): object detection classes names
            pixels_per_meter (int): Number of pixels in meter
            view_img (bool): Flag indicating frame display
            line_thickness (int): Line thickness for bounding boxes.
            line_color (RGB): color of centroids line
            centroid_color (RGB): colors of bbox centroids
        """
        self.names = names
        self.pixel_per_meter = pixels_per_meter
        self.view_img = view_img
        self.line_thickness = line_thickness
        self.line_color = line_color
        self.centroid_color = centroid_color

    def mouse_event_for_distance(self, event, x, y, flags, param):
        """
        This function is designed to move region with mouse events in a real-time video stream.

        Args:
            event (int): The type of mouse event (e.g., cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN, etc.).
            x (int): The x-coordinate of the mouse pointer.
            y (int): The y-coordinate of the mouse pointer.
            flags (int): Any flags associated with the event (e.g., cv2.EVENT_FLAG_CTRLKEY,
                cv2.EVENT_FLAG_SHIFTKEY, etc.).
            param (dict): Additional parameters you may want to pass to the function.
        """
        global selected_boxes
        global left_mouse_count
        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] = []
                        self.selected_boxes[track_id] = box

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

    def extract_tracks(self, tracks):
        """
        Extracts 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()

    def calculate_centroid(self, box):
        """
        Calculate the centroid of bounding box.

        Args:
            box (list): Bounding box data
        """
        return int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)

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

        Args:
            centroid1 (point): First bounding box data
            centroid2 (point): Second bounding box data
        """
        pixel_distance = math.sqrt((centroid1[0] - centroid2[0]) ** 2 + (centroid1[1] - centroid2[1]) ** 2)
        return pixel_distance / self.pixel_per_meter, (pixel_distance / self.pixel_per_meter) * 1000

    def start_process(self, im0, tracks):
        """
        Calculate distance between two bounding boxes based on tracking data.

        Args:
            im0 (nd array): Image
            tracks (list): List of tracks obtained from the object tracking process.
        """
        self.im0 = im0
        if tracks[0].boxes.id is None:
            if self.view_img:
                self.display_frames()
            return
        self.extract_tracks(tracks)

        self.annotator = Annotator(self.im0, line_width=2)

        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.items():
                    if trk_id == track_id:
                        self.selected_boxes[track_id] = box

        if len(self.selected_boxes) == 2:
            for trk_id, box in self.selected_boxes.items():
                centroid = self.calculate_centroid(self.selected_boxes[trk_id])
                self.centroids.append(centroid)

            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):
        """Display frame."""
        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__()

Khởi tạo lớp tính toán khoảng cách với các giá trị mặc định cho Visual, Image, track và distance tham số.

Mã nguồn trong ultralytics/solutions/distance_calculation.py
def __init__(self):
    """Initializes the distance calculation class with default values for Visual, Image, track and distance
    parameters.
    """

    # Visual & im0 information
    self.im0 = None
    self.annotator = None
    self.view_img = False
    self.line_color = (255, 255, 0)
    self.centroid_color = (255, 0, 255)

    # Predict/track information
    self.clss = None
    self.names = None
    self.boxes = None
    self.line_thickness = 2
    self.trk_ids = None

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

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

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

calculate_centroid(box)

Tính tâm của hộp giới hạn.

Thông số:

Tên Kiểu Sự miêu tả Mặc định
box list

Dữ liệu hộp giới hạn

bắt buộc
Mã nguồn trong ultralytics/solutions/distance_calculation.py
def calculate_centroid(self, box):
    """
    Calculate the centroid of bounding box.

    Args:
        box (list): Bounding box data
    """
    return int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)

calculate_distance(centroid1, centroid2)

Tính khoảng cách giữa hai centroids.

Thông số:

Tên Kiểu Sự miêu tả Mặc định
centroid1 point

Dữ liệu hộp giới hạn đầu tiên

bắt buộc
centroid2 point

Dữ liệu hộp giới hạn thứ hai

bắt buộc
Mã nguồn trong ultralytics/solutions/distance_calculation.py
def calculate_distance(self, centroid1, centroid2):
    """
    Calculate distance between two centroids.

    Args:
        centroid1 (point): First bounding box data
        centroid2 (point): Second bounding box data
    """
    pixel_distance = math.sqrt((centroid1[0] - centroid2[0]) ** 2 + (centroid1[1] - centroid2[1]) ** 2)
    return pixel_distance / self.pixel_per_meter, (pixel_distance / self.pixel_per_meter) * 1000

display_frames()

Khung hiển thị.

Mã nguồn trong ultralytics/solutions/distance_calculation.py
def display_frames(self):
    """Display frame."""
    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)

Trích xuất kết quả từ dữ liệu được cung cấp.

Thông số:

Tên Kiểu Sự miêu tả Mặc định
tracks list

Danh sách các bản nhạc thu được từ quá trình theo dõi đối tượng.

bắt buộc
Mã nguồn trong ultralytics/solutions/distance_calculation.py
def extract_tracks(self, tracks):
    """
    Extracts 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)

Chức năng này được thiết kế để di chuyển vùng có sự kiện chuột trong luồng video thời gian thực.

Thông số:

Tên Kiểu Sự miêu tả Mặc định
event int

Loại sự kiện chuột (ví dụ: cv2. EVENT_MOUSEMOVE, CV2. EVENT_LBUTTONDOWN, v.v.).

bắt buộc
x int

Tọa độ x của con trỏ chuột.

bắt buộc
y int

Tọa độ y của con trỏ chuột.

bắt buộc
flags int

Bất kỳ cờ nào liên quan đến sự kiện (ví dụ: cv2. EVENT_FLAG_CTRLKEY, CV2. EVENT_FLAG_SHIFTKEY, v.v.).

bắt buộc
param dict

Các tham số bổ sung mà bạn có thể muốn chuyển cho hàm.

bắt buộc
Mã nguồn trong ultralytics/solutions/distance_calculation.py
def mouse_event_for_distance(self, event, x, y, flags, param):
    """
    This function is designed to move region with mouse events in a real-time video stream.

    Args:
        event (int): The type of mouse event (e.g., cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN, etc.).
        x (int): The x-coordinate of the mouse pointer.
        y (int): The y-coordinate of the mouse pointer.
        flags (int): Any flags associated with the event (e.g., cv2.EVENT_FLAG_CTRLKEY,
            cv2.EVENT_FLAG_SHIFTKEY, etc.).
        param (dict): Additional parameters you may want to pass to the function.
    """
    global selected_boxes
    global left_mouse_count
    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] = []
                    self.selected_boxes[track_id] = box

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

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

Cấu hình tính toán khoảng cách và hiển thị các thông số.

Thông số:

Tên Kiểu Sự miêu tả Mặc định
names dict

Tên lớp phát hiện đối tượng

bắt buộc
pixels_per_meter int

Số pixel tính bằng mét

10
view_img bool

Cờ cho biết hiển thị khung

False
line_thickness int

Độ dày dòng cho các hộp giới hạn.

2
line_color RGB

Màu sắc của dòng Centroid

(255, 255, 0)
centroid_color RGB

Màu sắc của Centroid bbox

(255, 0, 255)
Mã nguồn trong ultralytics/solutions/distance_calculation.py
def set_args(
    self,
    names,
    pixels_per_meter=10,
    view_img=False,
    line_thickness=2,
    line_color=(255, 255, 0),
    centroid_color=(255, 0, 255),
):
    """
    Configures the distance calculation and display parameters.

    Args:
        names (dict): object detection classes names
        pixels_per_meter (int): Number of pixels in meter
        view_img (bool): Flag indicating frame display
        line_thickness (int): Line thickness for bounding boxes.
        line_color (RGB): color of centroids line
        centroid_color (RGB): colors of bbox centroids
    """
    self.names = names
    self.pixel_per_meter = pixels_per_meter
    self.view_img = view_img
    self.line_thickness = line_thickness
    self.line_color = line_color
    self.centroid_color = centroid_color

start_process(im0, tracks)

Tính toán khoảng cách giữa hai hộp giới hạn dựa trên dữ liệu theo dõi.

Thông số:

Tên Kiểu Sự miêu tả Mặc định
im0 nd array

Ảnh

bắt buộc
tracks list

Danh sách các bản nhạc thu được từ quá trình theo dõi đối tượng.

bắt buộc
Mã nguồn trong ultralytics/solutions/distance_calculation.py
def start_process(self, im0, tracks):
    """
    Calculate distance between two bounding boxes based on tracking data.

    Args:
        im0 (nd array): Image
        tracks (list): List of tracks obtained from the object tracking process.
    """
    self.im0 = im0
    if tracks[0].boxes.id is None:
        if self.view_img:
            self.display_frames()
        return
    self.extract_tracks(tracks)

    self.annotator = Annotator(self.im0, line_width=2)

    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.items():
                if trk_id == track_id:
                    self.selected_boxes[track_id] = box

    if len(self.selected_boxes) == 2:
        for trk_id, box in self.selected_boxes.items():
            centroid = self.calculate_centroid(self.selected_boxes[trk_id])
            self.centroids.append(centroid)

        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





Đã tạo 2024-01-05, Cập nhật 2024-05-08
Tác giả: Burhan-Q (1), AyushExel (1), RizwanMunawar (1)