├── .gitignore ├── LICENSE ├── MultiPatch.py ├── README.md ├── _config.yml ├── apperance_patch_generator.py ├── apperancefeatures.p ├── autoencoder.py ├── datasetgenerator.py ├── denoising_autoencoder.py ├── draw_square.py ├── estimateVote.py ├── imagestacker.py ├── imdsetgenimg.py ├── main.py ├── motionfeatures.py ├── mylib.py ├── sample_motion.py ├── sampledataset └── scripts ├── trainmotionfeat.py └── utils.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 rollermax 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MultiPatch.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torchvision.transforms as transforms 4 | 5 | class MultiPatch(nn.Module): 6 | def __init__(self, input_channels=3, patch_sizes=[15, 21, 25], num_classes=10): 7 | super(MultiPatchModel, self).__init__() 8 | 9 | # Patch extraction layers 10 | self.patch_extractors = nn.ModuleList([ 11 | nn.Sequential( 12 | nn.Unfold(kernel_size=patch_size, stride=patch_size), 13 | nn.Flatten(start_dim=1) 14 | ) for patch_size in patch_sizes 15 | ]) 16 | 17 | # Calculate the total feature dimension after patch extraction 18 | def calculate_patch_features(image_size, patch_sizes): 19 | patch_features = [] 20 | for patch_size in patch_sizes: 21 | patches_per_dim = image_size // patch_size 22 | patch_features.append(patches_per_dim * patches_per_dim) 23 | return patch_features 24 | 25 | # Assume a standard input image size (e.g., 224x224) 26 | image_size = 224 27 | patch_feature_counts = calculate_patch_features(image_size, patch_sizes) 28 | total_patch_features = sum( 29 | count * (patch_size * patch_size * input_channels) 30 | for count, patch_size in zip(patch_feature_counts, patch_sizes) 31 | ) 32 | 33 | # MLP for merging patches 34 | self.merger = nn.Sequential( 35 | nn.Linear(total_patch_features, 512), 36 | nn.ReLU(), 37 | nn.Dropout(0.3), 38 | nn.Linear(512, 256), 39 | nn.ReLU(), 40 | nn.Dropout(0.3), 41 | nn.Linear(256, num_classes) 42 | ) 43 | 44 | def forward(self, x): 45 | # Extract patches from different sizes 46 | patch_features = [ 47 | extractor(x) for extractor in self.patch_extractors 48 | ] 49 | 50 | # Concatenate all patch features 51 | merged_patches = torch.cat(patch_features, dim=1) 52 | 53 | # Pass through MLP 54 | output = self.merger(merged_patches) 55 | 56 | return output 57 | 58 | (* # Example usage 59 | def main(): 60 | # Create model instance 61 | model = MultiPatchModel( 62 | input_channels=3, # RGB images 63 | patch_sizes=[15, 21, 25], 64 | num_classes=10 # e.g., for CIFAR-10 65 | ) 66 | 67 | # Example input (batch_size, channels, height, width) 68 | x = torch.randn(32, 3, 224, 224) 69 | 70 | # Forward pass 71 | output = model(x) 72 | print(f"Output shape: {output.shape}") 73 | 74 | if __name__ == "__main__": 75 | main() *) 76 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Profile Views](https://komarev.com/ghpvc/?username=nabulago&color=green) 2 | ## Detecting anomalous events in videos by learning deep representations of appearance and motion 3 | 4 | An implementation of paper **Detecting anomalous events in videos by learning deep representations of appearance and motion** on python, opencv and tensorflow. This paper uses the _stacked denoising autoencoder_ for the the _feature_ training on the _appearance_ and _motion flow_ features as input for different window sizes and using _multiple SVM_ as a weighted single classifier. 5 | 6 | ### File details 7 | Most of the files contains the script and details in the files. Once scpit splices the imges of different size for appearance model: windows size - _15x15_, _18x18_, 20x20 8 | Denoising auto encoder file to train the model from the pickle file where you have created the dataset from the images. 9 | 10 | ##Added new file with include architecture basic in pytorch with new implementation called *main.py* file 11 | *windows size* - _15x15_, _17x17_, 21x21 12 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman 2 | title: [Anomolous event detection in video using learned features from the appearnce and motion modalities] 3 | description: [A simple implemenation of the paper using the python, OpenCV and TensorFlow] 4 | show_downloads: ["true"] 5 | google_analytics: [UA-112285598-1] 6 | -------------------------------------------------------------------------------- /apperance_patch_generator.py: -------------------------------------------------------------------------------- 1 | ## 2 | # To run from console 3 | # python appearance_path_generator.py -p /UCSD_Anomaly_Dataset.v1p2/UCSDped1/Train -dd Project/data 4 | # or 5 | # python --path /home/hduser/Desktop/Project/UCSD_Anomaly_Dataset.v1p2/UCSDped1/Train --datadst /home/hduser/Desktop 6 | 7 | # of if you are using the pycharm provide script arguments 8 | # in the ide menu Run--> Edit configurations --> Script paramters 9 | # Add these parameter values 10 | # --path your_path_to_dataset_trainfolder --datadst path_where_you_want_store_spliced_data 11 | __author__ = "Maruti Goswami" 12 | 13 | # Load images 14 | # Store in array 15 | # Load 10 images 16 | # Sliding window slices 17 | # for now the path is given from the 18 | 19 | 20 | 21 | import argparse # For parsing arguments 22 | import cv2 23 | import os, os.path 24 | import imutils 25 | import time 26 | import pickle 27 | import mylib 28 | 29 | # import tensorflow as tf 30 | # import autoencoder 31 | 32 | # # #################### # 33 | # # Flags definition # 34 | # # #################### # 35 | # flags = tf.app.flags 36 | # FLAGS = flags.FLAGS 37 | 38 | # # Global configuration 39 | # flags.DEFINE_string('model_name', '', 'Model name.') 40 | # flags.DEFINE_string('dataset', 'mnist', 'Which dataset to use. ["mnist", "cifar10"]') 41 | # flags.DEFINE_string('cifar_dir', '', 'Path to the cifar 10 dataset directory.') 42 | # flags.DEFINE_integer('seed', -1, 'Seed for the random generators (>= 0). Useful for testing hyperparameters.') 43 | # flags.DEFINE_boolean('restore_previous_model', False, 'If true, restore previous model corresponding to model name.') 44 | # flags.DEFINE_boolean('encode_train', False, 'Whether to encode and store the training set.') 45 | # flags.DEFINE_boolean('encode_valid', False, 'Whether to encode and store the validation set.') 46 | # flags.DEFINE_boolean('encode_test', False, 'Whether to encode and store the test set.') 47 | 48 | 49 | # # Stacked Denoising Autoencoder specific parameters 50 | # flags.DEFINE_integer('n_components', 256, 'Number of hidden units in the dae.') 51 | # flags.DEFINE_string('corr_type', 'none', 'Type of input corruption. ["none", "masking", "salt_and_pepper"]') 52 | # flags.DEFINE_float('corr_frac', 0., 'Fraction of the input to corrupt.') 53 | # flags.DEFINE_integer('xavier_init', 1, 'Value for the constant in xavier weights initialization.') 54 | # flags.DEFINE_string('enc_act_func', 'tanh', 'Activation function for the encoder. ["sigmoid", "tanh"]') 55 | # flags.DEFINE_string('dec_act_func', 'none', 'Activation function for the decoder. ["sigmoid", "tanh", "none"]') 56 | # flags.DEFINE_string('main_dir', 'dae/', 'Directory to store data relative to the algorithm.') 57 | # flags.DEFINE_string('loss_func', 'mean_squared', 'Loss function. ["mean_squared" or "cross_entropy"]') 58 | # flags.DEFINE_integer('verbose', 0, 'Level of verbosity. 0 - silent, 1 - print accuracy.') 59 | # flags.DEFINE_integer('weight_images', 0, 'Number of weight images to generate.') 60 | # flags.DEFINE_string('opt', 'gradient_descent', '["gradient_descent", "ada_grad", "momentum"]') 61 | # flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.') 62 | # flags.DEFINE_float('momentum', 0.5, 'Momentum parameter.') 63 | # flags.DEFINE_integer('num_epochs', 10, 'Number of epochs.') 64 | # flags.DEFINE_integer('batch_size', 10, 'Size of each mini-batch.') 65 | 66 | # assert FLAGS.dataset in ['mnist', 'cifar10'] 67 | # assert FLAGS.enc_act_func in ['sigmoid', 'tanh'] 68 | # assert FLAGS.dec_act_func in ['sigmoid', 'tanh', 'none'] 69 | # assert FLAGS.corr_type in ['masking', 'salt_and_pepper', 'none'] 70 | # assert 0. <= FLAGS.corr_frac <= 1. 71 | # assert FLAGS.loss_func in ['cross_entropy', 'mean_squared'] 72 | # assert FLAGS.opt in ['gradient_descent', 'ada_grad', 'momentum'] 73 | 74 | 75 | ap = argparse.ArgumentParser (description="Apperance and Motion DeepNet") 76 | ap.add_argument ("-i", "--image", required=False, help="Path to the image") 77 | ap.add_argument ("-p", "--path", required=False, help="Main directory of dataset") 78 | ap.add_argument ("-dd", "--datadst", help="Directory where you want to save dataset") 79 | args = vars (ap.parse_args ()) 80 | 81 | # (winW,winH) = (20,20) 82 | winSz = [15, 18, 20] # Thes are the window sizes for sliding window 83 | 84 | pyrCnt = 0 85 | imgCount = 0 86 | 87 | # for sz in winSz: 88 | # print "Window size: " + str(sz) 89 | print ("Start script") 90 | 91 | imageDir = args["path"] # specify your path here 92 | dataDest = args["datadst"] 93 | 94 | if args["path"] is None: 95 | imageDir = raw_input("Please enter the path to the dataset:") 96 | print ("Entered image dataset path:") 97 | print (imageDir) 98 | if not os.path.exists(str(imageDir)): 99 | print ("Directory doen't exists") 100 | 101 | print ("Source dataset Directory: " + str (imageDir)) 102 | 103 | 104 | 105 | if args["datadst"] is None: 106 | dataDest = raw_input("Please enter the path where you want to store the processed or spliced images:") 107 | print (dataDest) 108 | print ("Entered spliced or porcessed image dataset path:") 109 | if not os.path.exists(str(dataDest)): 110 | print ("Directory doen't exists") 111 | dataDest = str (dataDest) + '/appearance_spliced_images' 112 | mylib.createDirectory(dataDest) 113 | 114 | print ("Destination directory to store processed dataset: " + dataDest) 115 | 116 | 117 | image_path_list = [] 118 | 119 | print "start: " 120 | # create a list all files in directory and 121 | # append files with a valid extension to image_path_list 122 | folders = os.listdir(imageDir) 123 | print("Image directories : " + imageDir) 124 | print("Folders: " + str (sorted (folders))) 125 | folders.remove('.DS_Store') 126 | folders.remove('._.DS_Store') 127 | imageTmp = [] 128 | 129 | # fObject = open(dataDest+"/"+"imagelist.txt","a+") 130 | # f_main = open( dataDest+"/"+"image_15.txt","a+") 131 | fObject = open (dataDest + "/" + "all_imagelist.txt", 'w') 132 | f_main = open (dataDest + "/" + "all_image_15.txt", 'w') 133 | 134 | # fObject = open ("/home/hduser/Desktop/ped2_imagelist.txt", 'w') 135 | # f_main = open ("/home/hduser/Desktop/ped2_image_15.txt", 'w') 136 | 137 | appDatasetAll = open(os.path.join(dataDest,"apperance.p") ,'wb') 138 | appDataset15 = open(os.path.join(dataDest,"apperance15.p") ,'wb') 139 | appDataset18 = open(os.path.join(dataDest,"apperance18.p") ,'wb') 140 | appDataset20 = open(os.path.join(dataDest,"apperance20.p") ,'wb') 141 | if not imageDir.startswith ("._.D"): 142 | for folder in sorted(folders): 143 | if not folder.startswith ("."): 144 | mylib.createDirectory(os.path.join(dataDest,folder)) 145 | 146 | for folder in sorted (folders): 147 | print ("folder : " + folder) 148 | if not folder.startswith ("."): 149 | print ("Folder : " + str (folder)) 150 | # print "List of files: " + os.listdir(imageDir+"/"+str(folder)) 151 | print ("Fall: " + imageDir + "/" + str (folder)) 152 | for file in sorted(os.listdir (os.path.join(imageDir,folder))): 153 | # print "File: " +file 154 | # print file 155 | 156 | if not mylib.checkValidExtension(file): 157 | print("Please provide file with proper extensions") 158 | continue 159 | 160 | tpath = str (imageDir) + "/" + str (folder) + "/" + file 161 | 162 | 163 | # loop through image_path_list to open each image 164 | for imagePath in image_path_list: 165 | print imagePath 166 | image = cv2.imread (tpath, 0) 167 | 168 | # display the image on screen with imshow() 169 | # after checking that it loaded 170 | if image is not None: 171 | 172 | for sz in winSz: 173 | 174 | # mylib.createDirectory(os.path.join(dataDest, folder)) 175 | mylib.createDirectory(os.path.join(dataDest, folder, str(sz))) 176 | if not sz == 15: 177 | mylib.createDirectory (dataDest + "/" + folder + "/" + str (sz)) 178 | print ("In the loop of " + str (sz)) 179 | 180 | tz = sz 181 | 182 | if sz == 15: 183 | resized = cv2.resize (image, (240, 165), interpolation=cv2.INTER_LINEAR) 184 | # resized = resizeToMainWindowSize(image,240, 165) 185 | elif sz == 18: 186 | resized = cv2.resize (image, (252, 162), interpolation=cv2.INTER_LINEAR) 187 | # resized = resizeToMainWindowSize(image,252, 162) 188 | elif sz == 20: 189 | resized = cv2.resize (image, (240, 160), interpolation=cv2.INTER_LINEAR) 190 | # resized = resizeToMainWindowSize(image,240,160) 191 | 192 | for (x, y, window) in mylib.sliding_window (resized, stepSize=15, windowSize=(tz, tz)): 193 | 194 | # if the window does not meet our desired window size, ignore it 195 | # if window.shape[0] != winH or window.shape[1] != winW: 196 | if window.shape[0] != sz or window.shape[1] != sz: 197 | continue 198 | clone = resized.copy () 199 | # cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2) 200 | cv2.rectangle (clone, (x, y), (x + sz, y + sz), (0, 255, 0), 1) 201 | 202 | #cv2.imshow ("Image", image) 203 | #cv2.imshow ("Clone", clone) 204 | #cv2.imshow ("window", window) 205 | 206 | if sz == 15: 207 | tmpFileName = str(imgCount)+ "_"+ str(sz) + "_" + file.split ('.')[0] + ".jpeg" 208 | nmi = os.path.join(folder,str(sz),tmpFileName) 209 | print (nmi) 210 | nm1 = os.path.join(dataDest,nmi) 211 | fObject.writelines (str(nmi)+ "\n") 212 | f_main.writelines (str(nmi)+ "\n") 213 | cv2.imwrite (nm1, window) 214 | # Flatten the image 215 | windowflat = window.flatten() 216 | 217 | # normalize the images 218 | windowflat = cv2.normalize(windowflat.astype(float),windowflat.astype(float), alpha=0,beta=1,norm_type=cv2.NORM_MINMAX) 219 | print(windowflat) 220 | pickle.dump(windowflat,appDatasetAll) 221 | pickle.dump (windowflat, appDataset15) 222 | 223 | imgCount = imgCount + 1 224 | 225 | if sz == 18: 226 | print("In loop of 18") 227 | # nm1 = dataDest+"/"+ folder+ "/" + str(sz)+"/" + str(imgCount)+"_"+str(sz)+"_"+file.split('.')[0]+".jpeg" 228 | nm2 = dataDest + "/" + folder + "/" + str (sz) + "/" + str ( 229 | imgCount) + "_" + str (sz) + "_" + file.split ('.')[0] + ".jpeg" 230 | print (nm2) 231 | fObject.writelines ( 232 | folder + "/" + str (sz) + "/" + str (imgCount) + "_" + str (sz) + "_" + 233 | file.split ('.')[0] + ".jpeg" + "\n") 234 | f_main.writelines ( 235 | folder + "/" + str (sz) + "/" + str (imgCount) + "_" + str (sz) +"_"+ 236 | file.split ('.')[0] + ".jpeg" + "\n") 237 | 238 | tlp1 = str (sz) + "x" + str (sz) + " Resized Window Frame" 239 | resWin = cv2.resize (window, (15, 15), interpolation=cv2.INTER_LINEAR) 240 | #cv2.imshow (tlp1, resWin) 241 | cv2.imwrite (nm2, resWin) 242 | 243 | # Flatten the image 244 | windowflat = resWin.flatten() 245 | 246 | # normalize the images 247 | windowflat = cv2.normalize (windowflat.astype (float), windowflat.astype (float), 248 | alpha=0, beta=1, norm_type=cv2.NORM_MINMAX) 249 | print(windowflat) 250 | pickle.dump (windowflat, appDatasetAll) 251 | pickle.dump (windowflat, appDataset18) 252 | 253 | 254 | imgCount = imgCount + 1 255 | 256 | if sz == 20: 257 | # print "In loop of 20" 258 | # nm1 = dataDest+"/"+ folder+ "/" + str(sz)+"/" + str(imgCount)+"_"+str(sz)+"_"+file.split('.')[0]+".jpeg" 259 | nm2 = dataDest + "/" + folder + "/" + str (sz) + "/" + str ( 260 | imgCount) + "_" + str (sz) + "_15_" + file.split ('.')[0] + ".jpeg" 261 | 262 | fObject.writelines ( 263 | folder + "/" + str (sz) + "/" + str (imgCount) + "_" + str (sz) + "_" + 264 | file.split ('.')[0] + ".jpeg" + "\n") 265 | f_main.writelines ( 266 | folder + "/" + str (sz) + "/" + str (imgCount) + "_" + str (sz) + "_" + 267 | file.split ('.')[0] + ".jpeg" + "\n") 268 | # print nm1 269 | # print nm2 270 | tlp = str (sz) + "x" + str (sz) + " Window" 271 | cv2.imshow (tlp, window) 272 | tlp1 = str (sz) + "x" + str (sz) + " Resized Window Frame" 273 | #cv2.imshow (tlp1, resWin) 274 | # cv2.imwrite(nm1,window) 275 | resWin = cv2.resize (window, (15, 15), interpolation=cv2.INTER_LINEAR) 276 | cv2.imwrite (nm2, resWin) 277 | 278 | # Flatten the image 279 | windowflat = resWin.flatten () 280 | 281 | # normalize the images 282 | windowflat = cv2.normalize (windowflat.astype (float), windowflat.astype (float), 283 | alpha=0, beta=1, norm_type=cv2.NORM_MINMAX) 284 | pickle.dump (windowflat, appDatasetAll) 285 | pickle.dump (windowflat, appDataset20) 286 | 287 | imgCount = imgCount + 1 288 | 289 | 290 | # dae= autoencoder.DenoisingAutoencoder(model_name='dae', n_components=256, enc_act_func='tanh',dec_act_func='none', loss_func='mean_squared', num_epochs=10, batch_size=10,xavier_init=1, opt='gradient_descent', learning_rate=0.01, momentum=0.9, corr_type='none', corr_frac=0., verbose=1, seed=-1) 291 | 292 | # Now we make a matrix for each image for now only one image 293 | # stacking with different windows sizes 294 | # For more complexity we can use color image but it will add more data depend on the channles we will be able to estimate 295 | 296 | # Optical flow images 297 | 298 | # Combine the images for all the window sizes 299 | 300 | 301 | # print "Image: " + str(imgCount) 302 | # Here we will pre-process the image and corrupt it 303 | 304 | # DAE section 305 | # cv2.imshow("Patch",image[x:(x+winW),y:(y+winH)]) 306 | mylib.exitScript() 307 | #time.sleep (0.025) 308 | # cv2.imshow(imagePath, image) 309 | # pyrCnt = 0 310 | # pyrCnt = pyrCnt + 1 311 | # pyrCnt = 0 312 | imgCount = 0 313 | elif image is None: 314 | print ("Error loading: " + imagePath) 315 | # Segmentation of file ends here 316 | print "End of File : " + str (file) 317 | 318 | mylib.exitScript () 319 | # cv2.destroyAllWindows() 320 | print "End of Folder : " + str (folder) 321 | print "Press q or esc to exit" 322 | mylib.exitScript () 323 | fObject.close () 324 | f_main.close () 325 | appDatasetAll.close() 326 | appDataset15.close() 327 | appDataset18.close() 328 | appDataset20.close() 329 | print("End of script") 330 | print("Arguments: " + str (args)) 331 | -------------------------------------------------------------------------------- /apperancefeatures.p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nabulago/anomaly-event-detection/34316265202884301726a41fe591373141e563b3/apperancefeatures.p -------------------------------------------------------------------------------- /autoencoder.py: -------------------------------------------------------------------------------- 1 | """ Auto Encoder Example. 2 | Build a 2 layers auto-encoder with TensorFlow to compress images to a 3 | lower latent space and then reconstruct them. 4 | References: 5 | Y. LeCun, L. Bottou, Y. Bengio, and P. Haffner. "Gradient-based 6 | learning applied to document recognition." Proceedings of the IEEE, 7 | 86(11):2278-2324, November 1998. 8 | Links: 9 | [MNIST Dataset] http://yann.lecun.com/exdb/mnist/ 10 | Author: Aymeric Damien 11 | Project: https://github.com/aymericdamien/TensorFlow-Examples/ 12 | """ 13 | from __future__ import division, print_function, absolute_import 14 | from sklearn.cross_validation import train_test_split 15 | import tensorflow as tf 16 | import numpy as np 17 | import matplotlib.pyplot as plt 18 | import pickle 19 | import os,os.path 20 | # Import MNIST data 21 | # from tensorflow.examples.tutorials.mnist import input_data 22 | # mnist = input_data.read_data_sets("/tmp/data/", one_hot=True) 23 | pathToDataset ="path_to_data_folder/data" 24 | dataSetList = ['motionfeatures.p'] 25 | ptdt = 'path_to_data_folder/data' 26 | print(ptdt) 27 | print(dataSetList) 28 | print( pathToDataset+dataSetList[0]) 29 | if os.path.isfile(str(pathToDataset)+str(dataSetList[0])): 30 | print (str(pathToDataset)+str(dataSetList[0])) 31 | mydataset = open(str(pathToDataset)+str(dataSetList[0]),'rb') 32 | lds= pickle.load(mydataset) 33 | print (lds) 34 | print (lds.shape) 35 | # train, test = train_test_split() 36 | print (np.round(len(lds[1])*0.3).astype('uint8')) 37 | print (np.round(len(lds[1])*0.8).astype('uint8')-np.round(len(lds[1])*0.5).astype('uint8')) 38 | train = lds[:,0:35] 39 | test = lds[:,35:50] 40 | print(train.shape) 41 | print(test.shape) 42 | print(test) 43 | # Training Parameters 44 | learning_rate = 0.01 45 | num_steps = 3000 46 | #batch_size = 256 47 | batch_size = 1 48 | display_step = 1000 49 | examples_to_show = 10 50 | 51 | # # Network Parameters 52 | num_hidden_1 = 256 # 1st layer num features 53 | num_hidden_2 = 128 # 2nd layer num features (the latent dim) 54 | #num_hidden_1 = 1024 # 1st layer num features 55 | #num_hidden_2 = 512 # 2nd layer num features (the latent dim) 56 | #num_hidden_3 = 256 57 | #num_hidden_4 = 128 58 | num_input = 225 # MNIST data input (img shape: 15*15) 59 | 60 | # tf Graph input (only pictures) 61 | X = tf.placeholder("float", [None, num_input]) 62 | 63 | weights = { 64 | 'encoder_h1': tf.Variable(tf.random_normal([num_input, num_hidden_1])), 65 | 'encoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_hidden_2])), 66 | # 'encoder_h3': tf.Variable(tf.random_normal([num_hideen_2, num_hidden_3])), 67 | # 'encoder_h4': tf.Variable(tf.random_normal([num_hidden_3, num_hidden_4])), 68 | 69 | # 'decoder_h4': tf.Variable(tf.random_normal([num_hidden_4, num_hidden_3])), 70 | # 'decoder_h3': tf.Variable(tf.random_normal([num_hidden_3, num_hidden_2])), 71 | 'decoder_h1': tf.Variable(tf.random_normal([num_hidden_2, num_hidden_1])), 72 | 'decoder_h2': tf.Variable(tf.random_normal([num_hidden_1, num_input])), 73 | 74 | } 75 | biases = { 76 | 'encoder_b1': tf.Variable(tf.random_normal([num_hidden_1])), 77 | 'encoder_b2': tf.Variable(tf.random_normal([num_hidden_2])), 78 | # 'encoder_b3': tf.Variable(tf.random_normal([num_hidden_3])), 79 | # 'encoder_b4': tf.Variable(tf.random_normal([num_hidden_4])), 80 | 81 | 'decoder_b1': tf.Variable(tf.random_normal([num_hidden_1])), 82 | 'decoder_b2': tf.Variable(tf.random_normal([num_input])), 83 | } 84 | 85 | # Building the encoder 86 | def encoder(x): 87 | # Encoder Hidden layer with sigmoid activation #1 88 | layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']), 89 | biases['encoder_b1'])) 90 | # Encoder Hidden layer with sigmoid activation #2 91 | layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']), 92 | biases['encoder_b2'])) 93 | 94 | return layer_2 95 | 96 | 97 | # Building the decoder 98 | def decoder(x): 99 | # Decoder Hidden layer with sigmoid activation #1 100 | layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']), 101 | biases['decoder_b1'])) 102 | # Decoder Hidden layer with sigmoid activation #2 103 | layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']), 104 | biases['decoder_b2'])) 105 | return layer_2 106 | 107 | # Construct model 108 | encoder_op = encoder(X) 109 | decoder_op = decoder(encoder_op) 110 | 111 | # Prediction 112 | y_pred = decoder_op 113 | print(y_pred) 114 | # Targets (Labels) are the input data. 115 | y_true = X 116 | 117 | # Define loss and optimizer, minimize the squared error 118 | loss = tf.reduce_mean(tf.pow(y_true - y_pred, 2)) 119 | optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(loss) 120 | 121 | # Initialize the variables (i.e. assign their default value) 122 | init = tf.global_variables_initializer() 123 | 124 | # Start Training 125 | # Start a new TF session 126 | with tf.Session() as sess: 127 | 128 | # Run the initializer 129 | sess.run(init) 130 | 131 | # Training 132 | for i in range(1, num_steps+1): 133 | # Prepare Data 134 | # Get the next batch of MNIST data (only images are needed, not labels) 135 | #batch_x, _ = mnist.train.next_batch (batch_size) 136 | batch_x = train.T 137 | # Run optimization op (backprop) and cost op (to get loss value) 138 | _, l = sess.run([optimizer, loss], feed_dict={X: batch_x}) 139 | # Display logs per step 140 | if i % display_step == 0 or i == 1: 141 | print('Step %i: Minibatch Loss: %f' % (i, l)) 142 | 143 | # Testing 144 | # Encode and decode images from test set and visualize their reconstruction. 145 | n = 4 146 | canvas_orig = np.empty((15 * n, 15 * n)) 147 | canvas_recon = np.empty((15 * n, 15 * n)) 148 | for i in range(n): 149 | # MNIST test set 150 | #batch_x, _ = mnist.test.next_batch(n) 151 | batch_x = test.T 152 | # Encode and decode the digit image 153 | g = sess.run(decoder_op, feed_dict={X: batch_x}) 154 | 155 | # Display original images 156 | for j in range(n): 157 | # Draw the original digits 158 | canvas_orig[i * 15:(i + 1) * 15, j * 15:(j + 1) * 15] = \ 159 | batch_x[j].reshape([15, 15]) 160 | # Display reconstructed images 161 | for j in range(n): 162 | # Draw the reconstructed digits 163 | canvas_recon[i * 15:(i + 1) * 15, j * 15:(j + 1) * 15] = \ 164 | g[j].reshape([15, 15]) 165 | 166 | print("Original Images") 167 | plt.figure(figsize=(n, n)) 168 | plt.imshow(canvas_orig, origin="upper", cmap="gray") 169 | plt.show() 170 | 171 | print("Reconstructed Images") 172 | plt.figure(figsize=(n, n)) 173 | plt.imshow(canvas_recon, origin="upper", cmap="gray") 174 | plt.show() 175 | -------------------------------------------------------------------------------- /datasetgenerator.py: -------------------------------------------------------------------------------- 1 | ''' 2 | flatten image, normalize, stack and add to dataset 3 | ''' 4 | import cv2 5 | import numpy as np 6 | from os.path import isfile, join 7 | from os import listdir 8 | from random import shuffle 9 | from shutil import copyfile 10 | import os 11 | import pickle 12 | import cv2 13 | datasetPath = '/apperance' 14 | # trainFolders = ['Train001','Train002'] 15 | 16 | # image_15.txt has the filepaths of all the apperance windows 17 | # of different scales 18 | 19 | fileData = open('./apperance/image_15.txt','r').read().split('\n') 20 | fileData = sorted(fileData) 21 | #print len(fileData) 22 | fileData = list(set(fileData)) 23 | print len(fileData) 24 | i = 0 25 | # while i<5: 26 | # print fileData[i] 27 | # sampleOne = fileData[i] 28 | # i = i +1 29 | #print "out" 30 | # shuffle(fileData) # Don't shuffle the data if you want to consecutive samples 31 | #print "data shuffled" 32 | # i = 0 33 | images = np.ones((225,),dtype='uint8') 34 | print type(images) 35 | print fileData[10] 36 | 37 | for i,flD in enumerate(fileData): 38 | print "File data : " + str(i) +str(fileData[i]) 39 | if i==20: 40 | break 41 | 42 | with open("apperance_main.pkl",'wb') as imageDataset: 43 | for flD in fileData: 44 | if i%100 == 0: 45 | print "====================" 46 | print "Completed 100 images" 47 | print "====================" 48 | print "Total images saved : " + str(i) 49 | if i%1000 == 0: 50 | print "====================" 51 | print "Completed 1000 images" 52 | print "====================" 53 | print "Total images saved : " + str(i) 54 | 55 | if i > 20000: 56 | print "====================" 57 | print "Completed 20000 files" 58 | print "====================" 59 | break 60 | # print "Second loop" 61 | print flD 62 | # print fileData 63 | # print "File data : " +str(fileData[i]) 64 | # sampleOne = fileData[i] 65 | # fileShow = datasetPath+"/"+sampleOne 66 | fileShow = datasetPath+"/"+str(fileData[i]) 67 | # print fileShow 68 | image = cv2.imread(fileShow,0) 69 | # if not image.any(): continue 70 | # print image.shape 71 | image = cv2.resize(image, (15,15),0) 72 | #cv2.imshow("image",image) 73 | image.shape 74 | # print "image shape" + str(image.flatten().shape) 75 | # print type(image) 76 | image = image.flatten() 77 | #cv2.imshow("images",images) 78 | image = cv2.normalize((image).astype('float'), None, 0, 1, cv2.NORM_MINMAX) 79 | #print image 80 | # print "After normalization" +str(image.shape) 81 | images = np.column_stack((images,image)) 82 | 83 | #print type(images) 84 | #print images.shape 85 | #print images 86 | # print images.shape 87 | # k = cv2.waitKey(1) & 0xff 88 | # if k == 27: 89 | # pickle.dump(image,imageDataset,-1) 90 | # imageDataset.close() 91 | # if not imageDataset.closed: 92 | # imageDataset.close() 93 | # cv2.destoyAllWindows() 94 | # break 95 | # elif k == ord('q'): 96 | # pickle.dump(image,imageDataset,-1) 97 | # if not imageDataset.closed: 98 | # imageDataset.close() 99 | # cv2.destoyAllWindows() 100 | # break 101 | 102 | # k = cv2.waitKey(0) & 0xff 103 | # if k == 27: 104 | # cv2.destoyAllWindows() 105 | # break 106 | # elif k == ord('q'): 107 | # cv2.destoyAllWindows() 108 | # break 109 | i = i +1 110 | pickle.dump(image,imageDataset,-1) 111 | # if not imageDataset.closed: 112 | # pickle.dump(images,imageDataset,-1) 113 | # imageDataset.close() 114 | if not imageDataset.closed: 115 | imageDataset.close() 116 | #imd = open('motionfeatures.p','rb') 117 | #imdataset = pickle.load(imd) 118 | -------------------------------------------------------------------------------- /denoising_autoencoder.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import math 3 | import pickle 4 | import numpy as np 5 | import tensorflow as tf 6 | from utils import corrupt 7 | # from libs.utils import corrupt 8 | 9 | listOfDatasets = ['apperance_features_train.p','apperance_features_test.p','motion_features_train.p','motion_features_original_test.p'] 10 | 11 | # Provide data-set path here 12 | datasetPath = 'apperancefeatures.p' 13 | #datasetPath = 'apperancedataset.p' 14 | 15 | opendataset = open(datasetPath,'r') 16 | dataset = pickle.load(opendataset) 17 | opendataset.close() 18 | 19 | # opendataset = open(datasetPath,'r') 20 | # dataset = pickle.load(opendataset) 21 | # opendataset.close() 22 | 23 | 24 | 25 | if sys.version_info.major == 3: 26 | print dataset[:, 0:500].shape 27 | print dataset[:, 501:700].shape 28 | else: 29 | print (dataset[:, 0:500].shape) 30 | print (dataset[:, 501:700].shape) 31 | 32 | 33 | # %% 34 | #def autoencoder(dimensions=[784, 512, 256, 64]): 35 | def autoencoder(dimensions=[225, 1024, 512, 256, 64]): 36 | 37 | """Build a deep denoising autoencoder w/ tied weights. 38 | 39 | Parameters 40 | ---------- 41 | dimensions : list, optional 42 | The number of neurons for each layer of the autoencoder. 43 | 44 | Returns 45 | ------- 46 | x : Tensor 47 | Input placeholder to the network 48 | z : Tensor 49 | Inner-most latent representation 50 | y : Tensor 51 | Output reconstruction of the input 52 | cost : Tensor 53 | Overall cost to use for training 54 | """ 55 | # input to the network 56 | x = tf.placeholder(tf.float32, [None, dimensions[0]], name='x') 57 | print(tf.shape(x)) 58 | # Probability that we will corrupt input. 59 | # This is the essence of the denoising autoencoder, and is pretty 60 | # basic. We'll feed forward a noisy input, allowing our network 61 | # to generalize better, possibly, to occlusions of what we're 62 | # really interested in. But to measure accuracy, we'll still 63 | # enforce a training signal which measures the original image's 64 | # reconstruction cost. 65 | # 66 | # We'll change this to 1 during training 67 | # but when we're ready for testing/production ready environments, 68 | # we'll put it back to 0. 69 | corrupt_prob = tf.placeholder(tf.float32, [1]) 70 | current_input = corrupt(x) * corrupt_prob + x * (1 - corrupt_prob) 71 | 72 | # Build the encoder 73 | encoder = [] 74 | for layer_i, n_output in enumerate(dimensions[1:]): 75 | print("Layer : " + str(layer_i)) 76 | n_input = int(current_input.get_shape()[1]) 77 | W = tf.Variable( 78 | tf.random_uniform([n_input, n_output], 79 | -1.0 / math.sqrt(n_input), 80 | 1.0 / math.sqrt(n_input))) 81 | b = tf.Variable(tf.zeros([n_output])) 82 | encoder.append(W) 83 | # output = tf.nn.tanh(tf.matmul(current_input, W) + b) 84 | output = tf.nn.sigmoid(tf.matmul(current_input, W) + b) 85 | current_input = output 86 | print(output) 87 | # latent representation 88 | z = current_input 89 | # Here use the classifier for the latent representaion 90 | 91 | encoder.reverse() 92 | # Build the decoder using the same weights 93 | for layer_i, n_output in enumerate(dimensions[:-1][::-1]): 94 | W = tf.transpose(encoder[layer_i]) 95 | b = tf.Variable(tf.zeros([n_output])) 96 | # output = tf.nn.tanh(tf.matmul(current_input, W) + b) 97 | output = tf.nn.sigmoid(tf.matmul(current_input, W) + b) 98 | current_input = output 99 | # now have the reconstruction through the network 100 | y = current_input 101 | # cost function measures pixel-wise difference 102 | print(x.shape) 103 | print(tf.shape(x)) 104 | print(y.shape) 105 | print tf.shape(y) 106 | 107 | # cost = - tf.add(tf.matmul(tf.transpose(x), tf.log(y)), tf.matmul(tf.transpose(1-x), tf.log(1-y))) 108 | cost = - tf.add(tf.matmul(x,tf.transpose(tf.log(y))), tf.matmul(1-x, tf.transpose( tf.log(1-y) ) )) 109 | # cost = tf.sqrt(tf.reduce_mean(tf.square(y - x))) 110 | return {'x': x, 'z': z, 'y': y, 111 | 'corrupt_prob': corrupt_prob, 112 | 'cost': cost} 113 | 114 | # %% 115 | 116 | 117 | def test_dataset(): 118 | import tensorflow as tf 119 | import matplotlib.pyplot as plt 120 | 121 | # %% 122 | # load Dataset 123 | 124 | dataset = dataset # Here we will set out dataset 125 | mean_img = np.mean(dataset) 126 | dataset_train, dataset_test = dataset[:,0:35], dataset[:,36:51] 127 | print "Train slice of dataset" + str(dataset_train.shape) 128 | print "Test slice of dataset" + str(dataset_test.shape) 129 | mean_img = np.mean(dataset_train, axis=1) 130 | print "Mean Image : "+str(mean_img.shape) 131 | ae = autoencoder(dimensions=[225, 1024, 512, 256, 64]) 132 | 133 | # %% 134 | learning_rate = 0.001 135 | # optimizer = tf.train.AdamOptimizer(learning_rate).minimize(ae['cost']) 136 | optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(ae['cost']) 137 | 138 | # %% 139 | # We create a session to use the graph 140 | sess = tf.Session() 141 | sess.run(tf.global_variables_initializer()) 142 | 143 | # %% 144 | # Fit all training data 145 | batch_size = 2 146 | # batch_size = 50 147 | n_epochs = 10 148 | for epoch_i in range(n_epochs): 149 | # print dataset_train.shape[1] // batch_size 150 | for batch_i in range(dataset_train.shape[1] // batch_size): 151 | # for batch_i in range(dataset.train.num_examples // batch_size): 152 | # batch_xs, _ = dataset.train.next_batch(batch_size) 153 | print batch_i 154 | batch_xs = dataset_train[:,batch_i:batch_i + batch_size] 155 | print "Batch_Xs shape "+str(batch_xs.shape) 156 | print "Mean Image " + str(mean_img.shape) 157 | 158 | 159 | for img in batch_xs.T: 160 | print "Image shape : " + str(img.shape) 161 | print "Mean Shape: " + str(mean_img.T.shape) 162 | 163 | train = np.array([img.T - mean_img for img in batch_xs.T]) 164 | sess.run(optimizer, feed_dict={ 165 | ae['x']: train, ae['corrupt_prob']: [1.0]}) 166 | print(epoch_i, sess.run(ae['cost'], feed_dict={ 167 | ae['x']: train, ae['corrupt_prob']: [1.0]})) 168 | 169 | # %% 170 | # Plot example reconstructions 171 | n_examples = 15 172 | # test_xs, _ = dataset.test.next_batch(n_examples) 173 | # test_xs = dataset_test.T[batch_i:batch_i + batch_size, :] 174 | test_xs = dataset_test.T 175 | print "Testxs : " +str(test_xs.shape) 176 | test_xs_norm = np.array([img - mean_img for img in test_xs]) 177 | print "Test xs Norm : " + str(test_xs_norm.shape) 178 | 179 | recon = sess.run(ae['y'], feed_dict={ 180 | ae['x']: test_xs_norm, ae['corrupt_prob']: [0.0]}) 181 | print "Reconstruction shape: " + str(recon.shape) 182 | print "Reconstruction Complete" 183 | 184 | fig, axs = plt.subplots(2, n_examples, figsize=(10, 2)) 185 | 186 | for example_i in range(n_examples): 187 | axs[0][example_i].imshow( 188 | # np.reshape(test_xs[example_i, :], (28, 28))) 189 | np.reshape(test_xs[example_i, :], (15, 15))) 190 | axs[1][example_i].imshow( 191 | # np.reshape([recon[example_i, :] + mean_img], (28, 28))) 192 | np.reshape([recon[example_i, :] + mean_img], (15, 15))) 193 | print 'Plot complete now showing...' 194 | fig.show() 195 | plt.draw() 196 | plt.title("1st function - dataset ones but used our dataset") 197 | plt.waitforbuttonpress() 198 | 199 | 200 | def train_appearance_features(): 201 | import tensorflow as tf 202 | import matplotlib.pyplot as plt 203 | 204 | # %% 205 | # load Dataset 206 | 207 | appearance_dataset = dataset # Here we will set out dataset 208 | mean_img = np.mean(appearance_dataset) 209 | appearance_train, appearance_test = dataset[:,0:35], dataset[:,36:51] 210 | print appearance_train.shape 211 | print appearance_test.shape 212 | # mean_img = np.mean(dataset.train.images, axis=0) 213 | ae = autoencoder(dimensions=[225, 1024, 512, 256, 64]) 214 | 215 | # %% 216 | learning_rate = 0.001 217 | # optimizer = tf.train.AdamOptimizer(learning_rate).minimize(ae['cost']) 218 | optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(ae['cost']) 219 | 220 | # %% 221 | # We create a session to use the graph 222 | sess = tf.Session() 223 | sess.run(tf.global_variables_initializer()) 224 | 225 | # %% 226 | # Fit all training data 227 | batch_size = 6 228 | # batch_size = 50 229 | n_epochs = 2 230 | for epoch_i in range(n_epochs): 231 | # print dataset_train.shape[1] // batch_size 232 | for batch_i in range(appearance_train.shape[1] // batch_size): 233 | batch_xs = appearance_train.T[batch_i:batch_i + batch_size,:] 234 | train = np.array([img - mean_img for img in batch_xs]) 235 | sess.run(optimizer, feed_dict={ 236 | ae['x']: train, ae['corrupt_prob']: [1.0]}) 237 | print(epoch_i, sess.run(ae['cost'], feed_dict={ 238 | ae['x']: train, ae['corrupt_prob']: [1.0]})) 239 | 240 | # %% 241 | # Plot example reconstructions 242 | n_examples = 15 243 | # test_xs, _ = dataset.test.next_batch(n_examples) 244 | for batch_i in range(appearance_train.shape[1]//batch_size): 245 | print batch_i, appearance_train.shape[1],batch_size 246 | test_xs = appearance_test.T[batch_i:batch_i+batch_size,:] 247 | test_xs_norm = np.array([img - mean_img for img in test_xs]) 248 | recon = sess.run(ae['y'], feed_dict={ 249 | ae['x']: test_xs_norm, ae['corrupt_prob']: [0.0]}) 250 | fig, axs = plt.subplots(2, n_examples, figsize=(10, 2)) 251 | for example_i in range(n_examples): 252 | axs[0][example_i].imshow( 253 | # np.reshape(test_xs[example_i, :], (28, 28))) 254 | np.reshape(test_xs[example_i, :], (15, 15))) 255 | axs[1][example_i].imshow( 256 | np.reshape([recon[example_i, :] + mean_img], (15, 15))) 257 | fig.show() 258 | plt.draw() 259 | plt.title('Appearance features') 260 | plt.waitforbuttonpress() 261 | 262 | def train_motion_features(): 263 | pass 264 | 265 | def train_joint_features(): 266 | # type: () -> object 267 | pass 268 | 269 | if __name__ == '__main__': 270 | test_dataset() 271 | # train_appearance_features() 272 | # train_motion_features() 273 | # train_joint_features() 274 | -------------------------------------------------------------------------------- /draw_square.py: -------------------------------------------------------------------------------- 1 | ''' 2 | This file has the code for drawing the ROI 3 | It will draw the ROI as transparent window with border 4 | use the function drawROI if you want to use elsewhere 5 | or copy the function as it is. 6 | Here is the function with the sample code give 7 | which will draw two ROI going in different direction 8 | on draw no funcitonality added. 9 | ''' 10 | 11 | 12 | import cv2 13 | import numpy as np 14 | 15 | def drawROI(img, x, y, alp, winsz, color): 16 | ''' 17 | It will draw the ROI as transparent window with border 18 | Args: 19 | img : Image which you want to draw the ROI 20 | x, y : x and y starting coordinates for the ROI 21 | alp : Alpha or transparency value between 0.0 and 1.0 22 | winsz : Windows size of the ROI winsz x winsz single value 23 | color : Same color for the border and the filled color in square 24 | 25 | Returns: 26 | Retuns the processed image 27 | image = drawROI() 28 | ''' 29 | ovly = img.copy() 30 | out = img.copy() 31 | 32 | cv2.rectangle(ovly, (x, y), (x+winsz, y+winsz), (color), -1) 33 | cv2.rectangle(ovly, (x, y), (x+winsz, y+winsz), (color), 2) 34 | cv2.addWeighted(ovly, alp, out, 1 - alp, 0,out) 35 | 36 | return out 37 | 38 | image = cv2.imread('sampleimg.tif',0) 39 | image1 = cv2.imread('sampleimg.tif',1) 40 | print(image1.shape) 41 | h,w = 158,238 42 | wSz = 20 43 | x, y = 0,0 44 | overlay1 = np.ones((15,15,3))*[0,255,0] 45 | # while (1): 46 | for j in range(0,h,wSz): 47 | # myy = j+wSz 48 | # print(myy) 49 | for i in range(0,w,wSz): 50 | # myx = i +wSz 51 | # print(myx) 52 | imgs = drawROI(image1,i+wSz,j+wSz,0.6,wSz,(255,0,0)) 53 | imgs = drawROI(imgs,j+wSz,i+wSz,0.6,wSz,(0,0,255)) 54 | cv2.imshow('Images',imgs) 55 | k = cv2.waitKey(10) & 0xFF 56 | if k == 27: 57 | cv2.destroyAllWindows() 58 | break 59 | elif k == ord('q'): 60 | cv2.destroyAllWindows() 61 | break 62 | 63 | 64 | # while(1): 65 | # alpha = 0.2 66 | # overlay = image1.copy() 67 | # cv2.imshow('Over1',overlay1) 68 | # output = image1.copy() 69 | # cv2.rectangle(overlay, (x, y), (x+wSz, y+wSz),(0, 0, 255), -1) 70 | # cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0,output) 71 | 72 | # cv2.imshow('Color',image1) 73 | # cv2.imshow('Overlay',overlay) 74 | # cv2.imshow('Output',output) 75 | 76 | 77 | 78 | # cutout = image1[x:x+wSz,y:y+wSz] 79 | # cv2.imshow('Cutout',cutout) # Overlay image 80 | # # cv2.addWeighted(overlay1, alpha, cutout, 1 - alpha, 0,cutout) 81 | 82 | # x = x + 15 83 | # y = y + 15 84 | -------------------------------------------------------------------------------- /estimateVote.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Classifiers 3 | Here the code will be used to define the classifier 4 | ''' 5 | import numpy as np 6 | import cv2 7 | import tensorflow as tf 8 | import os 9 | import sklearn 10 | from sklearn import cross_validation, grid_search 11 | from sklearn.metrics import confusion_matrix, classification_report 12 | from sklearn.svm import SVC 13 | from sklearn.externals import joblib 14 | 15 | # Apperance, Motion, Joint features 16 | # cfTup = [a,b,c] # For single sample 17 | # 18 | # cfTup = [[],[],[]] # For multiple samples 19 | 20 | def estimateClassifierValues(cfTup): 21 | 22 | yield [weightedAlpha,weightedScore] 23 | 24 | def train_svm_classifer(features, labels, model_output_path): 25 | """ 26 | train_svm_classifer will train a SVM, saved the trained and SVM model and 27 | report the classification performance 28 | 29 | features: array of input features 30 | labels: array of labels associated with the input features 31 | model_output_path: path for storing the trained svm model 32 | """ 33 | # save 20% of data for performance evaluation 34 | X_train, X_test, y_train, y_test = cross_validation.train_test_split(features, labels, test_size=0.2) 35 | 36 | param = [ 37 | { 38 | "kernel": ["linear"], 39 | "C": [1, 10, 100, 1000] 40 | }, 41 | { 42 | "kernel": ["rbf"], 43 | "C": [1, 10, 100, 1000], 44 | "gamma": [1e-2, 1e-3, 1e-4, 1e-5] 45 | } 46 | ] 47 | 48 | # request probability estimation 49 | svm = SVC(probability=True) 50 | 51 | # 10-fold cross validation, use 4 thread as each fold and each parameter set can be train in parallel 52 | clf = grid_search.GridSearchCV(svm, param, 53 | cv=10, n_jobs=4, verbose=3) 54 | 55 | clf.fit(X_train, y_train) 56 | 57 | if os.path.exists(model_output_path): 58 | joblib.dump(clf.best_estimator_, model_output_path) 59 | else: 60 | print("Cannot save trained svm model to {0}.".format(model_output_path)) 61 | 62 | print("\nBest parameters set:") 63 | print(clf.best_params_) 64 | 65 | y_predict=clf.predict(X_test) 66 | 67 | # labels=sorted(list(set(labels))) 68 | labels = [0,1] 69 | print("\nConfusion matrix:") 70 | print("Labels: {0}\n".format(",".join(labels))) 71 | print(confusion_matrix(y_test, y_predict, labels=labels)) 72 | 73 | print("\nClassification report:") 74 | print(classification_report(y_test, y_predict)) 75 | -------------------------------------------------------------------------------- /imagestacker.py: -------------------------------------------------------------------------------- 1 | #sampleScript 2 | import cv2 3 | import numpy as np 4 | import os, os.path 5 | 6 | def nameSplitter(text): 7 | ''' 8 | It will return the original image file name striped with the prefixes 9 | Image name is the frame no in the folder. 10 | ''' 11 | splitText = text.split("_")[-1] 12 | return splitText 13 | 14 | def extractFileName(text): 15 | #print text 16 | imagePathSplit = text.split('/') 17 | tmp = text.split('/')[0] 18 | imageFileName = text.split('/')[2].split('.')[0] 19 | #return imageFileName 20 | return imagePathSplit 21 | #return [imageNo,tmp,imageFileName] 22 | 23 | 24 | # Dataset path where training images in format of the pickle file or other sliced image contains 25 | datasetPath = "/data/train/" 26 | # List of folders which you want to use as for now to test the codes is working 2 folders are taken 27 | listOfFolders = ['Train001','Train002'] 28 | # List of slice sizes which are 15, 18 and 20 respectively 29 | listOfWindows = ['15','rs18','rs20'] 30 | # Sample name for the sliced images 31 | imageList15 = ['0_15_001.jpeg','1_15_001.jpeg'] 32 | imageListrs18 = ['90_rs18_15_001.jpeg','91_rs18_15_001.jpeg'] 33 | imageListrs18 = ['120_rs20_15_001.jpeg','121_rs20_15_001.jpeg'] 34 | 35 | # /train/folder/windowsize/imageno_windowsize_image.jpeg 36 | # /test/folder/windowsize/imageno_windowsize_image.jpeg 37 | imageN = 'Train001/15/0_15_001.jpeg' 38 | imageD = 'folder/windowsize/imageno_windowsize_image.jpeg' 39 | 40 | 41 | listOfImagesAll = [] 42 | 43 | for lf in listOfFolders: 44 | for ws in listOfWindows: 45 | print lf+"/"+ws 46 | 47 | #imn, tst, tb = extractFileName(imageD) 48 | #print (imn, tst, tb) 49 | print ("------------") 50 | print (extractFileName(imageN)[2]) 51 | print (nameSplitter(extractFileName(imageN)[2])) 52 | -------------------------------------------------------------------------------- /imdsetgenimg.py: -------------------------------------------------------------------------------- 1 | ''' 2 | This create the dataset for the flatten, normalize and stack 3 | the images of motion features which are gray 4 | ''' 5 | import cv2 6 | import numpy as np 7 | import pickle 8 | import os 9 | from random import shuffle 10 | trainfolders = ['Train001', 'Train006', 'Train011', 'Train016', 'Train021', 'Train026', 'Train031', 11 | 'Train002', 'Train007', 'Train012', 'Train017', 'Train022', 'Train027', 'Train032', 12 | 'Train003','Train008', 'Train013', 'Train018', 'Train023', 'Train028', 'Train033', 13 | 'Train004', 'Train009', 'Train014', 'Train019', 'Train024', 'Train029', 'Train034', 14 | 'Train005', 'Train010', 'Train015', 'Train020', 'Train025', 'Train030'] 15 | 16 | # trainfolders = sorted(trainfolders)[:10] 17 | fileData = [] 18 | flGray = 'wingray' # Folder where the gray motion features are stored 19 | # Path to the training folder as the extracted images are in the train folders 20 | path = 'UCSD_Anomaly_Dataset.v1p2/UCSDped1/Train/' 21 | 22 | 23 | for tfds in trainfolders: 24 | flds = str(path) + str(tfds) +'/'+str(flGray) 25 | print flds 26 | fileData = fileData + [flds] 27 | # fileData = sorted(fileData) 28 | print len(fileData) 29 | fileData = list(set(fileData)) 30 | print len(fileData) 31 | i = 0 32 | # while i<5: 33 | # print fileData[i] 34 | # sampleOne = fileData[i] 35 | # i = i +1 36 | #print "out" 37 | # shuffle(fileData) 38 | filesAll = [] 39 | cnt = 20 40 | images = np.ones((225,),dtype='float') 41 | 42 | with open("temporary.p",'wb') as imageDataset: 43 | for fdt in fileData: 44 | for fls in os.listdir(fdt): 45 | fl1 = str(fdt)+"/"+str(fls) 46 | print fl1 47 | filesAll = filesAll + [fl1] 48 | # if cnt < 0: 49 | # break 50 | # cnt = cnt - 1 51 | # print "data shuffled" 52 | # print filesAll 53 | # i = 0 54 | # print type(images) 55 | # while i self.anomaly_threshold: 257 | anomaly_results['is_anomalous'] = True 258 | anomaly_results['anomalous_patches'].append({ 259 | 'patch_id': idx, 260 | 'patch_size': patch_size, 261 | 'reconstruction_error': mean_error.item() 262 | }) 263 | 264 | return anomaly_results 265 | 266 | def forward(self, image): 267 | """ 268 | Forward pass for training or inference 269 | 270 | Args: 271 | image (torch.Tensor or numpy.ndarray): Input image 272 | 273 | Returns: 274 | dict: Anomaly detection results 275 | """ 276 | return self.detect_anomalies(image) 277 | 278 | # Example usage and testing 279 | def main(): 280 | # Create anomaly detector 281 | anomaly_detector = CameraInputAnomalyDetector( 282 | input_channels=3, 283 | patch_sizes=[15, 17, 21], 284 | input_resolutions=[(640, 480), (1920, 1080)], 285 | anomaly_threshold=0.05 286 | ) 287 | 288 | # Simulate camera inputs (VGA and HD) 289 | vga_input = torch.randn(3, 480, 640) # VGA camera input 290 | hd_input = torch.randn(3, 1080, 1920) # HD camera input 291 | 292 | # Detect anomalies 293 | vga_anomalies = anomaly_detector(vga_input) 294 | hd_anomalies = anomaly_detector(hd_input) 295 | 296 | # Print results 297 | print("VGA Camera Anomaly Detection:") 298 | print(f"Is Anomalous: {vga_anomalies['is_anomalous']}") 299 | print("HD Camera Anomaly Detection:") 300 | print(f"Is Anomalous: {hd_anomalies['is_anomalous']}") 301 | 302 | if __name__ == "__main__": 303 | main() 304 | -------------------------------------------------------------------------------- /motionfeatures.py: -------------------------------------------------------------------------------- 1 | # Dense optical flow 2 | # https://gist.github.com/myfavouritekk/2cee1ec99b74e962f816 3 | 4 | """ 5 | This file is used to extract motion features from 6 | the images and not from the video. 7 | The motion features are in color and gray scale format 8 | The gray frame is used as the motion features 9 | not the motion vectors of x and y direction 10 | each frame in color and gray scale, 11 | every block from each frame in color and gray 12 | are stored in separate folder 13 | Change the directories to save in the path 14 | Provide the training folder path 15 | Dependency on the Tkinter package for the folder selection dialog 16 | If not installed please install Tkinter for version less than 3 17 | for 18 | Some functions the imutils package is used please install it before running the script 19 | 20 | """ 21 | import numpy as np 22 | import cv2 23 | import os 24 | import os.path 25 | import argparse 26 | import imutils 27 | import time 28 | import mylib 29 | # Python version 2.7 30 | import Tkinter, tkFileDialog 31 | # Python Version 3.0 32 | 33 | root = Tkinter.Tk () 34 | dirname = tkFileDialog.askdirectory (parent=root, initialdir="~/$USER/Desktop", title='Please select a directory') 35 | if len (dirname) > 0: 36 | print "You chose %s" % dirname 37 | 38 | 39 | 40 | winW, winH = 15, 15 41 | 42 | dPath = os.listdir (dirname) 43 | 44 | # Or You can get all the files path using this line 45 | # This block of code is working to collect files 46 | # listOfFiles = [] 47 | # for rootFolder, allFolders, allFiles in os.walk(datapath): 48 | # for fldr in allFolders: 49 | # for fls in allFiles: 50 | # fullFileName = os.path.join(rootFolder,fldr,fls) 51 | # listOfFiles.append(str(fullFileName)) 52 | 53 | # Folders to exclude if are generated using the other script 54 | # These are the folder for the extracted patches and frames 55 | # optframe: optical flow color frame, 56 | # optgrayframe: optical flow gray frames, 57 | # wincolor: extracted patch in color, 58 | # wingray: extracted patch in grayscale 59 | 60 | listtoremove = ['._.DS_Store', '.DS_Store', 'optframe', 'optgrayframe', 'wincolor','wingray'] 61 | 62 | for fd in dPath: 63 | # Here we will list all the folders that have the frames 64 | if fd in listtoremove: 65 | dPath.remove(fd) 66 | dPath = sorted(dPath) # Sort and remove the ._.DS_Store, .DS_Strore folder 67 | 68 | print dPath 69 | 70 | # Sample path is taken but not used anywhere in this script 71 | path = "/UCSD_Anomaly_Dataset.v1p2/UCSDped1/Train/Train001" 72 | 73 | windSizes = ['15', '18', '20', 'rs18', 'rs20'] 74 | frameSizes = [[240, 165], [252, 162], [240, 160]] 75 | # This path is used for the script 76 | folders = dPath 77 | # files = os.listdir(path) 78 | # files = sorted(files)[2:] 79 | # print files 80 | 81 | # Start the webcam 82 | # cap = cv2.VideoCapture(0) 83 | # cap = cv2.VideoCapture('../data/vtest.avi') 84 | 85 | # Take the first frame and convert it to gray 86 | # ret, frame = cap.read() 87 | 88 | #index = 0 89 | 90 | Divisor = 255 # For the grid of the vectors 91 | #print "Type of : " + str(type(dPath)) 92 | for idx, f in enumerate (dPath): 93 | print "Folder: " + str (f) 94 | # print os.path.join(str(datasetPath), str(f)) 95 | print os.path.join (str (dirname), str (f)) 96 | # files = sorted (os.listdir (os.path.join (str (datasetPath), str (f)))) 97 | files = sorted (os.listdir (os.path.join (str (dirname), str (f)))) 98 | 99 | if '.DS_Store' in files: 100 | files.remove('.DS_Store') 101 | if '._.DS_Store' in files: 102 | files.remove ('._.DS_Store') 103 | if 'wingray' in files: 104 | files.remove ('wingray') 105 | if 'optframe' in files: 106 | files.remove ('optframe') 107 | if 'wincolor' in files: 108 | files.remove('wincolor') 109 | if 'optgrayframe' in files: 110 | files.remove ('optgrayframe') 111 | 112 | for flp in files: 113 | # print flp 114 | if flp in listtoremove: 115 | files.remove (flp) 116 | 117 | #print listOfFiles 118 | # print files 119 | # index = 0 120 | print "Length of file list: " + str (len (files)) 121 | for index, file in enumerate (files): 122 | # print "Full File Path: " + os.path.join(str(datasetPath), str(f), str(files[index])) 123 | #print "Full File Path: " + os.path.join(str(datasetPath), str(f), str(files[index+1])) 124 | # #prin t index 125 | # # Create the HSV color image 126 | #if len(files)>=199: break 127 | # print index 128 | # print index+1 129 | # print files[index] 130 | # print files[index+1] 131 | if index <= 198: 132 | prevFrameName = files[index] 133 | nextFrameName = files[index + 1] 134 | # print 135 | # print files[index+1] 136 | # print file 137 | # print os.path.join(str(datasetPath), str(f), str(files[index])) 138 | # print os.path.join(str(dPath), str(f), str(files[index])) 139 | 140 | 141 | print (os.path.join (str (dirname), str (f), str (files[index]))) 142 | prevFrame = cv2.imread (os.path.join (str (dirname), str (f), str (files[index]))) 143 | cv2.namedWindow ('Prev Frame') 144 | cv2.imshow ('Prev frame', prevFrame) 145 | 146 | if os.path.isfile (os.path.join (str (dirname), str (f), str (files[index]))): 147 | print ("File found: " + str (os.path.join (str (dirname), str (f), str (files[index])))) 148 | # print (os._exists(os.path.join(str (dirname), str (f), str (files[index])))) 149 | prevFrame = cv2.imread (os.path.join (str (dirname), str (f), str (files[index]))) 150 | else: 151 | print ("File not found: " + str (str (os.path.join (str (dirname), str (f), str (files[index]))))) 152 | pass 153 | 154 | #print index+1 155 | if os.path.isfile (os.path.join (str (dirname), str (f), str (files[index + 1]))): 156 | print ("File found: " + str (os.path.join (str (dirname), str (f), str (files[index + 1])))) 157 | nextFrame = cv2.imread (os.path.join (str (dirname), str (f), str (files[index + 1]))) 158 | else: 159 | print ('File not found:') 160 | pass 161 | 162 | prevFrame = cv2.resize(prevFrame, (240, 165), 1) 163 | nextFrame = cv2.resize(nextFrame, (240, 165), 1) 164 | mainFrame = prevFrame.copy() 165 | prevGray = cv2.cvtColor(prevFrame, cv2.COLOR_BGR2GRAY) 166 | nextGray = cv2.cvtColor(nextFrame, cv2.COLOR_BGR2GRAY) 167 | 168 | hsvImg = np.zeros_like(prevFrame) 169 | hsvImg[..., 1] = 255 170 | cv2.startWindowThread() 171 | cv2.namedWindow('Previous Frame', 1) 172 | cv2.imshow('Previous Frame', prevFrame) 173 | cv2.startWindowThread() 174 | cv2.namedWindow('Next Frame', 1) 175 | cv2.imshow('Next Frame', nextFrame) 176 | 177 | flow = cv2.calcOpticalFlowFarneback(prevGray, nextGray, None, 0.5, 3, 15, 3, 5, 1.2, 0 ) 178 | #Obtain the flow magnitude and direction angle 179 | mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) 180 | #print flow 181 | xFlow, yFlow = flow[..., 0], flow[..., 1] 182 | xFlow, yFlow = flow[..., 0], flow[..., 1] 183 | # Normalize horizontal and vertical components 184 | horz = cv2.normalize (flow[..., 0], None, 0, 255, cv2.NORM_MINMAX) 185 | vert = cv2.normalize(flow[..., 1], None, 0, 255, cv2.NORM_MINMAX) 186 | 187 | horz = horz.astype('uint8') 188 | vert = vert.astype('uint8') 189 | # Show the components as images 190 | cv2.imshow('Horizontal Component', horz) 191 | cv2.imshow('Vertical Component', vert) 192 | 193 | #print frameShape 194 | 195 | # Update the color image 196 | hsvImg[..., 0] = 0.5 * ang * 180 / np.pi 197 | hsvImg[..., 1] = 255 198 | hsvImg[..., 2] = cv2.normalize(mag, None, 0, 256, cv2.NORM_MINMAX) 199 | rgbImg = cv2.cvtColor(hsvImg, cv2.COLOR_HSV2BGR) 200 | motionGray = cv2.cvtColor(rgbImg, cv2.COLOR_BGR2GRAY) 201 | 202 | motionGray1 = cv2.cvtColor(motionGray, cv2.COLOR_GRAY2BGR) 203 | # Display the resulting frame 204 | # cv2.startWindowThread() 205 | # cv2.namedWindow('Dense optical flow', 1) 206 | cv2.imshow('Dense optical flow', np.hstack((prevFrame, rgbImg, motionGray1))) 207 | #cv2.imshow('Maked image', cv2.bitwise_or(mainFrame, mainFrame, mask=motionGray)) 208 | tmppath = os.path.join (str (dirname), str (f)) 209 | if not os.path.exists(os.path.join(tmppath, "optframe")): 210 | os.makedirs(os.path.join(tmppath, "optframe")) 211 | if not os.path.exists(os.path.join(tmppath, "optgrayframe")): 212 | os.makedirs(os.path.join(tmppath, "optgrayframe")) 213 | cv2.imwrite(str(tmppath)+"/optframe/"+str(index+1)+"_"+str(file.split('.')[0])+".jpeg", rgbImg) 214 | cv2.imwrite(str(tmppath)+"/optgrayframe/"+str(index+1)+"_"+str(file.split('.')[0])+".jpeg", motionGray) 215 | 216 | # Here display the optical flow in the image 217 | mylib.dispOpticalFlow (prevFrame, flow, 10, 'Optical flow') 218 | 219 | winNo = 0 220 | 221 | rgbImg = cv2.resize(rgbImg, (240, 165), 1) 222 | motionGray = cv2.resize(motionGray, (240, 165), 1) 223 | for (x, y, window) in mylib.sliding_window (rgbImg, stepSize=15, windowSize=(15, 15)): 224 | if not os.path.exists(os.path.join(tmppath, "wincolor")): 225 | os.makedirs(os.path.join(tmppath, "wincolor")) 226 | if window.shape[0] != winH or window.shape[1] != winW: 227 | print winNo 228 | #cv2.imshow("Internal window", window) 229 | continue 230 | clone = rgbImg.copy() 231 | clone1 = motionGray.copy() 232 | cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2) 233 | cv2.rectangle(clone1, (x, y), (x + winW, y + winH), (0, 255, 0), 2) 234 | 235 | cv2.startWindowThread () 236 | cv2.namedWindow ("Clone", 1) 237 | cv2.imshow("Clone", clone) 238 | cv2.startWindowThread () 239 | cv2.namedWindow ("Window", 1) 240 | cv2.imshow("Smaller window", window) 241 | #print str(tmppath)+"/wincolor/"+str(winNo)+"_"+str(file.split('.')[0])+".jpeg"#, window 242 | # tmppth is the pth to the training folder' Train folder full path 243 | # Wincolor : the folder name - color patch folder 244 | # winno : patch no in the particular frame 245 | # file.split will get the frame no with jpeg extension added to it 246 | # Change the path accordingly where you want to save in the make directory lines 247 | cv2.imwrite(str(tmppath)+"/wincolor/"+str(winNo)+"_"+str(file.split('.')[0])+".jpeg", window) 248 | winNo = winNo + 1 249 | k1 = cv2.waitKey(1) & 0xFF 250 | if k1==27: 251 | cv2.destroyAllWindows() 252 | break 253 | elif k1 == ord('q'): 254 | cv2.destroyAllWindows() 255 | break 256 | elif k1 == ord('e'): 257 | cv2.destroyAllWindows() 258 | os._exit(0) 259 | #time.sleep(0.025) 260 | # Motion gray splicing 261 | winNo1 = 0 262 | motionGray = cv2.resize(motionGray, (240, 165), 1) 263 | for (x1, y1, window1) in mylib.sliding_window (motionGray, stepSize=15, windowSize=(15, 15)): 264 | if not os.path.exists(os.path.join(tmppath, "wingray")): 265 | os.makedirs(os.path.join(tmppath, "wingray")) 266 | # print winNo 267 | # print x, y, window.shape 268 | # print winH, winW 269 | # if the window does not meet our desired window size, ignore it 270 | # print "windows pre" 271 | # print "Windows pre shape : " +str(window1.shape) 272 | if window1.shape[0] != winH or window1.shape[1] != winW: 273 | # print window1.shape[0] 274 | # print window1.shape[1] 275 | # print winNo1 276 | #cv2.imshow("Internal window", window) 277 | # print "window1" 278 | continue 279 | # since we do not have a classifier, we'll just draw the window 280 | clone1 = motionGray.copy() 281 | # print "Clone 1 : " + str(clone1.shape) 282 | cv2.rectangle(clone1, (x1, y1), (x1 + winW, y1 + winH), (0, 255, 0), 2) 283 | 284 | #cv2.startWindowThread() 285 | #cv2.namedWindow("Clone", 1) 286 | #cv2.startWindowThread() 287 | #cv2.namedWindow("Window", 1) 288 | cv2.imshow("Clone gray", clone1) 289 | cv2.imshow("Smaller window gray", window1) 290 | #print str(tmppath)+"/wingray/"+str(winNo1)+"_"+str(file.split('.')[0])+".jpeg"#, window1 291 | cv2.imwrite(str(tmppath)+"/wingray/"+str(winNo1)+"_"+str(file.split('.')[0])+".jpeg", window1) 292 | 293 | tk1 = cv2.waitKey(1) & 0xFF 294 | if tk1==27: 295 | cv2.destroyAllWindows() 296 | break 297 | elif tk1 == ord('q'): 298 | cv2.destroyAllWindows() 299 | break 300 | elif tk1 == ord('e'): 301 | cv2.destroyAllWindows() 302 | os._exit(0) 303 | break 304 | #time.sleep(0.025) 305 | winNo1 = winNo1 + 1 306 | 307 | k = cv2.waitKey(1) & 0xFF 308 | if k==27: 309 | cv2.destroyAllWindows() 310 | break 311 | elif k == ord('q'): 312 | cv2.destroyAllWindows() 313 | break 314 | elif k == ord('e'): 315 | cv2.destroyAllWindows() 316 | # If problem due to exit syntax change/update accordingly 317 | os._exit(0) 318 | cv2.destroyAllWindows() 319 | os._exit(0) 320 | -------------------------------------------------------------------------------- /mylib.py: -------------------------------------------------------------------------------- 1 | # Library for customized functions 2 | import numpy as np 3 | import os, os.path 4 | import cv2 5 | import imutils 6 | 7 | 8 | # fileName = "/home/hduser/Documents/experiment/vg_text/001.tif" 9 | 10 | 11 | def checkValidExtension ( fileName ): 12 | ''' 13 | ======================================= 14 | Check for the valid file extension in 15 | given file name or string passed 16 | ======================================= 17 | 18 | 19 | Arguments: 20 | fileName: file name or filename string 21 | ''' 22 | 23 | valid_image_extensions = [".jpg", ".jpeg", ".png", ".tif", ".tiff", "jpg", "jpeg", "png", "tif", 24 | "tiff"] # specify your valid extensions here 25 | valid_image_extensions = [item.lower () for item in valid_image_extensions] 26 | 27 | if fileName.__contains__ ('/'): 28 | extension = fileName.lower ().split ("/")[-1].split ('.')[-1] 29 | else: 30 | extension = fileName.lower ().split ('.')[-1] 31 | 32 | # print extension 33 | if valid_image_extensions.__contains__ (extension): 34 | # print "Contains " + str (extension) 35 | return True 36 | else: 37 | return False 38 | 39 | 40 | 41 | # checkValidExtension(fileName) 42 | 43 | def pyramid ( image, scale=1.5, minSize=(15, 15) ): 44 | ''' 45 | ======================== 46 | Returns an image pyramid 47 | ======================== 48 | 49 | Arguments: 50 | image: Image file or object 51 | scale: Decreasing with a specified ratio 52 | minSize: Minimum image size 53 | 54 | Return: 55 | image: Sliced image 56 | ''' 57 | # yield the original image 58 | yield image 59 | 60 | # keep looping over the pyramid 61 | while True: 62 | # compute the new dimensions of the image and resize it 63 | w = int (image.shape[1] / scale) 64 | image = imutils.resize (image, width=w) 65 | 66 | # if the resized image does not meet the supplied minimum 67 | # size, then stop constructing the pyramid 68 | if image.shape[0] < minSize[1] or image.shape[1] < minSize[0]: 69 | break 70 | 71 | # yield the next image in the pyramid 72 | yield image 73 | 74 | 75 | def sliding_window ( image, stepSize, windowSize ): 76 | ''' 77 | Splits the image based on the window size and stride length 78 | 79 | Arguments: 80 | image: Pass image object or the numpy array 81 | stepSize: Stride length for frame 82 | windowSize: window size for the patch from image to extract 83 | Returns: 84 | x, y: Corrdinate of the main image with 85 | image: Cropped image of dimensions with respect to image size 86 | and given window size and stride length 87 | ''' 88 | # slide a window across the image 89 | for y in xrange (0, image.shape[0], stepSize): 90 | for x in xrange (0, image.shape[1], stepSize): 91 | # yield the current window 92 | yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]]) 93 | 94 | 95 | def createDirectory ( path ): 96 | ''' 97 | ================================ 98 | Create directory for a give path 99 | ================================ 100 | 101 | Arguments: 102 | path: Directory name or path of the directory 103 | 104 | Returns: 105 | No return values 106 | ''' 107 | if os.path.exists (path): 108 | print "Directory exists" 109 | elif not os.path.exists (path): 110 | os.mkdir (path) 111 | print "Directory created successfully: " + str (path) 112 | else: 113 | print "Could not create Directory" 114 | 115 | 116 | def resizeToMainWindowSize ( image, winSize ): 117 | ''' 118 | ================================================== 119 | Resize the window size for larger than given image 120 | ================================================== 121 | 122 | Arguments: 123 | image: Image you want to resize 124 | winSize: Window size of the image 125 | Returns: 126 | Resize image of given window size 127 | ''' 128 | if type (winSize) == int: 129 | return cv2.resize (image, (winSize, winSize), interpolation=cv2.INTER_CUBIC) 130 | elif type (winSize) == []: 131 | return cv2.resize (image, (winSize[0], winSize[1]), interpolation=cv2.INTER_CUBIC) 132 | elif type (winSize) == (): 133 | return cv2.resize (image, (winSize), interpolation=cv2.INTER_CUBIC) 134 | 135 | 136 | def drawRectangleOnImage ( image, x, y, winSize ): 137 | clone = image.copy () 138 | if type (winSize) == int: 139 | cv2.rectangle (clone, (x, y), (x + winSize[0], y + winSize[1]), (0, 255, 0), 2) 140 | return clone 141 | 142 | 143 | def drawROI ( img, x, y, alp, winsz, color ): 144 | ''' 145 | It will draw the ROI as transparent window with border over image 146 | 147 | Args: 148 | img : Image which you want to draw the ROI 149 | x, y : x and y starting coordinates for the ROI 150 | alp : Alpha or transparency value between 0.0 and 1.0 151 | winsz : Windows size of the ROI winsz x winsz single value 152 | color : Same color for the border and the filled color in square 153 | 154 | Returns: 155 | Returns the processed image 156 | 157 | image = drawROI('image.jpg',30,50,,0.6,20,(0,255,0)) 158 | 159 | ''' 160 | ovly = img.copy () 161 | out = img.copy () 162 | 163 | # Draw filled rectangle 164 | cv2.rectangle (ovly, (x, y), (x + winsz, y + winsz), (color), -1) 165 | # Draw line or border for rectangle 166 | cv2.rectangle (ovly, (x, y), (x + winsz, y + winsz), (color), 2) 167 | cv2.addWeighted (ovly, alp, out, 1 - alp, 0, out) 168 | 169 | return out 170 | 171 | 172 | def dispOpticalFlow (Image, Flow, Divisor, name ): 173 | """ 174 | 175 | Display image with a visualisation of a flow over the top. 176 | A divisor controls the density of the quiver plot. 177 | 178 | Arguments: 179 | Image: Image on which to display flow lines 180 | Flow : Flow vectors x and y 181 | Divisor: Spacing between the arrow nodes 182 | name: Name of the window 183 | """ 184 | PictureShape = np.shape (Image) 185 | # determine number of quiver points there will be 186 | Imax = int (PictureShape[0] / Divisor) 187 | Jmax = int (PictureShape[1] / Divisor) 188 | # create a blank mask, on which lines will be drawn. 189 | mask = np.zeros_like (Image) 190 | panel = np.zeros_like (Image) 191 | 192 | for i in range (1, Imax): 193 | for j in range (1, Jmax): 194 | X1 = (i) * Divisor 195 | Y1 = (j) * Divisor 196 | 197 | X2 = int (X1 + Flow[X1, Y1, 1]) 198 | Y2 = int (Y1 + Flow[X1, Y1, 0]) 199 | X2 = np.clip (X2, 0, PictureShape[0]) 200 | Y2 = np.clip (Y2, 0, PictureShape[1]) 201 | # add all the lines to the mask 202 | 203 | mask = cv2.arrowedLine (mask, (Y1, X1), (Y2, X2), [255, 255, 255], 1) 204 | # To show only arrows in the image 205 | # cv2.namedWindow("Panel", 0) 206 | # panel = panel+mask 207 | # cv2.imshow("Panel", panel) 208 | # superpose lines onto image 209 | 210 | img = cv2.add (Image, mask) 211 | # print image 212 | cv2.startWindowThread () 213 | cv2.namedWindow (name, 0) 214 | cv2.imshow (name, img) 215 | 216 | return [] 217 | 218 | def exitScript(): 219 | ky = cv2.waitKey (1) & 0xff 220 | if ky == 27: 221 | print "Exiting the program" 222 | os._exit(0) 223 | elif ky == ord ('q'): 224 | print "Exiting the program" 225 | os._exit(0) 226 | elif ky == ord ('e'): 227 | print "Exiting the program" 228 | os._exit (0) -------------------------------------------------------------------------------- /sample_motion.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | 5 | image1 = cv2.imread("/home/Desktop/maruti/cropped/8_009.jpg",0) 6 | image2 = cv2.imread("/home/Desktop/maruti/cropped/9_009.jpg",0) 7 | 8 | #image1 = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY) 9 | #image2 = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY) 10 | 11 | 12 | hsvImg = np.zeros_like(image1) 13 | hsvImg[1] = 255 14 | # cv2.startWindowThread() 15 | # cv2.namedWindow('Previous Frame',1) 16 | cv2.imshow('Previous Frame',image1) 17 | # cv2.startWindowThread() 18 | # cv2.namedWindow('Next Frame',2) 19 | cv2.imshow('Next Frame', image2) 20 | 21 | flow = cv2.calcOpticalFlowFarneback(image1, image2, None, 0.5, 3, 15, 3, 5, 1.2, 0) 22 | flow 23 | print(image1.shape) 24 | print(image2.shape) 25 | 26 | print(flow.shape) 27 | 28 | #Obtain the flow magnitude and direction angle 29 | print(flow[...,0]) 30 | print(flow[...,1]) 31 | mag, ang = cv2.cartToPolar( flow[...,0], flow[...,1]) 32 | print(mag.shape, ang.shape) 33 | #print(flow) 34 | #xFlow, yFlow = flow[..., 0], flow[..., 1] 35 | 36 | #print(frameShape) 37 | 38 | # Update the color image 39 | hsvImg[..., 0] = 0.5 * ang * 180 / np.pi 40 | # hsvImg[..., 1] = 255 41 | # hsvImg[..., 2] = cv2.normalize(mag, None, 0, 256, cv2.NORM_MINMAX) 42 | # rgbImg = cv2.cvtColor(hsvImg, cv2.COLOR_HSV2BGR) 43 | 44 | if cv2.waitKey(0) & 0xff == ord('q'): 45 | cv2.destroyAllWindows() 46 | print("end") 47 | -------------------------------------------------------------------------------- /sampledataset/scripts: -------------------------------------------------------------------------------- 1 | #Nullscript 2 | -------------------------------------------------------------------------------- /trainmotionfeat.py: -------------------------------------------------------------------------------- 1 | """ 2 | This script is used to train the motion features of the video, the work is just started 3 | so you would not able to use it now... 4 | 5 | """ 6 | import numpy as np 7 | import cv2 8 | import os, os.path 9 | 10 | # Here chanage the path to the train and dataset folder 11 | 12 | datasetPath = "UCSD_Anomaly_Dataset.v1p2/UCSDped1/Train" 13 | samplePath = datasetPath = "/UCSD_Anomaly_Dataset.v1p2/UCSDped1/Train/Train001" 14 | dPath = os.listdir(datasetPath) # List of all the folder in the train folder 15 | dPath = sorted(dPath)[2:] # Sort and remove the ._.DS_Store, .DS_Strore folder 16 | 17 | print (datasetPath) 18 | print (dPath) 19 | subfolders = [] 20 | # These are the samples folders which has sliced images for different features 21 | # optframe - Sliced images for optical frame in color 22 | # optgrayframe - Sliced images for optical frame in grayscale 23 | # wincolor - Sliced images of original video color 24 | # wingray - Sliced images of originial video in gray scale 25 | sampleFolders = ['optframe', 'optgrayframe', 'wincolor', 'wingray'] 26 | 27 | # for 28 | -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | """Some useful utilities when dealing with neural nets w/ tensorflow. 2 | 3 | Parag K. Mital, Jan. 2016 4 | """ 5 | import tensorflow as tf 6 | import numpy as np 7 | 8 | 9 | def montage_batch(images): 10 | """Draws all filters (n_input * n_output filters) as a 11 | montage image separated by 1 pixel borders. 12 | 13 | Parameters 14 | ---------- 15 | batch : numpy.ndarray 16 | Input array to create montage of. 17 | 18 | Returns 19 | ------- 20 | m : numpy.ndarray 21 | Montage image. 22 | """ 23 | img_h = images.shape[1] 24 | img_w = images.shape[2] 25 | n_plots = int(np.ceil(np.sqrt(images.shape[0]))) 26 | m = np.ones( 27 | (images.shape[1] * n_plots + n_plots + 1, 28 | images.shape[2] * n_plots + n_plots + 1, 3)) * 0.5 29 | 30 | for i in range(n_plots): 31 | for j in range(n_plots): 32 | this_filter = i * n_plots + j 33 | if this_filter < images.shape[0]: 34 | this_img = images[this_filter, ...] 35 | m[1 + i + i * img_h:1 + i + (i + 1) * img_h, 36 | 1 + j + j * img_w:1 + j + (j + 1) * img_w, :] = this_img 37 | return m 38 | 39 | 40 | # %% 41 | def montage(W): 42 | """Draws all filters (n_input * n_output filters) as a 43 | montage image separated by 1 pixel borders. 44 | 45 | Parameters 46 | ---------- 47 | W : numpy.ndarray 48 | Input array to create montage of. 49 | 50 | Returns 51 | ------- 52 | m : numpy.ndarray 53 | Montage image. 54 | """ 55 | W = np.reshape(W, [W.shape[0], W.shape[1], 1, W.shape[2] * W.shape[3]]) 56 | n_plots = int(np.ceil(np.sqrt(W.shape[-1]))) 57 | m = np.ones( 58 | (W.shape[0] * n_plots + n_plots + 1, 59 | W.shape[1] * n_plots + n_plots + 1)) * 0.5 60 | for i in range(n_plots): 61 | for j in range(n_plots): 62 | this_filter = i * n_plots + j 63 | if this_filter < W.shape[-1]: 64 | m[1 + i + i * W.shape[0]:1 + i + (i + 1) * W.shape[0], 65 | 1 + j + j * W.shape[1]:1 + j + (j + 1) * W.shape[1]] = ( 66 | np.squeeze(W[:, :, :, this_filter])) 67 | return m 68 | 69 | 70 | 71 | 72 | # %% 73 | def corrupt(x): 74 | """Take an input tensor and add uniform masking. 75 | 76 | Parameters 77 | ---------- 78 | x : Tensor/Placeholder 79 | Input to corrupt. 80 | 81 | Returns 82 | ------- 83 | x_corrupted : Tensor 84 | 50 pct of values corrupted. 85 | """ 86 | return tf.multiply(x, tf.cast(tf.random_uniform(shape=tf.shape(x), 87 | minval=0, 88 | maxval=2, 89 | dtype=tf.int32), tf.float32)) 90 | 91 | 92 | # %% 93 | def weight_variable(shape): 94 | '''Helper function to create a weight variable initialized with 95 | a normal distribution 96 | 97 | Parameters 98 | ---------- 99 | shape : list 100 | Size of weight variable 101 | ''' 102 | initial = tf.random_normal(shape, mean=0.0, stddev=0.01) 103 | return tf.Variable(initial) 104 | 105 | 106 | # %% 107 | def bias_variable(shape): 108 | '''Helper function to create a bias variable initialized with 109 | a constant value. 110 | 111 | Parameters 112 | ---------- 113 | shape : list 114 | Size of weight variable 115 | ''' 116 | initial = tf.random_normal(shape, mean=0.0, stddev=0.01) 117 | return tf.Variable(initial) 118 | --------------------------------------------------------------------------------