ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
53.4k stars 13.48k forks source link

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' #933

Open rezabrg opened 5 years ago

rezabrg commented 5 years ago

Description

I'm having problem to run this program, the error under below Capture

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.
rafaelpsimoes commented 5 years ago

i have the same problem

ageitgey commented 5 years ago

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.

tarvihafatima commented 5 years ago

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".

yashpatel7025 commented 4 years ago

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

dinezh256 commented 4 years ago

I am having the same issue. Is image resolution causing the problem?

ghost commented 4 years ago

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)

vijaypearl commented 4 years ago

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'

nikhilsupertramp commented 4 years ago

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 linux cap = cv2.VideoCapture(0) on windows and 'im' must be ret, im = cap.read(). So it will read the image properly. if you do follow these steps the error must not occur.

nikhilsupertramp commented 4 years ago

Now I guess we can close this issue. @ageitgey @rezabrg @rafaelpsimoes

harichandu13 commented 4 years ago

@rezabrg download the required haarcascades it will work https://github.com/opencv/opencv/tree/master/data/haarcascades

bakhtiyar8 commented 4 years ago

In my case, I found that in the settings of windows 10, permission to access camera has been disabled to any applications

Bilel-Marzougui commented 4 years ago

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

Did you find a solution?

teja2002 commented 4 years ago

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

thoangnguyen1308 commented 4 years ago

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 ạ

sohanatarafder99 commented 4 years ago

The same is happening with me

AnabetsyR commented 4 years ago

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.

Shubhangipanda commented 4 years ago

I am facing the same issue? what is causing the problem?

Ehabur commented 4 years ago

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

Ashutosh002 commented 4 years ago

Having the same error... Please help out!!

Capture

error - Capture2

ceaqw commented 4 years ago

If you use a camera: cv2.VideoCapture(0) #win cv2.VideoCapture(-1) #linux If you play local video: cv2.VideoCapture("videoFilePath")

mohamedrashik commented 4 years ago

Same error

Traceback (most recent call last): File "basic/imageread.py", line 5, in img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(4.2.0) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

shrikantkhabale commented 4 years ago

complete image path helps me to resolve error example : cv2.imread("C:\Users\xyz\Desktop\Python\welcome.png")

giangnguyen-bker commented 4 years ago

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.

hapsuu commented 4 years ago

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.

thangphung215 commented 4 years ago

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 ạ

chuyển \ thành / thử xem bạn

GreatBruceYoung commented 4 years ago

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

How to solve?

pratsat18 commented 4 years ago

error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

Please help!

hapsuu commented 4 years ago

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 )

mmohamedrashik commented 4 years ago

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

okridgway commented 4 years ago

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.

anujk949 commented 4 years ago

img = cv2.imread(file_path)

check img variable by printing it, if it is null/none then cv2.cvtColor will give error. enter the write path so that img variable loads an image array Annotation 2020-05-22 115330

Soumarjit commented 4 years ago

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')

lovish1 commented 4 years ago

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.

Then If gets valid ..what to do then?

THINK989 commented 4 years ago

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:-

If you got into this problem when working with an image then here is the fix:-

If you got into this problem when working with a video then here is the fix:-

TruptiBobade commented 4 years ago

I have also faced the same issue. It is not able to locate the file name which you were mentioning in the given path. Either you can specify the path while calling the file or you can define path using OS. I hope this will resolve your issue. import cv2 image = cv2.imread("C:/XYZ/images/1.jpg") image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) plt.imshow(image, cmap="gray")

or

os.chdir('C:/XYZ/images/') image = cv2.imread("1.jpg") image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) plt.imshow(image, cmap="gray")**

akionsight commented 4 years ago

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.

I too did that but the error now says cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor' the code I wrote was

def detect_faces_in_video_clip(filename, window_title='Faces In Video Clip', cascade='haarcascade_frontalface_default.xml',box_colour=(0, 255, 0), line_thickness=2):

    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + cascade)

    cap = cv2.VideoCapture(filename)
    while True:
        _, img = cap.read()
        grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(grey, 1.1, 4)
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x,y), (x+w, y+h), box_colour, line_thickness)

        cv2.imshow(window_title, img)
        k = cv2.waitKey(30)
        if k == 27:
            break

detect_faces_in_video_clip('sample.mp4')
akionsight commented 4 years ago

any answer

THINK989 commented 4 years ago

@akionsight Did you try to make an output video like I have mentioned above to check which frame of the video is causing the error?

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 video
  • If you are sure that the path specified is correct, then chances are the file being read is corrupted either completely or in a few bits and pieces.

