这是我为模型中的测试视频生成预测的代码,它是这样工作的
首先,我们将创建两个空列表——一个用于存储预测,另一个用于存储实际标签 然后,我们将从测试集中取出每个视频,提取该视频的帧并将其存储在一个文件夹中(在当前目录中创建一个名为 temp 的文件夹来存储帧)。我们将在每次迭代时从此文件夹中删除所有其他文件 接下来,我们将从临时文件夹中读取所有帧,使用预训练模型提取这些帧的特征,预测标签,然后采用模式为该特定视频分配标签并将其附加到列表中 我们将为第二个列表中的每个视频附加实际标签
# 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])
这是错误
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