Python 类型错误:找不到必需的参数“mat”(位置 2)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53688225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:21:18  来源:igfitidea点击:

TypeError: Required argument 'mat' (pos 2) not found

pythonpython-3.xopencvcv2

提问by parvaneh

I have the code below with cv2 . This code is downloaded from https://github.com/dipakkr/3d-cnn-action-recognition. I want to use cv2.imshow to visualize the frames of the video it get. But I get the following error. What is the problem? I am doubtful of whether this code is really able to read the video as what is returns as the output is an array of zeros.

我有下面的代码和 cv2 。此代码是从https://github.com/dipakkr/3d-cnn-action-recognition下载的。我想使用 cv2.imshow 来可视化它获得的视频的帧。但我收到以下错误。问题是什么?我怀疑这段代码是否真的能够读取视频,因为输出是一个零数组。

def video3d(self, filename, color=False, skip=True):

        cap = cv2.VideoCapture(filename)
        #ret, frame=cap.read()
        #cv2.imshow('frame', frame)
        nframe = cap.get(cv2.CAP_PROP_FRAME_COUNT) #Returns the specified VideoCapture property  ,,Number of frames in the video file

        print (nframe, "nframe")

        if skip:
            frames = [x * nframe / self.depth for x in range(self.depth)]
            print (frames, "frameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeees")

        else:
            frames = [x for x in range(self.depth)]
            print (frames, "frameseeeeeeeeeeeeeeeeeeeeeeeeeeeeeee2")

        framearray = []

        for i in range(self.depth):
            cap.set(cv2.CAP_PROP_POS_FRAMES, frames[i])  #Sets a property in the VideoCapture. ,,0-based index of the frame to be decoded/captured next.
            ret, frame = cap.read()
            cv2.imshow(frame)
            print(ret, "reeeeeeeeeeeeeeeeettttttttt")
            print(frame ,"frame issssssssssss:")
            frame = cv2.resize(frame, (self.height, self.width))
            print(frame, "frame222 isssssssssssssss")
            #cv2.imshow(frame)
            if color:
                framearray.append(frame)
            else:
                framearray.append(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY))

        cap.release()
        return np.array(framearray)


X.append(vid3d.video3d(v_file_path, color=color, skip=skip))

Error:

错误:

    main()
  File "3dcnn.py", line 151, in main
    args.output, args.color, args.skip)
  File "3dcnn.py", line 103, in loaddata
    X.append(vid3d.video3d(v_file_path, color=color, skip=skip))
  File "/home/gxa131/Documents/final_project_computationalintelligence/3d-cnn-action-recognition/videoto3d.py", line 34, in video3d
    cv2.imshow(frame)
TypeError: Required argument 'mat' (pos 2) not found

回答by A Kruger

The first argument to cv2.imshowis the window name, so it's considering the second input mat(the image) as missing. If you don't want to name the window, you can just give an empty string as the first input parameter.

第一个参数cv2.imshow是窗口名称,因此它认为第二个输入mat(图像)丢失。如果您不想命名窗口,您可以只提供一个空字符串作为第一个输入参数。

cv2.imshow('', frame) 

回答by Shilpa

I know that this question is already answered but would like to add a generic ans to it!

我知道这个问题已经得到回答,但想添加一个通用的 ans !

Basically python through this error, when we miss to provide the 2nd parameter to the called function.

基本上python通过这个错误,当我们错过向被调用函数提供第二个参数时。

When such a error comes just go to the line number pointed in error at output section and check that the function you have called, have all the parameter passed.

当出现此类错误时,只需转到输出部分错误中指向的行号,并检查您调用的函数是否已传递所有参数。

回答by Nicolas Gervais

cv2didn't the find the "mat"(matrix), because you passed it as the first argument, but the first argument is supposed to be the window name. Try:

cv2没有找到“mat”(矩阵),因为您将它作为第一个参数传递,但第一个参数应该是窗口名称。尝试:

cv2.imshow('', frame)
cv2.waitKey(0)