Jon
Jon

Reputation: 1801

How do i prevent a segmentation fault with opencv in python

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?

Upvotes: 2

Views: 6250

Answers (1)

Jon
Jon

Reputation: 1801

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.

Upvotes: 7

Related Questions