realutils.detect.face

Overview:

Detect human faces in real images.

Inspired by project akanametov/yolo-face.

../../_images/face_detect_demo.plot.py.svg

This is an overall benchmark of all the face detect models:

../../_images/face_detect_benchmark.plot.py.svg

The models are hosted on huggingface - deepghs/yolo-face.

detect_real_faces

realutils.detect.face.detect_real_faces(image: str | PathLike | bytes | bytearray | BinaryIO | Image, model_name: str = 'yolov11s-face', conf_threshold: float = 0.25, iou_threshold: float = 0.7, **kwargs) List[Tuple[Tuple[int, int, int, int], str, float]][source]

Detect human faces in real images using YOLO models.

This function applies a pre-trained YOLO model to detect faces in the given anime image. It supports different model levels and versions, allowing users to balance between detection speed and accuracy.

Parameters:
  • image (ImageTyping) – The input image for face detection. Can be various image types supported by ImageTyping.

  • model_name (str) – Optional custom model name. If provided, it overrides the auto-generated model name.

  • conf_threshold (float) – The confidence threshold for detections. Only detections with confidence scores above this threshold will be returned. Default is 0.25.

  • iou_threshold (float) – The Intersection over Union (IoU) threshold for non-maximum suppression. Detections with IoU above this threshold will be merged. Default is 0.7.

Returns:

A list of detected faces. Each face is represented by a tuple containing: - Bounding box coordinates as (x0, y0, x1, y1) - The string ‘face’ (as this function only detects faces) - The confidence score of the detection

Return type:

List[Tuple[Tuple[int, int, int, int], str, float]]

Example:
>>> from realutils.detect import detect_real_faces
>>>
>>> detect_real_faces('yolo/solo.jpg')
[((168, 79, 245, 199), 'face', 0.7996422052383423)]
>>> detect_real_faces('yolo/2girls.jpg')
[((721, 152, 1082, 726), 'face', 0.8811314702033997), ((158, 263, 509, 714), 'face', 0.8745490908622742)]
>>> detect_real_faces('yolo/3+cosplay.jpg')
[((351, 228, 410, 302), 'face', 0.8392542600631714), ((384, 63, 427, 116), 'face', 0.8173024654388428), ((195, 109, 246, 161), 'face', 0.8126493692398071)]
>>> detect_real_faces('yolo/multiple.jpg')
[((1074, 732, 1258, 987), 'face', 0.8792377710342407), ((1378, 536, 1541, 716), 'face', 0.8607611656188965), ((554, 295, 759, 557), 'face', 0.8541485071182251), ((897, 315, 1068, 520), 'face', 0.8539882898330688), ((1194, 230, 1329, 403), 'face', 0.8324605226516724)]
>>> from imgutils.detect import detection_visualize
>>> from matplotlib import pyplot as plt
>>>
>>> image = 'yolo/solo.jpg'
>>> result = detect_real_faces(image)
>>>
>>> # visualize it
>>> plt.imshow(detection_visualize(image, result))
>>> plt.show()