【環境】
windows8.1
python2.7
opencv3
【フォルダ構成】
horikita_hasimoto
|---joyu(ネットから取ってきた画像フォルダ)
| |---hasimoto.jpg
| |---horikita.jpg
|---joyu_face(プログラムで抜き出す顔画像フォルダ)
|---haarcascade_frontalface_alt.xml(カスケードファイル)
|---tashite_nidewaru.py(たして2で割るプログラム)
|---kao_toridasi.py(顔を抜き出すプログラム)
【準備】
ネットから堀北真希と橋本環奈の画像を取ってきます。
私は次の画像を使いました。
【プログラム実行方法】
kao_toridasi.py
# -*- coding: utf-8 -*-
import cv2
import os
import glob
# プログラムのあるフォルダパスの代入
current_dir = os.getcwd()
# joyuフォルダから全ての画像をリスト化
joyu_list = glob.glob(current_dir + "\\joyu\\*")
# カスケードファイル
cascade_path = "haarcascade_frontalface_alt.xml"
for joyu in joyu_list:
#ファイル読み込み
image = cv2.imread(joyu)
if(image is None):
pass
continue
#グレースケール変換
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#カスケード分類器の特徴量を取得する
cascade = cv2.CascadeClassifier(cascade_path)
#顔認識の実行
facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.2, minNeighbors=2, minSize=(10, 10))
for rect in facerect:
#顔だけ切り出して保存
x = rect[0]
y = rect[1]
width = rect[2]
height = rect[3]
dst = image[y:y+height, x:x+width]
resize_image = cv2.resize(dst,(256,256))
new_image_path = current_dir + '\\joyu_face\\' + str(i) + '.jpg'
cv2.imwrite(new_image_path, resize_image)
i += 1
上記プログラムを実行するとjoyu_faceに顔画像が保存されるので、
画像名をhorikita.jpg、hasimoto.jpgに変更してください。
tashite_nidewaru.py
# -*- coding: utf-8 -*-
import cv2
import os
current_dir = os.getcwd()
image_path = current_dir + "\\joyu_face\\"
# ファイル読み込み
horikita = cv2.imread(image_path + "\\horikita.jpg")
hasimoto = cv2.imread(image_path + "\\hasimoto.jpg")
# グレースケール変換
horikita_gray = cv2.cvtColor(horikita, cv2.COLOR_BGR2GRAY)
hasimoto_gray = cv2.cvtColor(hasimoto, cv2.COLOR_BGR2GRAY)
# デフォルトの状態ではかなり気色悪い画像になってしまったため、
# 色を4種類にしてみました
for i in xrange(len(horikita_gray)):
for j in xrange(len(horikita_gray[0])):
if horikita_gray[i][j] >= 192:
horikita_gray[i][j] = 192
elif horikita_gray[i][j] >= 128:
horikita_gray[i][j] = 128
elif horikita_gray[i][j] >= 64:
horikita_gray[i][j] = 64
else:
horikita_gray[i][j] = 0
for i in xrange(len(hasimoto_gray)):
for j in xrange(len(hasimoto_gray[0])):
if hasimoto_gray[i][j] >= 192:
hasimoto_gray[i][j] = 192
elif hasimoto_gray[i][j] >= 128:
hasimoto_gray[i][j] = 128
elif hasimoto_gray[i][j] >= 64:
hasimoto_gray[i][j] = 64
else:
hasimoto_gray[i][j] = 0
horikita_hasimoto = horikita_gray / 2 + hasimoto_gray / 2
for i in xrange(len(horikita_hasimoto)):
for j in xrange(len(horikita_hasimoto[0])):
if horikita_hasimoto[i][j] >= 192:
horikita_hasimoto[i][j] = 192
elif horikita_hasimoto[i][j] >= 128:
horikita_hasimoto[i][j] = 128
elif horikita_hasimoto[i][j] >= 64:
horikita_hasimoto[i][j] = 64
else:
horikita_hasimoto[i][j] = 0
# 画像保存
new_image_path = current_dir + '\\horikita_hasimoto.jpg'
cv2.imwrite(new_image_path, horikita_hasimoto)
new_image_path = current_dir + '\\horikita.jpg'
cv2.imwrite(new_image_path, horikita_gray)
new_image_path = current_dir + '\\hasimoto.jpg'
cv2.imwrite(new_image_path, hasimoto_gray)
【結果】
【感想】
一番最初にカラーで試しましたが、気色の悪いものになりました。
しかし、白黒でも美人とは思えないですね・・・(;一_一)
改善点は多々ありそうです。