【发布时间】:2021-02-13 02:47:25
【问题描述】:
错误: Build\OpenCV\opencv-4.4.0\modules\imgproc\src\color.cpp:182: 错误: (-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()
【问题讨论】:
-
如果 test.jpg 存在并且没有损坏,那么它可能不在您的当前目录中。因此,您可能需要在 cv2.imread() 命令中提供 test.jpg 的路径。
标签: python python-3.x opencv camera cv2