【问题标题】:Raspberry Pi 3 Python and Opencv for facial recognition用于面部识别的 Raspberry Pi 3 Python 和 Opencv
【发布时间】:2016-08-23 09:00:24
【问题描述】:

无论是在虚拟环境还是普通 Python shell 中尝试执行脚本时,我都会收到此消息。

File "/home/pi/facesample1.py", line 10, in <module>
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
error: /home/pi/opencv-3.1.0/modules/imgproc/src/color.cpp:8000: error: (-215) scn == 3 || scn == 4 in function cvtColor

这是我的代码:

import cv2

#Load an image from file
image = cv2.imread("fronthead.jpg", 1)

#Load a cascade file for detecting faces
face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml')

#Convert to grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

#Look for faces in the image using the loaded cascade file
faces = face_cascade.detectMultiScale(gray, 1.1, 5)

print "Found "+str(len(faces))+" face(s)"

#Draw a rectangle around every found face
for (x,y,w,h) in faces:
    cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)

#Save the result image
cv2.imwrite('camresult.jpg',image)

为什么会出现这个错误?

【问题讨论】:

    标签: python c++ opencv raspberry-pi3


    【解决方案1】:

    您的 image 变量显然不是 3 或 4 通道图像。 因此,cvtColor() 无法将其转换为灰度。

    检查 image.shape 并查看它返回的内容是否正确(即最后一维为 3 或 4 的 3D 数组)。

    也很有可能imageNone,这通常意味着文件的路径错误。

    【讨论】:

    • 谢谢老兄。加载图像时我没有指定完整的驱动器。
    猜你喜欢
    • 2021-05-06
    • 2018-07-14
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 2018-05-26
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多