2016-08-23 36 views
0

我试图在虚拟环境或普通Python shell中执行脚本时收到此消息。Raspberry Pi 3 Python和Opencv用于面部识别

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) 

为什么会出现这个错误?

回答

0

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

检查image.shape并查看它返回了正确尺寸的东西(即具有最后尺寸为3或4的3D阵列)。

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

+0

谢谢兄弟。加载图像时我没有指定完整驱动器。 –