face_recognition: cv2.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'
- face_recognition version:4.1.0
- Python version:3.7
- Operating System:windows 10
Description
I’m having problem to run this program, the error under below
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) cv2.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’
What I Did
Paste the command(s) you ran and the output.
If there was a crash, please include the traceback here.
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 52
- Comments: 119 (1 by maintainers)
This a secondary error that basically just means the image you are trying to process didn’t load and was empty.
If you grabbed the image dara from the camera, it means the camera connection failed or isn’t configured correctly. If you loaded an image file, it means the loading failed.
I have same problem. My old code is : cap = cv2.VideoCapture(1)
Then I change my code, and problem has solved. Probably, opencv accepts my finger print input as a camera. Thus, I change VideoCapture parameter as follows: cap = cv2.VideoCapture(0)
ret, frame = cv2.VideoCapture(‘PATH’).read() You should put : ‘’’ if ret == False break ‘’’ above: ‘’’ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow(‘gray’, gray) ‘’’ Because when it comes to the final frame of the video, then there will be no frame for ‘’’ cv2.cvtCOLOR(frame, cv2.COLOR_BGR2GRAY) ‘’‘’ _src.empty() in function ‘cv::cvtColor’ means that is no source for the function.
If you use a camera:
cv2.VideoCapture(0)
#wincv2.VideoCapture(-1)
#linux If you play local video:cv2.VideoCapture("videoFilePath")
this problem occcurs when you dont declare what ‘im’ is or the image has not been loaded to the variable ‘im’/. if you’re using your webcam to capture then use
cap = cv2.VideoCapture(-1)
on linuxcap = cv2.VideoCapture(0)
on windows and ‘im’ must beret, im = cap.read()
. So it will read the image properly. if you do follow these steps the error must not occur.@rezabrg download the required haarcascades it will work https://github.com/opencv/opencv/tree/master/data/haarcascades
gray = cv2.cvtColor(image_frame, cv2.COLOR_BGR2GRAY)
error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’
mn biết lỗi này là sao k ạ
how to solve this error. anyone has answer for this 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’
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’ how to solve this
I was facing this issue and by removing special characters from the image_file_name the issue was resolved.
Example: Renaming “48172454-thymianblätter.jpg” to “48172454-thymian.jpg”.
I realized I wasn’t in the same dir as the image.I was trying to load an image from Desktop using just the image name.jpg. I changed dir into Desktop and everything worked fine.
complete image path helps me to resolve error example : cv2.imread(“C:\Users\xyz\Desktop\Python\welcome.png”)
I am using hik vision’s camera and i am getting same error…i think my laptop’s processor is not able to load the frames due to very high resolution and frame rate
i have the same problem
The problem is your image location. Just check carefully if you make a mistake on the location. For example if your image stored under a folder and rather in the same folder of your source code then dont use color = cv2.imread(“butterfly.jpg”, 1 ) instead color = cv2.imread(“images/your-folder/butterfly.jpg”, 1 )
In my case, I found that in the settings of windows 10, permission to access camera has been disabled to any applications
If the path is correct and the name of the image is OK, but you are still getting the error
use:
from skimage import io
img = io.imread(file_path)
instead of:
cv2.imread(file_path)
The function
imread
loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).See this stackoverflow for more information.
Hi! you can solve the error like this:
you need to change the line “name_image” for the name the image or video:
For some reason the file won’t find you if we don’t give you the full address
also for me…
I found another solution
capture = cv2.VideoCapture(1, cv2.CAP_DSHOW)
this is what worked for me 😁
I also faced the same error and i fixed it by correcting the directory path. Earlier my code was cv2.imread(“basic/imageread.png”,1)
i fixed it by changing the path,make sure your path is correct. cv2.imread(“…/basic/imageread.png”,1)
IDE: Pycharm OS: Ubuntu:18
I have the same problem the reason was the image name in the folder was different from the one i was calling from cv2.imread function. properly load the images. hope it helps
For me too!
error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’
I was facing the same issue, but as soon as I changed my image to a low-resolution image the code worked.
Hello Everyone,
I had run into the same problem a few days back. Here’s my perspective on why this error came up and how can it be fixed.
The reason behind the error:-
cv2.imread() or cv2.VideoCapture()
can’t find where the file is and hence gives src.empty() since None is returned instead of image or videoIf you got into this problem when working with an image then here is the fix:-
from PIL import Image and Image.open()
and see if that works and try to display the image by using matplotlib.If you got into this problem when working with a video then here is the fix:-
cv2.VideoCapture('video path')
Try something like below example.I hope this solves your problem and you can better debug your video next time you encounter something similar.
JUST complete image path, it helps me to resolve error. example : cv2.imread(“C:\Users\xyz\Desktop\Python\1.png”) OR img = cv2.imread(‘C:\Users\ASUS\Desktop\OCR Files\pytessetact using openCv\1.png’)
I have encountered the same issue in another environment.
The
src.empty()
issue seems to be related to the file path.I changed the relative path to an absolute path, and it worked for me.
For example, the relative path throws
src.empty()
errors:But using an absolute path may work:
In my case, I forgot to leave
cap.release()
out of the while block (due to pythons indentation rules, since I come from JS). I know it’s a dumb mistake, but this might help someone.This DOESN’T WORK:
This WORKS:
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’
Solution — This errors tells you that In your dataset you have special characters named images, to solve this remove the special characters from your images names
Just putting this out there in case it helps anyone, this line of code helped me fix the error:
videoCapture = cv2.VideoCapture(0, cv2.CAP_DSHOW)
Thanks!! It worked
thank you… it worked excellently…
If you are using your phone as a webcam you can use
capture = cv2.VideoCapture(1, cv2.CAP_ANDROID)
How to solve?
chuyển \ thành / thử xem bạn
cv2.error: OpenCV(4.2.0) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function ‘cvtColor’
Same here, the suggestions under this topic didnt work for me.
Having the same error… Please help out!!
error -
I am having the same issue. Is image resolution causing the problem?