Permalink
Browse files

Added a Live Detection App to showcase the usage of the classes. Plea…

…se see apps/videofacerec/app_screenshot.jpg for a screenshot (using my non-published celebrity database as training).
  • Loading branch information...
1 parent a16cc11 commit 5d67759d24be2be32afc5095e7c68552dc49cb70 @bytefish committed Feb 8, 2012
@@ -0,0 +1,54 @@
+import sys
+# append facerec to module search path
+sys.path.append("..")
+import cv2
+from facedet.detector import SkinFaceDetector
+import numpy as np
+import os
+
+
+def extract_faces(src_dir, dst_dir, detector, face_sz = (130,130)):
+ """
+ Extracts the faces from all images in a given src_dir and writes the extracted faces
+ to dst_dir. Needs a facedet.Detector object to perform the actual detection.
+
+ Args:
+ src_dir [string]
+ dst_dir [string]
+ detector [facedet.Detector]
+ face_sz [tuple]
+ """
+ if not os.path.exists(dst_dir):
+ try:
+ os.mkdir(dst_dir)
+ except:
+ raise OSError("Can't create destination directory (%s)!" % (dst_dir))
+ for dirname, dirnames, filenames in os.walk(src_dir):
+ for subdir in dirnames:
+ src_subdir = os.path.join(dirname, subdir)
+ dst_subdir = os.path.join(dst_dir,subdir)
+ if not os.path.exists(dst_subdir):
+ try:
+ os.mkdir(dst_subdir)
+ except:
+ raise OSError("Can't create destination directory (%s)!" % (dst_dir))
+ for filename in os.listdir(src_subdir):
+ name, ext = os.path.splitext(filename)
+ src_fn = os.path.join(src_subdir,filename)
+ img = cv2.imread(src_fn)
+ rects = detector.detect(img)
+ for i,rect in enumerate(rects):
+ x0,y0,x1,y1 = rect
+ face = img[y0:y1,x0:x1]
+ face = cv2.resize(face, face_sz, interpolation = cv2.INTER_CUBIC)
+ print os.path.join(dst_subdir, "%s_%s_%d%s" % (subdir, name,i,ext))
+ cv2.imwrite(os.path.join(dst_subdir, "%s_%s_%d%s" % (subdir, name,i,ext)), face)
+
+if __name__ == "__main__":
+ if len(sys.argv) < 3:
+ print "usage: python extract_faces.py <src_dir> <dst_dir>"
+ sys.exit()
+ src_dir = sys.argv[1]
+ dst_dir = sys.argv[2]
+ detector = SkinFaceDetector(threshold=0.3, cascade_fn="/home/philipp/projects/opencv2/OpenCV-2.3.1/data/haarcascades/haarcascade_frontalface_alt2.xml")
+ extract_faces(src_dir=src_dir, dst_dir=dst_dir, detector=detector)
@@ -0,0 +1,46 @@
+from facerec.dataset import DataSet
+from facerec.feature import *
+from facerec.distance import *
+from facerec.classifier import NearestNeighbor, SVM
+from facerec.model import PredictableModel
+from facerec.validation import KFoldCrossValidation
+from facerec.visual import plot_eigenvectors
+from facerec.preprocessing import *
+from facerec.operators import ChainOperator
+from facerec.svm import grid_search
+from facerec.lbp import *
+
+import numpy as np
+import logging,sys
+# set up a handler for logging
+handler = logging.StreamHandler(sys.stdout)
+formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
+handler.setFormatter(formatter)
+# add handler to facerec modules
+logger = logging.getLogger("facerec")
+logger.addHandler(handler)
+logger.setLevel(logging.DEBUG)
+# load a dataset
+dataSet = DataSet("/home/philipp/facerec/data/c2", sz=(100,100))
+# define Fisherfaces as feature extraction method
+feature = Identity()
+#from svmutil import *
+# define a SVM with RBF Kernel as classifier
+#param = svm_parameter("-q")
+#param.kernel_type = RBF
+#classifier = SVM(param=param)
+# define a 1-NN classifier with Euclidean Distance
+classifier = NearestNeighbor(dist_metric=EuclideanDistance(), k=5)
+# define the model as the combination
+model = PredictableModel(feature=feature, classifier=classifier)
+
+#best_parameter, results = grid_search(model, dataSet.data, dataSet.labels, k=5)
+#print results
+# show fisherfaces
+#model.compute(dataSet.data, dataSet.labels)
+#plot_eigenvectors(model.feature.eigenvectors, 1, sz=dataSet.data[0].shape, filename="/home/philipp/01.png")
+#plot_eigenvectors(model.feature.model2.eigenvectors, 1, sz=(dataSet.data[0].shape[0]-2,dataSet.data[0].shape[1]-2), filename="/home/philipp/01.png")
+# perform a 5-fold cross validation
+cv = KFoldCrossValidation(model, k=10)
+cv.validate(dataSet.data, dataSet.labels)
+print cv
@@ -0,0 +1,117 @@
+import numpy as np
+import cv2
+import os
+
+image_extensions = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.pbm', '.pgm', '.ppm']
+
+def splitfn(fn):
+ path, fn = os.path.split(fn)
+ name, ext = os.path.splitext(fn)
+ return path, name, ext
+
+def anorm2(a):
+ return (a*a).sum(-1)
+def anorm(a):
+ return np.sqrt( anorm2(a) )
+
+def homotrans(H, x, y):
+ xs = H[0, 0]*x + H[0, 1]*y + H[0, 2]
+ ys = H[1, 0]*x + H[1, 1]*y + H[1, 2]
+ s = H[2, 0]*x + H[2, 1]*y + H[2, 2]
+ return xs/s, ys/s
+
+def to_rect(a):
+ a = np.ravel(a)
+ if len(a) == 2:
+ a = (0, 0, a[0], a[1])
+ return np.array(a, np.float64).reshape(2, 2)
+
+def rect2rect_mtx(src, dst):
+ src, dst = to_rect(src), to_rect(dst)
+ cx, cy = (dst[1] - dst[0]) / (src[1] - src[0])
+ tx, ty = dst[0] - src[0] * (cx, cy)
+ M = np.float64([[ cx, 0, tx],
+ [ 0, cy, ty],
+ [ 0, 0, 1]])
+ return M
+
+
+def lookat(eye, target, up = (0, 0, 1)):
+ fwd = np.asarray(target, np.float64) - eye
+ fwd /= anorm(fwd)
+ right = np.cross(fwd, up)
+ right /= anorm(right)
+ down = np.cross(fwd, right)
+ R = np.float64([right, down, fwd])
+ tvec = -np.dot(R, eye)
+ return R, tvec
+
+def mtx2rvec(R):
+ w, u, vt = cv2.SVDecomp(R - np.eye(3))
+ p = vt[0] + u[:,0]*w[0] # same as np.dot(R, vt[0])
+ c = np.dot(vt[0], p)
+ s = np.dot(vt[1], p)
+ axis = np.cross(vt[0], vt[1])
+ return axis * np.arctan2(s, c)
+
+def draw_str(dst, (x, y), s):
+ cv2.putText(dst, s, (x+1, y+1), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 0), thickness = 2, linetype=cv2.CV_AA)
+ cv2.putText(dst, s, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.0, (255, 255, 255), linetype=cv2.CV_AA)
+
+class Sketcher:
+ def __init__(self, windowname, dests, colors_func):
+ self.prev_pt = None
+ self.windowname = windowname
+ self.dests = dests
+ self.colors_func = colors_func
+ self.dirty = False
+ self.show()
+ cv2.setMouseCallback(self.windowname, self.on_mouse)
+
+ def show(self):
+ cv2.imshow(self.windowname, self.dests[0])
+
+ def on_mouse(self, event, x, y, flags, param):
+ pt = (x, y)
+ if event == cv2.EVENT_LBUTTONDOWN:
+ self.prev_pt = pt
+ if self.prev_pt and flags & cv2.EVENT_FLAG_LBUTTON:
+ for dst, color in zip(self.dests, self.colors_func()):
+ cv2.line(dst, self.prev_pt, pt, color, 5)
+ self.dirty = True
+ self.prev_pt = pt
+ self.show()
+ else:
+ self.prev_pt = None
+
+
+# palette data from matplotlib/_cm.py
+_jet_data = {'red': ((0., 0, 0), (0.35, 0, 0), (0.66, 1, 1), (0.89,1, 1),
+ (1, 0.5, 0.5)),
+ 'green': ((0., 0, 0), (0.125,0, 0), (0.375,1, 1), (0.64,1, 1),
+ (0.91,0,0), (1, 0, 0)),
+ 'blue': ((0., 0.5, 0.5), (0.11, 1, 1), (0.34, 1, 1), (0.65,0, 0),
+ (1, 0, 0))}
+
+cmap_data = { 'jet' : _jet_data }
+
+def make_cmap(name, n=256):
+ data = cmap_data[name]
+ xs = np.linspace(0.0, 1.0, n)
+ channels = []
+ eps = 1e-6
+ for ch_name in ['blue', 'green', 'red']:
+ ch_data = data[ch_name]
+ xp, yp = [], []
+ for x, y1, y2 in ch_data:
+ xp += [x, x+eps]
+ yp += [y1, y2]
+ ch = np.interp(xs, xp, yp)
+ channels.append(ch)
+ return np.uint8(np.array(channels).T*255)
+
+def nothing(*arg, **kw):
+ pass
+
+def clock():
+ return cv2.getTickCount() / cv2.getTickFrequency()
@@ -0,0 +1,150 @@
+import numpy as np
+import cv2
+from time import clock
+from numpy import pi, sin, cos
+import common
+
+class VideoSynthBase(object):
+ def __init__(self, size=None, noise=0.0, bg = None, **params):
+ self.bg = None
+ self.frame_size = (640, 480)
+ if bg is not None:
+ self.bg = cv2.imread(bg, 1)
+ h, w = self.bg.shape[:2]
+ self.frame_size = (w, h)
+
+ if size is not None:
+ w, h = map(int, size.split('x'))
+ self.frame_size = (w, h)
+ self.bg = cv2.resize(self.bg, self.frame_size)
+
+ self.noise = float(noise)
+
+ def render(self, dst):
+ pass
+
+ def read(self, dst=None):
+ w, h = self.frame_size
+
+ if self.bg is None:
+ buf = np.zeros((h, w, 3), np.uint8)
+ else:
+ buf = self.bg.copy()
+
+ self.render(buf)
+
+ if self.noise > 0.0:
+ noise = np.zeros((h, w, 3), np.int8)
+ cv2.randn(noise, np.zeros(3), np.ones(3)*255*self.noise)
+ buf = cv2.add(buf, noise, dtype=cv2.CV_8UC3)
+ return True, buf
+
+class Chess(VideoSynthBase):
+ def __init__(self, **kw):
+ super(Chess, self).__init__(**kw)
+
+ w, h = self.frame_size
+
+ self.grid_size = sx, sy = 10, 7
+ white_quads = []
+ black_quads = []
+ for i, j in np.ndindex(sy, sx):
+ q = [[j, i, 0], [j+1, i, 0], [j+1, i+1, 0], [j, i+1, 0]]
+ [white_quads, black_quads][(i + j) % 2].append(q)
+ self.white_quads = np.float32(white_quads)
+ self.black_quads = np.float32(black_quads)
+
+ fx = 0.9
+ self.K = np.float64([[fx*w, 0, 0.5*(w-1)],
+ [0, fx*w, 0.5*(h-1)],
+ [0.0,0.0, 1.0]])
+
+ self.dist_coef = np.float64([-0.2, 0.1, 0, 0])
+ self.t = 0
+
+ def draw_quads(self, img, quads, color = (0, 255, 0)):
+ img_quads = cv2.projectPoints(quads.reshape(-1, 3), self.rvec, self.tvec, self.K, self.dist_coef) [0]
+ img_quads.shape = quads.shape[:2] + (2,)
+ for q in img_quads:
+ cv2.fillConvexPoly(img, np.int32(q*4), color, cv2.CV_AA, shift=2)
+
+ def render(self, dst):
+ t = self.t
+ self.t += 1.0/30.0
+
+ sx, sy = self.grid_size
+ center = np.array([0.5*sx, 0.5*sy, 0.0])
+ phi = pi/3 + sin(t*3)*pi/8
+ c, s = cos(phi), sin(phi)
+ ofs = np.array([sin(1.2*t), cos(1.8*t), 0]) * sx * 0.2
+ eye_pos = center + np.array([cos(t)*c, sin(t)*c, s]) * 15.0 + ofs
+ target_pos = center + ofs
+
+ R, self.tvec = common.lookat(eye_pos, target_pos)
+ self.rvec = common.mtx2rvec(R)
+
+ self.draw_quads(dst, self.white_quads, (245, 245, 245))
+ self.draw_quads(dst, self.black_quads, (10, 10, 10))
+
+
+
+classes = dict(chess=Chess)
+
+def create_capture(source):
+ '''
+ source: <int> or '<int>' or '<filename>' or 'synth:<params>'
+ '''
+ try: source = int(source)
+ except ValueError: pass
+ else:
+ return cv2.VideoCapture(source)
+ source = str(source).strip()
+ if source.startswith('synth'):
+ ss = filter(None, source.split(':'))
+ params = dict( s.split('=') for s in ss[1:] )
+ try: Class = classes[params['class']]
+ except: Class = VideoSynthBase
+
+ return Class(**params)
+ return cv2.VideoCapture(source)
+
+
+presets = dict(
+ empty = 'synth:',
+ lena = 'synth:bg=../cpp/lena.jpg:noise=0.1',
+ chess = 'synth:class=chess:bg=../cpp/lena.jpg:noise=0.1:size=640x480'
+)
+
+if __name__ == '__main__':
+ import sys
+ import getopt
+
+ print 'USAGE: video.py [--shotdir <dir>] [source0] [source1] ...'
+ print "source: '<int>' or '<filename>' or 'synth:<params>'"
+ print
+
+ args, sources = getopt.getopt(sys.argv[1:], '', 'shotdir=')
+ args = dict(args)
+ shotdir = args.get('--shotdir', '.')
+ if len(sources) == 0:
+ sources = [ presets['chess'] ]
+
+ print 'Press SPACE to save current frame'
+
+ caps = map(create_capture, sources)
+ shot_idx = 0
+ while True:
+ imgs = []
+ for i, cap in enumerate(caps):
+ ret, img = cap.read()
+ imgs.append(img)
+ cv2.imshow('capture %d' % i, img)
+ ch = cv2.waitKey(1)
+ if ch == 27:
+ break
+ if ch == ord(' '):
+ for i, img in enumerate(imgs):
+ fn = '%s/shot_%d_%03d.bmp' % (shotdir, i, shot_idx)
+ cv2.imwrite(fn, img)
+ print fn, 'saved'
+ shot_idx += 1
Oops, something went wrong.

0 comments on commit 5d67759

Please sign in to comment.