From 492f397ae2e0004f4c6d318c0e58a789de75b733 Mon Sep 17 00:00:00 2001 From: Muhammad Rizwan Munawar Date: Mon, 29 Jan 2024 00:34:00 +0500 Subject: [PATCH] Add millimeters in `solutions/distance_caculation.py` + `object-cropping.md` visuals (#7860) Co-authored-by: UltralyticsAssistant Co-authored-by: Glenn Jocher --- docs/en/guides/index.md | 2 +- docs/en/guides/object-cropping.md | 9 ++++ ultralytics/solutions/distance_calculation.py | 22 +++------ ultralytics/utils/plotting.py | 45 +++++++++++++++++++ 4 files changed, 60 insertions(+), 18 deletions(-) diff --git a/docs/en/guides/index.md b/docs/en/guides/index.md index 9d63089f..eddc443c 100644 --- a/docs/en/guides/index.md +++ b/docs/en/guides/index.md @@ -37,7 +37,7 @@ Here's a compilation of in-depth guides to help you master different aspects of - [Objects Counting in Regions](region-counting.md) 🚀 NEW: Explore counting objects in specific regions with Ultralytics YOLOv8 for precise and efficient object detection in varied areas. - [Security Alarm System](security-alarm-system.md) 🚀 NEW: Discover the process of creating a security alarm system with Ultralytics YOLOv8. This system triggers alerts upon detecting new objects in the frame. Subsequently, you can customize the code to align with your specific use case. - [Heatmaps](heatmaps.md) 🚀 NEW: Elevate your understanding of data with our Detection Heatmaps! These intuitive visual tools use vibrant color gradients to vividly illustrate the intensity of data values across a matrix. Essential in computer vision, heatmaps are skillfully designed to highlight areas of interest, providing an immediate, impactful way to interpret spatial information. -- [Instance Segmentation with Object Tracking](instance-segmentation-and-tracking.md) 🚀 NEW: Explore our feature on Object Segmentation in Bounding Boxes Shape, providing a visual representation of precise object boundaries for enhanced understanding and analysis. +- [Instance Segmentation with Object Tracking](instance-segmentation-and-tracking.md) 🚀 NEW: Explore our feature on [Object Segmentation](https://docs.ultralytics.com/tasks/segment/) in Bounding Boxes Shape, providing a visual representation of precise object boundaries for enhanced understanding and analysis. - [VisionEye View Objects Mapping](vision-eye.md) 🚀 NEW: This feature aim computers to discern and focus on specific objects, much like the way the human eye observes details from a particular viewpoint. - [Speed Estimation](speed-estimation.md) 🚀 NEW: Speed estimation in computer vision relies on analyzing object motion through techniques like [object tracking](https://docs.ultralytics.com/modes/track/), crucial for applications like autonomous vehicles and traffic monitoring. - [Distance Calculation](distance-calculation.md) 🚀 NEW: Distance calculation, which involves measuring the separation between two objects within a defined space, is a crucial aspect. In the context of Ultralytics YOLOv8, the method employed for this involves using the bounding box centroid to determine the distance associated with user-highlighted bounding boxes. diff --git a/docs/en/guides/object-cropping.md b/docs/en/guides/object-cropping.md index 554eeb8c..126d3df2 100644 --- a/docs/en/guides/object-cropping.md +++ b/docs/en/guides/object-cropping.md @@ -16,6 +16,15 @@ Object cropping with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly - **Reduced Data Volume**: By extracting only relevant objects, object cropping helps in minimizing data size, making it efficient for storage, transmission, or subsequent computational tasks. - **Enhanced Precision**: YOLOv8's object detection accuracy ensures that the cropped objects maintain their spatial relationships, preserving the integrity of the visual information for detailed analysis. + +## Visuals + +| Airport Luggage | +|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| +| ![Conveyor Belt at Airport Suitcases Cropping using Ultralytics YOLOv8](https://github.com/RizwanMunawar/RizwanMunawar/assets/62513924/648f46be-f233-4307-a8e5-046eea38d2e4) | +| Suitcases Cropping at airport conveyor belt using Ultralytics YOLOv8 | + + !!! Example "Object Cropping using YOLOv8 Example" === "Object Cropping" diff --git a/ultralytics/solutions/distance_calculation.py b/ultralytics/solutions/distance_calculation.py index 79c1785e..207d1c4b 100644 --- a/ultralytics/solutions/distance_calculation.py +++ b/ultralytics/solutions/distance_calculation.py @@ -121,21 +121,7 @@ class DistanceCalculation: 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 - - def plot_distance_and_line(self, distance): - """ - Plot the distance and line on frame - Args: - distance (float): Distance between two centroids - """ - cv2.rectangle(self.im0, (15, 25), (280, 70), (255, 255, 255), -1) - cv2.putText( - self.im0, f"Distance : {distance:.2f}m", (20, 55), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 0), 2, cv2.LINE_AA - ) - cv2.line(self.im0, self.centroids[0], self.centroids[1], self.line_color, 3) - cv2.circle(self.im0, self.centroids[0], 6, self.centroid_color, -1) - cv2.circle(self.im0, self.centroids[1], 6, self.centroid_color, -1) + return pixel_distance / self.pixel_per_meter, (pixel_distance / self.pixel_per_meter) * 1000 def start_process(self, im0, tracks): """ @@ -166,8 +152,10 @@ class DistanceCalculation: centroid = self.calculate_centroid(self.selected_boxes[trk_id]) self.centroids.append(centroid) - distance = self.calculate_distance(self.centroids[0], self.centroids[1]) - self.plot_distance_and_line(distance) + 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 = [] diff --git a/ultralytics/utils/plotting.py b/ultralytics/utils/plotting.py index 629de701..c6b45219 100644 --- a/ultralytics/utils/plotting.py +++ b/ultralytics/utils/plotting.py @@ -519,6 +519,51 @@ class Annotator: self.im, label, (int(mask[0][0]) - text_size[0] // 2, int(mask[0][1]) - 5), 0, 0.7, (255, 255, 255), 2 ) + def plot_distance_and_line(self, distance_m, distance_mm, centroids, line_color, centroid_color): + """ + Plot the distance and line on frame. + + Args: + distance_m (float): Distance between two bbox centroids in meters. + distance_mm (float): Distance between two bbox centroids in millimeters. + centroids (list): Bounding box centroids data. + line_color (RGB): Distance line color. + centroid_color (RGB): Bounding box centroid color. + """ + (text_width_m, text_height_m), _ = cv2.getTextSize( + f"Distance M: {distance_m:.2f}m", cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2 + ) + cv2.rectangle(self.im, (15, 25), (15 + text_width_m + 10, 25 + text_height_m + 20), (255, 255, 255), -1) + cv2.putText( + self.im, + f"Distance M: {distance_m:.2f}m", + (20, 50), + cv2.FONT_HERSHEY_SIMPLEX, + 0.8, + (0, 0, 0), + 2, + cv2.LINE_AA, + ) + + (text_width_mm, text_height_mm), _ = cv2.getTextSize( + f"Distance MM: {distance_mm:.2f}mm", cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2 + ) + cv2.rectangle(self.im, (15, 75), (15 + text_width_mm + 10, 75 + text_height_mm + 20), (255, 255, 255), -1) + cv2.putText( + self.im, + f"Distance MM: {distance_mm:.2f}mm", + (20, 100), + cv2.FONT_HERSHEY_SIMPLEX, + 0.8, + (0, 0, 0), + 2, + cv2.LINE_AA, + ) + + cv2.line(self.im, centroids[0], centroids[1], line_color, 3) + cv2.circle(self.im, centroids[0], 6, centroid_color, -1) + cv2.circle(self.im, centroids[1], 6, centroid_color, -1) + def visioneye(self, box, center_point, color=(235, 219, 11), pin_color=(255, 0, 255), thickness=2, pins_radius=10): """ Function for pinpoint human-vision eye mapping and plotting.