-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_features.py
More file actions
68 lines (56 loc) · 2.28 KB
/
Copy pathextract_features.py
File metadata and controls
68 lines (56 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
This code is based on the work of https://github.com/ptirupat/AnomalyDetection_CVPR18 it has been modified to extract features for many videos
in their work they hav referenced the followigng repository https://github.com/WaqasSultani/AnomalyDetectionblob/master/Testing_Anomaly_Detector_public.py
"""
#MAIN FILE: This code is used to extract features to reproduce the baseline work
#CHANGE the input and output file paths and run the file
import os
from c3d import *
import collections
import configuration as cfg
import time
import csv
import os
from os import listdir
import numpy as np
from datetime import datetime
import shutil
from utils.video_util import *
from utils.array_util import *
#get Files
videosPath = cfg.input_folder #directory where video files are stored
allFiles= listdir(videosPath)
allFiles.sort()
print(allFiles)
outputPath = cfg.output_folder #directory where you want to save the Extracted Features
def extractVideoFeatures():
startTime = datetime.now()
# build models
feature_extractor = c3d_feature_extractor()
print("Models initialized")
for i in range(len(allFiles)):
videoP = videosPath + allFiles[i]
runExtractor(videoP, feature_extractor)
print("Total Time taken: " , str(datetime.now() - startTime))
def runExtractor(filePath, featEx):
videoName = os.path.basename(filePath).split('.')[0] #get File name
# read video
video_clips, num_frames = get_video_clips(filePath)
print("Number of clips in the video : ", len(video_clips), " ", num_frames)
# extract features
rgb_features = []
for i, clip in enumerate(video_clips):
clip = np.array(clip)
if len(clip) < params.frame_count:
continue
clip = preprocess_input(clip)
rgb_feature = featEx.predict(clip)[0] #4096 features are collected for each video clip
rgb_features.append(rgb_feature)
print("Processed clip : ", i)
rgb_features = np.array(rgb_features)
# bag features
rgb_feature_bag = interpolate(rgb_features, params.features_per_bag) # takes the features and puts them into 32 bags
print("Length of this bag ", len(rgb_feature_bag))
#print(rgb_feature_feature_bag.squeeze())
np.savetxt(outputPath+videoName+'.txt', rgb_feature_bag.squeeze())
extractVideoFeatures()