简体   繁体   English

Raspberry Pi 3 Python和Opencv用于面部识别

[英]Raspberry Pi 3 Python and Opencv for facial recognition

I am receiving this message when trying to execute script whether in virtual environment or a normal Python shell. 无论是在虚拟环境中还是在普通的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

Here is my code: 这是我的代码:

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)

Why am I getting this error? 为什么会出现此错误?

Your image variable is apparently not a 3 or 4 channel image. 您的image变量显然不是3或4频道图片。 Thus, cvtColor() is unable to transform it to grayscale. 因此, cvtColor()无法将其转换为灰度。

Check image.shape and see that it returns something with the right dimensions (ie a 3D array with last dimension 3 or 4). 检查image.shape ,看看它返回的尺寸正确(例如,最后一个尺寸为3或4的3D数组)。

It is also quite possible that image is None , which usually means that the path to the file is wrong. image也很可能是None ,这通常意味着文件路径错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM