Skip to content

Instantly share code, notes, and snippets.

@ckhung
Created August 15, 2017 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckhung/eb59f9de6cc60fbf746f0c5c6ac80f41 to your computer and use it in GitHub Desktop.
Save ckhung/eb59f9de6cc60fbf746f0c5c6ac80f41 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# wrapper for https://github.com/ageitgey/face_recognition
from __future__ import print_function
import face_recognition
import argparse, ntpath, sys, re, json, csv
parser = argparse.ArgumentParser(
description='locate faces in pictures',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('SUBCMD', help='valid subcommands: range, features, encoding')
parser.add_argument('image_files', nargs='*')
args = parser.parse_args()
for fn in args.image_files:
pic = face_recognition.load_image_file(fn)
if args.SUBCMD == 'range':
res = face_recognition.face_locations(pic)
print(fn, end='')
for c in res:
# if c[1] > c[3]:
# print("ouch! {} > {}", c[1], c[3])
# if c[0] > c[2]:
# print("ouch! {} > {}", c[0], c[2])
left = c[1] if c[1] < c[3] else c[3]
width = c[1] + c[3] - left*2
top = c[0] if c[0] < c[2] else c[2]
height = c[0] + c[2] - top*2
print(' {0:d} {1:d} {2:d} {3:d}'.format(left, top, width, height), end='')
print('')
elif args.SUBCMD == 'features':
res = face_recognition.face_landmarks(pic)
for d in res:
d['name'] = fn
print(json.dumps(res))
elif args.SUBCMD == 'encoding':
res = face_recognition.face_encodings(pic)
for d in res:
d = d.tolist()
d.insert(0, fn)
print(','.join(map(str, d)))
else:
sys.exit('unknown SUBCMD "{}"'.format(args.SUBCMD))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment