<jupyter_start><jupyter_text><jupyter_code>!wget https://www.dropbox.com/sh/75v5ehq4cdg5g5g/AABJtkTnNc8LcVTfH1gE_uFoa/Anomaly-Videos-Part-1.zip?dl=0
!unzip /content/Anomaly-Videos-Part-1.zip?dl=0
!rm -rf /content/Anomaly-Videos-Part-1.zip?dl=0
! git clone https://github.com/Tessellate-Imaging/Monk_Object_Detection.git
 
! cd Monk_Object_Detection/18_mmaction/installation && cat requirements.txt | xargs -n 1 -L 1 pip install -vvvv
 
! pip install pillow==6.0.0
# restart runtime
! cd Monk_Object_Detection/18_mmaction/lib/ && pip install -vvvv -e .
 
! pip uninstall -y mmcv
 
! cd Monk_Object_Detection/18_mmaction/lib/mmcv && MMCV_WITH_OPS=1 pip install -vvvv -e .
# restart runtime
pwd
!rm -r /content/Anomaly-Videos-Part-1
! mkdir Dataset
! mkdir Dataset/Videos

!mkdir Anomaly
from distutils.dir_util import copy_tree

# copy subdirectory example
fromDirectory = "/content/drive/MyDrive/Anomaly-Videos-Part-1/Arrest"
toDirectory = "/content/Anomaly"

copy_tree(fromDirectory, toDirectory)

fromDirectory = "/content/drive/MyDrive/Anomaly-Videos-Part-1/Assault"
toDirectory = "/content/Anomaly"

copy_tree(fromDirectory, toDirectory)
fromDirectory = "/content/drive/MyDrive/Anomaly-Videos-Part-2/Burglary"
toDirectory = "/content/Anomaly"

copy_tree(fromDirectory, toDirectory)
pwd
!mkdir Normal
# /content/drive/MyDrive/Training-Normal-Videos-Part-1
fromDirectory = "/content/drive/MyDrive/Training-Normal-Videos-Part-1"
toDirectory = "/content/Normal"

copy_tree(fromDirectory, toDirectory)
fromDirectory = "/content/drive/MyDrive/Anomaly-Videos-Part-3/Robbery"
toDirectory = "/content/Anomaly"

copy_tree(fromDirectory, toDirectory)

from random import sample

files = os.listdir('/content/Normal')
for file in sample(files,63):
    os.remove('/content/Normal/' + file)
import os
print("Class Anomaly: {}".format(len(os.listdir("/content/Anomaly"))))
print("Class Normal: {}".format(len(os.listdir("/content/Normal"))))

classes = ["Anomaly", "Normal"]
f = open("Dataset/classes.txt", 'w');
for i in range(len(classes)):
    f.write(classes[i] + "\n");
f.close();

import os
from tqdm import tqdm
combined = [];

pwd
!mkdir All
#put the anomaly and normal in ALL folder

!mv  /content/Anomaly /content/All 
!mv  /content/Normal /content/All 
folder_name = "/content/All/"
class_name = "Anomaly";
folder_name += class_name + "/";

vid_list = os.listdir(folder_name);
for i in tqdm(range(len(vid_list))):
    os.system("cp " + folder_name + vid_list[i] + " Dataset/Videos/")
    wr = vid_list[i] + " " + str(classes.index(class_name))
    combined.append(wr)



folder_name = "/content/All/"
class_name = "Normal";
folder_name += class_name + "/";

vid_list = os.listdir(folder_name);
for i in tqdm(range(len(vid_list))):
    os.system("cp " + folder_name + vid_list[i] + " Dataset/Videos/")
    wr = vid_list[i] + " " + str(classes.index(class_name))
    combined.append(wr)

combined
combined[:5], len(combined)

import random
random.shuffle(combined)

combined[:5], len(combined)


train_list = combined[:100];
val_list = combined[100:];
val_list
f = open("Dataset/train.txt", 'w')
for i in range(len(train_list)):
    f.write(train_list[i] + "\n");
f.close();

f = open("Dataset/val.txt", 'w')
for i in range(len(val_list)):
    f.write(val_list[i] + "\n");
f.close();
import os
import sys
sys.path.append("Monk_Object_Detection/18_mmaction/lib");
from train_engine import Detector_Videos
gtf = Detector_Videos();
video_dir = 'Dataset/Videos/';
anno_file = 'Dataset/train.txt';
classes_list_file = 'Dataset/classes.txt';

gtf.Train_Video_Dataset(video_dir, anno_file, classes_list_file);
video_dir = 'Dataset/Videos/';
anno_file = 'Dataset/val.txt';

gtf.Val_Video_Dataset(video_dir, anno_file);
gtf.Dataset_Params(batch_size=8, num_workers=4)
gtf.List_Models();
gtf.Model_Params(model_name="i3d_r50", gpu_devices=[0])
gtf.Hyper_Params(lr=0.001, momentum=0.9, weight_decay=0.0001)
gtf.Training_Params(num_epochs=100, val_interval=10)

gtf.Train();

cd /content/Dataset

! wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1-3cbejxIEhCu4oBqRt4fBg593kTeKpG0' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1-3cbejxIEhCu4oBqRt4fBg593kTeKpG0" -O act_ucf101_trained.zip && rm -rf /tmp/cookies.txt
! wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1_urp_a_ZupFD2Is7a_JVjD4PWgmrQnfO' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1_urp_a_ZupFD2Is7a_JVjD4PWgmrQnfO" -O act_anomaly_trained.zip && rm -rf /tmp/cookies.txt
! unzip -qq act_anomaly_trained.zip

