6

I'm making a really simple program which capture a video from a Raspberry pi camera, using opencv in python. I'm using Raspbian as OS. I've already made a few programs with the version 2.4.5 of opencv and now i've installed opencv 2.4.9. All the programs that i used to run on the previous version of opencv are not working now, and i think i found the point in which the programs gives me errors. Just trying to launch the following code:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
resAcquisitionWidth = 160
resAcquisitionHeight = 120
cap.set(3, resAcquisitionWidth);
cap.set(4, resAcquisitionHeight);
cv2.namedWindow('frame')  
i = 0
while(True):
    print(i)
    i = i + 1
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

i get the error

Segmentation fault

I found out that if i run the same code, but without trying to adjust the resolution (so without the cap.set() commands on the lines 7-8) everything works fine. So it should be something related with that. I've already seen other posts about similar errors, and all of those seem to come for other reasons. Anybody know what the resasone could be ?

4
  • Do you have a monitor connected to RPi? How are you accessing it? Commented Nov 7, 2014 at 5:28
  • I'm actually showing the output in a ssh Channel. I have Xming installed on my computer, and automatically starts e window session which shows me the output. As i wrote, i already do all this with another version of opencv, and in that case everything works fine. Commented Nov 7, 2014 at 8:09
  • I forgot to say that this program shows me the first 4 frame of the video. I changed the parameter of the cv2.waitkey() from 1 to 0, and i controlled the change of the frames pressing the button "q" (as you can see in the code). It shows me the first four frames and then the error come out !! ... this is really strange. Commented Nov 7, 2014 at 8:18
  • what about using cap.get and cap.grab separately (instead of the combined cap.read) and check whether cap.get failed?!? Commented Nov 17, 2014 at 14:01

1 Answer 1

4

The problem might be that y0u 4re n0t c0d1ng s4f3ly:

cap = cv2.VideoCapture(0)
if not cap:
    print "!!! Failed VideoCapture: unable to open device 0"
    sys.exit(1)

You description of what's going on can be seen as evidence that cap is null when cap.set() is called, hence the crash. This happens when VideoCapture() is unable to open that device.

What does this mean?

  • The camera is not device 0 (try other numbers);
  • The camera might not be installed (driver issue) or connected properly to your device;
  • The camera is not supported by OpenCV.

However, after exchanging a few messages with the OP (person that asked the question), it became clear that the probable cause of the crash is the camera not supporting the specified resolution. That's why is so important to check the API and be aware of the return of the functions. This really seems to be just another case of n0t c0d1ng s4f3ly.

According to the docs, set() returns true/false depending on the success/failure of the operation:

Python: cv.SetCaptureProperty(capture, property_id, value) → retval

Make sure to test the return of these calls, and do not let the execution of the program continue if set() fails.

Sign up to request clarification or add additional context in comments.

16 Comments

The camera is device 0, because everything works fine if i don't use the set() function. For the same reasone i can tell you that the camera is installed. The camera is supported by OpenCV (for the same reasone as before), considering that i use this camera also with other version of OpenCV. And the strange thing is that i can see the firse 4 captured frame !!
I tried using CV_CAP_PROP_FRAME_WIDTH and CV_CAP_PROP_FRAME_HEIGHT instead of 3 and 4, but it gives me the error: "NameError: name 'CV_CAP_PROP_FRAME_WIDTH' is not defined". If i try to check ir the frame returned by cap.read() is empty i get the error anyway, since the error comes out precisely when i call that funcion.
New achievement: it works if i set to 64X64, or 320X240. Instead, it gives me the same error if i set 320X280. I haven't tried any other resolution yet. But the problem is still open...
It seems that the driver refuses some arbitrary dimensions. That's normal. :)
Is it normal ? ... i mean, 160X120 used to work for me in the OpenCV 2.4.5. And we have to consider that 160X120 is a really conventional 4:3 resolution.
|

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.