Last active
September 28, 2022 21:35
-
-
Save PulkitS01/aa6e8e0ab04b1855620efe8bcef55943 to your computer and use it in GitHub Desktop.
Video Classification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# checking the accuracy of the predicted tags | |
from sklearn.metrics import accuracy_score | |
accuracy_score(predict, actual)*100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#defining the model architecture | |
model = Sequential() | |
model.add(Dense(1024, activation='relu', input_shape=(25088,))) | |
model.add(Dropout(0.5)) | |
model.add(Dense(512, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(256, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(128, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(101, activation='softmax')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#defining the model architecture | |
model = Sequential() | |
model.add(Dense(1024, activation='relu', input_shape=(25088,))) | |
model.add(Dropout(0.5)) | |
model.add(Dense(512, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(256, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(128, activation='relu')) | |
model.add(Dropout(0.5)) | |
model.add(Dense(101, activation='softmax')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# creating the base model of pre-trained VGG16 model | |
base_model = VGG16(weights='imagenet', include_top=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
base_model = VGG16(weights='imagenet', include_top=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# defining a function to save the weights of best model | |
from keras.callbacks import ModelCheckpoint | |
mcp_save = ModelCheckpoint('weight.hdf5', save_best_only=True, monitor='val_loss', mode='min') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# compiling the model | |
model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 # for capturing videos | |
import math # for mathematical operations | |
import matplotlib.pyplot as plt # for plotting the images | |
%matplotlib inline | |
import pandas as pd | |
from keras.preprocessing import image # for preprocessing the images | |
import numpy as np # for mathematical operations | |
from keras.utils import np_utils | |
from skimage.transform import resize # for resizing images | |
from sklearn.model_selection import train_test_split | |
from glob import glob | |
from tqdm import tqdm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import keras | |
from keras.models import Sequential | |
from keras.applications.vgg16 import VGG16 | |
from keras.layers import Dense, InputLayer, Dropout, Flatten | |
from keras.layers import Conv2D, MaxPooling2D, GlobalMaxPooling2D | |
from keras.preprocessing import image | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from tqdm import tqdm | |
from sklearn.model_selection import train_test_split |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Flatten | |
from keras.layers import Conv2D, MaxPooling2D | |
from keras.preprocessing import image | |
import numpy as np | |
import pandas as pd | |
from tqdm import tqdm | |
from keras.applications.vgg16 import VGG16 | |
import cv2 | |
import math | |
import os | |
from glob import glob | |
from scipy import stats as s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# loading the trained weights | |
model.load_weights("weights.hdf5") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# compiling the model | |
model.compile(loss='categorical_crossentropy',optimizer='Adam',metrics=['accuracy']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# normalizing the pixel values | |
max = X_train.max() | |
X_train = X_train/max | |
X_test = X_test/max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# creating two lists to store predicted and actual tags | |
predict = [] | |
actual = [] | |
# for loop to extract frames from each test video | |
for i in tqdm(range(test_videos.shape[0])): | |
count = 0 | |
videoFile = test_videos[i] | |
cap = cv2.VideoCapture('UCF/'+videoFile.split(' ')[0].split('/')[1]) # capturing the video from the given path | |
frameRate = cap.get(5) #frame rate | |
x=1 | |
# removing all other files from the temp folder | |
files = glob('temp/*') | |
for f in files: | |
os.remove(f) | |
while(cap.isOpened()): | |
frameId = cap.get(1) #current frame number | |
ret, frame = cap.read() | |
if (ret != True): | |
break | |
if (frameId % math.floor(frameRate) == 0): | |
# storing the frames of this particular video in temp folder | |
filename ='temp/' + "_frame%d.jpg" % count;count+=1 | |
cv2.imwrite(filename, frame) | |
cap.release() | |
# reading all the frames from temp folder | |
images = glob("temp/*.jpg") | |
prediction_images = [] | |
for i in range(len(images)): | |
img = image.load_img(images[i], target_size=(224,224,3)) | |
img = image.img_to_array(img) | |
img = img/255 | |
prediction_images.append(img) | |
# converting all the frames for a test video into numpy array | |
prediction_images = np.array(prediction_images) | |
# extracting features using pre-trained model | |
prediction_images = base_model.predict(prediction_images) | |
# converting features in one dimensional array | |
prediction_images = prediction_images.reshape(prediction_images.shape[0], 7*7*512) | |
# predicting tags for each array | |
prediction = model.predict_classes(prediction_images) | |
# appending the mode of predictions in predict list to assign the tag to the video | |
predict.append(y.columns.values[s.mode(prediction)[0][0]]) | |
# appending the actual tag of the video | |
actual.append(videoFile.split('/')[1].split('_')[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# creating an empty list | |
train_image = [] | |
# for loop to read and store frames | |
for i in tqdm(range(train.shape[0])): | |
# loading the image and keeping the target size as (224,224,3) | |
img = image.load_img('train_1/'+train['image'][i], target_size=(224,224,3)) | |
# converting it to array | |
img = image.img_to_array(img) | |
# normalizing the pixel value | |
img = img/255 | |
# appending the image to the train_image list | |
train_image.append(img) | |
# converting the list to numpy array | |
X = np.array(train_image) | |
# shape of the array | |
X.shape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# reshaping the training as well as validation frames in single dimension | |
X_train = X_train.reshape(59075, 7*7*512) | |
X_test = X_test.reshape(14769, 7*7*512) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# creating dummies of target variable for train and validation set | |
y_train = pd.get_dummies(y_train) | |
y_test = pd.get_dummies(y_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# open the .txt file which have names of test videos | |
f = open("testlist01.txt", "r") | |
temp = f.read() | |
videos = temp.split('\n') | |
# creating a dataframe having video names | |
test = pd.DataFrame() | |
test['video_name'] = videos | |
test = test[:-1] | |
test.head() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# getting the test list | |
f = open("testlist01.txt", "r") | |
temp = f.read() | |
videos = temp.split('\n') | |
# creating the dataframe | |
test = pd.DataFrame() | |
test['video_name'] = videos | |
test = test[:-1] | |
test_videos = test['video_name'] | |
test.head() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# getting the names of all the images | |
images = glob("train_1/*.jpg") | |
train_image = [] | |
train_class = [] | |
for i in tqdm(range(len(images))): | |
# creating the image name | |
train_image.append(images[i].split('/')[1]) | |
# creating the class of image | |
train_class.append(images[i].split('/')[1].split('_')[1]) | |
# storing the images and their class in a dataframe | |
train_data = pd.DataFrame() | |
train_data['image'] = train_image | |
train_data['class'] = train_class | |
# converting the dataframe into csv file | |
train_data.to_csv('UCF/train_new.csv',header=True, index=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# open the .txt file which have names of training videos | |
f = open("trainlist01.txt", "r") | |
temp = f.read() | |
videos = temp.split('\n') | |
# creating a dataframe having video names | |
train = pd.DataFrame() | |
train['video_name'] = videos | |
train = train[:-1] | |
train.head() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# extracting features for training frames | |
X_train = base_model.predict(X_train) | |
X_train.shape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
train = pd.read_csv('UCF/train_new.csv') | |
train.head() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# shape of images | |
X_train.shape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# training the model | |
model.fit(X_train, y_train, epochs=200, validation_data=(X_test, y_test), callbacks=[mcp_save], batch_size=128) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# storing the frames from training videos | |
for i in tqdm(range(train.shape[0])): | |
count = 0 | |
videoFile = train['video_name'][i] | |
cap = cv2.VideoCapture('UCF/'+videoFile.split(' ')[0].split('/')[1]) # capturing the video from the given path | |
frameRate = cap.get(5) #frame rate | |
x=1 | |
while(cap.isOpened()): | |
frameId = cap.get(1) #current frame number | |
ret, frame = cap.read() | |
if (ret != True): | |
break | |
if (frameId % math.floor(frameRate) == 0): | |
# storing the frames in a new folder named train_1 | |
filename ='train_1/' + videoFile.split('/')[1].split(' ')[0] +"_frame%d.jpg" % count;count+=1 | |
cv2.imwrite(filename, frame) | |
cap.release() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# extracting features for validation frames | |
X_test = base_model.predict(X_test) | |
X_test.shape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# separating the target | |
y = train['class'] | |
# creating the training and validation set | |
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2, stratify = y) |
i also can't work on training_tags.py.
It didn't store the frames in train_1 folder.
i am not able to store the frames in train_1 folder.. Suggest an idea for this
this code is not working what is "UCF" in code "cv2.VideoCapture('UCF/'" ?
It's called "Videos" earlier, the code needs some troubleshooting to get working. In-process myself.
i am not able to store the frames in train_1 folder.. Suggest an idea for this
You'll need to create the directory
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello! what is the data or file in that "UFC" folder?