How can I use PIL to crop a select area based on face detection?
-
30-09-2019 - |
Pergunta
Hi I am wanting to use the python imaging library to crop images to a specific size for a website. I have a problem, these images are meant to show people's faces so I need to automatically crop based on them.
I know face detection is a difficult concept so I'm thinking of using the face.com API http://developers.face.com/tools/#faces/detect which is fine for what I want to do.
I'm just a little stuck on how I would use this data to crop a select area based on the majority of faces.
Can anybody help?
Joe
Solução
There is a library for python that have a concept of smart-cropping that among other options, can use face detection to do a smarter cropping. It uses opencv under the hood, but you are isolated from it. https://github.com/globocom/thumbor
Outras dicas
If you have some rectangle that you want to excise from an image, here's what I might try first:
- (optional) If the image is large, do a rough square crop centered on the face with dimensions
sqrt(2)
larger than the longer edge (if rectangular). Worst-case (45° rotation), it will still grab everything important. - Rotate based on the face orientation (something like
rough_crop.rotate(math.degrees(math.atan(ydiff/xdiff))
, trig is fun) - Do a final crop. If you did the initial crop, the face should be centered, otherwise you'll have to transform (rotate) all your old coordinates to the new image (more trig!).