diff options
Diffstat (limited to 'scripts/faces.py')
| -rw-r--r-- | scripts/faces.py | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/scripts/faces.py b/scripts/faces.py new file mode 100644 index 0000000..d072376 --- /dev/null +++ b/scripts/faces.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +""" +Detect faces in an image and return their bounding boxes and 128-D encodings. + +Usage: python3 faces.py <image_path> + +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() |
