Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement/img detection extended #130

Merged
merged 11 commits into from
Nov 14, 2024
Merged

Conversation

aljazkonec1
Copy link
Contributor

This PR adapts ImgDetectionsExtended to use dai.RotatedRect object for storing information about the (rotated) bounding box. This simplifies porting to depthai in the future and reduces code duplication. Examples were also updated to take advantage of this new definition.

Some smaller updates were also made due to the use of dai v3 alpha6.

@github-actions github-actions bot added messages Changes affecting ml.messages parsers Changes affecting ml.parsers labels Nov 13, 2024
@codecov-commenter
Copy link

codecov-commenter commented Nov 13, 2024

Codecov Report

Attention: Patch coverage is 76.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 32.84%. Comparing base (37a2d92) to head (19e7f25).
Report is 5 commits behind head on main.

✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
depthai_nodes/ml/messages/img_detections.py 77.77% 4 Missing ⚠️
depthai_nodes/ml/messages/creators/detection.py 83.33% 1 Missing ⚠️
...pthai_nodes/ml/parsers/mediapipe_palm_detection.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #130      +/-   ##
==========================================
- Coverage   33.40%   32.84%   -0.56%     
==========================================
  Files          68       68              
  Lines        3739     3711      -28     
==========================================
- Hits         1249     1219      -30     
- Misses       2490     2492       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@klemen1999 klemen1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM, missing getter and setter for label_name. And let's also update README for messages with the new definition of ImgDetectionExtended

@aljazkonec1
Copy link
Contributor Author

Generally LGTM, missing getter and setter for label_name. And let's also update README for messages with the new definition of ImgDetectionExtended
I'll update this now and add some other functionality before opening up for review

@aljazkonec1 aljazkonec1 marked this pull request as ready for review November 13, 2024 13:18
Copy link
Collaborator

@jkbmrz jkbmrz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kkeroo
Copy link
Collaborator

kkeroo commented Nov 13, 2024

I think user expirience now is worse with ImgDetectionExtended because we add another "layer" called RotatedRect. There is no simple way to access the x1,y1,... or angles directly from the message. Previously, you could x_center = detection.x_center, now you have to x_center = detection.rotated_rect.center.x. User will not find it straight away. Same for functions: previously xmin, ymin, xmax, ymax = detection.get_xyxy_bbox(), now xmin = detection.rect.getOuterRect().

What I would like if we keep RotatedRect is that we add properties to ImgDetectionExtended like x_center and return detection.rotated_rect.center.x and also functions.

@aljazkonec1
Copy link
Contributor Author

I see your point @kkeroo but there are two things to consider:

  1. DAI already has RotatedRect and uses it in a lot of functions (like ImageManipNodeV2). Therefore, for the user to access these functionalities, he would have to convert to RotatedRect object. Why not do it for them then?
  2. When updating examples (and in some other code), I found the usage of RotatedRect to be easier than having to manually write x_center, y_center. I could just do rect = detection.rotated_rect and then focus on it as a whole rectangle instead of focusing on every single coordinate/shape on its own.

I do however still agree on having utils functions for converting to/from "depthai objects" (like RotatedRect, Point2f, Size2f, etc etc) to numpy arrays. But this can be done in another PR where we clearly define these functions and make the syntax as close to depthai syntax as possible.

@kkeroo
Copy link
Collaborator

kkeroo commented Nov 14, 2024

  1. DAI already has RotatedRect and uses it in a lot of functions (like ImageManipNodeV2). Therefore, for the user to access these functionalities, he would have to convert to RotatedRect object. Why not do it for them then?

Valid point.

  1. When updating examples (and in some other code), I found the usage of RotatedRect to be easier than having to manually write x_center, y_center. I could just do rect = detection.rotated_rect and then focus on it as a whole rectangle instead of focusing on every single coordinate/shape on its own.

Depends on the visualization. I see you used cv2.polylines

rect = detection.rotated_rect
points = rect.getPoints()
bbox = np.array([[point.x, point.y] for point in points])
bbox = bbox.astype(int)
cv2.polylines(frame, [bbox], isClosed=True, color=(255, 0, 0), thickness=2)

If we just add method to the ImgDetectionExtended we can have it shorter and more intuitive. And inside the method we explicitly use rect.getPoints:

def get_bbox(self):
    rect = detection.rotated_rect
    points = rect.getPoints()
    bbox = np.array([[point.x, point.y] for point in points])
    bbox = bbox.astype(int)
    return bbox

user wil then just call

bbox = detection.get_bbox()
cv2.polylines(...)

and the same for the rect.getOuterRect(). This just adds the option for getting right bbox coordinates in one line and we keep RotatedRect for the cases you provided.

@aljazkonec1
Copy link
Contributor Author

Okay I agree with this style now.

@aljazkonec1 aljazkonec1 merged commit d738aa5 into main Nov 14, 2024
10 checks passed
@aljazkonec1 aljazkonec1 deleted the improvement/ImgDetectionExtended branch November 14, 2024 11:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
messages Changes affecting ml.messages parsers Changes affecting ml.parsers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants