Python Forum
Model.predict() always returning the same value of 1 for opencv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Model.predict() always returning the same value of 1 for opencv
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Which network architecture should be chosen to predict disease severity? AlekseyPython 2 241 Aug-21-2021, 07:12 PM
Last Post: jefsummers
  How to predict output in a regression problem? Rezaafz 1 618 Apr-03-2021, 04:16 PM
Last Post: jefsummers
  Keras.Predict into Dataframe Finpyth 13 3,271 Aug-31-2020, 07:22 AM
Last Post: hussainmujtaba
  Analyze, predict the next step in a sequence. Antonio0608 0 818 Jul-23-2020, 05:53 PM
Last Post: Antonio0608
  Error When Using sklearn Predict Function firebird 0 845 Mar-21-2020, 04:34 PM
Last Post: firebird
  Predict Longitude and Latitude Using Python vibeandvisualize 1 916 Dec-27-2019, 12:10 PM
Last Post: Larz60+
  How to predict with date as input for DecisionTreeRegressor sandeep_ganga 0 762 Dec-12-2019, 03:29 AM
Last Post: sandeep_ganga
  Can someone explain how does svr_rbf.predict(dates) work? j2ee 0 2,315 Feb-22-2018, 06:50 PM
Last Post: j2ee
  how to predict next value taking in account several variables? drogontargaryen 2 2,787 Aug-23-2017, 02:16 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020