今回は前回行った実験「nupicのSpatial PoolerとopenCVを用いて、声優の愛美と山崎はるかの顔照合してみた!」の最中に気づいたことを共有したいと思います。
前回の実験では顔照合をするために1ピクセル256段階のデータを128以下は0、128以上は1となるようにエンコードを行なった。つまり、画像データを0,1のみで表したことになります。この0,1のみのデータを可視化してみると、意外と顔の形がわかって面白かったので、見て下さい!
目的
画像を0,1に変換して3Dプロットで可視化する!
実験方法
使用するもの
・matplotlib
・画像
実験の流れ
・画像を50*50にリサイズ
・グレースケール変換
・0,1にエンコード
・matplotで3D描画
ではLet's 実験!
import numpy as np
import sys
import cv2
from PIL import Image
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from nupic.encoders.category import CategoryEncoder
from nupic.algorithms.spatial_pooler import SpatialPooler
def resize(filename, w, h):
imageName = filename
img = Image.open(imageName)
img_resize_lanczos = img.resize((w, h), Image.LANCZOS)
savepath = 're'+filename
img_resize_lanczos.save(savepath)
img_resize = cv2.imread(savepath)
return img_resize
def encodeImage(img):
for i, y in enumerate(img):
for j, x in enumerate(y):
# print x
if x >= 128:
img[i][j] = 1
else:
img[i][j] = 0
return img
def plot3D(data):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x2 = []
y2 = []
z2 = []
x3= []
y3 = []
z3 = []
print "------------------"
print data
for i in range(data.shape[1]):
for j in range(data.shape[0]):
if data[i][j] == 1:
x2.append(j+1)
y2.append(i+1)
z2.append(5)
else:
x3.append(j+1)
y3.append(i+1)
z3.append(5)
ax.scatter(x3, y3, z3)
ax.scatter(x2, y2, z2, c="red")
plt.show()
imagePath = 'aimi.jpg'
imagePath2 = 'pyon.jpg'
imagePath3 = 'aimi2.jpg'
imagePath4 = 'aimi3.jpg'
img_resize = resize(imagePath, 50, 50)
img_resize2 = resize(imagePath2, 50, 50)
img_resize3 = resize(imagePath3, 50, 50)
img_resize4 = resize(imagePath4, 50, 50)
# グレースケール化
img_gray = cv2.cvtColor(img_resize, cv2.COLOR_BGR2GRAY)
img_gray2 = cv2.cvtColor(img_resize2, cv2.COLOR_BGR2GRAY)
img_gray3 = cv2.cvtColor(img_resize3, cv2.COLOR_BGR2GRAY)
img_gray4 = cv2.cvtColor(img_resize4, cv2.COLOR_BGR2GRAY)
encodedList = encodeImage(img_gray)
encodedList2 = encodeImage(img_gray2)
encodedList3 = encodeImage(img_gray3)
encodedList4 = encodeImage(img_gray4)
print encodedList.shape
plot3D(encodedList)
plot3D(encodedList2)
sys.exit()
出力結果 3D描画
愛美さん
・表側
・裏側
山崎はるかさん
どうですか画像データを0,1にしても意外とあいみんとぴょん吉に見えませんか?
この結果からは顔認識はすごく少ないデータでも可能なのではないかということがわかります。少ないデータ量になれば、時間がかかる機械学習に入力するときでも全体の時間を削減することができます!
これからも勉強していきたいと思います。
ありがとうございました!