Skip to main content

Simple multi-model face detection library

Project description

FaceSwitch

Detect faces in any image with one line of Python — swap the detector without changing your code.

FaceSwitch is a lightweight Python library that wraps multiple face detection engines behind a single, consistent interface. Pick the detector that suits your needs, swap it later without rewriting anything, and only install what you actually use.


What does it do?

You give it an image. It tells you where the faces are.

from faceswitch.detectors.yolo import YoloDetector
import cv2

image = cv2.imread("photo.jpg")
detector = YoloDetector()
faces = detector.detect(image)

for face in faces:
    print(f"Face at ({face.x1}, {face.y1}) → ({face.x2}, {face.y2})")

Every detector returns the same thing — a list of face boxes — so switching from YOLO to HOG (or any other detector) is just changing one import line.


Installation

You need Python 3.10 or newer.

Step 1 — install the base library:

pip install faceswitch

Step 2 — install the detector you want to use:

Detector What it is Install command
HOG Classic CPU-based detector (fast, lightweight, no GPU needed) pip install "faceswitch[hog]"
YOLO Deep learning detector (more accurate, works best with GPU) pip install "faceswitch[yolo]"
RetinaFace High-accuracy ResNet detector (best for difficult angles and small faces) pip install "faceswitch[retinaface]"

| RetinaFace | Deep learning face detector using ResNet+FPN with multi-scale anchors for high-accuracy detection. | pip install "faceswitch[retinaface]" |

Or install everything at once:

pip install "faceswitch[all]"

Quick start — 3 lines to detect faces

import cv2
from faceswitch.detectors.hog import HogDetector   # swap this line to switch detectors

image = cv2.imread("photo.jpg")
faces = HogDetector().detect(image)

print(f"Found {len(faces)} face(s)")

To use a different detector, change only the import:

from faceswitch.detectors.yolo import YoloDetector         # YOLO
from faceswitch.detectors.retinaface import RetinaFaceDetector  # RetinaFace

Everything else stays exactly the same.


Understanding the results

detector.detect(image) always returns a list of FaceBox objects. Each one looks like this:

FaceBox(x1=120, y1=45, x2=210, y2=160, confidence=0.97)
Field Meaning
x1, y1 Top-left corner of the face box (pixels)
x2, y2 Bottom-right corner of the face box (pixels)
confidence How sure the detector is (0.0 to 1.0). Some detectors don't provide this (None).

Draw the boxes on your image:

import cv2
from faceswitch.detectors.yolo import YoloDetector

image = cv2.imread("photo.jpg")
faces = YoloDetector().detect(image)

for face in faces:
    cv2.rectangle(image, (face.x1, face.y1), (face.x2, face.y2), (0, 255, 0), 2)

cv2.imwrite("result.jpg", image)
print(f"Saved result.jpg with {len(faces)} face(s) highlighted")

Choosing the right detector

HOG YOLO RetinaFace
Speed Fast Medium Slower
Accuracy Basic High Very high
GPU needed? No Optional No
Best for Quick scripts, low-power machines General use, real-time video Difficult angles, small faces, production use
Install size Small (~80MB) Large (~500MB with torch) Large (~200MB with TensorFlow)

Not sure? Start with HOG. If it misses faces, switch to YOLO or RetinaFace — your code won't change.


Available detectors

HOG — pip install "faceswitch[hog]"

Uses dlib's Histogram of Oriented Gradients detector. Works entirely on CPU, very fast, good for frontal faces.

from faceswitch.detectors.hog import HogDetector
faces = HogDetector().detect(image)

YOLO — pip install "faceswitch[yolo]"

Uses Ultralytics YOLOv8. Deep learning-based, excellent accuracy on varied poses and lighting. Downloads a model on first use (~6MB).

from faceswitch.detectors.yolo import YoloDetector
faces = YoloDetector().detect(image)

RetinaFace — pip install "faceswitch[retinaface]"

Uses the serengil/retinaface ResNet+FPN model. Best accuracy on small faces, side profiles, and crowded images. Downloads model weights on first use (~120MB).

from faceswitch.detectors.retinaface import RetinaFaceDetector
faces = RetinaFaceDetector().detect(image)

Common questions

Do I need a GPU? No. All detectors run on CPU. YOLO and RetinaFace are faster with a GPU but don't require one.

I get "ImportError: ... install faceswitch[hog]" You installed the base library but not the detector dependency. Run the install command from the table above.

The detector downloads a model on first use — is that normal? Yes. YOLO (~6MB) and RetinaFace (~120MB) download their model weights automatically the first time you use them. After that, they're cached locally.

Can I use my own image loading library instead of OpenCV? Yes, as long as the image is a NumPy array with shape (height, width, 3) and uint8 dtype (standard BGR or RGB image). cv2.imread() gives you this automatically.


More detectors coming

FaceSwitch is actively maintained. New detectors are added regularly — run pip install --upgrade faceswitch to get the latest.

Current version: 0.2.2 | GitHub | PyPI

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

faceswitch-0.2.2.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

faceswitch-0.2.2-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file faceswitch-0.2.2.tar.gz.

File metadata

  • Download URL: faceswitch-0.2.2.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for faceswitch-0.2.2.tar.gz
Algorithm Hash digest
SHA256 3816a77b9ad75a8d7b70c143c2c663af1deda22b15922cda020a971aa1199577
MD5 e6b9a1ae424e09eb80dfcdf7ff7bb3e5
BLAKE2b-256 0e875b0f8278402c4f920136132dc7de9c6ae91293971835f42a5228353ed2d9

See more details on using hashes here.

File details

Details for the file faceswitch-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: faceswitch-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for faceswitch-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4af1fa371c29124454e5b80c4359640b10aa97f6839ec91b594902d6f68ce9e8
MD5 b4416f09af54449b25340ab59607f3eb
BLAKE2b-256 a629141b4721c0fe4c257a6773c26c324520e1258ee807dd89fdcb492f6409cb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page