Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Image resize and preserve aspect ratio : #609 #3301

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

NeerajKomuravalli
Copy link
Contributor

@TobyRoseman
As discussed,
I have added two basic functions:

  1. resize_with_original_aspect_ratio : to resize images by preserving aspect ratio using the user given parameter => min_side_length/max_side_length.
  2. resize_annotations : to resize annotations based on any new image dimension provided by the user.

I couldn't find any test cases written for any functions in image_analysis.py in the test folder so I did not add any test cases for these as well.

Let me know if any changes are required to the code submitted.

@TobyRoseman TobyRoseman self-requested a review August 25, 2020 00:09
Copy link
Collaborator

@TobyRoseman TobyRoseman left a comment

Choose a reason for hiding this comment

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

@NeerajKomuravalli - Thanks for the pull request.

We'll need to add some unit test for this new functionality. I think test_image_type.py is probably the right place for those unit tests.

raise ValueError("annotations are expected in the form of list")

for ann_index in range(len(annotations)):
annotations[ann_index]["coordinates"]["height"] = annotations[ann_index]["coordinates"]["height"]*target_image_shape[1]/original_image_shape[1]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Recalculating target_image_shape[0]/original_image_shape[0] and target_image_shape[1]/original_image_shape[1] twice for each element of annotations is not optimal.

It's better to calculate those values once, outside of the loop, and then just use them inside the loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, I will fix it.

target_image_shape : tuple | list
Target image dimesions in the format of (width, height)
"""
if type(annotations) is not list:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should work for SArray too. I think with out this check it would work for SArrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually did not intent it work for SArray but I think make it work for SArray makes a lot of sense. Will modify it to work for SArray as well.

"""

from ...data_structures.sarray import SArray as _SArray
import turicreate as _tc
Copy link
Collaborator

Choose a reason for hiding this comment

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

You don't need this line. It's already imported at the top of the file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove it from there, also I want to point out that the same thing is present in the resize function.

side_length_type = "min_length"
side_length = min_side_length
else:
raise ValueError("Value of min_side_length parameter cannot be negetive")
Copy link
Collaborator

Choose a reason for hiding this comment

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

negetive -> negative.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will make the modification.

side_length_type = "max_length"
side_length = max_side_length
else:
raise ValueError("Value of max_side_length parameter cannot be negetive")
Copy link
Collaborator

Choose a reason for hiding this comment

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

negetive -> negative.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will make the modification.

Resize a single image

>>> img = turicreate.Image('https://static.turi.com/datasets/images/sample.jpg')
>>> resized_img = turicreate.image_analysis.resize_with_original_aspect_ratio(img,min_side_length=500,max_side_length=None, channels=1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

With example code, we want a space after each comma.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will make the modification.


Returns
-------
out : turicreate.Image
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's could also be an SArray, if the input is an SArray, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, you are right. I completely missed it. Will add SArray here

if min_side_length is None and max_side_length is None:
raise ValueError("Cannot resize when neither `min_side_length` or `max_side_length` is not provided")

if (min_side_length is not None and min_side_length > 0) and (max_side_length is not None and max_side_length > 0):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Later on you seem to be checking the case were the side lengths are negative. The error message here doesn't mention negative. You probably want to remove the negative checks here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh, yeah! No need to do negative checks here.

side_length = max_side_length
else:
raise ValueError("Value of max_side_length parameter cannot be negetive")
else:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think you need this. I think you're already checking the case were either is provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True, will make the modifications.

)


def _get_new_image_dimensions_with_original_aspect_ratio(image, side_length_type, side_length):
Copy link
Collaborator

Choose a reason for hiding this comment

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

It would be cleaner to make this an inner function to resize_with_original_aspect_ratio.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do.

@TobyRoseman
Copy link
Collaborator

linking to open issue: #609.

@NeerajKomuravalli
Copy link
Contributor Author

Will add test cases In test_image_type.py and push all the modifications you recommended in the next push.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants