Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: UTF-8
- import cv2
- from PIL import Image
- import numpy as np
- import os
- # サンプル顔認識特徴量ファイル
- cascade_path = "./lib/lbpcascade_animeface.xml"
- def face_rect(file_name, image_path, new_dir_path):
- # これは、BGRの順になっている気がする
- color = (255, 255, 255) # 白0
- image = Image.open(image_path).convert("RGB")
- image = np.asarray(image)
- print(image_path)
- print(image.shape)
- # グレースケール変換
- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
- # 分類器を作る?みたいな作業
- cascade = cv2.CascadeClassifier(cascade_path)
- # 顔認識の実行
- facerect = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1))
- if len(facerect) > 0:
- # 検出した顔を囲む矩形の作成
- if not os.path.isdir(new_dir_path):
- os.mkdir(new_dir_path)
- for i, rect in enumerate(facerect):
- # cv2.rectangle(image, tuple(rect[0:2]), tuple(rect[0:2] + rect[2:4]), color, thickness=2)
- x = rect[0]
- y = rect[1]
- width = rect[2]
- height = rect[3]
- dst = image[y:y + height, x:x + width, ::-1]
- new_image_path = new_dir_path + "/" + file_name + str(i) + ".png"
- cv2.imwrite(new_image_path, dst)
- else:
- print("no face")
Add Comment
Please, Sign In to add comment