コード例 #1
0
def generate_c3d_features(c3d, filename):
    """
    Function to process the current video file and generate the c3d fetures.

    Parameters
    c3d             :torch.nn.Module
                     The c3d feature extraction model.
    filename        :str
                     The filename of the videos to return the features

    Returns
    -------
    features        :numpy.array
                     The features of the video in the required shape.
    """
    total_length = find_length(filename)
    block = generate_block(filename)
    start_time = time.time()
    feature_arr = []
    for i, curr_block in enumerate(block):
        print("\t\t\t\t\t [{}/{}]".format(i + 1, total_length),
              sep='\r',
              end='\r')
        features = c3d(curr_block['block'].to(device))
        #features = (features - features.mean())/(features.max() - features.mean())
        feature_arr.append(features.cpu().detach().numpy())
        del features
    total_time = time.time() - start_time
    features = np.rollaxis(np.array(feature_arr), 1)[0]
    return features
コード例 #2
0
def predict_scores(c3d, anomaly_ann, filename, seg_length, base_path):
	"""
	Function to predict the anomaly scores of a video and plot the scores
	by iterating over the segment length of the video.

	Parameters
	----------
	c3d             :torch.nn.Module
					 The c3d feature extraction model.
	anomaly_ann     :torch.nn.Module
					 The anomaly ann model that predicts the anomaly scores.
	filename        :str
					 Filename of the plot figure.
	seg_length      :int
					 length of the video segment to be processed as
					 multipliers of 16.
	base_path       :str
					 Path of the folder containing the video files.

	Returns
	-------
	None
	"""
	block = generate_block(os.path.join(base_path, filename), seg_length)
	score_arr = []
	start_time = time.time()
	for i, curr_block in enumerate(block):
		features = c3d(curr_block)
		features = features.norm(dim=0)
		features = (features-features.mean())/(features.max()-features.mean())
		scores = anomaly_ann(features)
		del features
		print(scores,sep='\r', end='\r')
		score_arr.append(scores.item())
		del scores
	total_time = time.time()-start_time
	plot_score(score_arr, os.path.join('results/plots', base_path.split('/')[-1]), filename.split('.')[0]+'.png')
コード例 #3
0
def find_length(filename, seg_length=1):
    """
    Function to find the length of the video in terms of specified segments.
    """
    return sum(1 for _ in generate_block(filename, 1))
コード例 #4
0
#db_access = db()
vid_base_path = args.vid_base_path
features_base_path = args.features_base_path
vid_file = args.vid_file
features_base_path = args.features_base_path

vid_path = os.path.join(vid_base_path, vid_file)
feature_file = vid_file.split('.')[0] + '.csv'
feature_path = os.path.join(features_base_path, feature_file)

assert (
    os.path.exists(vid_path)), "Video, '{}' does not exist".format(vid_path)
assert (os.path.exists(feature_path)
        ), "Feature file, '{}' does not exist".format(feature_path)

vid = generate_block(vid_path, 1, return_frame=True)
features = generate_tensor(feature_path)
weights_path = args.weights_path

detector = anomaly_ann(weights_path, no_sigmoid=True)

cv2.namedWindow("preview")
font = cv2.FONT_HERSHEY_SIMPLEX
text_pos = (10, 30)
score_processor = score_processor(base_threshold, alert_threshold, True)

min, max = get_min_max(feature_file)

for i, block in enumerate(vid):
    score = detector(features[i]).item()
    score = (score - min) / (max - min)
コード例 #5
0
base_threshold = 0.3
alert_threshold = 0.6
weights_dict = {'c3d' : 'weights/c3d.pickle', 'ann': 'weights/weights_L1L2.mat'}
verbose = True

parser = argparse.ArgumentParser()
parser.add_argument("--vid", type=str, default='SampleVideos/videos/RoadAccidents022_x264.mp4')
parser.add_argument('--no_sigmoid', action='store_true', help='If true, no sigmoid for ann')
args = parser.parse_args()

filename = args.vid
no_sigmoid = args.no_sigmoid
if filename == '0':
    filename = 0

vid = generate_block(filename, 1, return_frame=True)
csv_index = filename.split('/')[-1].split('.')[0]+'.csv'
min, max = get_min_max(csv_index)

detector = anomaly_detector(weights_dict, no_sigmoid='True')
score_processor = score_processor(base_threshold, alert_threshold, True)

cv2.namedWindow("preview")
font = cv2.FONT_HERSHEY_SIMPLEX
text_pos = (10, 30)

for i, block in enumerate(vid):
    score = detector.predict(block['block'])
    print(score)
    score = (score-min)/(max-min)
    disp_score = score*100