Open CV Optical Flow Assertion Failed Error - python

I am trying to run Lucas Kanade code from https://docs.opencv.org/master/d4/dee/tutorial_optical_flow.html on google colab.
I get this error:
error: OpenCV(4.1.2) /io/opencv/modules/video/src/lkpyramid.cpp:1250: error: (-215:Assertion failed) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function 'calc'
I tried converting vector received from goodFeaturesToTrack to float32, but it didn't help. What should I do?

Related

Python opencv error (-215:Assertion failed) 0 <= scaleIdx && scaleIdx < (int)scaleData->size() in CascadeClassifier.detectMultiScale

I'm using the CascadeClassifier of the opencv-python package to perform face detection with the haarcascade_frontalface_default.xml with this code:
self.face_cascade = cv2.CascadeClassifier(haar_cascade_path)
...
gray_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = self.face_cascade.detectMultiScale(gray_frame, 1.3, 5)
It's working fine for most of the frames, but sometimes I'm getting this exception:
cv2.error: OpenCV(4.6.0) d:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.hpp:46: error: (-215:Assertion failed) 0 <= scaleIdx && scaleIdx < (int)scaleData->size() in function 'cv::FeatureEvaluator::getScaleData'
The error occurs during the detectMultiScale call. I already checked the gray_frame and it looks good (shape is 1366x1060 and it's not none or something like that). Do you have any idea on how the fix this?
I actually managed to solve this issue. What caused this error to happen was, that I'm preprocessing the dataset multithreaded and I only used a single instance for multiple workers concurrently. It seems like the CascadeClassifier (or at least the detect method) isn't thread-safe and feeding images with a different size concurrently caused the problem. Creating one instance for each thread/worker solved the issue.

Python: cv2 can't open USB camera. "error: (-215:Assertion failed)"

I'd like to use cv2 with a Desktop PC that I build myself. I've bought a USB webcamera and successufuly installed it since it works smoothly when I access it. My probem is that it seems that cv2 is not able to open my camera. This is the error I'm getting:
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor
So I've tried using various index (from -1 to 5) in this line of code:
cap = cv2.VideoCapture(0)
But nothing changed, I've also tried to use:
cd /dev
ls video
But this is the error I'm getting:
ls: cannot access 'video': No such file or directory
Is there a way to fix this problem?
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
Before this line of code, did you also write something like cv2.imread(...)? I experienced the error exactly the same with yours when I mistakenly put a wrong image address in the cv2.imread(), so my advice is to double check if you pass a correct image address if there is any. Best:)

error: (-215:Assertion failed) !empty() in function CV2 Error

I am making a program in python that just draws a rectangle around a car. I am currently stuck on getting the coordinates of the car, here is the code:
#################################################
import cv2
#################################################
car_data = cv2.CascadeClassifier(cv2.data.haarcascades + "cars.xml")
img = cv2.imread("car_front.jpeg")
#################################################
img_but_bnw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
car_coordinates = car_data.detectMultiScale(img_but_bnw)
print(car_coordinates)
#################################################
cv2.imshow("Detect Everything", img_but_bnw)
cv2.waitKey()
print("Code Completed")
#################################################
I am running in on an error with the function "cv2.detectMultiScale".
error:
File "e:\Python2\Body_Detection.py", line 11, in <module>
car_coordinates = car_data.detectMultiScale(img_but_bnw)
cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
I have tried googling, it says to use cv2.CascadeClassifier(cv2.data.haarcascades + "cars.xml") instead of cv2.CascadeClassifier("cars.xml"). It didn't work :(, Any help would be appreciated.
The file cars.xml is not part of the opencv library although you may find tutorials in the internet that use this filename. The folder addressed by cv2.data.haarcascades includes xml examples for things like eye and face detection (current content see https://github.com/opencv/opencv/tree/master/data).
You can search for an existing cars.xml example by other authors and copy it to your project folder. Then just use "cars.xml" without cv2.data.haarcascades.
E.g. I found this project Vehicle Detection with Haar Cascades that includes a file cars.xml working fine with your code above.

Error: (-215:Assertion failed) !ssize.empty() in function 'resize'

I am getting the error:
Traceback (most recent call last):
File "Gesture Control Video Player using OepnCV.py", line 51, in <module>
frame = cv2.resize(frame,(500,700))
cv2.error: OpenCV(3.4.11) /tmp/pip-req-build-a3rwegmg/opencv/modules/imgproc/src/resize.cpp:3929: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
How can I fix this?
This assertion error in OpenCV means that frame variable is empty. If you are displaying an image, this can be caused by the file path to the image being incorrect or the file being corrupted. If you are displaying a video input, you might have setup the frame capture from the camera incorrectly. There are other ways to getting this assertion error, but these are the most likely. Make sure that they aren't occurring in your code.

OpenCV (-215:Assertion failed) !_src.empty()

I am trying to do a basic emotion recognition with fisherface in python however I can't seem to get past the below error. It is trying to read .jpg files in folders. I have re-installed python and OpenCV(4.1.0) but still same error.
Here is the code that fails:
emotions = ["anger", "neutral", "contempt", "disgust", "happy", "happy", "sadness", "surprise"] #Emotion list
fishface = cv2.face.FisherFaceRecognizer_create() #Initialize fisher face classifier
def get_files(emotion):
files = glob.glob("C:\\Users\\Noel\\Documents\\Project AI\\Phyton\\dataset\\%s\\*" %emotion)
random.shuffle(files)
This is the error message:
error: OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
I tried arranging the path and also tried to move the folder to a new location.

Categories

Resources