def reduce(): from config import FEATURE_FILE_TRAINING from config import FEATURE_FILE_SUBSET from config import PROCESS_NUM mats = [] tot_samples = 0 for i in xrange(PROCESS_NUM): sub_mat = numpy.load(FEATURE_FILE_SUBSET + str(i) + ".cache" + ".npy") mats.append(sub_mat) tot_samples += sub_mat.shape[1] haar = Feature(TRAINING_IMG_WIDTH, TRAINING_IMG_HEIGHT) mat = numpy.zeros((haar.featuresNum, tot_samples), numpy.float32) sample_readed = 0 for i in xrange(PROCESS_NUM): for m in xrange(mats[i].shape[0]): # feature number for n in xrange(mats[i].shape[1]): # sample number mat[m][n + sample_readed] = mats[i][m][n] sample_readed += mats[i].shape[1] numpy.save(FEATURE_FILE_TRAINING, mat) return mat
def __init__(self, face_dir="", nonface_dir="", train=True, limit=30): #tot_samples = 0 self.Face = ImageSet(face_dir, sampleNum=POSITIVE_SAMPLE) self.nonFace = ImageSet(nonface_dir, sampleNum=NEGATIVE_SAMPLE) tot_samples = self.Face.sampleNum + self.nonFace.sampleNum self.classifier = AdaBoost self.haar = Feature(TRAINING_IMG_WIDTH, TRAINING_IMG_HEIGHT) if os.path.isfile(FEATURE_FILE_TRAINING + ".npy"): self._mat = numpy.load(FEATURE_FILE_TRAINING + ".npy") else: if DEBUG_MODEL is True: self._mat = numpy.zeros((self.haar.featuresNum, tot_samples)) for i in xrange(self.Face.sampleNum): print(i) featureVec = self.haar.calFeatureForImg( self.Face.images[i]) for j in xrange(self.haar.featuresNum): self._mat[j][i] = featureVec[j] for i in xrange(self.nonFace.sampleNum): featureVec = self.haar.calFeatureForImg( self.nonFace.images[i]) print(i) for j in xrange(self.haar.featuresNum): self._mat[j][i + self.Face.sampleNum] = featureVec[j] numpy.save(FEATURE_FILE_TRAINING, self._mat) else: from mapReduce import map from mapReduce import reduce map(self.Face, self.nonFace) self._mat = reduce() featureNum, sampleNum = self._mat.shape assert sampleNum == (POSITIVE_SAMPLE + NEGATIVE_SAMPLE) assert featureNum == FEATURE_NUM Label_Face = [+1 for i in xrange(POSITIVE_SAMPLE)] Label_NonFace = [-1 for i in xrange(NEGATIVE_SAMPLE)] self._label = numpy.array(Label_Face + Label_NonFace) self.limit = limit self.classifierNum = 0 self.strong_classifier = [None for i in xrange(limit)]
def routine(images, filename): tot_samples = len(images) haar = Feature(TRAINING_IMG_WIDTH, TRAINING_IMG_HEIGHT) mat = numpy.zeros((haar.featuresNum, tot_samples), dtype=numpy.float32) for i in xrange(tot_samples): featureVec = haar.calFeatureForImg(images[i]) for j in xrange(haar.featuresNum): mat[j][i] = featureVec[j] numpy.save(filename, mat)
def makeClassifierPic(self): from config import TRAINING_IMG_HEIGHT from config import TRAINING_IMG_WIDTH from config import WHITE from config import BLACK from config import FIGURES from config import HAAR_FEATURE_TYPE_I from config import HAAR_FEATURE_TYPE_II from config import HAAR_FEATURE_TYPE_III from config import HAAR_FEATURE_TYPE_IV from config import HAAR_FEATURE_TYPE_V IMG_WIDTH = TRAINING_IMG_WIDTH IMG_HEIGHT = TRAINING_IMG_HEIGHT haar = Feature(IMG_WIDTH, IMG_HEIGHT) featuresAll = haar.features selFeatures = [] # selected features for n in range(self.N): selFeatures.append(featuresAll[self.G[n].opt_dimension]) classifierPic = numpy.zeros((IMG_HEIGHT, IMG_WIDTH)) for n in range(self.N): feature = selFeatures[n] alpha = self.alpha[n] direction = self.G[n].opt_direction (types, x, y, width, height) = feature image = numpy.array([[155 for i in range(IMG_WIDTH)] for j in range(IMG_HEIGHT)]) assert x >= 0 and x < IMG_WIDTH assert y >= 0 and y < IMG_HEIGHT assert width > 0 and height > 0 if direction == +1: black = BLACK white = WHITE else: black = WHITE white = BLACK if types == HAAR_FEATURE_TYPE_I: for i in range(y, y + height * 2): for j in range(x, x + width): if i < y + height: image[i][j] = black else: image[i][j] = white elif types == HAAR_FEATURE_TYPE_II: for i in range(y, y + height): for j in range(x, x + width * 2): if j < x + width: image[i][j] = white else: image[i][j] = black elif types == HAAR_FEATURE_TYPE_III: for i in range(y, y + height): for j in range(x, x + width * 3): if j >= (x + width) and j < (x + width * 2): image[i][j] = black else: image[i][j] = white elif types == HAAR_FEATURE_TYPE_IV: for i in range(y, y + height*3): for j in range(x, x + width): if i >= (y + height) and i < (y + height * 2): image[i][j] = black else: image[i][j] = white elif types == HAAR_FEATURE_TYPE_V: for i in range(y, y + height * 2): for j in range(x, x + width * 2): if (j < x + width and i < y + height) or\ (j >= x + width and i >= y + height): image[i][j] = white else: image[i][j] = black else: raise Exception("Unkown type feature") #classifierPic += image * alpha * direction classifierPic += image pyplot.matshow(image, cmap = "gray") if DEBUG_MODEL == True: pylab.show() else: pyplot.savefig(FIGURES + "feature_" + str(n) + ".jpg") from image import Image classifierPic = Image._normalization(classifierPic) pylab.matshow(classifierPic, cmap = "gray") if DEBUG_MODEL == True: pylab.show() else: pyplot.savefig(FIGURES + "boosted_features.jpg")
from config import TRAINING_NONFACE from haarFeature import Feature from image import ImageSet from adaboost import AdaBoost from adaboost import getCachedAdaBoost import os import numpy Face = ImageSet(TRAINING_FACE, sampleNum = POSITIVE_SAMPLE) nonFace = ImageSet(TRAINING_NONFACE, sampleNum = NEGATIVE_SAMPLE) tot_samples = Face.sampleNum + nonFace.sampleNum haar = Feature(TRAINING_IMG_WIDTH, TRAINING_IMG_HEIGHT) if os.path.isfile(FEATURE_FILE_TRAINING + ".npy"): _mat = numpy.load(FEATURE_FILE_TRAINING + ".npy") else: if DEBUG_MODEL is True: _mat = numpy.zeros((haar.featuresNum, tot_samples)) for i in xrange(Face.sampleNum): featureVec = haar.calFeatureForImg(Face.images[i]) for j in xrange(haar.featuresNum): _mat[j][i ] = featureVec[j] for i in xrange(nonFace.sampleNum):
def scanImgAtScale(self, model, image, scale): assert isinstance(image, numpy.ndarray) ImgHeight, ImgWidth = image.shape SEARCH_WIN_WIDTH = int(TRAINING_IMG_WIDTH * scale) SEARCH_WIN_HEIGHT = int(TRAINING_IMG_HEIGHT * scale) width = ImgWidth - SEARCH_WIN_WIDTH - 10 height = ImgHeight - SEARCH_WIN_HEIGHT - 10 step = SEARCH_WIN_WIDTH/SEARCH_WIN_STEP subWinNum = (width/step + 1) * (height/step + 1) subImages = numpy.zeros(subWinNum, dtype = object) subWins = numpy.zeros(subWinNum, dtype = object) idx = 0 for x in xrange(0, width, step): for y in xrange(0, height, step): subWins[idx] = (x, y, SEARCH_WIN_WIDTH, SEARCH_WIN_HEIGHT) subImages[idx] = Image(Mat = image[y:y+SEARCH_WIN_HEIGHT, x:x+SEARCH_WIN_WIDTH]) idx += 1 subImgNum = idx selFeatures = numpy.zeros(model.N, dtype=object) haar_scaled = Feature(SEARCH_WIN_WIDTH, SEARCH_WIN_HEIGHT) haar_train = Feature(TRAINING_IMG_WIDTH, TRAINING_IMG_HEIGHT) for n in xrange(model.N): selFeatures[n] = haar_train.features[ model.G[n].opt_dimension ] + tuple([model.G[n].opt_dimension]) mat = numpy.zeros((haar_train.featuresNum, subImgNum), dtype=numpy.float16) for feature in selFeatures: (types, x, y, w, h, dim) = feature x = int(x * scale) y = int(y * scale) w = int(w * scale) h = int(h * scale) for i in xrange(subImgNum): if types == HAAR_FEATURE_TYPE_I: mat[dim][i] = haar_scaled.VecFeatureTypeI(subImages[i].vecImg, x, y, w, h) elif types == HAAR_FEATURE_TYPE_II: mat[dim][i] = haar_scaled.VecFeatureTypeII(subImages[i].vecImg, x, y, w, h) elif types == HAAR_FEATURE_TYPE_III: mat[dim][i] = haar_scaled.VecFeatureTypeIII(subImages[i].vecImg, x, y, w, h) elif types == HAAR_FEATURE_TYPE_IV: mat[dim][i] = haar_scaled.VecFeatureTypeIV(subImages[i].vecImg, x, y, w, h) output = model.grade(mat) rectangle = [] for i in xrange(len(output)): if output[i] > AB_TH: candidate = numpy.array(subWins[i]) x, y, w, h = candidate rectangle.append((x, y, w, h, output[i])) return rectangle
from utils import * from haarFeature import Feature from image import image_prep from adaboost import AdaBoost from adaboost import trained_model import os import numpy as np import pdb Face = image_prep(face_dir, num = face_samples) nonFace = image_prep(nonface_dir, num = face_samples) tot_samples = Face.num + nonFace.num haar = Feature(img_y_size, img_x_size) if os.path.isfile(feature_dir + ".npy"): img1 = np.load(feature_dir + ".npy") else: # pdb.set_trace() img1 = np.zeros((haar.featuresNum, tot_samples)) #pdb.set_trace() for i in range(Face.num): featureVec = haar.calFeatureForImg(Face.images[i]) for j in range(haar.featuresNum): img1[j][i] = featureVec[j] for i in range(nonFace.num): featureVec = haar.calFeatureForImg(nonFace.images[i]) for j in range(haar.featuresNum): img1[j][i + Face.num] = featureVec[j]
def makeimg_classif(self): imgx = img_x_size imgy = img_y_size haar = Feature(imgx, imgy) haarfeatures = haar.features best_features = [] for n in range(self.N): best_features.append(haarfeatures[self.H[n].opti_shape]) img_classif = np.zeros((imgy, imgx)) for n in range(self.N): feature = best_features[n] alpha = self.alpha[n] direct1 = self.H[n].opti_shape (types, x, y, width, height) = feature image = np.array([[155 for i in range(imgx)] for j in range(imgy)]) assert x >= 0 and x < imgx assert y >= 0 and y < imgy assert width > 0 and height > 0 if direct1 == +1: black = bl white = wh else: black = wh white = bl if types == haar_type1: for i in range(y, y + height * 2): for j in range(x, x + width): if i < y + height: image[i][j] = black else: image[i][j] = white elif types == haar_type2: for i in range(y, y + height): for j in range(x, x + width * 2): if j < x + width: image[i][j] = white else: image[i][j] = black elif types == haar_type3: for i in range(y, y + height): for j in range(x, x + width * 3): if j >= (x + width) and j < (x + width * 2): image[i][j] = black else: image[i][j] = white elif types == haar_type4: for i in range(y, y + height*3): for j in range(x, x + width): if i >= (y + height) and i < (y + height * 2): image[i][j] = black else: image[i][j] = white else: for i in range(y, y + height * 2): for j in range(x, x + width * 2): if (j < x + width and i < y + height) or\ (j >= x + width and i >= y + height): image[i][j] = white else: image[i][j] = black img_classif += image plt.figure() plt.matshow(image, cmap = "gray") plt.savefig(fig_dir + "features" + str(n) + ".jpg") from image import Image_perp plt.figure() img_classif = Image_perp.Normimg(img_classif) plt.matshow(img_classif, cmap = "gray") plt.savefig(fig_dir + "boosted_features.jpg")