我正在尝试使用OpenCV和Python运行人脸识别程序 我在stackoverflow上找到了这个代码,但主要问题是一个错误,上面写着:
Traceback (most recent call last):
File "/Users/n1/Desktop/FaceDetection/face.py", line 8, in <module>
gray = imread(fname, CV_LOAD_IMAGE_GRAYSCALE )
NameError: name 'CV_LOAD_IMAGE_GRAYSCALE' is not defined
代码就是这个:
from cv2 import *
import numpy as np
face_cascade = CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = CascadeClassifier('haarcascade_eye.xml')
fname='123.jpg'
img = imread(fname)
gray = imread(fname, CV_LOAD_IMAGE_GRAYSCALE ( 0) )
rows,cols = gray.shape
gray = np.array(gray, dtype='uint8')
faces = face_cascade.detectMultiScale(gray, 1.3, 5, 0)
print ('faces=', faces)
for (x,y,w,h) in faces:
rectangle(img, (x,y), ((x+w),(x+h)), (255,0,0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey, ew, eh) in eyes:
rectangle(roi_color, (x,y), ((x+w), (y+h)), (50, 50, 50), 3)
imshow('eyes=%s' % (eyes,), roi_color)
imshow("img", img)
waitKey(0)
destroyAllWindows()
答案 0 :(得分:5)
>>> import cv2
>>> help(cv2)
...
IMREAD_ANYCOLOR = 4
IMREAD_ANYDEPTH = 2
IMREAD_COLOR = 1
IMREAD_GRAYSCALE = 0 #that will be it ;)
IMREAD_LOAD_GDAL = 8
IMREAD_UNCHANGED = -1
...
VERSION
3.0.0-dev
(CV_LOAD_IMAGE_GRAYSCALE来自过时的[现已删除] cv api)
答案 1 :(得分:3)
在OpenCV 3.1 for C ++中,您必须使用cv::ImreadModes::IMREAD_GRAYSCALE
上的<opencv2/imgcodecs.hpp>
答案 2 :(得分:0)
简单更改:“ CV_LOAD_IMAGE_GRAYSCALE”
至:“ IMREAD_GRAYSCALE”
它将起作用。