Skip to content

Instantly share code, notes, and snippets.

@h1dia
Last active January 1, 2017 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h1dia/59340904433744efbb401c056afc8627 to your computer and use it in GitHub Desktop.
Save h1dia/59340904433744efbb401c056afc8627 to your computer and use it in GitHub Desktop.
顔画像切り出しスクリプト
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