def testAdaptiveClassifier(dataset_path, ID, alpha, rho, holeFilling, areaFiltering, P, connectivity, Morph, shadowRemoval): print 'Reading dataset...' train_frames_bw, test_frames_bw, train_gts, test_gts, frame_size = readDataset( dataset_path, ID, False) if shadowRemoval: train_frames_c, test_frames_c, train_gts, test_gts, frame_size_c = readDataset( dataset_path, ID, True) print 'Dataset has been read!' init_mu = np.zeros([1, train_frames_bw.shape[1]]) init_std = np.zeros([1, train_frames_bw.shape[1]]) init_mu_c = np.zeros([1, train_frames_bw.shape[1], 3]) init_std_c = np.zeros([1, train_frames_bw.shape[1], 3]) clf = AdaptiveClassifier(alpha, rho, init_mu, init_std, init_mu_c, init_std_c) clf = clf.fit(train_frames_bw, train_gts) if shadowRemoval: precision, recall, fscore = clf.performance_measures_shadowRemoval( train_frames_c, test_frames_bw, test_frames_c, test_gts, frame_size, holeFilling, areaFiltering, P, connectivity, Morph) else: precision, recall, fscore = clf.performance_measures( test_frames_bw, test_gts, frame_size, holeFilling, areaFiltering, P, connectivity, Morph) print 'F-score:', fscore print 'Precision: ', precision print 'Recall: ', recall
def optimalAlphaAdaptive(dataset_path, ID, optimal_rho, holeFilling, areaFiltering, P, connectivity, Morph, shadRemov): print 'Computing optimal alpha for ' + ID + ' dataset ...' Precision_vec = [] Recall_vec = [] fscore_vec = [] alpha_vec = np.arange(0, 30, 1) print 'Reading dataset...' train_frames_bw, test_frames_bw, train_gts, test_gts, frame_size = readDataset( dataset_path, ID, False) if shadRemov: train_frames_c, test_frames_c, train_gts, test_gts, frame_size = readDataset( dataset_path, ID, True) print 'Dataset has been read!' init_mu = np.zeros([1, train_frames_bw.shape[1]]) init_std = np.zeros([1, train_frames_bw.shape[1]]) init_mu_c = np.zeros([1, train_frames_bw.shape[1], 3]) init_std_c = np.zeros([1, train_frames_bw.shape[1], 3]) for alpha in alpha_vec: print 'Evaluating with alpha = ', alpha clf = AdaptiveClassifier(alpha, optimal_rho, init_mu, init_std, init_mu_c, init_std_c) clf = clf.fit(train_frames_bw, train_gts) if shadRemov: precision, recall, fscore = clf.performance_measures_shadowRemoval( train_frames_c, test_frames_bw, test_frames_c, test_gts, frame_size, holeFilling, areaFiltering, P, connectivity, Morph) else: precision, recall, fscore = clf.performance_measures( test_frames_bw, test_gts, frame_size, holeFilling, areaFiltering, P, connectivity, Morph) Precision_vec = np.append(Precision_vec, precision) Recall_vec = np.append(Recall_vec, recall) fscore_vec = np.append(fscore_vec, fscore) min, max, idxmin, idxmax = cv2.minMaxLoc(fscore_vec) print 'Maximum F1-Score with ', ID, ' dataset is ', max, ' with alpha = ', alpha_vec[ idxmax[1]] print 'Precision selected with dataset ', ID, ' is ', Precision_vec[ idxmax[1]] print 'Recall selected with dataset ', ID, ' is ', Recall_vec[idxmax[1]] return Precision_vec, Recall_vec, fscore_vec, alpha_vec
def optimizeAlphaRho(alpha_values, rho_values, dataset_path, ID, Color): train_frames, test_frames, train_gts, test_gts = readDataset( dataset_path, ID, Color) frames = np.vstack((train_frames, test_frames)) labels = np.vstack((train_gts, test_gts)) train_idx = [range(int(round(frames.shape[0] * 0.5)))] test_idx = [range(int(round(frames.shape[0] * 0.5)), frames.shape[0])] init_mu = np.zeros([1, train_frames.shape[1]]) init_std = np.zeros([1, train_frames.shape[1]]) parameters = {'alpha': alpha_values, 'rho': rho_values} # perform grid search to optimize alpha and rho grid = GridSearchCV(AdaptiveClassifier(init_mu, init_std), parameters, cv=zip(train_idx, test_idx)) grid.fit(frames, labels) # save results to disk f = open('gridsearch_' + ID + '.pckl', 'wb') pickle.dump(grid, f) f.close() return grid
def optimalAlphaAdaptive(dataset_path, ID, Color, optimal_rho): print 'Computing optimal alpha for ' + ID + ' dataset ...' Precision_vec = [] Recall_vec = [] fscore_vec = [] alpha_vec = np.linspace(0, 30, 21) train_frames, test_frames, train_gts, test_gts = readDataset( dataset_path, ID, False) init_mu = np.zeros([1, train_frames.shape[1]]) init_std = np.zeros([1, train_frames.shape[1]]) for alpha in alpha_vec: clf = AdaptiveClassifier(alpha, optimal_rho, init_mu, init_std) clf = clf.fit(train_frames, train_gts) precision, recall, fscore = clf.performance_measures( test_frames, test_gts) Precision_vec = np.append(Precision_vec, precision) Recall_vec = np.append(Recall_vec, recall) fscore_vec = np.append(fscore_vec, fscore) min, max, idxmin, idxmax = cv2.minMaxLoc(fscore_vec) print 'Maximum F1-Score with ', ID, ' dataset is ', max, ' with alpha = ', alpha_vec[ idxmax[1]] print 'Precision selected with dataset ', ID, ' is ', Precision_vec[ idxmax[1]] print 'Recall selected with dataset ', ID, ' is ', Recall_vec[idxmax[1]] return Precision_vec, Recall_vec, fscore_vec, alpha_vec
def estimateOpticalFlow(dataset_path, blockSize, areaOfSearch, Backward, showResult): print 'Optical Flow computation with BlockSize = ',blockSize, ' and AreaOfSearch = ',areaOfSearch frames_list, gt_list, frames_path, gt_path = readDataset(dataset_path) nFrames = len(frames_list) for idx in range(0, nFrames, 2): print ' --> Analyzing sequence ',frames_list[idx],' and ',frames_list[idx+1],'...' # print 'Evaluating frame '+ pr_name frame_dir_0 = os.path.join(frames_path, frames_list[idx]) frame_dir_1 = os.path.join(frames_path, frames_list[idx+1]) if Backward: toExplore_img = cv2.imread(frame_dir_0, 0) curr_img = cv2.imread(frame_dir_1, 0) else: curr_img = cv2.imread(frame_dir_0, 0) toExplore_img = cv2.imread(frame_dir_1, 0) OF_image = calcOpticalFlowBM(curr_img, toExplore_img, blockSize, areaOfSearch) if showResult: showOpticalFlowArrows(OF_image, curr_img, frames_list[idx], saveResult=True) showOpticalFlowHSVBlockMatching(OF_image, frames_list[idx], visualization='HS', saveResult=True) np.save('results/OpticalFlow/files/OF_'+frames_list[idx], OF_image) return
def estimateOpticalFlowTVL1(dataset_path, showResult, Backward): print 'Optical Flow computation with Farneback method' frames_list, gt_list, frames_path, gt_path = readDataset(dataset_path) nFrames = len(frames_list) for idx in range(0, nFrames, 2): print ' --> Analyzing sequence ',frames_list[idx],' and ',frames_list[idx+1],'...' # print 'Evaluating frame '+ pr_name frame_dir_0 = os.path.join(frames_path, frames_list[idx]) frame_dir_1 = os.path.join(frames_path, frames_list[idx+1]) if Backward: os.system('./IPOL_TVL1/tvl1flow ' + frame_dir_1 + ' ' + frame_dir_0 + ' IPOL_TVL1/flow.flo') #toExplore_img = cv2.imread(frame_dir_0, 0) curr_img = cv2.imread(frame_dir_1, 0) else: os.system('./IPOL_TVL1/tvl1flow ' + frame_dir_0 + ' ' + frame_dir_1 + ' IPOL_TVL1/flow.flo') curr_img = cv2.imread(frame_dir_0, 0) #toExplore_img = cv2.imread(frame_dir_1, 0) data2D = readFLO('./IPOL_TVL1/flow.flo') OF_image = np.zeros([data2D.shape[0], data2D.shape[1], 3]) OF_image[:,:,0:2] = data2D if showResult: showOpticalFlowArrows(OF_image, curr_img, frames_list[idx], saveResult = True ) showOpticalFlowHSVBlockMatching(OF_image, frames_list[idx], visualization='HS', saveResult=True) np.save('results/OpticalFlow/files/OF_'+frames_list[idx], OF_image) return
def estimateOpticalFlowFarneback(dataset_path, showResult, Backward): print 'Optical Flow computation with Farneback method' frames_list, gt_list, frames_path, gt_path = readDataset(dataset_path) nFrames = len(frames_list) for idx in range(0, nFrames, 2): print ' --> Analyzing sequence ',frames_list[idx],' and ',frames_list[idx+1],'...' # print 'Evaluating frame '+ pr_name frame_dir_0 = os.path.join(frames_path, frames_list[idx]) frame_dir_1 = os.path.join(frames_path, frames_list[idx+1]) if Backward: toExplore_img = cv2.imread(frame_dir_0, 0) curr_img = cv2.imread(frame_dir_1, 0) else: curr_img = cv2.imread(frame_dir_0, 0) toExplore_img = cv2.imread(frame_dir_1, 0) #OF_image = cv2.calcOpticalFlowFarneback(curr_img, toExplore_img, pyr_scale=0.5, levels=5, winsize=15, iterations=9, poly_n=7, poly_sigma=1.5, flags=0) #OF_image = cv2.calcOpticalFlowFarneback(curr_img, toExplore_img, pyr_scale=0.5, levels=3, winsize=15, iterations=3, poly_n=5, poly_sigma=1.2, flags=0) OF_image = cv2.calcOpticalFlowFarneback(curr_img, toExplore_img, pyr_scale=0.5, levels=5, winsize=20, iterations=10, poly_n=7, poly_sigma=1.1, flags=0) if showResult: showOpticalFlowArrows(OF_image, curr_img, frames_list[idx], saveResult = True ) showOpticalFlowHSVBlockMatching(OF_image, frames_list[idx], visualization='HS', saveResult=True) np.save('results/OpticalFlow/files/OF_'+frames_list[idx], OF_image) return
def testBackgroundEstimation(alpha, dataset_path, ID, Color): train_frames, test_frames, train_gts, test_gts = readDataset(dataset_path, ID, Color) mu_vec, std_vec = modelGaussianDistribution(train_frames) predictions = predictBackgroundForeground(test_frames, mu_vec, std_vec, alpha, Color) precision, recall, fscore = evaluateBackgroundEstimation(predictions, test_gts) print 'F-score:', fscore print 'Precision: ', precision print 'Recall: ', recall
def testAdaptiveClassifier(alpha, rho, dataset_path, ID): train_frames, test_frames, train_gts, test_gts = readDataset( dataset_path, ID, False) init_mu = np.zeros([1, train_frames.shape[1]]) init_std = np.zeros([1, train_frames.shape[1]]) clf = AdaptiveClassifier(alpha, rho, init_mu, init_std) clf = clf.fit(train_frames, train_gts) precision, recall, fscore = clf.performance_measures(test_frames, test_gts) print 'F-score:', fscore print 'Precision: ', precision print 'Recall: ', recall
def optimalBlockSizeArea(dataset_path, gt_path, Backward): print 'Computing optimal values of Block Size and Area of search' frames_list, gt_list, frames_path, gt_path = readDataset(dataset_path) nFrames = len(frames_list) blockSize_vec = np.arange(20, 61, 10) areaOfSearch_vec = np.arange(5, 51, 5) MSEN_mat_1 = np.zeros([blockSize_vec.size, areaOfSearch_vec.size]) MSEN_mat_2 = np.zeros([blockSize_vec.size, areaOfSearch_vec.size]) PEPN_mat_1 = np.zeros([blockSize_vec.size, areaOfSearch_vec.size]) PEPN_mat_2 = np.zeros([blockSize_vec.size, areaOfSearch_vec.size]) idxBS = 0 for blockSize in blockSize_vec: idxAoF = 0 for areaOfSearch in areaOfSearch_vec: print ' --> Optical Flow computation with BlockSize = ', blockSize, ' and AreaOfSearh = ', areaOfSearch for idx in range(0, nFrames, 2): print ' Analyzing sequence ', frames_list[idx], ' and ', frames_list[idx + 1], '...' # print 'Evaluating frame '+ pr_name frame_dir_0 = os.path.join(frames_path, frames_list[idx]) frame_dir_1 = os.path.join(frames_path, frames_list[idx + 1]) if Backward: toExplore_img = cv2.imread(frame_dir_0, 0) curr_img = cv2.imread(frame_dir_1, 0) else: curr_img = cv2.imread(frame_dir_0, 0) toExplore_img = cv2.imread(frame_dir_1, 0) OF_image = calcOpticalFlowBM(curr_img, toExplore_img, blockSize, areaOfSearch) np.save('results/OpticalFlow/files/OF_' + frames_list[idx], OF_image) MSEN, PEPN = evaluateOpticalFlow('results/OpticalFlow/files/', gt_path) MSEN_mat_1[idxBS,idxAoF] = MSEN[0] MSEN_mat_2[idxBS,idxAoF] = MSEN[1] PEPN_mat_1[idxBS,idxAoF] = PEPN[0] PEPN_mat_2[idxBS,idxAoF] = PEPN[1] idxAoF = idxAoF + 1 idxBS = idxBS + 1 return MSEN_mat_1, MSEN_mat_2, PEPN_mat_1, PEPN_mat_2, blockSize_vec, areaOfSearch_vec
def testBackgroundSubtractorMOG(dataset_path, ID, method, save_masks, Color): # Check directories output_dir = dataset_path + ID + '/' + method print 'Running background subtraction using ' + method + ' ...' print 'Output masks will be saved at "' + output_dir + '" directory' if not os.path.exists(output_dir): os.makedirs(output_dir) # Use MOG or MOG2 according to input method if method == 'MOG': fgbg = cv2.BackgroundSubtractorMOG() elif method == 'MOG2': fgbg = cv2.BackgroundSubtractorMOG2() # Get input frames size frames_path = dataset_path + ID + '/input' frame_list = sorted(os.listdir(frames_path)) nrows, ncols, nchannels = cv2.imread( os.path.join(frames_path, frame_list[0])).shape mask_vec = np.zeros([1, nrows * ncols]) # Read dataset train_frames, test_frames, train_gts, test_gts = readDataset( dataset_path, ID, Color) # Run MOG for idx in range(len(test_frames)): if Color: im = np.uint8( np.reshape(test_frames[idx], (nrows, ncols, nchannels))) else: im = np.uint8(np.reshape(test_frames[idx], (nrows, ncols))) fgmask = fgbg.apply(im, learningRate=0.01) fgmask = fgmask.ravel() fgmask[fgmask == 127] = 0 fgmask[fgmask > 127] = 1 mask_vec = np.vstack((mask_vec, fgmask)) # Save current mask if save_masks: cv2.imwrite(output_dir + '/img_' + ('%03d' % idx) + '.png', np.reshape(255 * fgmask, (nrows, ncols))) # Get predictions and compute performance measures predictions = mask_vec[1:, :] precision, recall, fscore = evaluateBackgroundEstimation( predictions, test_gts) print 'F-score:', fscore print 'Precision: ', precision print 'Recall: ', recall
def optimalAlpha(dataset_path, ID, Color): print 'Computing optimal alpha for '+ID+' dataset ...' Precision_vec = [] Recall_vec = [] fscore_vec = [] alpha_vec = np.arange(0, 30, 0.2) train_frames, test_frames, train_gts, test_gts = readDataset(dataset_path, ID, Color) mu_vec, std_vec = modelGaussianDistribution(train_frames) ## To write the mean and std amage #mu = np.reshape(mu_vec, (240, 320, 3)) #std = np.reshape(std_vec, (240, 320, 3)) #cv2.imwrite("mu_Color_ID3.png", mu.astype(np.uint8)) #cv2.imwrite("std_Color_ID3.png", std.astype(np.uint8)) for alpha in alpha_vec: predictions = predictBackgroundForeground(test_frames, mu_vec, std_vec, alpha, Color) precision, recall, fscore = evaluateBackgroundEstimation(predictions, test_gts) Precision_vec = np.append(Precision_vec, precision) Recall_vec = np.append(Recall_vec, recall) fscore_vec = np.append(fscore_vec, fscore) min, max, idxmin, idxmax = cv2.minMaxLoc(fscore_vec) print 'Maximum F1-Score with ', ID, ' dataset is ', max, ' with alpha = ', alpha_vec[idxmax[1]] print 'Precision selected with dataset ', ID, ' is ', Precision_vec[idxmax[1]] print 'Recall selected with dataset ', ID, ' is ', Recall_vec[idxmax[1]] # Plot F1, Precision and recall per alpha fig = plt.figure() fig.suptitle('F-Score, Precision and Recall vs alpha (' + ID + ')', fontsize=15) ax3 = fig.add_subplot(111) ax3.set_xlabel('Alpha', fontsize=11) ax3.plot(alpha_vec, fscore_vec, c='orange', linewidth=3.0, label='F1-Score') ax3.plot(alpha_vec, Recall_vec, c='g', linewidth=3.0, label='Recall') ax3.plot(alpha_vec, Precision_vec, c='b', linewidth=3.0, label='Precision') plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) fig.savefig('plotPrecRecFsc_' + ID + '.png', bbox_inches='tight') return Precision_vec, Recall_vec, fscore_vec, alpha_vec
def testKalmanTracking(dataset_path, ID, alpha, rho, holeFilling, areaFiltering, P, connectivity, Morph, GT_available, estimate_speed=False): # read dataset #train_rgb, test_rgb, train_gray, test_gray, train_gt, test_gt, frame_size = readDatasetStabilized(dataset_path, ID, True, GT_available) train_rgb, test_rgb, train_gray, test_gray, train_gt, test_gt, frame_size = readDataset(dataset_path, ID, True, GT_available) # compute masks init_mu = np.zeros([1, train_gray.shape[1]]) init_std = np.zeros([1, train_gray.shape[1]]) init_mu_c = np.zeros([1, train_gray.shape[1], 3]) init_std_c = np.zeros([1, train_gray.shape[1], 3]) clf = AdaptiveClassifier(alpha, rho, init_mu, init_std, init_mu_c, init_std_c) clf = clf.fit(train_gray, train_gt) predictions = clf.predict(test_gray, test_gt) predictions = clf.postProcessing(predictions, frame_size, holeFilling, areaFiltering, P, connectivity, Morph) # tracking starts here distance_threshold = 40 object_list = kalman.computeTrackingKalman(test_rgb, predictions, frame_size, distance_threshold, True, speed=estimate_speed)
def optimalPAdaptive(dataset_path, ID, optimal_rho, holeFilling, areaFiltering, connectivity): print 'Computing optimal P for ' + ID + ' dataset ...' alpha_vec = np.arange(1, 30, 1) P_vec = np.arange(0, 1001, 50) auc_vec = [] print 'Reading dataset...' train_frames, test_frames, train_gts, test_gts, frame_size = readDataset( dataset_path, ID, False) print 'Dataset has been read!' init_mu = np.zeros([1, train_frames.shape[1]]) init_std = np.zeros([1, train_frames.shape[1]]) for p in P_vec: print 'Evaluating with P = ', p, ' pixels' Precision_vec = [] Recall_vec = [] fscore_vec = [] for alpha in alpha_vec: clf = AdaptiveClassifier(alpha, optimal_rho, init_mu, init_std) clf = clf.fit(train_frames, train_gts) precision, recall, fscore = clf.performance_measures( test_frames, test_gts, frame_size, holeFilling, areaFiltering, p, connectivity, False) Precision_vec = np.append(Precision_vec, precision) Recall_vec = np.append(Recall_vec, recall) fscore_vec = np.append(fscore_vec, fscore) AUC = auc(Recall_vec, Precision_vec) auc_vec = np.append(auc_vec, AUC) return auc_vec, P_vec
def testMedianFlowTracking(dataset_path, ID, alpha, rho, holeFilling, areaFiltering, P, connectivity, Morph, GT_available): # read dataset train_rgb, test_rgb, train_gray, test_gray, train_gt, test_gt, frame_size = readDataset(dataset_path, ID, True, GT_available) # compute masks init_mu = np.zeros([1, train_gray.shape[1]]) init_std = np.zeros([1, train_gray.shape[1]]) init_mu_c = np.zeros([1, train_gray.shape[1], 3]) init_std_c = np.zeros([1, train_gray.shape[1], 3]) clf = AdaptiveClassifier(alpha, rho, init_mu, init_std, init_mu_c, init_std_c) clf = clf.fit(train_gray, train_gt) predictions = clf.predict(test_gray, test_gt) predictions = clf.postProcessing(predictions, frame_size, holeFilling, areaFiltering, P, connectivity, Morph) n_frames, _, d = test_rgb.shape h, w = frame_size os.mkdir(dataset_path + '/' + ID + '/tmp') for frame_number in range(0, n_frames): frame = np.reshape(test_rgb[frame_number, :, :], (h, w, 3)) cv2.imwrite(dataset_path + '/' + ID + '/tmp/tmp_'+('%03d' % frame_number)+'.png', frame) # tracking starts here frame_num = 0 prev_bbox = [] tracker_list = [] perspective = True idx_removed = [] num_trackers = 0 video = cv2.VideoCapture(dataset_path+'/'+ID+'/tmp/tmp_%03d.png') start = time.time() for frame_number in range(0, n_frames): ok, frame = video.read() mask = np.reshape(predictions[frame_number, :], (h, w)) # Initialize new trackers if necessary tracker_list, num_new_trackers = utils.initialize_trackers(frame, mask, tracker_list, prev_bbox, perspective) if num_new_trackers > 0: num_trackers = num_trackers + num_new_trackers # Update trackers bbox_list = [] for i in range(np.shape(tracker_list)[0]): ok, bbox = tracker_list[i].update(frame) bbox_list.append(bbox) # Check if the predicted bboxes (bbox_list) overlap with a CC # if they do not overlap, then they must be removed idx_removed = utils.check_overlap_out(bbox_list, mask, perspective) tmp = np.shape(bbox_list)[0] idx = 0 while idx < tmp: if idx in idx_removed: del tracker_list[idx] del bbox_list[idx] tmp = tmp - 1 idx = idx + 1 # update bboxes prev_bbox = bbox_list car_ID_list = range(1, num_trackers + 1) num_cars = utils.count_cars(bbox_list) # currently on frame # Draw bounding boxes for i in range(np.shape(bbox_list)[0]): p1 = (int((bbox_list[i])[0]), int((bbox_list[i])[1])) p2 = (int((bbox_list[i])[0] + (bbox_list[i])[2]), int((bbox_list[i])[1] + (bbox_list[i])[3])) cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1) cv2.rectangle(frame, (p2[0] + 2, p2[1] - 18), (p2[0] + 38, p2[1] - 2), (0, 0, 0), -1, 1) cv2.putText(frame, 'ID: ' + str(car_ID_list[i]), (p2[0] + 5, p2[1] - 6), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1) # Display result cv2.putText(frame, 'Number of cars: ' + str(num_cars), (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (0, 255, 0), 1) # cv2.imshow('Tracking', frame) result = np.concatenate((frame, np.stack([mask * 255, mask * 255, mask * 255], axis=-1)), 1) cv2.imwrite("Results/ComboImage_" + str(frame_num) + '.png', result) frame_num = frame_num + 1 shutil.rmtree(dataset_path+'/'+ID+'/tmp') end = time.time() fps = n_frames / (end - start) print ('FPS for tracking = %.2f' % fps)