-- coding: utf-8 --
class FaceRecognition:
def init(self, cascade_path):
self.faces = {}
self.classifier = cv.CascadeClassifier(cascade_path)
def search_faces(self, directory):
directory = os.path.normpath(directory)
if not os.path.isdir(directory):
raise IOError("The directory '" + directory + "' doesn't exist!")
for curdir, subdirs, files in os.walk(directory):
for file_name in files:
if file_name.endswith('.jpg') or file_name.endswith('.png'):
file_path = os.path.join(curdir, file_name)
image = cv.imread(file_path)
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
faces = self.classifier.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))
if len(faces) > 0:
self.faces[file_name] = {'path': file_path, 'faces': faces.tolist()}
暂无评论