Loading docs/source/api_doc/detect/face.rst 0 → 100644 +14 −0 Original line number Diff line number Diff line imgutils.detect.face ========================== .. currentmodule:: imgutils.detect.face .. automodule:: imgutils.detect.face detect_faces ------------------------------ .. autofunction:: detect_faces docs/source/api_doc/detect/face_detect.benchmark.py 0 → 100644 +34 −0 Original line number Diff line number Diff line import random from benchmark import BaseBenchmark, create_plot_cli from imgutils.detect import detect_faces class FaceDetectBenchmark(BaseBenchmark): def __init__(self, level): BaseBenchmark.__init__(self) self.level = level def load(self): from imgutils.detect.face import _open_face_detect_model _ = _open_face_detect_model(level=self.level) def unload(self): from imgutils.detect.face import _open_face_detect_model _open_face_detect_model.cache_clear() def run(self): image_file = random.choice(self.all_images) _ = detect_faces(image_file, level=self.level) if __name__ == '__main__': create_plot_cli( [ ('face (yolov8s)', FaceDetectBenchmark('s')), ('face (yolov8n)', FaceDetectBenchmark('n')), ], title='Benchmark for Anime Face Detections', run_times=10, try_times=5, )() docs/source/api_doc/detect/face_detect.demo.py 0 → 100644 +19 −0 Original line number Diff line number Diff line from imgutils.detect import detect_faces from imgutils.detect.visual import detection_visualize from plot import image_plot def _detect(img, **kwargs): return detection_visualize(img, detect_faces(img, **kwargs)) if __name__ == '__main__': image_plot( (_detect('nian.png'), 'large scale'), (_detect('two_bikini_girls.png'), 'closed faces'), (_detect('genshin_post.jpg'), 'multiple'), (_detect('mostima_post.jpg'), 'anime style'), save_as='face_detect.dat.svg', columns=2, figsize=(12, 9), ) docs/source/api_doc/detect/index.rst +1 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ imgutils.detect .. toctree:: :maxdepth: 3 face head person visual Loading imgutils/detect/face.py 0 → 100644 +77 −0 Original line number Diff line number Diff line """ Overview: Detect human faces in anime images. Trained on dataset `Anime Face CreateML <https://universe.roboflow.com/my-workspace-mph8o/anime-face-createml>`_ with YOLOv8. .. image:: face_detect.dat.svg :align: center This is an overall benchmark of all the face detect models: .. image:: face_detect.benchmark.py.svg :align: center """ from functools import lru_cache from typing import List, Tuple from huggingface_hub import hf_hub_download from ._yolo import _image_preprocess, _data_postprocess from ..data import ImageTyping, load_image, rgb_encode from ..utils import open_onnx_model @lru_cache() def _open_face_detect_model(level: str = 's'): return open_onnx_model(hf_hub_download( 'deepghs/imgutils-models', f'face_detect/face_detect_best_{level}.onnx' )) def detect_faces(image: ImageTyping, level: str = 's', max_infer_size=640, conf_threshold: float = 0.45, iou_threshold: float = 0.7) \ -> List[Tuple[Tuple[int, int, int, int], str, float]]: """ Overview: Detect human faces in anime images. :param image: Image to detect. :param level: The model level being used can be either `s` or `n`. The `n` model runs faster with smaller system overface, while the `s` model achieves higher accuracy. The default value is `s`. :param max_infer_size: The maximum image size used for model inference, if the image size exceeds this limit, the image will be resized and used for inference. The default value is `640` pixels. :param conf_threshold: The confidence threshold, only detection results with confidence scores above this threshold will be returned. The default value is `0.45`. :param iou_threshold: The detection area coverage overlap threshold, areas with overlaps above this threshold will be discarded. The default value is `0.7`. :return: The detection results list, each item includes the detected area `(x0, y0, x1, y1)`, the target type (always `face`) and the target confidence score. Examples:: >>> from imgutils.detect import detect_faces, detection_visualize >>> >>> image = 'mostima_post.jpg' >>> result = detect_faces(image) # detect it >>> result [ ((29, 441, 204, 584), 'face', 0.7874319553375244), ((346, 59, 529, 275), 'face', 0.7510495185852051), ((606, 51, 895, 336), 'face', 0.6986488103866577) ] >>> >>> # visualize it >>> from matplotlib import pyplot as plt >>> plt.imshow(detection_visualize(image, result)) >>> plt.show() """ image = load_image(image, mode='RGB') new_image, old_size, new_size = _image_preprocess(image, max_infer_size) data = rgb_encode(new_image)[None, ...] output, = _open_face_detect_model(level).run(['output0'], {'images': data}) return _data_postprocess(output[0], conf_threshold, iou_threshold, old_size, new_size, ['face']) Loading
docs/source/api_doc/detect/face.rst 0 → 100644 +14 −0 Original line number Diff line number Diff line imgutils.detect.face ========================== .. currentmodule:: imgutils.detect.face .. automodule:: imgutils.detect.face detect_faces ------------------------------ .. autofunction:: detect_faces
docs/source/api_doc/detect/face_detect.benchmark.py 0 → 100644 +34 −0 Original line number Diff line number Diff line import random from benchmark import BaseBenchmark, create_plot_cli from imgutils.detect import detect_faces class FaceDetectBenchmark(BaseBenchmark): def __init__(self, level): BaseBenchmark.__init__(self) self.level = level def load(self): from imgutils.detect.face import _open_face_detect_model _ = _open_face_detect_model(level=self.level) def unload(self): from imgutils.detect.face import _open_face_detect_model _open_face_detect_model.cache_clear() def run(self): image_file = random.choice(self.all_images) _ = detect_faces(image_file, level=self.level) if __name__ == '__main__': create_plot_cli( [ ('face (yolov8s)', FaceDetectBenchmark('s')), ('face (yolov8n)', FaceDetectBenchmark('n')), ], title='Benchmark for Anime Face Detections', run_times=10, try_times=5, )()
docs/source/api_doc/detect/face_detect.demo.py 0 → 100644 +19 −0 Original line number Diff line number Diff line from imgutils.detect import detect_faces from imgutils.detect.visual import detection_visualize from plot import image_plot def _detect(img, **kwargs): return detection_visualize(img, detect_faces(img, **kwargs)) if __name__ == '__main__': image_plot( (_detect('nian.png'), 'large scale'), (_detect('two_bikini_girls.png'), 'closed faces'), (_detect('genshin_post.jpg'), 'multiple'), (_detect('mostima_post.jpg'), 'anime style'), save_as='face_detect.dat.svg', columns=2, figsize=(12, 9), )
docs/source/api_doc/detect/index.rst +1 −0 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ imgutils.detect .. toctree:: :maxdepth: 3 face head person visual Loading
imgutils/detect/face.py 0 → 100644 +77 −0 Original line number Diff line number Diff line """ Overview: Detect human faces in anime images. Trained on dataset `Anime Face CreateML <https://universe.roboflow.com/my-workspace-mph8o/anime-face-createml>`_ with YOLOv8. .. image:: face_detect.dat.svg :align: center This is an overall benchmark of all the face detect models: .. image:: face_detect.benchmark.py.svg :align: center """ from functools import lru_cache from typing import List, Tuple from huggingface_hub import hf_hub_download from ._yolo import _image_preprocess, _data_postprocess from ..data import ImageTyping, load_image, rgb_encode from ..utils import open_onnx_model @lru_cache() def _open_face_detect_model(level: str = 's'): return open_onnx_model(hf_hub_download( 'deepghs/imgutils-models', f'face_detect/face_detect_best_{level}.onnx' )) def detect_faces(image: ImageTyping, level: str = 's', max_infer_size=640, conf_threshold: float = 0.45, iou_threshold: float = 0.7) \ -> List[Tuple[Tuple[int, int, int, int], str, float]]: """ Overview: Detect human faces in anime images. :param image: Image to detect. :param level: The model level being used can be either `s` or `n`. The `n` model runs faster with smaller system overface, while the `s` model achieves higher accuracy. The default value is `s`. :param max_infer_size: The maximum image size used for model inference, if the image size exceeds this limit, the image will be resized and used for inference. The default value is `640` pixels. :param conf_threshold: The confidence threshold, only detection results with confidence scores above this threshold will be returned. The default value is `0.45`. :param iou_threshold: The detection area coverage overlap threshold, areas with overlaps above this threshold will be discarded. The default value is `0.7`. :return: The detection results list, each item includes the detected area `(x0, y0, x1, y1)`, the target type (always `face`) and the target confidence score. Examples:: >>> from imgutils.detect import detect_faces, detection_visualize >>> >>> image = 'mostima_post.jpg' >>> result = detect_faces(image) # detect it >>> result [ ((29, 441, 204, 584), 'face', 0.7874319553375244), ((346, 59, 529, 275), 'face', 0.7510495185852051), ((606, 51, 895, 336), 'face', 0.6986488103866577) ] >>> >>> # visualize it >>> from matplotlib import pyplot as plt >>> plt.imshow(detection_visualize(image, result)) >>> plt.show() """ image = load_image(image, mode='RGB') new_image, old_size, new_size = _image_preprocess(image, max_infer_size) data = rgb_encode(new_image)[None, ...] output, = _open_face_detect_model(level).run(['output0'], {'images': data}) return _data_postprocess(output[0], conf_threshold, iou_threshold, old_size, new_size, ['face'])