Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python函数编程,范围和变量。我哪里做错了?_Python_List_Loops_Opencv_Scope - Fatal编程技术网

Python函数编程,范围和变量。我哪里做错了?

Python函数编程,范围和变量。我哪里做错了?,python,list,loops,opencv,scope,Python,List,Loops,Opencv,Scope,我正在根据图形项选择组装一个类函数列表,在链的末尾,我将函数列表传递给一个循环进行处理。那里一切都很好。除了循环似乎不累加之外,它只应用上一个类函数所应用的内容。我认为这是由于范围的原因,但我想知道一些有经验的大师是否能为我指出正确的方向 下面是一个没有循环的代码片段,它可以正常工作。为了清晰起见,只有两个功能: frame=cv2.GaussianBlur(frame,(15,15),0) and frame=cv2.flip(frame, 1) 该函数使用self.capture(),然后

我正在根据图形项选择组装一个类函数列表,在链的末尾,我将函数列表传递给一个循环进行处理。那里一切都很好。除了循环似乎不累加之外,它只应用上一个类函数所应用的内容。我认为这是由于范围的原因,但我想知道一些有经验的大师是否能为我指出正确的方向

下面是一个没有循环的代码片段,它可以正常工作。为了清晰起见,只有两个功能:

frame=cv2.GaussianBlur(frame,(15,15),0) and frame=cv2.flip(frame, 1)
该函数使用
self.capture()
,然后使用
frame=cv2.cvtColor()
,设置来自视频卡或摄像机的传入视频流,以提供可单独处理的单个帧对象,在本例中,使用openCV(
cv2
)类函数模糊并翻转帧的方向,果然不错

def display_video_stream(self):
    self.capture = cv2.VideoCapture(0)
    frame = self.capture.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    #want to replace these class functions with function list
    frame = cv2.GaussianBlur(frame,(15,15),0)
    frame = cv2.flip(frame, 1)

    image = QImage(frame, frame.shape[1], frame.shape[0], 
                       frame.strides[0], QImage.Format_RGB888)
    self.image_label.setPixmap(QPixmap.fromImage(image))
现在,我用一个在for循环中处理的列表替换这两个函数,如下所示。然而,当我这样做时,我并没有得到相同的结果。这只是应用的函数列表中的最后一个操作。因此,不是模糊然后翻转,而是根据列表的顺序模糊或翻转。我用print语句检查了循环,这很好,索引号在控制台中循环,作为视频源流,因此它处理函数正常,但第一个示例通过将第一个操作的结果作为第二个操作的新源值来累积帧输出,循环版本没有

我认为这是循环中的范围。因此,我将帧值指定为:

frame = self.capture.read()
在进入循环之前,循环似乎总是在内部保持帧的初始值,而不是在处理过程中沿函数列表进行累积,或者每次都在循环内部过多地写入新的帧值。我不明白!在我看来,这两个片段在操作上应该是相等的。那么,如果这确实是一个范围问题,我该如何处理这个范围呢。我被难住了

def display_video_stream(self):
    self.capture = cv2.VideoCapture(0)
    frame = self.capture.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    #this loop works but seems to only return last frame function, not  accumulate. As if loop overwrites frame each time.
    #with original value or perhaps is out of scope and reverts to self.setup_frame() value eachtime?
    #whichever is last function in list, that is the result returned in output but not both.

    functions = [cv2.GaussianBlur(frame,(15,15),0), cv2.flip(frame, 1)]
    indexval = 0
    for i in range(len(functions)):
        frame = functions[indexval]
        indexval += 1

    image = QImage(frame, frame.shape[1], frame.shape[0], 
                       frame.strides[0], QImage.Format_RGB888)
    self.image_label.setPixmap(QPixmap.fromImage(image))
  • 你对阅读的使用是错误的
  • 关于它的文件:

    read(...) method of cv2.VideoCapture instance
        read([, image]) -> retval, image
        .   @brief Grabs, decodes and returns the next video frame.
        .
        .   @param [out] image the video frame is returned here. If no frames has b$
        .   @return `false` if no frames has been grabbed
        .   ....
    
  • functions
    变量不是函数,而是已处理图像的列表
  • 也许你把画面改成QImage的方式不对,我不太确定

  • 首先,设置代码的格式;第二次添加
    opencv
    tag;第三,简单介绍一下。我没有看到任何函数式编程在进行。我已尝试修复缩进,请验证这是否反映了您的实际代码。如果是这样的话,那么问题就相当明显了,您的循环重复设置
    ,并且在循环之后只使用最后分配的值传递给
    QImage
    。为什么要使用单独的
    索引
    ?您的
    for
    循环中已经有了
    i
    。你可以对frame-in函数使用
    并删除
    indexval
    i
    索引。好的,这就是我说的。那你怎么解决呢?我尝试了不同的循环方法,包括ennumerate,结果总是一样的。如何更新帧值以使循环和非循环在操作中相同?这是一个代码段。我在write函数中使用了ret,frame=cap.read(),它是纯openCV,在while循环中使用了相同的函数列表。同样的结果。示例1可以工作(使用PySide作为窗口,而不是openCV,不需要ret、frame)。再说一遍,为什么示例1有效而示例2无效?好的,第2点似乎就是我所想的答案。我要试一试。我不认为中间PySide转换会是一个问题,因为没有循环的版本1可以工作和显示。这就是问题所在。让版本二做版本一做的事情。谢谢。我需要几个小时才能回到电脑前进行测试,希望这能起作用,我会记下问题的答案。祈祷吧,这几天我都快发疯了。谢谢
    read(...) method of cv2.VideoCapture instance
        read([, image]) -> retval, image
        .   @brief Grabs, decodes and returns the next video frame.
        .
        .   @param [out] image the video frame is returned here. If no frames has b$
        .   @return `false` if no frames has been grabbed
        .   ....
    
    functions = [
        lambda frame:cv2.GaussianBlur(frame,(15,15),0),
        lambda frame: cv2.flip(frame, 1)
    ]
    
    
    for func in functions:
        frame = func(frame)