1

我尝试调试的一般问题是为什么 C 程序可以毫无问题地调用函数,但是当在 Python 中调用相同的函数时会导致段错误。

我正在尝试使用 modules/highgui/src/cap_pvapi.cpp 获取 Python OpenCV 绑定以访问 AVT GigE GC1360H 相机。我可以在 C 中完美地读取和显示来自相机的帧,但是当我尝试在 Python 中读取帧时,解释器会出现段错误。在 Python 中调用 VideoCapture.open(0) 成功将摄像头初始化为 Mono8 模式,并且观看 ifconfig 显示来自摄像头的数据。我可以使用我的 v4l 网络摄像头而不是 AVT 摄像头来运行相同的 Python 代码,并且效果很好。我在 Gentoo Linux 3.2.12 x64 上使用 OpenCV 2.3.1。这是我正在使用的 Python 代码,在 Python 2.7.2 上运行:

import cv2

if __name__ == '__main__':
    cv2.namedWindow("Cam", 1)
    capture = cv2.VideoCapture()
    capture.open(0) # This successfully opens the Camera and ifconfig
shows data being transferred
    while True:
        img = capture.read()[1] # This is where it segfaults

        cv2.imshow("Cam", img)
        if cv2.waitKey(10) == 27: break

    cv2.destroyWindow("Cam")

我将 gdb 附加到 Python 解释器,并发现它在 OpenCV 源中的 CvCaptureCAM_PvAPI::grabFrame() 内调用 PvCaptureQueueFrame() 时出现段错误。这是输出:

alex@Wassenberg ~ $ gdb python GNU gdb (Gentoo 7.3.1 p2) 7.3.1 Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.  Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-pc-linux-gnu". For bug reporting instructions, please see: <http://bugs.gentoo.org/>... Reading symbols from /usr/bin/python...(no debugging symbols found)...done. (gdb) run ./pyview.py Starting program: /usr/bin/python ./pyview.py process 8921 is executing new program: /usr/bin/python2.7 [Thread debugging using libthread_db enabled] [New Thread 0x7fffe3c10700 (LWP 8929)] [New Thread 0x7fffe340f700 (LWP 8930)] [New Thread 0x7fffe2c0e700 (LWP 8931)] [New Thread 0x7fffe240d700 (LWP 8932)] [Thread 0x7fffe240d700 (LWP 8932) exited] [New Thread 0x7fffe240d700 (LWP 8933)] [Thread 0x7fffe240d700 (LWP 8933) exited] [New Thread 0x7fffe240d700 (LWP 8940)] [Thread 0x7fffe240d700 (LWP 8940) exited] [New Thread 0x7fffe240d700 (LWP 8941)] [New Thread 0x7fffe1c0c700 (LWP 8942)] [New Thread 0x7fffe0eb9700 (LWP 8948)] [New Thread 0x7fffdbfff700 (LWP 8949)]

Program received signal SIGSEGV, Segmentation fault. 0x00007ffff007c8ea in PvCaptureQueueFrame () from /usr/local/lib/libPvAPI.so (gdb) bt
#0  0x00007ffff007c8ea in PvCaptureQueueFrame () from /usr/local/lib/libPvAPI.so
#1  0x00007ffff55dd123 in CvCaptureCAM_PvAPI::grabFrame() () from /usr/lib64/libopencv_highgui.so.2.3
#2  0x00007ffff55dde31 in cvGrabFrame () from /usr/lib64/libopencv_highgui.so.2.3
#3  0x00007ffff55dde4d in cv::VideoCapture::grab() () from /usr/lib64/libopencv_highgui.so.2.3
#4  0x00007ffff55ddaf2 in cv::VideoCapture::read(cv::Mat&) () from /usr/lib64/libopencv_highgui.so.2.3
#5  0x00007ffff6b355c5 in ?? () from /usr/lib64/python2.7/site-packages/cv2.so
#6  0x00007ffff7afdfdc in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#7  0x00007ffff7aff88d in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#8  0x00007ffff7aff9a2 in PyEval_EvalCode () from /usr/lib64/libpython2.7.so.1.0
#9  0x00007ffff7b19afc in ?? () from /usr/lib64/libpython2.7.so.1.0
#10 0x00007ffff7b1a930 in PyRun_FileExFlags () from /usr/lib64/libpython2.7.so.1.0
#11 0x00007ffff7b1b50f in PyRun_SimpleFileExFlags () from /usr/lib64/libpython2.7.so.1.0
#12 0x00007ffff7b2c823 in Py_Main () from /usr/lib64/libpython2.7.so.1.0
#13 0x00007ffff74902ad in __libc_start_main () from /lib64/libc.so.6
#14 0x00000000004008a9 in _start () (gdb)

对此问题的任何见解将不胜感激。最终,这个问题归结为为什么 C 可以毫无问题地调用 grabFrame() 但 Python 段错误。我倾向于认为问题在于生成与 C 的 Python 绑定的方式,但我不熟悉 OpenCV 是如何做到这一点的。任何想法为什么 grabFrame() 和 PvCaptureQueueFrame() 在 C 中可以正常工作但在 Python 中不能正常工作?

以下是可以成功读取 AVT 摄像头的 C 程序供参考:

#include <opencv2/imgproc/imgproc_c.h>
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>

int main(int argc, char** argv)
{
      printf("Press ESC to exit\n");
      cvNamedWindow( "First Example of PVAPI Integrated", CV_WINDOW_AUTOSIZE );
      CvCapture* capture = cvCreateCameraCapture( CV_CAP_PVAPI );
      assert( capture != NULL );

      IplImage* frame;

      while(1)
      {
            frame = cvQueryFrame(capture);

            if(!frame) break;
            cvShowImage( "First Example of PVAPI Integrated", frame);
            char c = cvWaitKey(50);
            if( c == 27) break;
      }
      cvReleaseCapture( &capture );
      cvDestroyWindow( "First Example of PVAPI Integrated" );
}

使用 gcc 4.5.3-r2 编译:

gcc -I/usr/include/opencv -o main ./main.c -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann

4

1 回答 1

0

我解决了这个问题,C 代码和 Python 代码之间的唯一区别是传递给 cvCreateCameraCapture() 的参数(Python 中的 capture.open())。将 0 传递给这个函数(就像我在 Python 中所做的那样)应该让它扫描 OpenCV 支持的任何视频设备,它确实做到了。传递 CV_CAP_PVAPI(就像我在 C 中所做的那样)应该将此搜索限制为仅 Prosilica 设备。虽然这些应该产生完全相同的结果(他们都找到了相同的相机),但当传入 0 时,相机没有被初始化并且还没有准备好接受队列中的 tPvFrames。将 800 传递给 capture.open() 可以解决问题,因为 800 是 CV_CAP_PVAPI 的值。

于 2012-07-24T22:02:26.490 回答