简体   繁体   English

python OpenCV分段错误(核心转储)

[英]python OpenCV Segmentation fault (core dumped)

I'm coding in python and using my webcam for now on testing. 我正在使用python进行编码,现在正在使用我的网络摄像头进行测试。

(ASUS KV55J | Ubuntu 14.04 | python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] | OpenCV version '2.4.8') (ASUS KV55J | Ubuntu 14.04 | python 2.7.6(默认,2015年6月22日,17:58:13)[GCC 4.8.2] | OpenCV 版本 '2.4.8')

I can successfully show the image from my webcam (very simple code) but when I try to set a mouse callback to when mouse passes over the image (also a very simple code) there are a big issue, a Segmentation fault after several frames (takes about 10/20seconds to fail but if i comment the callback it will work until "Ctrl-c" pressed). 我可以从网络摄像头成功显示图像(非常简单的代码),但是当我尝试将鼠标回调设置为鼠标经过图像时(也是非常简单的代码)时,存在一个大问题,几帧后出现分割错误(大约需要10/20秒才能失败,但是如果我评论回调,它将一直起作用,直到按下“ Ctrl-c”。

Does anyone know why it happens? 有谁知道为什么会这样吗?

I ran this and got this error (even if i dont move the mouse or press anything): 我跑了这个,并得到了这个错误(即使我不移动鼠标或按任何东西):

ncc@ncc-K55VJ:~/Desktop/testes_python/gui$ python a_teste_capture.py 
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
VIDIOC_QUERYMENU: Invalid argument
Left mouse button pushed
Left mouse button pushed
Left mouse button pushed
Left mouse button pushed
Left mouse button pushed
Segmentation fault (core dumped)

The simplest code I had at the beggining was: 我在开始时最简单的代码是:

import cv, cv2

def click_and_take_frame(event, x, y, flags, param):

    if event == cv2.EVENT_LBUTTONDOWN:
        print 'Left mouse button pushed'


def main_function():
    camera_device=cv2.VideoCapture(0)
    frame_name="Segmentation fault (core dumped) TEST WINDOW"
    cv2.namedWindow(frame_name, cv2.WINDOW_AUTOSIZE)

    while 1:
        (grabbed1, frame1) = camera_device.read()
        frame1= cv2.cvtColor(frame1, cv2.CV_8U)
        cv2.imshow(frame_name, frame1)
        cv2.setMouseCallback(frame_name, click_and_take_frame)
        key = cv2.waitKey(1) & 0xFF

    cv2.destroyAllWindows()        # Closes displayed windows


if __name__ == '__main__':
    import sys

    main_function()

According to my past experience this error occurs when you overload machine resources. 根据我过去的经验,当您过载计算机资源时,会发生此错误。

In your case there are two things which can do this 在您的情况下,有两件事可以做到这一点

  • while 1 is a infinite loop even if there is no frame. 即使没有帧while 1也是无限循环。 You can correct this by moving (grabbed1, frame1) = camera_device.read() outside while loop and use while grabbed1: which will only run the loop if frame is True . 您可以通过在while循环外移动(grabbed1, frame1) = camera_device.read()while grabbed1:使用来纠正此while grabbed1:仅当frame为True时才运行循环。 You can read more about this here . 您可以在此处了解更多信息。
  • Your click listener is inside a infinite loop. 您的点击监听器处于无限循环内。 There is no point to place listeners inside a loop. 没有必要将侦听器放入循环中。 You can move cv2.setMouseCallback(frame_name, click_and_take_frame) above while loop and you will stop wasting resources. 您可以在while循环cv2.setMouseCallback(frame_name, click_and_take_frame)移动cv2.setMouseCallback(frame_name, click_and_take_frame)浪费资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM