Python 3.8.5 cv2 -215:断言失败 [英] Python 3.8.5 cv2 -215:Assertion failed

查看:120
本文介绍了Python 3.8.5 cv2 -215:断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Error: Build\OpenCV\opencv-4.4.0\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function '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()

推荐答案

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

This error shows up when the image you are passing to it is empty i.e. OpenCV is not able to read the image. Are you sure that the test.jpg is in the same directory as this code?

检查扩展名.可能是 jpeg .

您有两个选择-

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

这篇关于Python 3.8.5 cv2 -215:断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