imgutils.tagging.format

Overview:

A tool that maps tags to a single string, commonly used for image feature labeling during model training.

add_underline

imgutils.tagging.format.add_underline(tag)[source]

Adds underscores to a tag string to make it compatible with image labeling conventions.

Parameters:

tag (str) – The input tag string.

Returns:

The tag string with underscores added.

Return type:

str

remove_underline

imgutils.tagging.format.remove_underline(tag)[source]

Removes underscores from a tag string, restoring it to its original form.

Parameters:

tag (str) – The input tag string.

Returns:

The tag string with underscores removed.

Return type:

str

tags_to_text

imgutils.tagging.format.tags_to_text(tags: Mapping[str, float], use_spaces: bool = False, use_escape: bool = True, include_score: bool = False, score_descend: bool = True) str[source]
Overview:

Transform tags to text for data labeling.

Parameters:
  • tags – Tags.

  • use_spaces – Use whitespace instead of _ in text, default is False.

  • use_escape – Escape unsafe characters in text, such as ( to \(, default is True.

  • include_score – Add score in text, default is False.

  • score_descend – Sort in descending order by the score of each tag. Default is True.

Examples::
>>> from imgutils.tagging import tags_to_text
>>>
>>> # a group of tags
>>> tags = {
...     'panty_pull': 0.6826801300048828,
...     'panties': 0.958938717842102,
...     'drinking_glass': 0.9340789318084717,
...     'areola_slip': 0.41196826100349426,
...     '1girl': 0.9988248348236084,
... }
>>>
>>> tags_to_text(tags)
'1girl, panties, drinking_glass, panty_pull, areola_slip'
>>> tags_to_text(tags, use_spaces=True)
'1girl, panties, drinking glass, panty pull, areola slip'
>>> tags_to_text(tags, include_score=True)
'(1girl:0.999), (panties:0.959), (drinking_glass:0.934), (panty_pull:0.683), (areola_slip:0.412)'