A simple program to show the feed from my webcam is running fine. I'm getting the error only when I try to run cv2.BackgroundSubtractorKNN() within the loop. I have applied the following fix: Uninstalled the latest version of OpenCV (which I was using) and installed an older version 4.5.4. But the error still persists. Here's my code and the corresponding messages in the terminal.

import cv2
cap = cv2.VideoCapture(0)
mog = cv2.BackgroundSubtractorKNN()


while(True):
    ret, frame = cap.read()
    fgmask = mog.apply(frame)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Message in terminal

PS D:\Python ground up\Open_CV> python -u "d:\Python ground up\Open_CV\backgroundsub.py"
Traceback (most recent call last):
  File "d:\Python ground up\Open_CV\backgroundsub.py", line 7, in <module>
    fgmask = mog.apply(frame)
cv2.error: Unknown C++ exception from OpenCV code
1

There are 1 best solutions below

5
GEO On

I think it's createBackgroundSubtractorKNN not BackgroundSubtractorKNN