'cv2.VideoCapture'对象不可迭代

时间:2019-05-05 15:17:08

标签: python-3.x opencv

我无法迭代cv2.videocapture()上的文件。我有一个包含视频文件的目录。我想通过循环运行从这些视频中提取帧。但是我收到一个错误,提示“ cv2.videocapture()”不可迭代。

FILES = []
FILES.append('/mnt/sdd/luhas123/research/datasets/newExp7/angrily_surprised_train.mp4')
FILES.append('/mnt/sdd/luhas123/research/datasets/newExp7/awestruck_train.mp4')
FILES.append('/mnt/sdd/luhas123/research/datasets/newExp7/happily_surprised_train.mp4')

def extract_features(FILE_NAME):
    print("DEBUG:<demo_frame> Entered demo_frame......")
    cap = cv2.VideoCapture(FILE_NAME)
    if not cap.isOpened():
        raise Exception("could not open ", FILE_NAME)
    return cap
    cnt1 = 0
    features = []
    frames = []
    x_test_newExp = []
    while(cnt1 % 8 == 0):
        cnt1 += 1
        ret, img = cap.read() #captures images from video
        if ret == 0:
            print("DEBUG:<demo_frame> train_frames dimension = ", len(train_frames))   
            break
        detected_face = []
        img = cv2.resize(img,(640,360))
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #color to gray
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    x_test_newExp = np.asarray(x_test_newExp)
    features = model().predict([x_test_newExp.reshape(x_test_newExp.shape[0],1,48,48)])
    print("DEBUG:<extract_features> features size = ", len(features))
    print("DEBUG:<extract_features> frames size = ", len(frames))
    cap.release()
    cv2.destroyAllWindows()
    return features, frames  

import os

#####################################################main function##################################                    
if __name__ == '__main__':
    global train_features
    global train_frames
    for file in FILES:
        path=os.path.join(file)
        #extract features from the video
        features, frames = extract_features(path)
        train_features += features
        train_frames += frames
        Y_train += i
    #save the train features, frames and labels in three different files
    np.save("train_frames_svm", train_frames)
    np.save("train_features_svm", train_features)
    np.save("Y_train_svm", Y_train)
    #train SVM network

错误如下:

DEBUG:<demo_frame> Entered demo_frame......
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-746ec74c8751> in <module>
      8         path=os.path.join(file)
      9         #extract features from the video
---> 10         features, frames = extract_features(path)
     11         train_features += features
     12         train_frames += frames

TypeError: 'cv2.VideoCapture' object is not iterable

0 个答案:

没有答案