def hog(self): print('---HOG---') img_to_hog = lambda img: utils.hog(img) self.img_set = np.array(list(map(img_to_hog, self.img_set))) with open('para.json', 'r') as f: js = json.load(f) block_wid = js['hog_dic']['block_width'] block_hei = js['hog_dic']['block_height'] n_ori = js['hog_dic']['n_orient'] self.img_set = self.img_set.reshape( (-1, n_ori, int(self.width / block_wid + 0.5), int(self.height / block_hei + 0.5))) self.img_set = np.transpose(self.img_set, (0, 2, 3, 1)) self.n_example, self.width, self.height, self.color = self.img_set.shape self.n_size_reset()
def load_img(self, path, render=True, hog=False): try: img = mpimg.imread(path) if render: implot = self.mplw.canvas.imax.imshow(img, cmap="gray") if hog: return utils.hog(img) else: arr = img if len(img.shape) == 3: arr = np.average(arr, axis=2) if img.dtype == "float32": arr = np.floor(256*arr) hist = np.histogram(arr.flatten(), bins=256)[0] if render: self.mplw.canvas.hsax.clear() x = np.linspace(0,254,255, dtype="int") self.mplw.canvas.hsax.bar(x, hist[:-1]) self.mplw.canvas.draw() return hist[:-1] except Exception as e: print(e) self.log(f"Invalid image file ({e})")
def hog_sliding_window(img, classifier, window_size, stride, boundary): np_img = np.array(img) img_w, img_h, _ = np_img.shape score_board = np.zeros(np_img.shape[0:2]) count_board = np.zeros(np_img.shape[0:2]) for window in window_size: w, h = window for x in range(0, img_h-h, stride): for y in range(0, img_w-w, stride): count_board = plus1(count_board, (x,y,w,h)) croped_img = utils.seperate_region(img, (x, y, w, h)) croped_img = croped_img.resize((64,64)) hog = utils.hog(np.array(croped_img)) hog = hog.reshape((-1,8,4,4)) hog = np.transpose(hog, (0,2,3,1)) if classifier(hog): score_board = plus1(score_board, (x,y,w,h)) b_img = (score_board / count_board) > boundary b_img = b_img.reshape((img_w, img_h, 1)) b_img = b_img * img result_image = Image.fromarray(b_img) return result_image
def face_detect_patch(patch, w, theta): patch = patch / 255.0 hog_patch = hog(patch) return prediction(np.transpose(np.concatenate(([[1]], hog_patch))), w, theta)
from sklearn.externals import joblib as jb from sklearn.svm import LinearSVC import utils import argparse argp = argparse.ArgumentParser() argp.add_argument("-d", "--dataset", required=True, help="dataset file") argp.add_argument("-m", "--model", required=True, help="Path model being saved") args = vars(argp.parse_args()) Numbers, Labels = utils.getting_data(args["dataset"]) data = [] for image in Numbers: image = utils.norm_img(image, 20) image = utils.mass_center(image, (20, 20)) dat = utils.hog(image, orientations=18, pixelsPerCell=(5, 5), cellsPerBlock=(1, 1), normalize=True) data.append(dat) model = LinearSVC(random_state=40) model.fit(data, Labels) jb.dump(model, args["model"])