Model.predict()总是为opencv返回相同的值1

时间:2017-12-14 08:02:20

标签: python opencv

当我尝试在我的代码中使用model.predict()时,无论我放入1的图像,它似乎都不起作用并且每次都会给我model.predict()的结果。< / p> 编辑:以上陈述不准确。有时模型会以2的置信度返回,但这种情况很少发生并且随机发生,我不知道为什么。

我使用它来根据文件中一堆训练图片的平均值将一张图片与一张训练xml文档进行比较。

如果您可以更改它,那么它只使用几张图片而不是training.xml,那就没问题了。只要做任何可能的事情,要么让它返回不同的值,或者它只是检测一个面,然后执行它的其他方面。

如果你需要任何帮助来弄清楚我会愿意给你什么,GPIO引脚将在以后实施,所以不要担心它们。

无论如何,这是我的代码:

"""Raspberry Pi Face Recognition Treasure Box
Treasure Box Script
Copyright 2013 Tony DiCola
"""
import cv2
import config
import face
import hardware
import time
import RPIO
from RPIO import PWM
import RPi.GPIO as GPIO
import os
import pygame
import pygame.camera
from array import array
import numpy as np
from PIL import Image

if __name__ == '__main__':
    pygame.camera.init()
    pygame.camera.list_cameras()
    # Load training data into model
    model = cv2.face.createEigenFaceRecognizer()
    model.load('training.xml')
    # Initialize camera and box.
    camera = config.get_camera()
    box = None
    while box is None:
            try:

                    box = hardware.Box()
            except:
                    pass
    # Move box to locked position.
    box.lock()
    print 'Running box...'
    print 'Press button to lock (if unlocked), or unlock if the correct face is detected.'
    print 'Press Ctrl-C to quit.'

    servo = PWM.Servo()
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(16, GPIO.OUT)
    GPIO.setup(21, GPIO.OUT)
    GPIO.setup(19, GPIO.OUT)
    GPIO.setup(6, GPIO.OUT)
    # Set initial box state.
    while True:
                    GPIO.output(21,False)
                    if not GPIO.input(25):
                            print 'TESTING...'
                            if not box.is_locked:
                                    # Lock the box if it is unlocked
                                    box.lock()
                                    print 'Box is now locked.'
                            else:
                                    GPIO.output(21,True)
                                    time.sleep(1)
                                    GPIO.output(21,False)
                                    print 'Button pressed, looking for face...'

                                    # Check for the positive face and unlock if found.
                                    image = camera.read()
                                    # Convert image to grayscale.
                                    image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
                                    # Get coordinates of single face in captured image.
                                    result = face.detect_single(image)
                                    if result is None:
                                            GPIO.output(6, True)
                                            time.sleep(2)
                                            GPIO.output(6, False)
                                            print 'Could not detect single face!  Check the image in capture.pgm' \
                                              ' to see what was captured and try again with only one face visible.'
                                            continue
                                    x,y,w,h = result
                                    crop = face.resize(face.crop(image,x,y,w,h))

                                    label = config.POSITIVE_LABEL
                                    #**Confidence declared here:**
                                    confidence = model.predict(crop)
                                    print 'Predicted {0} face with confidence {1} (lower is more confident).'.format(
                                    'POSITIVE' if label == config.POSITIVE_LABEL else 'NEGATIVE',
                                    confidence)
                                    if label == config.POSITIVE_LABEL and confidence < config.POSITIVE_THRESHOLD:
                                            print 'Recognized face!'
                                            box.unlock()
                                    else:
                                            print 'Did not recognize face!'

0 个答案:

没有答案