import os
import sys
sys.path.append("Monk_Object_Detection/18_mmaction/lib");
from infer_engine import Infer_Videos

gtf = Infer_Videos();

gtf.Dataset_Params('Dataset/classes.txt');

pwd
config_file = "/content/work_dirs/config.py"
checkpoint_file = "/content/work_dirs/latest.pth"

gtf.Model_Params(config_file, checkpoint_file, use_gpu=True)

import os
vid_list = os.listdir("Dataset/Videos")
config_file = "/content/work_dirs/config.py"
checkpoint_file = "/content/work_dirs/latest.pth"

gtf.Model_Params(config_file, checkpoint_file, use_gpu=True)
video_path = '/content/All/Anomaly/Robbery122_x264.mp4'
results = gtf.Predict(video_path)

/content/Dataset/work_dirs/test/vid1.mp4
results = gtf.Predict('/content/Dataset/work_dirs/test/vid6.mp4')
results
video_path = "/content/Dataset/work_dirs/test/vid1.mp4"
classes, scores = gtf.Predict(video_path)

gtf.Dataset_Params('/content/Dataset/classes.txt');

from infer_engine import Infer_Videos

gtf = Infer_Videos();

config_file = "/content/work_dirs/config.py"
checkpoint_file = "/content/work_dirs/latest.pth"

gtf.Model_Params(config_file, checkpoint_file, use_gpu=True)
video_path = '/content/All/Anomaly/Robbery145_x264.mp4'
results = gtf.Predict(video_path)

import os
import cv2
import numpy as np
import glob
from PIL import Image
path = '/content/Dataset/ds/'
MAX = len(glob.glob(path+'*.jpg')) # sample sequence: 303
opt_img_save_dir = '/content/as/'

index = 0
frame1 = np.array(Image.open(path+'{}.jpg'.format(index)))
prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
hsv = np.zeros_like(frame1)
hsv[...,1] = 255

for _ in range(MAX):
    index += 1
    try:
        frame2 = np.array(Image.open(path+'{}.jpg'.format(index))) # for test
        nxt = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
    except:
        print('end of video')
        break
    
    flow = cv2.calcOpticalFlowFarneback(prvs, nxt, None, 0.5, 3, 15, 3, 5, 1.2, 0)
    
    mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1])
    hsv[...,0] = ang*180/np.pi/2
    hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX)
    rgb = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR)

#     cv2.imshow('dense optical flow',rgb)
    k = cv2.waitKey(30) & 0xff
    if k == 27: # ESC
        break
    elif k == ord('s'):
        cv2.imwrite('opticalfb.jpg',frame2)
        cv2.imwrite('opticalhsv.jpg',rgb)
        
    # save optical flow image
    cv2.imwrite(opt_img_save_dir+'{}.jpg'.format(index-1), rgb)
    
    prvs = nxt

cv2.destroyAllWindows()
!python /content/work_dirs/1.py --pathIn /content/Anomaly-Videos-Part-1/Abuse/Abuse001_x264.mp4  --pathOut /content/Dataset/ds/
Image.open('/content/as/50.jpg')

%cd /content 
!wget https://www.dropbox.com/sh/75v5ehq4cdg5g5g/AABqY-3fJSmSMafFIlJXRE-9a/Anomaly-Videos-Part-4.zip?dl=0
!unzip /content/Anomaly-Videos-Part-4.zip?dl=0
!rm -r /content/Anomaly-Videos-Part-4.zip?dl=0
video_path = '/content/Anomaly-Videos-Part-4/Shoplifting/Shoplifting001_x264.mp4'
results = gtf.Predict(video_path)

video_path = '/content/Anomaly-Videos-Part-4/Stealing/Stealing058_x264.mp4'
results = gtf.Predict(video_path)

from random import sample

files = os.listdir('/content/Anomaly-Videos-Part-4/Shoplifting')
for file in sample(files):
  results = gtf.Predict('/content/Anomaly-Videos-Part-4/Shoplifting/' + file)
   
import os
for filename in os.listdir('/content/Anomaly-Videos-Part-4/Shoplifting'):
     gtf.Predict('/content/Anomaly-Videos-Part-4/Shoplifting/'+filename)
import os
for filename in os.listdir('/content/Anomaly-Videos-Part-4/Stealing'):
     gtf.Predict('/content/Anomaly-Videos-Part-4/Stealing/'+filename)
import os
for filename in os.listdir('/content/Anomaly-Videos-Part-4/Vandalism'):
     gtf.Predict('/content/Anomaly-Videos-Part-4/Vandalism/'+filename)
# /content/drive/MyDrive/Training-Normal-Videos-Part-1
import os
for filename in os.listdir('/content/drive/MyDrive/Training-Normal-Videos-Part-1'):
     gtf.Predict('/content/drive/MyDrive/Training-Normal-Videos-Part-1/'+filename)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15877689694224237, 'fraction_numerical': 0.02684031710079275, 'mean_word_length': 4.7456083279115155, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 7, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '168302', 'license': 'Apache-2.0', 'n_tokens_mistral': 3661, 'n_tokens_neox': 3488, 'n_words': 531, 'path': 'Copy_of_Copy_of_important2.ipynb', 'repo_name': 'ezzatmostafa96/Action-Recognition-in-Real-Time', 'size': '8830'}