In [11]:
from py_flandmark import PyFlandmark
from py_featurePool import PyFeaturePool

# flandmark = PyFlandmark("../../../data/flandmark_model.xml", False)
# flandmark = PyFlandmark("../../../data/FRONTAL_21L.xml", False)
flandmark = PyFlandmark("../../../data/300W/FDPM.xml", False)

bw = flandmark.getBaseWindowSize()
featurePool = PyFeaturePool(bw[0], bw[1], None)
featurePool.addFeatuaddSparseLBPfeatures()

flandmark.setFeaturePool(featurePool)

In [12]:
import time
import numpy as np
import os
from fnmatch import fnmatch
from PIL import Image
import matplotlib.pyplot as plt
%matplotlib inline

def rgb2gray(rgb):
	"""
	converts rgb array to grey scale variant
	accordingly to fomula taken from wiki
	(this function is missing in python)
	"""	
	return np.dot(rgb[...,:3], [0.299, 0.587, 0.144])

def read_bbox_from_txt(file_name):
	"""
		returns 2x2 matrix coordinates of 
		left upper and right lower corners
		of rectangle that contains face stored
		in columns of matrix
	"""
	f = open(file_name)
	str = f.read().replace(',', ' ')		
	f.close()
	ret = np.array(map(int,str.split()) ,dtype=np.int32)	
	ret = ret.reshape((2,2), order='F')	
	return ret

In [13]:
DIR = '../../../data/Images/'
JPGS = [f for f in os.listdir(DIR) if fnmatch(f, '*.jpg')]

jpg_name = JPGS[1]
file_name = jpg_name[:-4]
img = Image.open(DIR + jpg_name)	 		
arr = rgb2gray(np.asarray(img))

In [14]:
bbox2 = read_bbox_from_txt(DIR + jpg_name[:-4] + '.det')
start_time = time.time()
P = flandmark.detect_optimized(arr, bbox2)
print "Elapsed time: %s ms" % ((time.time() - start_time) * 1000)

plt.imshow(img)
plt.plot(P[0,:], P[1,:], 'rx')


Elapsed time: 81.3059806824 ms
Out[14]:
[<matplotlib.lines.Line2D at 0x7f7e3b89df10>]

In [15]:
features = featurePool.getFeaturesRaw(0)
plt.imshow(features)


Out[15]:
<matplotlib.image.AxesImage at 0x7f7e42f53250>

In [16]:
import cv2

# plt.imshow(arr.astype(np.uint8))
faceCascade = cv2.CascadeClassifier('../../../data/haarcascade_frontalface_alt.xml')
faces = faceCascade.detectMultiScale(arr.astype(np.uint8), 
                                     scaleFactor=1.1, 
                                     minNeighbors=5, 
                                     minSize=(30, 30), 
                                     flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
bbox = np.array((faces[0,0], faces[0,1], faces[0,0]+faces[0,2], faces[0,1]+faces[0,3]), dtype=np.int32)
bbox = bbox.reshape((2, 2), order='F')
bbox


Out[16]:
array([[ 71, 175],
       [ 73, 177]], dtype=int32)

In [17]:
start_time = time.time()
P = flandmark.detect_optimized(arr, bbox)
print "Elapsed time: %s ms" % ((time.time() - start_time) * 1000)

plt.imshow(img)
plt.plot(P[0,:], P[1,:], 'rx')


Elapsed time: 82.5798511505 ms
Out[17]:
[<matplotlib.lines.Line2D at 0x7f7e42f5e250>]

In [18]:
features = featurePool.getFeaturesRaw(0)
plt.imshow(features)


Out[18]:
<matplotlib.image.AxesImage at 0x7f7e42dc42d0>