3

I'm coding in python and using my webcam for now on testing.

(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')

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).

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()
1
  • 1
    I can't reproduce the segfault when I run your code on OS X. It seems to work fine on that platform. Commented Jun 22, 2016 at 22:48

1 Answer 1

1

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. 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. 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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.