dghs-imgutils


Namedghs-imgutils JSON
Version 0.4.1 PyPI version JSON
download
home_pagehttps://github.com/deepghs/imgutils
SummaryA convenient and user-friendly anime-style image data processing library that integrates various advanced anime-style image processing models.
upload_time2024-03-20 09:03:30
maintainerNone
docs_urlNone
authornarugo1992, 7eu7d7
requires_python>=3.8
licenseApache License, Version 2.0
keywords a convenient and user-friendly anime-style image data processing library that integrates various advanced anime-style image processing models.
VCS
bugtrack_url
requirements hbutils pillow numpy scikit-learn huggingface_hub tqdm opencv-python opencv-contrib-python pandas scipy emoji pilmoji shapely pyclipper deprecation
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # imgutils

[![PyPI](https://img.shields.io/pypi/v/dghs-imgutils)](https://pypi.org/project/dghs-imgutils/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/dghs-imgutils)
![Loc](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/narugo1992/8bfaa96eaa25cc9dac54debbf22d363d/raw/loc.json)
![Comments](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/narugo1992/8bfaa96eaa25cc9dac54debbf22d363d/raw/comments.json)

[![Code Test](https://github.com/deepghs/imgutils/workflows/Code%20Test/badge.svg)](https://github.com/deepghs/imgutils/actions?query=workflow%3A%22Code+Test%22)
[![Package Release](https://github.com/deepghs/imgutils/workflows/Package%20Release/badge.svg)](https://github.com/deepghs/imgutils/actions?query=workflow%3A%22Package+Release%22)
[![codecov](https://codecov.io/gh/deepghs/imgutils/branch/main/graph/badge.svg?token=XJVDP4EFAT)](https://codecov.io/gh/deepghs/imgutils)

![GitHub Org's stars](https://img.shields.io/github/stars/deepghs)
[![GitHub stars](https://img.shields.io/github/stars/deepghs/imgutils)](https://github.com/deepghs/imgutils/stargazers)
[![GitHub forks](https://img.shields.io/github/forks/deepghs/imgutils)](https://github.com/deepghs/imgutils/network)
![GitHub commit activity](https://img.shields.io/github/commit-activity/m/deepghs/imgutils)
[![GitHub issues](https://img.shields.io/github/issues/deepghs/imgutils)](https://github.com/deepghs/imgutils/issues)
[![GitHub pulls](https://img.shields.io/github/issues-pr/deepghs/imgutils)](https://github.com/deepghs/imgutils/pulls)
[![Contributors](https://img.shields.io/github/contributors/deepghs/imgutils)](https://github.com/deepghs/imgutils/graphs/contributors)
[![GitHub license](https://img.shields.io/github/license/deepghs/imgutils)](https://github.com/deepghs/imgutils/blob/master/LICENSE)

A convenient and user-friendly anime-style image data processing library that integrates various advanced anime-style
image processing models.

## Installation

You can simply install it with `pip` command line from the official PyPI site.

```shell
pip install dghs-imgutils
```

If your operating environment includes a available GPU, you can use the following installation command to achieve higher
performance:

```shell
pip install dghs-imgutils[gpu]
```

For more information about installation, you can refer
to [Installation](https://deepghs.github.io/imgutils/main/tutorials/installation/index.html).

## Supported or Developing Features

* [Tachie(差分) Detection and Clustering](https://github.com/deepghs/imgutils#tachie%E5%B7%AE%E5%88%86-detection-and-clustering)
* [Contrastive Character Image Pretraining](https://github.com/deepghs/imgutils#contrastive-character-image-pretraining)
* [Object Detection](https://github.com/deepghs/imgutils#object-detection)
* [Edge Detection / Lineart Generation](https://github.com/deepghs/imgutils#edge-detection--lineart-generation)
* [Monochrome Image Detection](https://github.com/deepghs/imgutils#monochrome-image-detection)
* [Image Rating](https://github.com/deepghs/imgutils#image-rating)
* [Truncated Image Check](https://github.com/deepghs/imgutils#truncated-image-check)
* [Image Tagging](https://github.com/deepghs/imgutils#image-tagging)
* [Character Extraction](https://github.com/deepghs/imgutils#character-extraction)

`imgutils` also includes many other features besides that. For detailed descriptions and examples, please refer to
the [official documentation](https://deepghs.github.io/imgutils/main/index.html). Here, we won't go into each of them
individually.

### Tachie(差分) Detection and Clustering

For the dataset, we need to filter the differences between the tachie(差分). As shown in the following picture

![tachie](https://deepghs.github.io/imgutils/main/_images/lpips_full.plot.py.svg)

We can use `lpips_clustering` to cluster such situations as shown below

```python
from imgutils.metrics import lpips_clustering

images = [f'lpips/{i}.jpg' for i in range(1, 10)]
print(images)
# ['lpips/1.jpg', 'lpips/2.jpg', 'lpips/3.jpg', 'lpips/4.jpg', 'lpips/5.jpg', 'lpips/6.jpg', 'lpips/7.jpg', 'lpips/8.jpg', 'lpips/9.jpg']
print(lpips_clustering(images))  # -1 means noises, the same as that in sklearn
# [0, 0, 0, 1, 1, -1, -1, -1, -1]
```

### Contrastive Character Image Pretraining

We can use `imgutils` to extract features from anime character images (containing only a single character), calculate
the visual dissimilarity between two characters, and determine whether two images depict the same character. We can also
perform clustering operations based on this metric, as shown below

![ccip](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/ccip_full.plot.py.svg)

```python
from imgutils.metrics import ccip_difference, ccip_clustering

# same character
print(ccip_difference('ccip/1.jpg', 'ccip/2.jpg'))  # 0.16583099961280823

# different characters
print(ccip_difference('ccip/1.jpg', 'ccip/6.jpg'))  # 0.42947039008140564
print(ccip_difference('ccip/1.jpg', 'ccip/7.jpg'))  # 0.4037521779537201
print(ccip_difference('ccip/2.jpg', 'ccip/6.jpg'))  # 0.4371533691883087
print(ccip_difference('ccip/2.jpg', 'ccip/7.jpg'))  # 0.40748104453086853
print(ccip_difference('ccip/6.jpg', 'ccip/7.jpg'))  # 0.392294704914093

images = [f'ccip/{i}.jpg' for i in range(1, 13)]
print(images)
# ['ccip/1.jpg', 'ccip/2.jpg', 'ccip/3.jpg', 'ccip/4.jpg', 'ccip/5.jpg', 'ccip/6.jpg', 'ccip/7.jpg', 'ccip/8.jpg', 'ccip/9.jpg', 'ccip/10.jpg', 'ccip/11.jpg', 'ccip/12.jpg']
print(ccip_clustering(images, min_samples=2))  # few images, min_sample should not be too large
# [0, 0, 0, 3, 3, 3, 1, 1, 1, 1, 2, 2]
```

For more usage, please refer
to [official documentation of CCIP](https://deepghs.github.io/imgutils/main/api_doc/metrics/ccip.html).

### Object Detection

Currently, object detection is supported for anime heads and person, as shown below

* Face Detection

![face detection](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/face_detect_demo.plot.py.svg)

* Head Detection

![head detection](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/head_detect_demo.plot.py.svg)

* Person Detection

![person detection](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/person_detect_demo.plot.py.svg)

Based on practical tests, head detection currently has a very stable performance and can be used for automation tasks.
However, person detection is still being further iterated and will focus on enhancing detection capabilities for
artistic illustrations in the future.

### Edge Detection / Lineart Generation

Anime images can be converted to line drawings using the model provided
by [patrickvonplaten/controlnet_aux](https://github.com/patrickvonplaten/controlnet_aux), as shown below.

![edge example](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/edge_demo.plot.py.svg)

It is worth noting that the `lineart` model may consume more computational resources, while `canny` is the fastest but
has average effect. Therefore, `lineart_anime` may be the most balanced choice in most cases.

### Monochrome Image Detection

When filtering the crawled images, we need to remove monochrome images. However, monochrome images are often not simply
composed of grayscale colors and may still contain colors, as shown by the first two rows of six images in the figure
below

![monochrome example](https://deepghs.github.io/imgutils/main/_images/monochrome.plot.py.svg)

We can use `is_monochrome` to determine whether an image is monochrome, as shown below:

```python
from imgutils.validate import is_monochrome

print(is_monochrome('mono/1.jpg'))  # monochrome images
# True
print(is_monochrome('mono/2.jpg'))
# True
print(is_monochrome('mono/3.jpg'))
# True
print(is_monochrome('mono/4.jpg'))
# True
print(is_monochrome('mono/5.jpg'))
# True
print(is_monochrome('mono/6.jpg'))
# True
print(is_monochrome('colored/7.jpg'))  # colored images
# False
print(is_monochrome('colored/8.jpg'))
# False
print(is_monochrome('colored/9.jpg'))
# False
print(is_monochrome('colored/10.jpg'))
# False
print(is_monochrome('colored/11.jpg'))
# False
print(is_monochrome('colored/12.jpg'))
# False
```

For more details, please refer to
the [official documentation](https://deepghs.github.io/imgutils/main/api_doc/validate/monochrome.html#module-imgutils.validate.monochrome)
.

### Image Rating

We can use `imgutils` to perform a rough and quick determination of the rating (`safe`, `r15`, or `r18`) of anime
images, as shown below.

![rating](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/rating.plot.py.svg)

```python
from imgutils.validate import anime_rating

print(anime_rating('rating/safe/1.jpg'))  # ('safe', 0.9999998807907104)
print(anime_rating('rating/safe/2.jpg'))  # ('safe', 0.9924363493919373)
print(anime_rating('rating/safe/3.jpg'))  # ('safe', 0.996969997882843)
print(anime_rating('rating/safe/4.jpg'))  # ('safe', 0.9966891407966614)
print(anime_rating('rating/r15/5.jpg'))  # ('r15', 0.9996721744537354)
print(anime_rating('rating/r15/6.jpg'))  # ('r15', 0.9998563528060913)
print(anime_rating('rating/r15/7.jpg'))  # ('r15', 0.9827859997749329)
print(anime_rating('rating/r15/8.jpg'))  # ('r15', 0.8339558839797974)
print(anime_rating('rating/r18/9.jpg'))  # ('r18', 0.9997004270553589)
print(anime_rating('rating/r18/10.jpg'))  # ('r18', 0.9997699856758118)
print(anime_rating('rating/r18/11.jpg'))  # ('r18', 0.9999485015869141)
print(anime_rating('rating/r18/12.jpg'))  # ('r18', 0.9994290471076965)
```

### Truncated Image Check

The following code can be used to detect incomplete image files (such as images interrupted during the download
process):

```python
from imgutils.validate import is_truncated_file

if __name__ == '__main__':
    filename = 'test_jpg.jpg'
    if is_truncated_file(filename):
        print('This image is truncated, you\'d better '
              'remove this shit from your dataset.')
    else:
        print('This image is okay!')

```

### Image Tagging

The `imgutils` library integrates various anime-style image tagging models, allowing for results similar to the
following:

![tagging demo images](https://deepghs.github.io/imgutils/main/_images/tagging_demo.plot.py.svg)

The ratings, features, and characters in the image can be detected, like this:

```python
import os
from imgutils.tagging import get_wd14_tags

rating, features, chars = get_wd14_tags('skadi.jpg')
print(rating)
# {'general': 0.0011444687843322754, 'sensitive': 0.8876402974128723, 'questionable': 0.106781005859375, 'explicit': 0.000277101993560791}
print(features)
# {'1girl': 0.997527003288269, 'solo': 0.9797663688659668, 'long_hair': 0.9905703663825989, 'breasts': 0.9761719703674316,
#  'looking_at_viewer': 0.8981098532676697, 'bangs': 0.8810765743255615, 'large_breasts': 0.9498510360717773,
#  'shirt': 0.8377365469932556, 'red_eyes': 0.945058286190033, 'gloves': 0.9457170367240906, 'navel': 0.969594419002533,
#  'holding': 0.7881088852882385, 'hair_between_eyes': 0.7687551379203796, 'very_long_hair': 0.9301245212554932,
#  'standing': 0.6703325510025024, 'white_hair': 0.5292627811431885, 'short_sleeves': 0.8677047491073608,
#  'grey_hair': 0.5859264731407166, 'thighs': 0.9536856412887573, 'cowboy_shot': 0.8056888580322266,
#  'sweat': 0.8394746780395508, 'outdoors': 0.9473626613616943, 'parted_lips': 0.8986269235610962,
#  'sky': 0.9385137557983398, 'shorts': 0.8408567905426025, 'alternate_costume': 0.4245271384716034,
#  'day': 0.931140661239624, 'black_gloves': 0.8830795884132385, 'midriff': 0.7279844284057617,
#  'artist_name': 0.5333830714225769, 'cloud': 0.64717698097229, 'stomach': 0.9516432285308838,
#  'blue_sky': 0.9655293226242065, 'crop_top': 0.9485014081001282, 'black_shirt': 0.7366660833358765,
#  'short_shorts': 0.7161656618118286, 'ass_visible_through_thighs': 0.5858667492866516,
#  'black_shorts': 0.6186309456825256, 'thigh_gap': 0.41193312406539917, 'no_headwear': 0.467605859041214,
#  'low-tied_long_hair': 0.36282333731651306, 'sportswear': 0.3756745457649231, 'motion_blur': 0.5091936588287354,
#  'baseball_bat': 0.951993465423584, 'baseball': 0.5634750723838806, 'holding_baseball_bat': 0.8232709169387817}
print(chars)
# {'skadi_(arknights)': 0.9869340658187866}

rating, features, chars = get_wd14_tags('hutao.jpg')
print(rating)
# {'general': 0.49491602182388306, 'sensitive': 0.5193622708320618, 'questionable': 0.003406703472137451,
#  'explicit': 0.0007208287715911865}
print(features)
# {'1girl': 0.9798132181167603, 'solo': 0.8046203851699829, 'long_hair': 0.7596215009689331,
#  'looking_at_viewer': 0.7620116472244263, 'blush': 0.46084529161453247, 'smile': 0.48454540967941284,
#  'bangs': 0.5152207016944885, 'skirt': 0.8023070096969604, 'brown_hair': 0.8653596639633179,
#  'hair_ornament': 0.7201820611953735, 'red_eyes': 0.7816740870475769, 'long_sleeves': 0.697688639163971,
#  'twintails': 0.8974947333335876, 'school_uniform': 0.7491052746772766, 'jacket': 0.5015512704849243,
#  'flower': 0.6401398181915283, 'ahoge': 0.43420469760894775, 'pleated_skirt': 0.4528769850730896,
#  'outdoors': 0.5730487704277039, 'tongue': 0.6739872694015503, 'hair_flower': 0.5545973181724548,
#  'tongue_out': 0.6946243047714233, 'bag': 0.5487751364707947, 'symbol-shaped_pupils': 0.7439308166503906,
#  'blazer': 0.4186026453971863, 'backpack': 0.47378358244895935, ':p': 0.4690653085708618, 'ghost': 0.7565015554428101}
print(chars)
# {'hu_tao_(genshin_impact)': 0.9262397289276123, 'boo_tao_(genshin_impact)': 0.942080020904541}
```

We currently integrate the following tagging models:

* [Deepdanbooru model](https://deepghs.github.io/imgutils/main/api_doc/tagging/deepdanbooru.html), but not recommended
  for production use.
* [wd14-v2 model](https://deepghs.github.io/imgutils/main/api_doc/tagging/wd14.html#), inspired
  by [SmilingWolf/wd-v1-4-tags](https://huggingface.co/spaces/SmilingWolf/wd-v1-4-tags).

In addition, if you need to convert the dict-formatted data mentioned above into the text format required for image
training and tagging, you can also use the `tags_to_text` function (see the
link [here](https://deepghs.github.io/imgutils/main/api_doc/tagging/format.html#tags-to-text)) for formatting, as shown
below:

```python
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
}

print(tags_to_text(tags))
# '1girl, panties, drinking_glass, panty_pull, areola_slip'
print(tags_to_text(tags, use_spaces=True))
# '1girl, panties, drinking glass, panty pull, areola slip'
print(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)'
```

### Character Extraction

When we need to extract the character parts from anime images, we can use
the [`segment-rgba-with-isnetis`](https://deepghs.github.io/imgutils/main/api_doc/segment/isnetis.html#segment-rgba-with-isnetis)
function for extraction and obtain an RGBA format image (with the background part being transparent), just like the
example shown below.

![isnetis](https://deepghs.github.io/imgutils/main/_images/isnetis_trans.plot.py.svg)

```python
from imgutils.segment import segment_rgba_with_isnetis

mask_, image_ = segment_rgba_with_isnetis('hutao.png')
image_.save('hutao_seg.png')

mask_, image_ = segment_rgba_with_isnetis('skadi.jpg')
image_.save('skadi_seg.png')
```

This model can be found at https://huggingface.co/skytnt/anime-seg .

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/deepghs/imgutils",
    "name": "dghs-imgutils",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "A convenient and user-friendly anime-style image data processing library that integrates various advanced anime-style image processing models.",
    "author": "narugo1992, 7eu7d7",
    "author_email": "narugo992@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/b2/d7/715751f3dfb780b57ae970afc41c1dfa14394cd6408025e36d7ea4a76eaf/dghs-imgutils-0.4.1.tar.gz",
    "platform": null,
    "description": "# imgutils\n\n[![PyPI](https://img.shields.io/pypi/v/dghs-imgutils)](https://pypi.org/project/dghs-imgutils/)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/dghs-imgutils)\n![Loc](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/narugo1992/8bfaa96eaa25cc9dac54debbf22d363d/raw/loc.json)\n![Comments](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/narugo1992/8bfaa96eaa25cc9dac54debbf22d363d/raw/comments.json)\n\n[![Code Test](https://github.com/deepghs/imgutils/workflows/Code%20Test/badge.svg)](https://github.com/deepghs/imgutils/actions?query=workflow%3A%22Code+Test%22)\n[![Package Release](https://github.com/deepghs/imgutils/workflows/Package%20Release/badge.svg)](https://github.com/deepghs/imgutils/actions?query=workflow%3A%22Package+Release%22)\n[![codecov](https://codecov.io/gh/deepghs/imgutils/branch/main/graph/badge.svg?token=XJVDP4EFAT)](https://codecov.io/gh/deepghs/imgutils)\n\n![GitHub Org's stars](https://img.shields.io/github/stars/deepghs)\n[![GitHub stars](https://img.shields.io/github/stars/deepghs/imgutils)](https://github.com/deepghs/imgutils/stargazers)\n[![GitHub forks](https://img.shields.io/github/forks/deepghs/imgutils)](https://github.com/deepghs/imgutils/network)\n![GitHub commit activity](https://img.shields.io/github/commit-activity/m/deepghs/imgutils)\n[![GitHub issues](https://img.shields.io/github/issues/deepghs/imgutils)](https://github.com/deepghs/imgutils/issues)\n[![GitHub pulls](https://img.shields.io/github/issues-pr/deepghs/imgutils)](https://github.com/deepghs/imgutils/pulls)\n[![Contributors](https://img.shields.io/github/contributors/deepghs/imgutils)](https://github.com/deepghs/imgutils/graphs/contributors)\n[![GitHub license](https://img.shields.io/github/license/deepghs/imgutils)](https://github.com/deepghs/imgutils/blob/master/LICENSE)\n\nA convenient and user-friendly anime-style image data processing library that integrates various advanced anime-style\nimage processing models.\n\n## Installation\n\nYou can simply install it with `pip` command line from the official PyPI site.\n\n```shell\npip install dghs-imgutils\n```\n\nIf your operating environment includes a available GPU, you can use the following installation command to achieve higher\nperformance:\n\n```shell\npip install dghs-imgutils[gpu]\n```\n\nFor more information about installation, you can refer\nto [Installation](https://deepghs.github.io/imgutils/main/tutorials/installation/index.html).\n\n## Supported or Developing Features\n\n* [Tachie(\u5dee\u5206) Detection and Clustering](https://github.com/deepghs/imgutils#tachie%E5%B7%AE%E5%88%86-detection-and-clustering)\n* [Contrastive Character Image Pretraining](https://github.com/deepghs/imgutils#contrastive-character-image-pretraining)\n* [Object Detection](https://github.com/deepghs/imgutils#object-detection)\n* [Edge Detection / Lineart Generation](https://github.com/deepghs/imgutils#edge-detection--lineart-generation)\n* [Monochrome Image Detection](https://github.com/deepghs/imgutils#monochrome-image-detection)\n* [Image Rating](https://github.com/deepghs/imgutils#image-rating)\n* [Truncated Image Check](https://github.com/deepghs/imgutils#truncated-image-check)\n* [Image Tagging](https://github.com/deepghs/imgutils#image-tagging)\n* [Character Extraction](https://github.com/deepghs/imgutils#character-extraction)\n\n`imgutils` also includes many other features besides that. For detailed descriptions and examples, please refer to\nthe [official documentation](https://deepghs.github.io/imgutils/main/index.html). Here, we won't go into each of them\nindividually.\n\n### Tachie(\u5dee\u5206) Detection and Clustering\n\nFor the dataset, we need to filter the differences between the tachie(\u5dee\u5206). As shown in the following picture\n\n![tachie](https://deepghs.github.io/imgutils/main/_images/lpips_full.plot.py.svg)\n\nWe can use `lpips_clustering` to cluster such situations as shown below\n\n```python\nfrom imgutils.metrics import lpips_clustering\n\nimages = [f'lpips/{i}.jpg' for i in range(1, 10)]\nprint(images)\n# ['lpips/1.jpg', 'lpips/2.jpg', 'lpips/3.jpg', 'lpips/4.jpg', 'lpips/5.jpg', 'lpips/6.jpg', 'lpips/7.jpg', 'lpips/8.jpg', 'lpips/9.jpg']\nprint(lpips_clustering(images))  # -1 means noises, the same as that in sklearn\n# [0, 0, 0, 1, 1, -1, -1, -1, -1]\n```\n\n### Contrastive Character Image Pretraining\n\nWe can use `imgutils` to extract features from anime character images (containing only a single character), calculate\nthe visual dissimilarity between two characters, and determine whether two images depict the same character. We can also\nperform clustering operations based on this metric, as shown below\n\n![ccip](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/ccip_full.plot.py.svg)\n\n```python\nfrom imgutils.metrics import ccip_difference, ccip_clustering\n\n# same character\nprint(ccip_difference('ccip/1.jpg', 'ccip/2.jpg'))  # 0.16583099961280823\n\n# different characters\nprint(ccip_difference('ccip/1.jpg', 'ccip/6.jpg'))  # 0.42947039008140564\nprint(ccip_difference('ccip/1.jpg', 'ccip/7.jpg'))  # 0.4037521779537201\nprint(ccip_difference('ccip/2.jpg', 'ccip/6.jpg'))  # 0.4371533691883087\nprint(ccip_difference('ccip/2.jpg', 'ccip/7.jpg'))  # 0.40748104453086853\nprint(ccip_difference('ccip/6.jpg', 'ccip/7.jpg'))  # 0.392294704914093\n\nimages = [f'ccip/{i}.jpg' for i in range(1, 13)]\nprint(images)\n# ['ccip/1.jpg', 'ccip/2.jpg', 'ccip/3.jpg', 'ccip/4.jpg', 'ccip/5.jpg', 'ccip/6.jpg', 'ccip/7.jpg', 'ccip/8.jpg', 'ccip/9.jpg', 'ccip/10.jpg', 'ccip/11.jpg', 'ccip/12.jpg']\nprint(ccip_clustering(images, min_samples=2))  # few images, min_sample should not be too large\n# [0, 0, 0, 3, 3, 3, 1, 1, 1, 1, 2, 2]\n```\n\nFor more usage, please refer\nto [official documentation of CCIP](https://deepghs.github.io/imgutils/main/api_doc/metrics/ccip.html).\n\n### Object Detection\n\nCurrently, object detection is supported for anime heads and person, as shown below\n\n* Face Detection\n\n![face detection](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/face_detect_demo.plot.py.svg)\n\n* Head Detection\n\n![head detection](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/head_detect_demo.plot.py.svg)\n\n* Person Detection\n\n![person detection](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/person_detect_demo.plot.py.svg)\n\nBased on practical tests, head detection currently has a very stable performance and can be used for automation tasks.\nHowever, person detection is still being further iterated and will focus on enhancing detection capabilities for\nartistic illustrations in the future.\n\n### Edge Detection / Lineart Generation\n\nAnime images can be converted to line drawings using the model provided\nby [patrickvonplaten/controlnet_aux](https://github.com/patrickvonplaten/controlnet_aux), as shown below.\n\n![edge example](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/edge_demo.plot.py.svg)\n\nIt is worth noting that the `lineart` model may consume more computational resources, while `canny` is the fastest but\nhas average effect. Therefore, `lineart_anime` may be the most balanced choice in most cases.\n\n### Monochrome Image Detection\n\nWhen filtering the crawled images, we need to remove monochrome images. However, monochrome images are often not simply\ncomposed of grayscale colors and may still contain colors, as shown by the first two rows of six images in the figure\nbelow\n\n![monochrome example](https://deepghs.github.io/imgutils/main/_images/monochrome.plot.py.svg)\n\nWe can use `is_monochrome` to determine whether an image is monochrome, as shown below:\n\n```python\nfrom imgutils.validate import is_monochrome\n\nprint(is_monochrome('mono/1.jpg'))  # monochrome images\n# True\nprint(is_monochrome('mono/2.jpg'))\n# True\nprint(is_monochrome('mono/3.jpg'))\n# True\nprint(is_monochrome('mono/4.jpg'))\n# True\nprint(is_monochrome('mono/5.jpg'))\n# True\nprint(is_monochrome('mono/6.jpg'))\n# True\nprint(is_monochrome('colored/7.jpg'))  # colored images\n# False\nprint(is_monochrome('colored/8.jpg'))\n# False\nprint(is_monochrome('colored/9.jpg'))\n# False\nprint(is_monochrome('colored/10.jpg'))\n# False\nprint(is_monochrome('colored/11.jpg'))\n# False\nprint(is_monochrome('colored/12.jpg'))\n# False\n```\n\nFor more details, please refer to\nthe [official documentation](https://deepghs.github.io/imgutils/main/api_doc/validate/monochrome.html#module-imgutils.validate.monochrome)\n.\n\n### Image Rating\n\nWe can use `imgutils` to perform a rough and quick determination of the rating (`safe`, `r15`, or `r18`) of anime\nimages, as shown below.\n\n![rating](https://github.com/deepghs/imgutils/blob/gh-pages/main/_images/rating.plot.py.svg)\n\n```python\nfrom imgutils.validate import anime_rating\n\nprint(anime_rating('rating/safe/1.jpg'))  # ('safe', 0.9999998807907104)\nprint(anime_rating('rating/safe/2.jpg'))  # ('safe', 0.9924363493919373)\nprint(anime_rating('rating/safe/3.jpg'))  # ('safe', 0.996969997882843)\nprint(anime_rating('rating/safe/4.jpg'))  # ('safe', 0.9966891407966614)\nprint(anime_rating('rating/r15/5.jpg'))  # ('r15', 0.9996721744537354)\nprint(anime_rating('rating/r15/6.jpg'))  # ('r15', 0.9998563528060913)\nprint(anime_rating('rating/r15/7.jpg'))  # ('r15', 0.9827859997749329)\nprint(anime_rating('rating/r15/8.jpg'))  # ('r15', 0.8339558839797974)\nprint(anime_rating('rating/r18/9.jpg'))  # ('r18', 0.9997004270553589)\nprint(anime_rating('rating/r18/10.jpg'))  # ('r18', 0.9997699856758118)\nprint(anime_rating('rating/r18/11.jpg'))  # ('r18', 0.9999485015869141)\nprint(anime_rating('rating/r18/12.jpg'))  # ('r18', 0.9994290471076965)\n```\n\n### Truncated Image Check\n\nThe following code can be used to detect incomplete image files (such as images interrupted during the download\nprocess):\n\n```python\nfrom imgutils.validate import is_truncated_file\n\nif __name__ == '__main__':\n    filename = 'test_jpg.jpg'\n    if is_truncated_file(filename):\n        print('This image is truncated, you\\'d better '\n              'remove this shit from your dataset.')\n    else:\n        print('This image is okay!')\n\n```\n\n### Image Tagging\n\nThe `imgutils` library integrates various anime-style image tagging models, allowing for results similar to the\nfollowing:\n\n![tagging demo images](https://deepghs.github.io/imgutils/main/_images/tagging_demo.plot.py.svg)\n\nThe ratings, features, and characters in the image can be detected, like this:\n\n```python\nimport os\nfrom imgutils.tagging import get_wd14_tags\n\nrating, features, chars = get_wd14_tags('skadi.jpg')\nprint(rating)\n# {'general': 0.0011444687843322754, 'sensitive': 0.8876402974128723, 'questionable': 0.106781005859375, 'explicit': 0.000277101993560791}\nprint(features)\n# {'1girl': 0.997527003288269, 'solo': 0.9797663688659668, 'long_hair': 0.9905703663825989, 'breasts': 0.9761719703674316,\n#  'looking_at_viewer': 0.8981098532676697, 'bangs': 0.8810765743255615, 'large_breasts': 0.9498510360717773,\n#  'shirt': 0.8377365469932556, 'red_eyes': 0.945058286190033, 'gloves': 0.9457170367240906, 'navel': 0.969594419002533,\n#  'holding': 0.7881088852882385, 'hair_between_eyes': 0.7687551379203796, 'very_long_hair': 0.9301245212554932,\n#  'standing': 0.6703325510025024, 'white_hair': 0.5292627811431885, 'short_sleeves': 0.8677047491073608,\n#  'grey_hair': 0.5859264731407166, 'thighs': 0.9536856412887573, 'cowboy_shot': 0.8056888580322266,\n#  'sweat': 0.8394746780395508, 'outdoors': 0.9473626613616943, 'parted_lips': 0.8986269235610962,\n#  'sky': 0.9385137557983398, 'shorts': 0.8408567905426025, 'alternate_costume': 0.4245271384716034,\n#  'day': 0.931140661239624, 'black_gloves': 0.8830795884132385, 'midriff': 0.7279844284057617,\n#  'artist_name': 0.5333830714225769, 'cloud': 0.64717698097229, 'stomach': 0.9516432285308838,\n#  'blue_sky': 0.9655293226242065, 'crop_top': 0.9485014081001282, 'black_shirt': 0.7366660833358765,\n#  'short_shorts': 0.7161656618118286, 'ass_visible_through_thighs': 0.5858667492866516,\n#  'black_shorts': 0.6186309456825256, 'thigh_gap': 0.41193312406539917, 'no_headwear': 0.467605859041214,\n#  'low-tied_long_hair': 0.36282333731651306, 'sportswear': 0.3756745457649231, 'motion_blur': 0.5091936588287354,\n#  'baseball_bat': 0.951993465423584, 'baseball': 0.5634750723838806, 'holding_baseball_bat': 0.8232709169387817}\nprint(chars)\n# {'skadi_(arknights)': 0.9869340658187866}\n\nrating, features, chars = get_wd14_tags('hutao.jpg')\nprint(rating)\n# {'general': 0.49491602182388306, 'sensitive': 0.5193622708320618, 'questionable': 0.003406703472137451,\n#  'explicit': 0.0007208287715911865}\nprint(features)\n# {'1girl': 0.9798132181167603, 'solo': 0.8046203851699829, 'long_hair': 0.7596215009689331,\n#  'looking_at_viewer': 0.7620116472244263, 'blush': 0.46084529161453247, 'smile': 0.48454540967941284,\n#  'bangs': 0.5152207016944885, 'skirt': 0.8023070096969604, 'brown_hair': 0.8653596639633179,\n#  'hair_ornament': 0.7201820611953735, 'red_eyes': 0.7816740870475769, 'long_sleeves': 0.697688639163971,\n#  'twintails': 0.8974947333335876, 'school_uniform': 0.7491052746772766, 'jacket': 0.5015512704849243,\n#  'flower': 0.6401398181915283, 'ahoge': 0.43420469760894775, 'pleated_skirt': 0.4528769850730896,\n#  'outdoors': 0.5730487704277039, 'tongue': 0.6739872694015503, 'hair_flower': 0.5545973181724548,\n#  'tongue_out': 0.6946243047714233, 'bag': 0.5487751364707947, 'symbol-shaped_pupils': 0.7439308166503906,\n#  'blazer': 0.4186026453971863, 'backpack': 0.47378358244895935, ':p': 0.4690653085708618, 'ghost': 0.7565015554428101}\nprint(chars)\n# {'hu_tao_(genshin_impact)': 0.9262397289276123, 'boo_tao_(genshin_impact)': 0.942080020904541}\n```\n\nWe currently integrate the following tagging models:\n\n* [Deepdanbooru model](https://deepghs.github.io/imgutils/main/api_doc/tagging/deepdanbooru.html), but not recommended\n  for production use.\n* [wd14-v2 model](https://deepghs.github.io/imgutils/main/api_doc/tagging/wd14.html#), inspired\n  by [SmilingWolf/wd-v1-4-tags](https://huggingface.co/spaces/SmilingWolf/wd-v1-4-tags).\n\nIn addition, if you need to convert the dict-formatted data mentioned above into the text format required for image\ntraining and tagging, you can also use the `tags_to_text` function (see the\nlink [here](https://deepghs.github.io/imgutils/main/api_doc/tagging/format.html#tags-to-text)) for formatting, as shown\nbelow:\n\n```python\nfrom imgutils.tagging import tags_to_text\n\n# a group of tags\ntags = {\n    'panty_pull': 0.6826801300048828,\n    'panties': 0.958938717842102,\n    'drinking_glass': 0.9340789318084717,\n    'areola_slip': 0.41196826100349426,\n    '1girl': 0.9988248348236084\n}\n\nprint(tags_to_text(tags))\n# '1girl, panties, drinking_glass, panty_pull, areola_slip'\nprint(tags_to_text(tags, use_spaces=True))\n# '1girl, panties, drinking glass, panty pull, areola slip'\nprint(tags_to_text(tags, include_score=True))\n# '(1girl:0.999), (panties:0.959), (drinking_glass:0.934), (panty_pull:0.683), (areola_slip:0.412)'\n```\n\n### Character Extraction\n\nWhen we need to extract the character parts from anime images, we can use\nthe [`segment-rgba-with-isnetis`](https://deepghs.github.io/imgutils/main/api_doc/segment/isnetis.html#segment-rgba-with-isnetis)\nfunction for extraction and obtain an RGBA format image (with the background part being transparent), just like the\nexample shown below.\n\n![isnetis](https://deepghs.github.io/imgutils/main/_images/isnetis_trans.plot.py.svg)\n\n```python\nfrom imgutils.segment import segment_rgba_with_isnetis\n\nmask_, image_ = segment_rgba_with_isnetis('hutao.png')\nimage_.save('hutao_seg.png')\n\nmask_, image_ = segment_rgba_with_isnetis('skadi.jpg')\nimage_.save('skadi_seg.png')\n```\n\nThis model can be found at https://huggingface.co/skytnt/anime-seg .\n",
    "bugtrack_url": null,
    "license": "Apache License, Version 2.0",
    "summary": "A convenient and user-friendly anime-style image data processing library that integrates various advanced anime-style image processing models.",
    "version": "0.4.1",
    "project_urls": {
        "Homepage": "https://github.com/deepghs/imgutils"
    },
    "split_keywords": [
        "a",
        "convenient",
        "and",
        "user-friendly",
        "anime-style",
        "image",
        "data",
        "processing",
        "library",
        "that",
        "integrates",
        "various",
        "advanced",
        "anime-style",
        "image",
        "processing",
        "models."
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "cd3776d9398894192b034a646c00b2a614a73c7bbac0b9a6477e05a1763b528e",
                "md5": "0054983058ace218201d75d2a943d68c",
                "sha256": "262e202a1069fa40216ea27f62adb7331ac4ee77695f08db9cb66e5b589e2e93"
            },
            "downloads": -1,
            "filename": "dghs_imgutils-0.4.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "0054983058ace218201d75d2a943d68c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 318399,
            "upload_time": "2024-03-20T09:03:27",
            "upload_time_iso_8601": "2024-03-20T09:03:27.862037Z",
            "url": "https://files.pythonhosted.org/packages/cd/37/76d9398894192b034a646c00b2a614a73c7bbac0b9a6477e05a1763b528e/dghs_imgutils-0.4.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b2d7715751f3dfb780b57ae970afc41c1dfa14394cd6408025e36d7ea4a76eaf",
                "md5": "68671c0787dbcd6755610565faf68615",
                "sha256": "dd6615969851042085cca761a53927fd65acdfce65ee71d7036da1e5d4cd8f14"
            },
            "downloads": -1,
            "filename": "dghs-imgutils-0.4.1.tar.gz",
            "has_sig": false,
            "md5_digest": "68671c0787dbcd6755610565faf68615",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 285358,
            "upload_time": "2024-03-20T09:03:30",
            "upload_time_iso_8601": "2024-03-20T09:03:30.409355Z",
            "url": "https://files.pythonhosted.org/packages/b2/d7/715751f3dfb780b57ae970afc41c1dfa14394cd6408025e36d7ea4a76eaf/dghs-imgutils-0.4.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-20 09:03:30",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "deepghs",
    "github_project": "imgutils",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "hbutils",
            "specs": [
                [
                    ">=",
                    "0.9.0"
                ]
            ]
        },
        {
            "name": "pillow",
            "specs": []
        },
        {
            "name": "numpy",
            "specs": []
        },
        {
            "name": "scikit-learn",
            "specs": []
        },
        {
            "name": "huggingface_hub",
            "specs": []
        },
        {
            "name": "tqdm",
            "specs": []
        },
        {
            "name": "opencv-python",
            "specs": []
        },
        {
            "name": "opencv-contrib-python",
            "specs": []
        },
        {
            "name": "pandas",
            "specs": []
        },
        {
            "name": "scipy",
            "specs": []
        },
        {
            "name": "emoji",
            "specs": [
                [
                    ">=",
                    "2.5.0"
                ]
            ]
        },
        {
            "name": "pilmoji",
            "specs": [
                [
                    ">=",
                    "1.3.0"
                ]
            ]
        },
        {
            "name": "shapely",
            "specs": []
        },
        {
            "name": "pyclipper",
            "specs": []
        },
        {
            "name": "deprecation",
            "specs": [
                [
                    ">=",
                    "2.0.0"
                ]
            ]
        }
    ],
    "lcname": "dghs-imgutils"
}
        
Elapsed time: 0.22472s