Guest User

Untitled

a guest
Jun 23rd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. # coding: UTF-8
  2. import cv2
  3. from PIL import Image
  4. import numpy as np
  5. import os
  6.  
  7. # サンプル顔認識特徴量ファイル
  8. cascade_path = "./lib/lbpcascade_animeface.xml"
  9.  
  10.  
  11.  
  12.  
  13. def face_rect(file_name, image_path, new_dir_path):
  14. # これは、BGRの順になっている気がする
  15. color = (255, 255, 255) # 白0
  16.  
  17. image = Image.open(image_path).convert("RGB")
  18. image = np.asarray(image)
  19.  
  20. print(image_path)
  21. print(image.shape)
  22.  
  23. # グレースケール変換
  24. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  25.  
  26. # 分類器を作る?みたいな作業
  27. cascade = cv2.CascadeClassifier(cascade_path)
  28.  
  29. # 顔認識の実行
  30. facerect = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1))
  31.  
  32. if len(facerect) > 0:
  33. # 検出した顔を囲む矩形の作成
  34. if not os.path.isdir(new_dir_path):
  35. os.mkdir(new_dir_path)
  36.  
  37. for i, rect in enumerate(facerect):
  38. # cv2.rectangle(image, tuple(rect[0:2]), tuple(rect[0:2] + rect[2:4]), color, thickness=2)
  39. x = rect[0]
  40. y = rect[1]
  41. width = rect[2]
  42. height = rect[3]
  43. dst = image[y:y + height, x:x + width, ::-1]
  44. new_image_path = new_dir_path + "/" + file_name + str(i) + ".png"
  45. cv2.imwrite(new_image_path, dst)
  46.  
  47. else:
  48. print("no face")
Add Comment
Please, Sign In to add comment