API Reference

cv2utils.haar_cascade

class cv2utils.haar_cascade.EyeCascade(weights_file: str = None, scale_factor: float = 1.1, min_neighbors: int = 3, min_size: tuple = (0, 0))

Eye detection using Haar feature-based cascade classifiers.

Parameters:
  • weights_file (str, optional) – Path to the xml file.
  • scale_factor (float, optional) – Scaling by some factor (create a scale pyramid)
  • min_neighbots (int, optional) – How many neighbors each candidate rectangle should have to retain it
  • size (tuple, optional) – Minimal size

Examples

>>> import cv2
>>> from cv2utils import EyeCascade
>>> eye_detector = EyeCascade()
detect_eyes(image)

Apply the image on classifier.

Parameters:image (numpy.array) – Image (BGR color space) for detection eye on it.
Returns:a list of all dicts containg all detections. Each dict contains: box (The bounding box is formatted as [x_initial, y_initial, x_final, y_final] under the key ‘box’) and label (identifies which object is detecting)
Return type:list

Examples

This classifier was trained with images with low resolution. So, for better results, apply the face detector first and create a Region Of Interest (ROI).

>>> import cv2
>>> from cv2utils import FaceCascade, EyeCascade
>>> image = imread("face.jpg")
>>> face_detector = FaceCascade()
>>> faces = face_detector.detect_faces(image)
>>> faces
[{'label': 'face', 'box': [199, 65, 591, 457]}]
>>>
>>> [x,y,x_final,y_final] = faces[0]['box']
>>> eye_detector = EyeCascade()
>>> eye_detector.detect_eyes(image[y:y_final, x:x_final])
[{'label': 'eye', 'box': [83, 132, 166, 215]}, {'label': 'eye', 'box': [218, 119, 298, 199]}]
class cv2utils.haar_cascade.FaceCascade(weights_file: str = None, scale_factor: float = 1.1, min_neighbors: int = 5, min_size: tuple = (10, 10))

Face detection using Haar feature-based cascade classifiers.

Parameters:
  • weights_file (str, optional) – Path to the xml file.
  • scale_factor (float, optional) – Scaling by some factor (create a scale pyramid)
  • min_neighbots (int, optional) – How many neighbors each candidate rectangle should have to retain it
  • size (tuple, optional) – Minimal size

Examples

>>> import cv2
>>> from cv2utils import FaceCascade
>>> face_detector = FaceCascade()
detect_faces(image)

Apply the image on classifier.

Parameters:image (numpy.array) – Image (BGR color space) for detection face on it.
Returns:a list of all dicts containg all detections. Each dict contains: box (The bounding box is formatted as [x_initial, y_initial, x_final, y_final] under the key ‘box’) and label (identifies which object is detecting)
Return type:list

Examples

>>> import cv2
>>> from cv2utils import FaceCascade, EyeCascade
>>> image = imread("face.jpg")
>>> face_detector = FaceCascade()
>>> faces = face_detector.detect_faces(image)
>>> faces
[{'label': 'face', 'box': [199, 65, 591, 457]}]

cv2utils.opencv_dnn

class cv2utils.opencv_dnn.FaceDnn(prototxt_file: str = None, caffemodel_file: str = None, threshold: float = 0.5, scale_factor: float = 1.0, size: tuple = (300, 300), mean: tuple = (104.0, 177.0, 123.0))

Implemented Face Detection using Deep Neural Network implemented in OpenCV.

OpenCV’s deep learning face detector is based on the Single Shot Detector (SSD) framework with a ResNet base network.

Parameters:
  • prototxt_file (str, optional) – Path to the Proto Caffe file.
  • caffemodel_file (str, optional) – Path to the pretrained Caffe model.
  • threshold (float, optional) – The model returns the confidence, by default filter detections with 50% confidence.
  • scale_factor (float, optional) – Scaling by some factor
  • size (tuple, optional) – Spatial size that the Neural Network expects.
  • mean (tuple, optional) – Mean subtraction.

Examples

>>> import cv2
>>> from cv2utils import FaceDnn
>>> detector = FaceDnn()
detect_faces(image)

Converts the image to blob and apply on the model.

Parameters:image (numpy.array) – Image (BGR color space) for detection face on it.
Returns:a list of all dicts containg all detections. Each dict contains: box (The bounding box is formatted as [x_initial, y_initial, x_final, y_final] under the key ‘box’), confidence (is the probability estimate for a bounding box to be matching the label) and label (identifies which object is detecting)
Return type:list

Examples

>>> import cv2
>>> from cv2utils import FaceDnn
>>> detector = FaceDnn()
>>> image = cv2.imread("face.jpg")
>>> detector.detect_faces(image)
[{'label': 'face', 'confidence': 0.9966524243354797, 'box': [210, 64, 522, 465]}]