Commit 718bb603 authored by YvanM's avatar YvanM

- avoid failure when face detection does not work on a picture and "-names" is

  used
- rotate picture before face detection
parent f270dcf8
...@@ -52,11 +52,11 @@ parser = argparse.ArgumentParser( ...@@ -52,11 +52,11 @@ parser = argparse.ArgumentParser(
"- CSV must not contain empty line\n" "- CSV must not contain empty line\n"
"- lines must be in the form \"surname,name\" or\n" "- lines must be in the form \"surname,name\" or\n"
" \"surname,name,NOPIC\" if you do not have this person's picture\n" " \"surname,name,NOPIC\" if you do not have this person's picture\n"
"- lines order must match pictures order\n" "- lines order must match pictures alphanumerical order\n"
"If you provide people names from pictures name, those should be \n" "If you provide people names from pictures name, those should be \n"
"in the form \"surname_name.jpg\".\n" "in the form \"surname_name.jpg\".\n"
"\n" "\n"
"ImageMagick and Ghostscript tools must be in your PATH.") "ImageMagick and Ghostscript tools must be in your $PATH.")
parser.add_argument("directory", parser.add_argument("directory",
help="DIRECTORY that contains pictures, one for each person") help="DIRECTORY that contains pictures, one for each person")
parser.add_argument("-n", "--names", parser.add_argument("-n", "--names",
...@@ -151,21 +151,25 @@ if args.crop: ...@@ -151,21 +151,25 @@ if args.crop:
# get face position # get face position
f = open(pic, 'rb') f = open(pic, 'rb')
img = Image.open(f) img = Image.open(f)
face = img.detect_faces() # face detection does not work when picture is rotated
face = img.auto_orient().detect_faces()
if len(face) == 0: if len(face) == 0:
eprint("Warning: " + pic + ": no face has been detected") eprint("Warning: " + pic + ": no face has been detected, using "
"original picture")
with open(os.path.join(directory, os.path.basename(pic)), 'wb') as out:
img.save_as_jpeg(out)
continue continue
elif len(face) > 1: elif len(face) > 1:
eprint("Warning: " + pic + ": more than one face has been " eprint("Warning: " + pic + ": more than one face has been "
"detected, the first one will be used") "detected, the first one will be used")
# makes the square around face a bit bigger to ease recognition # makes the square around face a bit bigger to ease recognition
face = enlargeFace(img.get_size(), face[0]) face = enlargeFace(img.auto_orient().get_size(), face[0])
# crop picture # crop picture
img = img.crop((face)) img = img.auto_orient().crop((face))
# enventually replace alpha channel by white color (avoids crash) # enventually replace alpha channel by white color (avoids crash)
img = img.set_background_color_rgb((255,255,255)) img = img.set_background_color_rgb((255,255,255))
# save a copy # save a copy
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment