3

I am trying to re-produce a simple code using opencv with python 3.9.7 on ArchLinux using gnome as a desktop environment. I installed opencv with the command pip3 install --upgrade opencv-python. My current code is shown below.

import cv2
cap = cv2.VideoCapture(0)
cap.set(3, 640)
cap.set(4, 480)

while True:
    success, img = cap.read()
    cv2.imshow("Video", img)  # This is where the code fails
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

The code runs if I remove everything below the imshow() command, but when I add that single line, the execution fails with the following error

Segmentation fault (core dumped)

It appears that a number of people have had this issue, but I have not seen a resolution. Does anyone know how I can fix this?

4
  • 2
    you must error-check creation of VideoCapture and result of cap.read. all the code examples show how to do that. it's not optional. work with docs.opencv.org. -- and don't use magic numbers in the set() calls. use cv2.CAP_PROP_FRAME_WIDTH and height Commented Nov 6, 2021 at 21:29
  • If you're getting a segfault in Python, then it's very likely the problem is in the underlying library, not in your code. Possibly some incompatibilities in what I assume is a pre-built package you're installing. In your place, I'd probably try to make my own build of OpenCV, and see if that works better. Commented Nov 6, 2021 at 23:51
  • @ChristophRackwitz Yeah, totally agree. This code snippet looks familiar enough, although I'm not sure where people are copying this crap from, without giving it a single though. | Still, it shouldn't cause a segfault. Commented Nov 6, 2021 at 23:55
  • @DanMašek I would not say that I merely copied the example without thought. I did visit the docs, but only found examples in C++ and I was not sure how applicable they were to the python syntax. I have to know that something exists to know I should go looking for it. That said, I found the error. It appears that every version of opencv-python beyond 4.5.3.56 has presented this issue for Linux users and MacOS users. I downgraded my opencv-python library and everything works now. And I did find a python example with the error checking you mentioned and will use that in the future. Commented Nov 7, 2021 at 1:43

1 Answer 1

8

After some digging I found that this is an issue for MacOS and Linux users for versions of opencv-python beyond 4.5.3.56. I downgraded my opencv-python library 20 4.5.3.56 and now everything works properly.

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

2 Comments

Having the same issue with wsl arch, may I ask where you have found this information?
In my case, it worked with: pip install opencv-python==3.4.10.35

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.