I'm trying to make run a face recognition program using OpenCV and Python.
I found this code here on stackoverflow, but the main problem is an error which says:
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
The code is this one :
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()
In OpenCV 3.1 for C++ you have to use cv::ImreadModes::IMREAD_GRAYSCALE
which is located on <opencv2/imgcodecs.hpp>
simpy change: "CV_LOAD_IMAGE_GRAYSCALE"
to: "IMREAD_GRAYSCALE"
it will work.
>>> 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
is from the outdated [and now removed] cv api )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With