Python Forum

Full Version: Model.predict() always returning the same value of 1 for opencv
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
when I try to use model.predict() in my code, it seems to not work and gives me a result of 1 every time, no matter the image I put into model.predict(), by the way Im using this to compare one picture to a training xml document based on the mean of a bunch of training pictures in a file, if you could just change it so it only uses a couple of pictures and not the training.xml then that would be fine, just do whatever is possible to either get it to return different values or so it can just detect a face then execute other aspects of it as a result

Also, when answering this if you see any more possible errors I would like for you to point them out because I need this soon, like, in 4 hours so I'd like to get it cleared out before then, and Ive been literally working on it for the past 14 hours with only bathroom breaks, sort of given up at this point and decided to ask the internet...

Anyways, here's my code:
"""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!'
If you need help with anything to figure out what anything does Ill willingly give it to you, the GPIO pins will be implemented later as to what they do so dont worry about them,

EDIT: Apparently sometimes the model would return with a confidence of 2, but this happens rarely and at random, I have no idea why