Config:
#!/usr/bin/env python
# coding: utf8
"""MMMM-Facial-Recognition-OCV3 - MagicMirror Module The MIT License (MIT)
Copyright (c) 2018 Mathieu Goulène (MIT License) Based on work by Paul-Vincent Roll (Copyright 2016) (MIT License) """
# to install builtins run `pip install future` from builtins import input
import cv2 import numpy as np import os
from lib.tools.config import ToolsConfig from lib.common.face import FaceDetection
class ToolsTrain:
def __init__(self):
self.face = ToolsConfig.getFaceDetection()
def prepareImage(self, filename):
"""Read an image as grayscale and resize it to the appropriate size for
training the face recognition model.
"""
return self.face.resize(cv2.imread(filename, cv2.IMREAD_GRAYSCALE),ToolsConfig.FACE_WIDTH, ToolsConfig.FACE_HEIGHT)
def train(self):
print("Reading training images...")
print('-' * 20)
faces = []
labels = []
imageDirsWithLabel = [[0, "negative"]]
imageDirs = os.listdir(ToolsConfig.TRAINING_DIR)
imageDirs = [x for x in imageDirs if not x.startswith('.') and not x.startswith('negative')]
pos_count = 0
for i in range(len(imageDirs)):
print("Assign label " + str(i + 1) + " to " + imageDirs[i])
imageDirsWithLabel.append([i + 1, imageDirs[i]])
print('-' * 20)
print('')
# Für jedes Label/Namen Paar:
# for every label/name pair:
for j in range(0, len(imageDirsWithLabel)):
# Label zu den Labels hinzufügen / Bilder zu den Gesichtern
for filename in ToolsConfig.walkFiles(ToolsConfig.TRAINING_DIR + str(imageDirsWithLabel[j][1]), '*.pgm'):
faces.append(self.prepareImage(filename))
labels.append(imageDirsWithLabel[j][0])
if imageDirsWithLabel[j][0] != 0:
pos_count += 1
# Print statistic on how many pictures per person we have collected
print('Read ' + str(pos_count) + ' positive images and ' + str(labels.count(0)) + ' negative images.')
print('')
for j in range(1, max(labels) + 1):
print(str(labels.count(j)) + " images from subject " + imageDirs[j - 1])
# Train model
print('-' * 20)
print('')
CMD:
sudo python tools.train.py
Error/Output
Remember to set the name list environment variable FACE_USERS Reading
training images...
-------------------- Assign label 1 to Jorick
--------------------
Read 7 positive images and 0 negative images.
7 images from subject Jorick
--------------------
Training model with threshold 80 Traceback (most recent call last):
File "tools.train.py", line 14, in <module>
ToolsTrain().train() File "/home/pi/MagicMirror/modules/MMM-Facial-Recognition-OCV3/lib/tools/train.py",
line 74, in train
model.write(ToolsConfig.TRAINING_FILE) AttributeError: 'cv2.face_LBPHFaceRecognizer' object has no attribute 'write'
Git package: https://github.com/normyx/MMM-Facial-Recognition-OCV3
Python version: Python 2.7.16