Commit 7e2b58e4 authored by YvanM's avatar YvanM

- when using "--crop" and detecting more than one face, save all detected faces

  to the "cropped" subdirectory
parent 718bb603
......@@ -36,13 +36,19 @@ must install:
This program might work on other platform, but I did not care to make it
portable.
Issues
======
You can consider this program as a proof of concept, as it has many issues:
- Face detection works better if the face is in front of the camera and if the
person is over a blank background.
person is over a blank background. Even with good pictures, the program often
detects more than one face, or fails to detect one. In such a case:
- manually crop these failing pictures
- put them in the "cropped" subdirectory created on first run
- re-run the program without face detection using pictures from the
"cropped" directory
- Generating the PDF might seem to hang when there is more than 30 pictures: try
to let it run a long time, or split the task and join PDFs later (it may be
possible to fix this by working on a lower resolution, but I did not try).
......
......@@ -152,29 +152,35 @@ if args.crop:
f = open(pic, 'rb')
img = Image.open(f)
# face detection does not work when picture is rotated
face = img.auto_orient().detect_faces()
faces = img.auto_orient().detect_faces()
if len(face) == 0:
if len(faces) == 0:
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
elif len(face) > 1:
elif len(faces) > 1:
eprint("Warning: " + pic + ": more than one face has been "
"detected, the first one will be used")
# makes the square around face a bit bigger to ease recognition
face = enlargeFace(img.auto_orient().get_size(), face[0])
# crop picture
img = img.auto_orient().crop((face))
# enventually replace alpha channel by white color (avoids crash)
img = img.set_background_color_rgb((255,255,255))
# save a copy
with open(os.path.join(directory, os.path.basename(pic)), 'wb') as out:
img.save_as_jpeg(out)
"detected. The first one will be used, others are available "
"in \"cropped\" directory")
for face_number,face in enumerate(faces):
# makes the square around face a bit bigger to ease recognition
coordinates = enlargeFace(img.auto_orient().get_size(), face)
# crop picture
face_img = img.auto_orient().crop((coordinates))
# enventually replace alpha channel by white color (avoids crash)
face_img = face_img.set_background_color_rgb((255,255,255))
# save a copy
if face_number == 0:
with open(os.path.join(directory, os.path.basename(pic)), 'wb') as out:
face_img.save_as_jpeg(out)
else:
with open(os.path.join(directory, os.path.basename(pic) + "." + str(face_number), ), 'wb') as out:
face_img.save_as_jpeg(out)
......
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