If you got into this problem when working with an image then here is the fix:-

  • Change the image, literally. If you don't want to change it then try from PIL import Image and Image.open() and see if that works and try to display the image by using matplotlib.
  • Changing or reproducing the same image is the best option in my opinion.

If you got into this problem when working with a video then here is the fix:-

  • After using cv2.VideoCapture('video path') Try something like below example.
# suppose this was the source 
cap = cv2.VideoCapture('data/input.mpg')
# Get width and height of the frame of video
width  = cap.get(3) # float width
height = cap.get(4) # float height    
# Make a video writer to see if video being taken as input inflict any changes you make
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
out_video = cv2.VideoWriter('result/output.avi', fourcc, 20.0, (int(width), int(height)), True)
# Then try this
while(cap.isOpened()):
        # Read each frame where ret is a return boollean value(True or False)
        ret, frame = cap.read()
        # if return is true continue because if it isn't then frame is of NoneType in that case you cant work on that frame
        if ret:
            # Any preprocessing you want to make on the frame goes here
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # See if preprocessing is working on the frame it should
            cv2.imshow('frame', gray)
            # finally write your preprocessed frame into your output video
            out_video.write(gray) # write the modifies frame into output video 
            # to forcefully stop the running loop and break out, if it doesnt work use ctlr+c
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
       # break out if frame has return NoneType this means that once script encounters such frame it breaks out 
       # You will get the error otherwise 
        else:
            break
# this will tell you from which frame the video is corrupted and need to be changed 
# this could be due to an empty frame. Empty frame might be a result of mistake made while creating or editing video in tools #like adobe premier pro or other products alike 
# You can check you output video it will contains all frame before the error is encountered

I hope this solves your problem and you can better debug your video next time you encounter something similar.

akionsight commented 4 years ago

@akionsight Did you try to make an output video like I have mentioned above to check which frame of the video is causing the error?

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 video
  • If you are sure that the path specified is correct, then chances are the file being read is corrupted either completely or in a few bits and pieces.

If you got into this problem when working with an image then here is the fix:-

  • Change the image, literally. If you don't want to change it then try from PIL import Image and Image.open() and see if that works and try to display the image by using matplotlib.
  • Changing or reproducing the same image is the best option in my opinion.

If you got into this problem when working with a video then here is the fix:-

  • After using cv2.VideoCapture('video path') Try something like below example.
# suppose this was the source 
cap = cv2.VideoCapture('data/input.mpg')
# Get width and height of the frame of video
width  = cap.get(3) # float width
height = cap.get(4) # float height    
# Make a video writer to see if video being taken as input inflict any changes you make
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
out_video = cv2.VideoWriter('result/output.avi', fourcc, 20.0, (int(width), int(height)), True)
# Then try this
while(cap.isOpened()):
        # Read each frame where ret is a return boollean value(True or False)
        ret, frame = cap.read()
        # if return is true continue because if it isn't then frame is of NoneType in that case you cant work on that frame
        if ret:
            # Any preprocessing you want to make on the frame goes here
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # See if preprocessing is working on the frame it should
            cv2.imshow('frame', gray)
            # finally write your preprocessed frame into your output video
            out_video.write(gray) # write the modifies frame into output video 
            # to forcefully stop the running loop and break out, if it doesnt work use ctlr+c
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
       # break out if frame has return NoneType this means that once script encounters such frame it breaks out 
       # You will get the error otherwise 
        else:
            break
# this will tell you from which frame the video is corrupted and need to be changed 
# this could be due to an empty frame. Empty frame might be a result of mistake made while creating or editing video in tools #like adobe premier pro or other products alike 
# You can check you output video it will contains all frame before the error is encountered

I hope this solves your problem and you can better debug your video next time you encounter something similar.

Yes I did but it did not work and still showed the same error

THINK989 commented 4 years ago

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.

I too did that but the error now says cv2.error: OpenCV(4.2.0) /Users/travis/build/skvark/opencv-python/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor' the code I wrote was

def detect_faces_in_video_clip(filename, window_title='Faces In Video Clip', cascade='haarcascade_frontalface_default.xml',box_colour=(0, 255, 0), line_thickness=2):

    face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + cascade)

    cap = cv2.VideoCapture(filename)
    while True:
        _, img = cap.read()
        grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(grey, 1.1, 4)
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x,y), (x+w, y+h), box_colour, line_thickness)

        cv2.imshow(window_title, img)
        k = cv2.waitKey(30)
        if k == 27:
            break

