This article is about fixing 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' in Ageitgey face-recognition
  • 24-Jan-2023
Lightrun Team
Author Lightrun Team
This article is about fixing 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' in Ageitgey 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’ in Ageitgey face-recognition

Lightrun Team
Lightrun Team

Explanation of the problem

The program is encountering an error when attempting to run the following line of code:

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

The error message being displayed is:

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’

This error is indicating that the “cvtColor” function from the OpenCV library is encountering an assertion failure because the input image, “im”, is empty. This is likely causing the program to crash.

The error message is coming from the OpenCV library, which is being used to perform image processing tasks. The specific function that is causing the error is “cvtColor”, which is used to convert an image from one color space to another. In this case, the code is attempting to convert the “im” variable from the BGR color space to the grayscale color space.

The error message is indicating that an assertion failure is occurring within the “cvtColor” function. This is likely due to the “im” variable being empty, which is causing the function to fail. The assertion failure is triggered by the following line of code:


This line of code is checking if the input image is empty, and if it is, the program is terminated with the error message.

To solve this problem, the cause of the empty input image must be identified and addressed. This could be due to a variety of reasons, such as the image not being properly loaded or initialized. One possible solution is to check the value of the “im” variable before passing it to the “cvtColor” function, to ensure that it is not empty. Another solution would be to check the path of the image and make sure it is correct and accessible. It is also possible that the image file is corrupted or missing, in this case, the correct image file needs to be located and added to the project.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for 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’

An “empty image” error in image processing refers to a situation where the image being processed did not load successfully. This can happen when the image data is not being properly read or when the image file does not exist. This issue can be caused by a variety of factors such as a failed camera connection or incorrect configuration.

One possible cause of this error is an issue with the code being used to capture the image. For example, in the case of using the OpenCV library in Python, the following code snippet may be used to capture an image from a camera:

import cv2

cap = cv2.VideoCapture(1)
ret, frame =

Here, the cv2.VideoCapture() function is used to initialize a video capturing object, and the integer parameter passed to it specifies the index of the camera to be used. In the case above, the index 1 is passed, indicating that the second camera on the system is to be used. However, if the camera is not connected or is not configured correctly, this may result in an “empty image” error.

A solution to this problem may be to check and adjust the code being used to capture the image. For example, in this case, the error was resolved by changing “cap = cv2.VideoCapture(1)” to “cap = cv2.VideoCapture(0)”. This suggests that the error may have been related to OpenCV interpreting the fingerprint input as a camera. By changing the index passed to the cv2.VideoCapture() function, the program is now using the first camera on the system. Additionally, you can check the return value of the read method, it should be True if it is able to read an image.

import cv2

cap = cv2.VideoCapture(0)
ret, frame =
if ret==True:
    # image is read successfully
    # empty image error

It is important to test the code with different camera indices and configurations to ensure that the correct camera is being used and that the image is being captured correctly.

Other popular problems with Ageitgey face-recognition

Problem: False Positive Identification

One of the most common problems with facial recognition systems is the issue of false positives. This occurs when the system incorrectly identifies a person as someone else. This can be caused by a variety of factors, such as poor lighting conditions, low-resolution images, or the presence of facial obstructions (e.g. glasses or hats).


One way to reduce the number of false positives is to use a more robust feature extraction algorithm. For example, instead of using simple edge detection to identify facial features, a more advanced algorithm such as Local Binary Patterns (LBP) can be used. This method is more robust to variations in lighting and can handle images with lower resolution.

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.decomposition import PCA

lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)

X =
y =

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)

pca = PCA(n_components=100, whiten=True, random_state=0)
X_train_pca = pca.fit_transform(X_train)
X_test_pca = pca.transform(X_test)

clf = RandomForestClassifier(), y_train)
y_pred = clf.predict(X_test_pca)

print(classification_report(y_test, y_pred))

Problem: Unbalanced Dataset

Another common problem with facial recognition systems is the issue of unbalanced datasets. This occurs when the dataset contains a disproportionate number of images of certain individuals or groups, leading to bias in the system. This can be particularly problematic in situations where the system is being used for security or surveillance, as it may lead to unequal treatment of different individuals or groups.


One way to address this problem is to use techniques such as oversampling or undersampling to balance the dataset. For example, oversampling can be used to replicate examples in the minority class to balance the dataset. While Undersampling can be used to remove examples from the majority class to balance the dataset.

from sklearn.utils import resample

X = pd.concat([X_train, y_train], axis=1)

not_face = X[X.label==0]
face = X[X.label==1]

face_upsampled = resample(face, replace=True, n_samples=len(not_face), random_state=27)

upsampled = pd.concat([not_face, face_upsampled])

y_train = upsampled.label
X_train = upsampled.drop('label', axis=1)

Problem: Low-Resolution Images

Another problem with facial recognition systems is the issue of low-resolution images. This can occur when the images used to train the system or the images being processed by the system are of low quality.


One way to address this problem is to use image enhancement techniques such as super-resolution to increase the resolution of the images. For example, you can use the package such as cv2 to resize the image to a higher resolution. Another technique that can be used is to use deep learning-based methods such as Generative Adversarial Networks (GANs) to generate high-resolution images from low-resolution images.

import cv2

def increase_resolution(image, scale_percent):
    width = int(image.shape[1] * scale_percent / 100)
    height = int(image.shape[0] * scale_percent / 100)
    dim = (width, height)
    return cv2.resize(image, dim, interpolation = cv2.INTER_LINEAR)

high_resolution_image = increase_resolution(low_resolution_image, 200)


from keras.layers import Input, Dense, Reshape, Flatten, Dropout, Concatenate
from keras.layers import BatchNormalization, Activation, ZeroPadding2D
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import UpSampling2D, Conv2D
from keras.models import Sequential, Model

def build_generator():
    model = Sequential()
    model.add(Dense(7*7*256, input_dim=100))
    model.add(Reshape((7, 7, 256)))
    model.add(Conv2D(128, kernel_size=3, padding="same"))
    model.add(Conv2D(64, kernel_size=3, padding="same"))
    model.add(Conv2D(3, kernel_size=3, padding="same"))
    return model

The above solutions are widely used in the industry but there are other techniques that can be used to address low resolution images such as using larger training dataset or using deep learning techniques to extract features from low resolution images etc.

A brief introduction to Ageitgey face-recognition

Ageitgey face-recognition is a technology that uses computer algorithms to identify and match human faces. The process of face recognition involves several steps such as detection, alignment, feature extraction, and matching. The first step, detection, involves identifying the presence of a face in an image or video. This can be done using techniques such as Haar cascades, which are a type of classifier that uses a series of simple features to detect faces in images.

Once a face is detected, the next step is alignment. This involves aligning the face so that it is facing forward and in a consistent position. This is important for feature extraction, as it ensures that the same features are being extracted from each face. After alignment, feature extraction is performed. This involves extracting unique facial characteristics such as the shape and texture of the eyes, nose, and mouth. These features are then used to create a unique “face template” which can be used for matching. Finally, the matching step involves comparing the face template to a database of known faces to find a match. The matching can be done using techniques such as Euclidean distance, Cosine similarity, or Mahalanobis distance.

Most popular use cases for Ageitgey face-recognition

  1. Security and Surveillance: Ageitgey face-recognition can be used in security and surveillance systems to identify and track individuals in real-time. This technology can be integrated with CCTV cameras to automatically detect and identify individuals as they enter a building or area. The system can also be used to track individuals as they move through the area, and alert security personnel if an individual is behaving suspiciously.
import cv2
import face_recognition

# Load the video
video = cv2.VideoCapture("example.mp4")

# Load the known faces
known_faces = []

# Initialize variables
face_locations = []
face_encodings = []

while True:
    # Get the current frame
    ret, frame =

    # Exit the loop if the video has ended
    if not ret:

    # Find the faces in the frame
    face_locations = face_recognition.face_locations(frame)
    face_encodings = face_recognition.face_encodings(frame, face_locations)

    # Loop through each face
    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        # Check if the face is a known face
        for known_face in known_faces:
            match = face_recognition.compare_faces([known_face], face_encoding)
            if match[0]:
                print("Known face detected")
    # Display the frame
    cv2.imshow("Video", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):

# Release the video capture object
  1. Biometric Authentication: Ageitgey face-recognition can be used as a biometric authentication method to grant access to devices, buildings, or sensitive information. This technology can be integrated with a security system to grant access to authorized individuals and deny access to unauthorized individuals. It can be used as an alternative to traditional authentication methods such as passwords or fingerprints.
  2. Personalized Services: Ageitgey face-recognition can be used to provide personalized services. For example, it can be used in retail stores to track customers’ preferences and make personalized recommendations. It can also be used in entertainment venues such as theme parks to offer personalized experiences based on a person’s preferences. This technology can also be used in healthcare to provide personalized medical treatment based on a patient’s unique facial characteristics.



It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.