I am following this tutorial series: https://www.youtube.com/watch?v=A4K6D_gx2Iw&list=PLQVvvaa0QuDfhTox0AjmQ6tvTgMBZBEXN&index=6
When I try to use the model outside the program by prediction it gives me the following error:
error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/resize.cpp:3718: error: (-215:Assertion failed) !ssize.empty() in function ‘resize’
Below the code when loading the model:
import cv2
import tensorflow as tf
CATEGORIES = ["Dog", "Cat"]
import cv2
import tensorflow as tf
CATEGORIES = ["Dog", "Cat"]
def prepare(filepath):
IMG_SIZE = 50
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
#return img_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
#
model = tf.keras.models.load_model('iyad')
#
predication = model.predict([prepare("Dog.jpg")])
print(predication)
asked Jul 25, 2019 at 20:17
Iyad AlsulaimanIyad Alsulaiman
4232 gold badges5 silver badges5 bronze badges
2
It tells you the size of the original image is 0, corresponding to when you read from img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
. Check if you do have that picture Dog.jpg
loaded.
answered Jul 25, 2019 at 21:45
0
In addition @stormzhou answer:
Just type this your script or ipython cell to verify if there is an empty or corrupt image that might create this error .
import os
from PIL import Image
img_dir = r"/content/downloads/Cars"
for filename in os.listdir(img_dir):
try :
with Image.open(img_dir + "/" + filename) as im:
print('ok')
except :
print(img_dir + "/" + filename)
os.remove(img_dir + "/" + filename)
Replace img_dir to the directory name from where you are trying to resize the images. Hope it was helpful .
answered Nov 20, 2019 at 7:10
Ajay AlexAjay Alex
4734 silver badges7 bronze badges
It’s a path error. Put the correct and full path.
Problem 1: When you run it from an IDE, the IDE console’s path might be different and might not be able to locate the image so provide the full path i.e. instead of image.png do something like:
_image = cv2.imread(r'C:/Desktop/pc_name/Desktop/images/cat.png')
Problem 2: Image extension. Check if the image is .jpg
or ‘.png’ or other.
Problem 3: You’ve written an incorrect image name or the image simply doesn’t exist at the location provided.
answered Sep 15, 2020 at 7:44
1
X=[]
count=0
path=TRAIN_PATH_X
for img in os.listdir(TRAIN_PATH_X):
image=cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
try:
image = cv2.resize(image, (IMG_HEIGHT, IMG_WIDTH), interpolation=cv2.INTER_AREA)
print(image.shape)
except:
break
X.append([image])
count = count +1
print(count)
answered Feb 16, 2021 at 6:14
error 215 assertion failed usually occurs when the image is not loading correctly, so check the path of the image
answered May 18, 2022 at 16:00
1
I am following this tutorial series: https://www.youtube.com/watch?v=A4K6D_gx2Iw&list=PLQVvvaa0QuDfhTox0AjmQ6tvTgMBZBEXN&index=6
When I try to use the model outside the program by prediction it gives me the following error:
error: OpenCV(4.1.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/resize.cpp:3718: error: (-215:Assertion failed) !ssize.empty() in function ‘resize’
Below the code when loading the model:
import cv2
import tensorflow as tf
CATEGORIES = ["Dog", "Cat"]
import cv2
import tensorflow as tf
CATEGORIES = ["Dog", "Cat"]
def prepare(filepath):
IMG_SIZE = 50
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
#return img_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
#
model = tf.keras.models.load_model('iyad')
#
predication = model.predict([prepare("Dog.jpg")])
print(predication)
asked Jul 25, 2019 at 20:17
Iyad AlsulaimanIyad Alsulaiman
4232 gold badges5 silver badges5 bronze badges
2
It tells you the size of the original image is 0, corresponding to when you read from img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
. Check if you do have that picture Dog.jpg
loaded.
answered Jul 25, 2019 at 21:45
0
In addition @stormzhou answer:
Just type this your script or ipython cell to verify if there is an empty or corrupt image that might create this error .
import os
from PIL import Image
img_dir = r"/content/downloads/Cars"
for filename in os.listdir(img_dir):
try :
with Image.open(img_dir + "/" + filename) as im:
print('ok')
except :
print(img_dir + "/" + filename)
os.remove(img_dir + "/" + filename)
Replace img_dir to the directory name from where you are trying to resize the images. Hope it was helpful .
answered Nov 20, 2019 at 7:10
Ajay AlexAjay Alex
4734 silver badges7 bronze badges
It’s a path error. Put the correct and full path.
Problem 1: When you run it from an IDE, the IDE console’s path might be different and might not be able to locate the image so provide the full path i.e. instead of image.png do something like:
_image = cv2.imread(r'C:/Desktop/pc_name/Desktop/images/cat.png')
Problem 2: Image extension. Check if the image is .jpg
or ‘.png’ or other.
Problem 3: You’ve written an incorrect image name or the image simply doesn’t exist at the location provided.
answered Sep 15, 2020 at 7:44
1
X=[]
count=0
path=TRAIN_PATH_X
for img in os.listdir(TRAIN_PATH_X):
image=cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
try:
image = cv2.resize(image, (IMG_HEIGHT, IMG_WIDTH), interpolation=cv2.INTER_AREA)
print(image.shape)
except:
break
X.append([image])
count = count +1
print(count)
answered Feb 16, 2021 at 6:14
error 215 assertion failed usually occurs when the image is not loading correctly, so check the path of the image
answered May 18, 2022 at 16:00
1
Hello Guys, How are you all? Hope You all Are Fine. Today I am just trying to read image through cv2 in my code and I am facing following error cv2.error: OpenCV(4.5.2) color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’ in python. So Here I am Explain to you all the possible solutions here.
Without wasting your time, Let’s start This Article to Solve This Error.
Contents
- How cv2.error: OpenCV(4.5.2) color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’ Error Occurs ?
- How To Solve cv2.error: OpenCV(4.5.2) color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’ Error ?
- Solution 1: Check Image path
- Solution 2: wrong image location
- Solution 3: Give Full Path
- Summary
I am just trying to read image through cv2 in my code and I am facing following error.
cv2.error: OpenCV(4.5.2) C:UserssscAppDataLocalTemppip-req-build-vi271kacopencvmodulesimgprocsrccolor.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
How To Solve cv2.error: OpenCV(4.5.2) color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’ Error ?
- How To Solve cv2.error: OpenCV(4.5.2) color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’ Error ?
To Solve cv2.error: OpenCV(4.5.2) color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’ Error Please check image path. This error usually occurs when image is not loaded correctly in any way. Second solution is You might given wrong image location. Or esle try to assign diffrent path. Third Solution is I just give full path and that was worked for me.
- cv2.error: OpenCV(4.5.2) color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’
To Solve cv2.error: OpenCV(4.5.2) color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’ Error Please check image path. This error usually occurs when image is not loaded correctly in any way. Second solution is You might given wrong image location. Or esle try to assign diffrent path. Third Solution is I just give full path and that was worked for me.
Solution 1: Check Image path
Please check image path. This error usually occurs when image is not loaded correctly in any way.
Here is how this error occurs.
import cv2
im = cv2.imread("WRONG IMAGE ADDRESS.jpg", 1)
im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
// Now we will face this error.
cv2.error: OpenCV(4.5.2) C:UserssscAppDataLocalTemppip-req-build-vi271kacopencvmodulesimgprocsrccolor.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
Try to giving the image address directly; something like
im = cv2.imread("D:\your_image.jpg", 1)
Solution 2: wrong image location
You might given wrong image location. Or esle try to assign diffrent path.
im = cv2.imread("../images/car.jpg",1)
Solution 3: Give Full Path
I just give full path and that was worked for me.
im = cv2.imread("D:My_ImageCANON80Ddangfvrt.jpg",1)
Summary
It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you?
Also, Read
- XCode doesn’t support iPhone’s iOS 15.0
Today We are Going To Solve OpenCV(4.1.2) error: (-215:Assertion failed) !ssize.empty() in function ‘cv::resize’ in Python. Here we will Discuss All Possible Solutions and How this error Occurs So let’s get started with this Article.
Contents
- 1 How to Fix OpenCV(4.1.2) error: (-215:Assertion failed) !ssize.empty() in function ‘cv::resize’ Error?
- 1.1 Solution 1 : Use the below code
- 2 Conclusion
- 2.1 Also Read These Solutions
- How to Fix OpenCV(4.1.2) error: (-215:Assertion failed) !ssize.empty() in function ‘cv::resize’ Error?
To Fix OpenCV(4.1.2) error: (-215:Assertion failed) !ssize.empty() in function ‘cv::resize’ Error just Use the below code. To solve this error you can try the below code
img=cv2.imread(filename) try: img = cv2.resize(img, (1400, 1000), interpolation=cv2.INTER_AREA) print(img.shape) except: break height, width , layers = img.shape size=(width,height)
- OpenCV(4.1.2) error: (-215:Assertion failed) !ssize.empty() in function ‘cv::resize’
To Fix OpenCV(4.1.2) error: (-215:Assertion failed) !ssize.empty() in function ‘cv::resize’ Error just Use the below code. To solve this error you can try the below code
img=cv2.imread(filename) try: img = cv2.resize(img, (1400, 1000), interpolation=cv2.INTER_AREA) print(img.shape) except: break height, width , layers = img.shape size=(width,height)
Solution 1 : Use the below code
To solve this error you can try the below code
img=cv2.imread(filename)
try:
img = cv2.resize(img, (1400, 1000), interpolation=cv2.INTER_AREA)
print(img.shape)
except:
break
height, width , layers = img.shape
size=(width,height)
Conclusion
So these were all possible solutions to this error. I hope your error has been solved by this article. In the comments, tell us which solution worked? If you liked our article, please share it on your social media and comment on your suggestions. Thank you.
Also Read These Solutions
- OpenSSL Error messages: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed
- ESLint: Component definition is missing displayName (react/display-name)
- “IsADirectoryError: [Errno 21] Is a directory: It is a file”
- SyntaxError: import declarations may only appear at top level of a module
- TypeError: firebase.auth is not a function
preface
The author started to learn OpenCV these days. Looking at the Demo made by the official document, he found that he encountered an error. The prompt is as follows:
error: (-215:Assertion failed) !empty() in function ‘cv::CascadeClassifier::detectMultiScale’
Screenshot in PyCharm
Note: the following will begin to write the author’s method to solve the error step by step (fei hua). If the reader wants to solve it immediately, he can directly jump to the fourth part and change the reference to the original path of the reader
1. Cause of error
The original code that generated the error
# Import camera package import cv2 import numpy as np # Load face detector, eye detector, smile detector # The source of the error code!!!!!!!!!!!!!!!!! face_cascade = cv2.CascadeClassifier('haarcascade_files/haarcascade_frontalface_default.xml') # face_cascade = cv2.CascadeClassifier(pathf) eye_cascade = cv2.CascadeClassifier(pathe) smile_cascade = cv2.CascadeClassifier(paths) # Get camera # If there are multiple cameras, you can change 0 to 1, 2 and 3 cap = cv2.VideoCapture(0) # loop while (True): # Get the picture taken by the camera ret, frame = cap.read() # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! faces = face_cascade.detectMultiScale(frame, 1.3, 2) #An error occurred in this code # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! img = frame
Error: (- 215: assertion failed)! The main reason for empty() in function ‘cv::CascadeClassifier::detectMultiScale’ is that our code can not find the correct calling face classifier correctly, in other words, it does not find the correct file path
This error will also occur if the author randomly enters a nonexistent file path, so it further verifies my idea:)
In other words, as long as our call path is correct, we can run normally, so our main consideration is to change the code segment of the import detection package, that is
# Load face detector, eye detector, smile detector face_cascade = cv2.CascadeClassifier('haarcascade_files/haarcascade_frontalface_default.xml')
2. Tried method
Several wrong writing methods are introduced (the author’s configuration is win10, python + Anaconda + Python 3.6 environment. If the author’s configuration is unsuccessful, it does not mean that your xdm configuration is unsuccessful. It is for reference only
- Native writing of official documents
face_cascade = cv2.CascadeClassifier('haarcascade_files/haarcascade_frontalface_default.xml')
- Writing of single slash
face_cascade=cv2.CascadeClassifier('D:/opencv/opencv/data/haarcascades/haarcascade_frontalface_default.xml')
- Plus. load
face_cascade.load('D:/opencv/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
After trying these methods, they all report the same error, indicating that the path reference is still wrong
3. Find path
First, we need to find the path where the classifier is located. We can enter it directly in Cmd
pip show opencv-python
To find and display the installation path of OpenCV
It should be noted that the path of OpenCV installed by Anaconda here is not placed with the original installation package. You need to use another way to view it on anaconda. You need to find the directory package with cv as the file name. The following is the location directory of that package
This is the file directory, which corresponds to the package we want to reference
4. Final solution
Change the index to: computer absolute path + double slash
The corresponding code is:
pathf = 'E:\EnvironmentsA\cv\opencv_python-3.4.5.20-cp36-cp36m-win_amd64\cv2\data\haarcascade_frontalface_default.xml' # Load face detector, eye detector, smile detector face_cascade = cv2.CascadeClassifier(pathf)
Then there will be no error when calling below
# Get the picture taken by the camera ret, frame = cap.read() faces = face_cascade.detectMultiScale(frame, 1.3, 2) img = frame
5. Test source code
Finally, the source code for testing is attached
# -*- coding:utf-8 -8- """ Author: FeverTwice Date: 2021--08--28 """ # Import camera package import cv2 import numpy as np pathf = 'E:\EnvironmentsA\cv\opencv_python-3.4.5.20-cp36-cp36m-win_amd64\cv2\data\haarcascade_frontalface_default.xml' pathe = 'E:\EnvironmentsA\cv\opencv_python-3.4.5.20-cp36-cp36m-win_amd64\cv2\data\haarcascade_eye.xml' paths = 'E:\EnvironmentsA\cv\opencv_python-3.4.5.20-cp36-cp36m-win_amd64\cv2\data\haarcascade_smile.xml' # Load face detector, eye detector, smile detector # face_cascade = cv2.CascadeClassifier('haarcascade_files/haarcascade_frontalface_default.xml') face_cascade = cv2.CascadeClassifier(pathf) eye_cascade = cv2.CascadeClassifier(pathe) smile_cascade = cv2.CascadeClassifier(paths) # Get camera # If there are multiple cameras, you can change 0 to 1, 2 and 3 cap = cv2.VideoCapture(0) # loop while (True): # Get the picture taken by the camera ret, frame = cap.read() faces = face_cascade.detectMultiScale(frame, 1.3, 2) img = frame for (x, y, w, h) in faces: # Draw the face, blue, brush width img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) # Draw the face area and detect the human eye in the face area to save computing resources face_area = img[y:y + h, x:x + w] ## Human eye detection # The human eye cascade classifier engine is used to recognize the face region, and the returned eyes are the eye coordinates eyes = eye_cascade.detectMultiScale(face_area, 1.3, 10) for (ex, ey, ew, eh) in eyes: # Draw the human eye socket, green, with a width of 1 cv2.rectangle(face_area, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 1) ## Smiley face detection # The smile cascade classifier engine is used for human eye recognition in the region, and the returned is the eye coordinates smiles = smile_cascade.detectMultiScale(face_area, scaleFactor=1.16, minNeighbors=65, minSize=(25, 25), flags=cv2.CASCADE_SCALE_IMAGE) for (ex, ey, ew, eh) in smiles: # Draw the smile box, the color is red, and the brush width is 1 cv2.rectangle(face_area, (ex, ey), (ex + ew, ey + eh), (0, 0, 255), 1) cv2.putText(img, 'Smile', (x, y - 7), 3, 1.2, (0, 0, 255), 2, cv2.LINE_AA) # Display the effect screen cv2.imshow('frame2', img) # Didn't 5ms monitor a keyboard action if cv2.waitKey(5) & 0xFF == ord('q'): break # Turn off the camera cap.release() # Close the image window cv2.destroyAllWindows()
Posted by Abner
at Aug 31, 2021 — 2:47 AM
Tag:
OpenCV
Pycharm
Python
Programming
Computer Vision