LoginSignup
0
2

More than 3 years have passed since last update.

フォルダの中の複数の画像ファイルから顔の部分を切り取る。

Posted at

前回記事では単一の画像で顔の切り出しを行なったが、より使いやすくするためにフォルダ内の複数画像で顔の切り出しを行う場合でも使えるようにする

参考サイト1
参考サイト2
参考サイト3

multiface_cut.pyとinput(処理したい画像が入ったフォルダ)とoutputs(処理後の画像を入れたいフォルダ)を同じ階層に配置してある。

 複数の画像が入ったフォルダから画像を取り出して処理し、特定のフォルダに保存するコード

multiface_cut.py
import cv2
import glob


HAAR_FILE = "./models/haarcascade_frontalface_default.xml"
cascade = cv2.CascadeClassifier(HAAR_FILE)


files = glob.glob(os.path.join("./input/*.jpg"))
files = [s.replace('./input/', '') for s in files]
for file_name in files:
    img = cv2.imread('input/'+file_name)
    img_g = cv2.imread('input/'+file_name,0)
    face = cascade.detectMultiScale(img_g)
    for x,y,w,h in face:
        face_cut = img[y:y + h, x:x + w]
        cv2.imwrite("./outputs/"+file_name, face_cut)
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2