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

Adding additional interface for Object Detection #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions hugging_py_face/__about__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
__title__ = 'hugging_py_face'
__package_name__ = 'hugging_py_face'
__version__ = '0.2.0'
__version__ = '0.2.1'
__description__ = "Hugging-Py-Face, the Python client for the Hugging Face Inference API."
__email__ = "[email protected]"
__author__ = 'Minura Punchihewa'
__github__ = 'https://github.com/MinuraPunchihewa/hugging_py_face'
__pypi__ = 'https://pypi.org/project/hugging-py-face/'
__license__ = 'GPL-3.0'
__copyright__ = 'Copyright (C) 2023 Minura Punchihewa'
__copyright__ = 'Copyright (C) 2023 Minura Punchihewa'
24 changes: 22 additions & 2 deletions hugging_py_face/computer_vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def image_classification_in_df(self, df: DataFrame, column: Text, model: Optiona
:return: a pandas DataFrame with the label for the images. Each label added will be the one with the highest confidence score for that particular image. The label will be added as a new column called 'predictions' to the original DataFrame.
"""
predictions = self._query_in_df(df, column, model=model, task="image-classification")
df["predictions"] = [prediction[0]['label'] for prediction in predictions]
try:
df["predictions"] = [prediction[0]['label'] for prediction in predictions]
except Exception:
df["predictions"] = predictions
return df

def object_detection(self, inputs: Union[Text, List], model: Optional[Text] = None) -> List:
Expand All @@ -45,4 +48,21 @@ def object_detection(self, inputs: Union[Text, List], model: Optional[Text] = No
if type(inputs) == list:
return self._query(inputs, model=model, task="object-detection")
elif type(inputs) == str:
return self._query(inputs, model=model, task="object-detection")
return self._query(inputs, model=model, task="object-detection")

def object_detection_in_df(self, df: DataFrame, column: Text, model: Optional[Text] = None) -> DataFrame:
Copy link
Owner

Choose a reason for hiding this comment

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

This is really awesome! I was just thinking that since this is object detection, we might need to include the coordinates as well? What do you think? We can maybe include these in separate columns in the resulting DataFrame?

Copy link
Author

Choose a reason for hiding this comment

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

All thanks goes to you good sir! I'm just a fan who hopes I'm not making things worse. Coordinates will definitely be helpful especially if we want to augment images to pinpoint objects. I was messing around in my development area but need a little more time to try that without adding too much bloat to the codebase.

Copy link
Owner

Choose a reason for hiding this comment

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

You are making it much better! I was wondering how to tackle the object detection task and your help is much appreciated. Please take your time and reach out to me anytime if you want to talk it out.

"""
Copy link
Owner

Choose a reason for hiding this comment

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

Something else that I was thinking about is, given the situation that multiple objects are detected in a single image, we will only be returning the first one. Maybe we can include all of the objects detected for a particular image in separate rows? What do you feel about this?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah I noticed this behavior. I think having all objects might be a good option and allows for extensibility. In my case I was happy with a single result for the first iteration. Having any accurate result was a huge win to start as far as I was concerned.

Copy link
Owner

Choose a reason for hiding this comment

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

Awesome! Let's see what we can do to support this.

Detect objects from a dataframe.

:param df: a pandas DataFrame containing the images to perform object detection on.
:param column: the name of the column containing the file paths or urls of the images perform object detection on.
:param model: the model to use for the object detection task. If not provided, the recommended model from Hugging Face will be used.
:return: a pandas DataFrame with the label for the images. Each label added will be the one with the highest confidence score for that particular image. The label will be added as a new column called 'predictions' to the original DataFrame.
"""
predictions = self._query_in_df(df, column, model=model, task="object-detection")

try:
df["predictions"] = [prediction[0]['label'] for prediction in predictions]
except Exception:
df["predictions"] = predictions
return df