python - OpenCV 错误 MagicMirror AttributeError: 'cv2.face_LBPHFaceRecognizer' 对象没有属性 'write'
问题描述
配置:
#!/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('')
命令:
sudo python tools.train.py
错误/输出
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 包: https ://github.com/normyx/MMM-Facial-Recognition-OCV3
Python版本: Python 2.7.16
解决方案
推荐阅读
- powershell - 夏普串口 RS232
- python - werkzeug.routing.BuildError:无法为端点“成功”构建 url。您是否忘记指定值 ['name']?
- c# - Winforms:“数字”运算符后缺少操作数
- python - Python循环文本文件以查找和打印
- node.js - 如何根据mongodb聚合中的字段值获取文档详细信息
- python-3.x - 我需要向 tkinter exe 程序添加代码吗?
- php - 无法使用 API 将具有变体的产品添加到 eBay
- c# - CefSharp 实时更改 UserAgent
- c++ - 这个 shared_ptr 如何自动转换为原始指针?
- vue.js - Vue.js 模态:如何从模态获得响应?