Skip to content

Webcam Viewer

Runs any detector on your webcam and shows how many things it found. This is the quickest way to try each detector.

Run it

python examples/webcam.py hand
python examples/webcam.py pose
python examples/webcam.py face
python examples/webcam.py face_mesh --camera 1

Code

examples/webcam.py
"""Run any detector on your webcam.

    python examples/webcam.py hand
    python examples/webcam.py pose --camera 1

Press q to quit.
"""

import argparse

import cv2 as cv

from detectors_world import create_detector

parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("detector", choices=["hand", "pose", "face", "face_mesh"])
parser.add_argument("--camera", type=int, default=0, help="camera index (default: 0)")
args = parser.parse_args()

cap = cv.VideoCapture(args.camera)
with create_detector(args.detector) as detector:
    while cap.isOpened():
        ok, frame = cap.read()
        if not ok:
            break
        frame = cv.flip(frame, 1)
        found = detector.detect(frame)
        cv.putText(frame, f"{args.detector}: {len(found)}", (10, 30), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        cv.imshow("Detectors World", frame)
        if cv.waitKey(1) & 0xFF == ord("q"):
            break
cap.release()
cv.destroyAllWindows()

How it works

  • create_detector(args.detector) lets the same loop work with every detector.
  • cv.flip(frame, 1) makes the image behave like a mirror. If you use hand labels, read Left and right.
  • len(found) works for all detectors, because detect() always returns a list.