asus4 / tf-lite-unity-sample

TensorFlow Lite Samples on Unity
834 stars 249 forks source link

Adjustment to SSD Bounding Box Size Calculation to Account for Texture Aspect Ratio #356

Closed kaku-panda closed 1 month ago

kaku-panda commented 1 month ago

Hello,

Firstly, I would like to express my gratitude for developing such a useful package that greatly enhances the implementation of object detection in Unity. It has been incredibly helpful.

I have identified an issue where the bounding boxes become disproportionately wide when the texture dimensions do not match the display aspect ratio. This seems to stem from not accounting for the texture's aspect ratio in the bounding box size calculation in the Invoke method.

Proposed Change: Include the texture's aspect ratio in the bounding box size calculations to ensure that bounding boxes are scaled and positioned accurately relative to the objects they are meant to represent.

↓ Current Code (SsdSample)

Vector2 size = (frameContainer.transform as RectTransform).rect.size;
for (int i = 0; i < 10; i++)
{
    SetFrame(frames[i], results[i], size);
}

↓Suggested Modification (SsdSample)

Vector2 ratio;
if (texture.width >= texture.height)
{
    ratio = new Vector2(1.0f, (float)texture.height / (float)texture.width);
}
else
{
    ratio = new Vector2((float)texture.width / (float)texture.height, 1.0f);
}
for (int i = 0; i < 10; i++)
{
    SetFrame(frames[i], results[i], size * ratio);
}

This modification will help to correct the aspect ratio of bounding boxes, making them more accurate and visually representative of the actual objects detected.

Thank you once again for your incredible work.

[PR Link] : https://github.com/asus4/tf-lite-unity-sample/pull/357#issue-2278335211

kaku-panda commented 1 month ago

⇩Suggested MOdififcation IMG_4749

⇩Current Code IMG_4750

asus4 commented 1 month ago

Fixed in #357. Thank you!