This is my code to generate predictions for test videos in the model and this is how it works
First, we will create two empty lists – one to store the predictions and the other to store the actual tags Then, we will take each video from the test set, extract frames for this video and store it in a folder (create a folder named temp in the current directory to store the frames). We will remove all the other files from this folder at each iteration Next, we will read all the frames from the temp folder, extract features for these frames using the pre-trained model, predict tags, and then take the mode to assign a tag for that particular video and append it in the list We will append actual tags for each video in the second list
# creating two lists to store predicted and actual tags
predict = []
actual = []
# for loop to extract frames from each test video
count = 0
for i in tqdm(range(test_videos.shape[0])):
videoFile = test_videos[i]
cap = cv2.VideoCapture(test_videos[i]) # capturing the video from the given path
frameRate = cap.get(cv2.CAP_PROP_FPS) #frame rate
x=1
# removing all other files from the temp folder
files = glob('E:/FCI Bio/GP/Dataset/Paper/3/3.1/OHP/Labeled_Dataset/videos/dataset/test/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 ='E:/FCI Bio/GP/Dataset/Paper/3/3.1/OHP/Labeled_Dataset/videos/dataset/test/temp/' + os.path.basename(videoFile).split('_')[0] + "_frame%d.jpg" % count;count+=1
cv2.imwrite(filename, frame)
cap.release()
# reading all the frames from temp folder
images = glob("E:/FCI Bio/GP/Dataset/Paper/3/3.1/OHP/Labeled_Dataset/videos/dataset/test/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(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(os.path.basename(videoFile).split('_')[0])
and this is the error
IndexError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_4364\3325358529.py in <module>
44 prediction = model.predict(prediction_images)
45 # appending the mode of predictions in predict list to assign the tag to the video
---> 46 predict.append(y.columns.values[s.mode(prediction)[0][0]])
47 # appending the actual tag of the video
48 actual.append(os.path.basename(videoFile).split('_')[0])
IndexError: arrays used as indices must be of integer (or boolean) type