def cascade(): """ :return: Beginning of cascade classification. Still being developed. """ pred = 0 bdt = joblib.load('models/adaboost256.pkl') frame = cv2.imread('images/gradient.jpg') # X = cf.compute_chans(frame).ravel().reshape(1, -1) # X = cf.compute_chans(cv2.resize(frame, (64, 128))).ravel().reshape(1, -1) x = cf.compute_chans(cv2.resize(frame, (64, 128))).ravel() # np.random.shuffle(x) N = 100 pred = [None] * N X = np.array([x for i in range(N)]) start = time.time() for i in range(N): for t, estimator in enumerate(bdt.estimators_): pred += _samme_proba(estimator, 2, X) p_t = pred[0, 1] / (t + 1) print('p_t is: ', p_t) if p_t < -.2 and t > 8: return False return False out = casc.cascade(X, bdt) print((time.time() - start) / N) start = time.time() for i in range(N): out = bdt.predict(X) print((time.time() - start) / N) print(type(bdt))
def test(img, bdt): stride = 6 w0, h0 = (16, 32) # img = cv2.pyrUp(img) # img = cv2.pyrUp(img) # img = cv2.pyrDown(img) clone = img.copy() # pyr = cv2.pyrUp(img) # start = time.time() img = cf.compute_chans(img) # print(time.time() - start) # start = time.time() frames, bbs = slide_window(img, w0, h0, stride) # print(time.time() - start) # f = frames[0,:100] # print(f) # start = time.time() y = bdt.predict(frames) cp = bdt.predict_proba(frames) print(cp.shape) print(.5 * np.log(cp[:, 0] / cp[:, 1])) # print(bdt.get_params()) # print(time.time() - start) for yi, bb in zip(y, bbs): if yi == 1: x0, y0, x1, y1 = np.int32(4 * bb) # print(bb) cv2.rectangle(clone, (x0, y0), (x1, y1), (0, 255, 0), 1) print(frames.shape) cv2.imshow('frame', clone) cv2.waitKey(0)
def load_images(): """ :return: Loads unscaled pedestrian frames, returns the feature channels for the frames at original and half scale. TODO: generalize to arbitrary scaling """ images = [] frames = [] frames_s = [] #pos_dir = '/media/nolsman/TRANSCEND/data/train/positive_unscaled' pos_dir = '/home/lane/Development/computer_vision/images' for fname in glob.glob(pos_dir + '/*'): img = cv2.imread(fname) channels = cf.compute_chans(img) channels_s = cf.compute_chans(cv2.pyrDown(img)) frames.append(channels) frames_s.append(channels_s) images.append(img) return images, frames, frames_s
def hard_negatives(N, clf): """ :param N: Number of negatives :param clf: classifier :return: Mines N hard negatives with the classifier clf. Basically searches for false positives. """ data_dir = '/media/nolsman/TRANSCEND/data' negatives = [None] * N annotations = json.load(open(data_dir + '/annotations.json')) image_locs = np.random.permutation(get_image_locs()) fcount = 0 n_tot = 0 while n_tot < N: fname = image_locs[fcount] fcount += 1 setID = re.search('set(\d)+', fname).group(0) vidID = re.search('V(\d)+', fname).group(0) ind = re.search('(?<=\_)(\d)+(?=\.)', fname).group(0) img = cv2.imread(fname) frame = cf.compute_chans(img) wins, bbs = detect(frame, clf, 1) perm = np.random.permutation(np.arange(wins.shape[0])) wins = wins[perm, :] bbs = bbs[perm, :] ncount = 0 for bb1, win in zip(bbs, wins): neg = True if ncount < 25: if ind in annotations[setID][vidID]['frames']: data = annotations[setID][vidID]['frames'][ind] for datum in data: bb2 = datum['pos'] if iou(bb1, bb2) > .1: neg = False if neg: negatives[n_tot] = win ncount += 1 n_tot += 1 if n_tot % 25 == 0: print(n_tot) if n_tot == N: break return negatives
def load_neg(N): """ :param N: :return: Load random negatives, labels automatically set to 0 """ img_neg = random_negatives(N) neg = [] for img in img_neg: x = cf.compute_chans(img).ravel() neg.append(x) X_n = np.array(neg) y_n = np.zeros(N) return X_n, y_n
def load_pos(): """ :return: Loads all positive training samples, automatically labeled 1 """ pos_dir = '/media/nolsman/TRANSCEND/data/train/positive' pos = [] for fname in glob.glob(pos_dir + '/*'): img = cv2.imread(fname) x = cf.compute_chans(img).ravel() pos.append(x) X_p = np.array(pos) y_p = np.ones(len(pos)) return X_p, y_p