detect_faces_in_video_clip('sample.mp4')

@akionsight

Your Code works perfectly on my input video. I don't know what else might cause the issue may be version. Are your python file and video at the same location?

nirupamudoi commented 4 years ago

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 video
  • If you are sure that the path specified is correct, then chances are the file being read is corrupted either completely or in a few bits and pieces.

If you got into this problem when working with an image then here is the fix:-

  • Change the image, literally. If you don't want to change it then try from PIL import Image and Image.open() and see if that works and try to display the image by using matplotlib.
  • Changing or reproducing the same image is the best option in my opinion.

If you got into this problem when working with a video then here is the fix:-

  • After using cv2.VideoCapture('video path') Try something like below example.
# suppose this was the source 
cap = cv2.VideoCapture('data/input.mpg')
# Get width and height of the frame of video
width  = cap.get(3) # float width
height = cap.get(4) # float height    
# Make a video writer to see if video being taken as input inflict any changes you make
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
out_video = cv2.VideoWriter('result/output.avi', fourcc, 20.0, (int(width), int(height)), True)
# Then try this
while(cap.isOpened()):
        # Read each frame where ret is a return boollean value(True or False)
        ret, frame = cap.read()
        # if return is true continue because if it isn't then frame is of NoneType in that case you cant work on that frame
        if ret:
            # Any preprocessing you want to make on the frame goes here
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            # See if preprocessing is working on the frame it should
            cv2.imshow('frame', gray)
            # finally write your preprocessed frame into your output video
            out_video.write(gray) # write the modifies frame into output video 
            # to forcefully stop the running loop and break out, if it doesnt work use ctlr+c
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
       # break out if frame has return NoneType this means that once script encounters such frame it breaks out 
       # You will get the error otherwise 
        else:
            break
# this will tell you from which frame the video is corrupted and need to be changed 
# this could be due to an empty frame. Empty frame might be a result of mistake made while creating or editing video in tools #like adobe premier pro or other products alike 
# You can check you output video it will contains all frame before the error is encountered

I hope this solves your problem and you can better debug your video next time you encounter something similar.

i am getting this problem while loading my images from dataset. I am running the code from the same folder within the same directory but the dataset is situated on a different folder. what should I do?

THINK989 commented 4 years ago

i am getting this problem while loading my images from dataset. I am running the code from the same folder within the same directory but the dataset is situated on a different folder. what should I do?

Hello @nirupamudoi That depends on your file structure If your file structure is like this

ROOT
    |
    |
    ----Data
              |
              ----Your Images
    |
    ----Script
              |
              ---- Your Python Script

Then you can use "../" in your python script for eg:-

cv2.imread("../Data/image_name.jpg")

Also, in any case, a complete path would work for eg:-

cv2.imread("D:/data/image_name.jpg")
vedantraut commented 4 years ago

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.

KARTHIK-VEL commented 4 years ago

[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (681) CvCapture_MSMF::initStream Failed to set mediaType (stream 0, (0x0 @ 1) MFVideoFormat_RGB24(unsupported media type) i having this error please help me

anuradhabn commented 4 years ago

For 'cv2.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'' error, I was able to resolve it with the below code. I think it has got to do with the resolution. Change the scalefactor from 1 to 1.3 or 1.4.

a) Image : faces = face_cascade.detectMultiScale(gray, 1.1, 4) & it works. b) Video : faces = face_cascade.detectMultiScale(gray, 1.9, 4) & it is still not working.

cihan063 commented 4 years ago

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.

It worked for me thanks a lot

Divyesh24 commented 4 years ago

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 linux cap = cv2.VideoCapture(0) on windows and 'im' must be ret, im = cap.read(). So it will read the image properly. if you do follow these steps the error must not occur. great it works,

VipulPandey-arch commented 4 years ago

in my case i have put the image path from the properties of the image as well as please write cv2. press tab(if using jupyter notebook) and the search for the correct syntax , for my case code is like this full = cv2.imread(r'C:\Users\vipul.pandey\Desktop\computer vision\Computer-Vision-with-Python\DATA\1.jpg') full = cv2.cvtColor(full, cv2.COLOR_BGR2RGB)

dont forget to put r before the path i think ur problem will be solved

akionsight commented 4 years ago

I found another solution

capture = cv2.VideoCapture(1, cv2.CAP_DSHOW)

this is what worked for me 😁

ritikkumar55 commented 4 years ago

I found another solution

capture = cv2.VideoCapture(1, cv2.CAP_DSHOW)

this is what worked for me 😁

also for me...