์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

์ฐธ์กฐ ultralytics/solutions/distance_calculation.py

์ฐธ๊ณ 

์ด ํŒŒ์ผ์€ https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/solutions/distance_calculation .py์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ํ’€ ๋ฆฌํ€˜์ŠคํŠธ ๐Ÿ› ๏ธ ์— ๊ธฐ์—ฌํ•˜์—ฌ ๋ฌธ์ œ ํ•ด๊ฒฐ์„ ๋„์™€์ฃผ์„ธ์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ™!



ultralytics.solutions.distance_calculation.DistanceCalculation

ํŠธ๋ž™์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์‹ค์‹œ๊ฐ„ ๋น„๋””์˜ค ์ŠคํŠธ๋ฆผ์—์„œ ๋‘ ์˜ค๋ธŒ์ ํŠธ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ 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__()

์‹œ๊ฐ, ์ด๋ฏธ์ง€, ํŠธ๋ž™ ๋ฐ ๊ฑฐ๋ฆฌ์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ ํด๋ž˜์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ 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)

๋ฐ”์šด๋”ฉ ๋ฐ•์Šค์˜ ์ค‘์‹ฌ์„ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
box list

๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ๋ฐ์ดํ„ฐ

ํ•„์ˆ˜
์˜ ์†Œ์Šค ์ฝ”๋“œ 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)

๋‘ ๊ตฌ์‹ฌ์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
centroid1 point

์ฒซ ๋ฒˆ์งธ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ๋ฐ์ดํ„ฐ

ํ•„์ˆ˜
centroid2 point

๋‘ ๋ฒˆ์งธ ๋ฐ”์šด๋”ฉ ๋ฐ•์Šค ๋ฐ์ดํ„ฐ

ํ•„์ˆ˜
์˜ ์†Œ์Šค ์ฝ”๋“œ 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()

๋””์Šคํ”Œ๋ ˆ์ด ํ”„๋ ˆ์ž„.

์˜ ์†Œ์Šค ์ฝ”๋“œ 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)

์ œ๊ณต๋œ ๋ฐ์ดํ„ฐ์—์„œ ๊ฒฐ๊ณผ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
tracks list

๊ฐœ์ฒด ์ถ”์  ํ”„๋กœ์„ธ์Šค์—์„œ ์–ป์€ ํŠธ๋ž™ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
์˜ ์†Œ์Šค ์ฝ”๋“œ 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)

์ด ๊ธฐ๋Šฅ์€ ์‹ค์‹œ๊ฐ„ ๋™์˜์ƒ ์ŠคํŠธ๋ฆผ์—์„œ ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ๊ฐ€ ์žˆ๋Š” ์˜์—ญ์„ ์ด๋™ํ•˜๋„๋ก ์„ค๊ณ„๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
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):
    """
    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))

๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ ๋ฐ ํ‘œ์‹œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
names dict

๊ฐ์ฒด ๊ฐ์ง€ ํด๋ž˜์Šค ์ด๋ฆ„

ํ•„์ˆ˜
pixels_per_meter int

๋ฏธํ„ฐ ๋‹จ์œ„ ํ”ฝ์…€ ์ˆ˜

10
view_img bool

ํ”„๋ ˆ์ž„ ํ‘œ์‹œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ”Œ๋ž˜๊ทธ

False
line_thickness int

๊ฒฝ๊ณ„ ์ƒ์ž์˜ ์„  ๋‘๊ป˜์ž…๋‹ˆ๋‹ค.

2
line_color RGB

์ค‘์‹ฌ์„  ์ƒ‰์ƒ

(255, 255, 0)
centroid_color RGB

B๋ฐ•์Šค ์ค‘์‹ฌ์ฒด์˜ ์ƒ‰์ƒ

(255, 0, 255)
์˜ ์†Œ์Šค ์ฝ”๋“œ 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)

์ถ”์  ๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋‘ ๊ฒฝ๊ณ„ ์ƒ์ž ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
im0 nd array

์ด๋ฏธ์ง€

ํ•„์ˆ˜
tracks list

๊ฐœ์ฒด ์ถ”์  ํ”„๋กœ์„ธ์Šค์—์„œ ์–ป์€ ํŠธ๋ž™ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
์˜ ์†Œ์Šค ์ฝ”๋“œ 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





์ƒ์„ฑ 2024-01-05, ์—…๋ฐ์ดํŠธ 2024-05-08
์ž‘์„ฑ์ž: Burhan-Q (1), ์•„์œ ์‹œ์—‘์…€ (1), ๋ฆฌ์ฆˆ์™„๋ฌด๋‚˜์™€๋ฅด (1)