Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide demonstration script for producing images cropped to the face #1

Open
gwern opened this issue Feb 4, 2017 · 3 comments
Open

Comments

@gwern
Copy link

gwern commented Feb 4, 2017

I experimented with using facedetect's cropping script on anime images to try to crop faces into separate files (the goal here being to experiment with modeling faces using WGAN to see if it improves on IllustrationGAN), but it didn't work at all. I tried using this, and it works much better but doesn't provide a simple interface/program for cropping images down to the face. I modified the example file to do this, and it seems to work OK:

import cv2
import sys
import os.path

def detect(cascade_file, filename, outputname):
    if not os.path.isfile(cascade_file):
        raise RuntimeError("%s: not found" % cascade_file)

    cascade = cv2.CascadeClassifier(cascade_file)
    image = cv2.imread(filename)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.equalizeHist(gray)

    faces = cascade.detectMultiScale(gray,
                                     # detector options
                                     scaleFactor = 1.1,
                                     minNeighbors = 5,
                                     minSize = (50, 50))
    i=0
    for (x, y, w, h) in faces:
        cropped = image[y: y + h, x: x + w]
        cv2.imwrite(outputname+str(i)+".png", cropped)
        i=i+1

if len(sys.argv) != 4:
    sys.stderr.write("usage: detect.py <animeface.xml file>  <input> <output prefix>\n")
    sys.exit(-1)

detect(sys.argv[1], sys.argv[2], sys.argv[3])

Saved as cropy.py, this can then be run on a single file like

python ~/src/lbpcascade_animeface/examples/crop.py ~/src/lbpcascade_animeface/lbpcascade_animeface.xml foo.jpg cropped

or run in parallel on many files using parallel like this:

cropFaces() {
    name=$(basename "$@")
    python ~/src/lbpcascade_animeface/examples/crop.py ~/src/lbpcascade_animeface/lbpcascade_animeface.xml "$@" faces/danbooru/"$name"
    }
export -f cropFaces
find ./danbooru/ -type f -name "*.jpg" | parallel cropFaces

It would be great if you could provide something like this Python crop program in the repo.

@nagadomi
Copy link
Owner

@gwern
Copy link
Author

gwern commented Feb 18, 2017

I see, thanks. Perhaps you could update the README.md for this project with a note that it's obsoleted by animeface-2009 and that animeface-2009 also provides a script for extracting faces?

@nagadomi
Copy link
Owner

I've updated README.
If someone sends a pull request to add a cropping script like face_collector.rb I will merge it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants