-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# to make sure the image is correctly received | ||
--- | ||
int16 plant_count |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
ros2_ws/src/python_workspace/python_workspace/scripts/tracker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import math | ||
|
||
class EuclideanDistTracker: | ||
def __init__(self): | ||
# Store the center positions of the objects | ||
self.center_points = {} | ||
# Keep the count of the IDs | ||
# each time a new object id detected, the count will increase by one | ||
self.id_count = 0 | ||
|
||
def reset(self): | ||
""" resets the count and also returns val of curr row | ||
""" | ||
self.center_points = {} | ||
row_count = self.id_count | ||
self.id_count = 0 | ||
return row_count | ||
|
||
|
||
def update(self, objects_rect): | ||
# Objects boxes and ids | ||
objects_bbs_ids = [] | ||
|
||
# Get center point of new object | ||
for rect in objects_rect: | ||
x1, y1, x2, y2 = rect | ||
cx = (x1 + x2) // 2 | ||
cy = (y1 + y2) // 2 | ||
|
||
# Find out if that object was detected already | ||
same_object_detected = False | ||
for id, pt in self.center_points.items(): | ||
dist = math.hypot(cx - pt[0], cy - pt[1]) | ||
|
||
if dist < 25: | ||
self.center_points[id] = (cx, cy) | ||
# print(self.center_points) | ||
objects_bbs_ids.append([x1, y1, x2, y2, id]) | ||
same_object_detected = True | ||
break | ||
|
||
# New object is detected we assign the ID to that object | ||
if same_object_detected is False: | ||
self.center_points[self.id_count] = (cx, cy) | ||
objects_bbs_ids.append([x1, y1, x2, y2, id]) | ||
self.id_count += 1 | ||
|
||
# Clean the dictionary by center points to remove IDS not used anymore | ||
new_center_points = {} | ||
for obj_bb_id in objects_bbs_ids: | ||
_, _, _, _, object_id = obj_bb_id | ||
center = self.center_points[object_id] | ||
new_center_points[object_id] = center | ||
|
||
# Update dictionary with IDs not used removed | ||
self.center_points = new_center_points.copy() | ||
return objects_bbs_ids |