#!/usr/bin/env python3 """ Detect faces in an image and return their bounding boxes and 128-D encodings. Usage: python3 faces.py Stdout: JSON array — one object per face: [{"box": [top, right, bottom, left], "encoding": [128 floats]}, ...] Returns "[]" when no faces are found or the image cannot be opened. Errors are written to stderr; stdout is always valid JSON. """ import sys import json def main(): if len(sys.argv) < 2: print("[]") return path = sys.argv[1] try: import face_recognition except ImportError as e: print(f"face_recognition not available: {e}", file=sys.stderr) print("[]") return try: img = face_recognition.load_image_file(path) except Exception as e: print(f"Could not load {path}: {e}", file=sys.stderr) print("[]") return try: locations = face_recognition.face_locations(img, model="hog") encodings = face_recognition.face_encodings(img, locations) result = [ {"box": list(loc), "encoding": enc.tolist()} for loc, enc in zip(locations, encodings) ] print(json.dumps(result)) except Exception as e: print(f"Detection error for {path}: {e}", file=sys.stderr) print("[]") if __name__ == "__main__": main()