循环迭代正确,但在处理视频帧时仅对前四个索引有效

count = 1for i in real_videonames_index:  videofile = filed[i]  success = True  vidcap = cv2.VideoCapture(videofile)  while success:    if (count%one_frame_each == 0):      success,image = vidcap.read()      image_gray = rgb2gray(image)      if image.shape[1]>640:        tmp = resize(image_gray,(math.floor(640 / image_gray.shape[1] * image_gray.shape[0]), 640),mode='constant')      name = '/content/drive/MyDrive/Training/REAL/' + str(count) + '.jpg'      print('正在创建 ....' + name)      cv2.imwrite(name, image)      print ('*', end="")    else:      success,image = vidcap.read()                                     count += 1    print('/n/n/n/n第{}个视频已成功完成/n/n/n'.format(i))  i += 1

在简单的迭代循环中工作正常,但在提取帧时仅对前三个索引有效?

捕获第一个视频帧后的错误如下。输入图片说明我在使用Google Colab。提前感谢。


回答:

实际上,错误出现在最后一个帧cv2获取了None值,因此可以通过再次获取帧来解决。正确的代码如下:

count = 0face_cascade = cv2.CascadeClassifier('/content/drive/MyDrive/Training/haarcascade_frontalface_default.xml')for i in real_videonames_index[51:76]:  videofile = videonames_list[i]  vidcap = cv2.VideoCapture(videofile)  success,image = vidcap.read()  while success:    if (count%one_frame_each == 0):      name = '/content/drive/MyDrive/Training/Validation Data/REAL/'+names[i]+ "_" + str(count) + '.jpg'      image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)      facesBase = face_cascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5)      for f in facesBase:        x, y, w, h = [ v for v in f ]        cv2.rectangle(image, (x,y), (x+w, y+h), (255,0,0), 3)        face_crop = image[y:y+h, x:x+w]        cv2.imwrite(name, face_crop)      success,image = vidcap.read()      print('读取新帧: {} 并命名为 {}'.format(success,count))    else:            success,image = vidcap.read()      print('读取新帧: {} 并命名为 {}'.format(success,count))    count += 1  print('第{}个真实视频已成功提取'.format(i))  i += 1

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注