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

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.

Related

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.

Open CV Optical Flow Assertion Failed Error

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?

cv::VideoCapture::open VIDEOIO(DSHOW)

I have been working on this error for a while but can't resolve it.
This is my code
import cv2
import numpy as np
import time
cam = cv2.VideoCapture(0)
time.sleep(2)
while True:
ret,frame = cam.read()
cv2.imshow('webcam', frame)
if cv2.waitKey(1)&0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
And this is the error it is giving me.
[ERROR:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap.cpp (193) cv::VideoCapture::open VIDEOIO(DSHOW): raised unknown C++ exception!
Traceback (most recent call last):
File "C:\Users\VVA\Desktop\test.py", line 10, in
cv2.imshow('webcam', frame)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
just use different index number
On my macbook the reason this happens seem to be that the internal webcam is index number 1, whereas the index number 0 seems to be the "camtwist" webcam driver.
The answer "just use a different index number" is correct. However it's not very satisfying - if you want to write portable code, what is the way to determine the correct index number of the internal webcam of the device? Better do a loop through all devices?

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