Last active
January 1, 2017 07:19
-
-
Save h1dia/59340904433744efbb401c056afc8627 to your computer and use it in GitHub Desktop.
顔画像切り出しスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import sys | |
import os | |
import os.path | |
def detect(filename, cascade_file = "../lbpcascade_animeface.xml"): | |
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.05, | |
minNeighbors = 5, | |
minSize = (32, 32)) | |
for (x, y, w, h) in faces: | |
cv2.imwrite("./out/" + filename, image[y:y+h, x:x+w]) | |
# main | |
files = os.listdir("./") | |
for filepath in sorted(files): | |
if filepath.endswith(".png"): | |
print(filepath) | |
detect(filepath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment