Python 3.8.5 cv2 -215:断言失败

时间:2020-10-30 15:58:41

标签: python python-3.x opencv camera cv2

错误:Build \ OpenCV \ opencv-4.4.0 \ modules \ imgproc \ src \ color.cpp:182:错误:(-215:断言失败)!_src.empty()在函数'cv :: cvtColor'中

代码:

import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('test.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display the output
cv2.imshow('img', img)
cv2.waitKey()

1 个答案:

答案 0 :(得分:0)

当您传递给它的图像为空,即OpenCV无法读取图像时,会显示此错误。 您确定test.jpg与该代码位于同一目录吗?

检查扩展名。可能是jpeg

您有两个选择-

  1. 提供cv2.imread函数中的完整路径。
  2. 将图像移动到代码所在的目录。