Code: Select all
sudo apt-get update && sudo apt-get install git && git clone https://github.com/tdicola/pi-facerec-box.git
cd pi-facerec-box
sudo ./install_dependencies.sh
Moderators: adafruit_support_bill, adafruit
Code: Select all
sudo apt-get update && sudo apt-get install git && git clone https://github.com/tdicola/pi-facerec-box.git
cd pi-facerec-box
sudo ./install_dependencies.sh
Code: Select all
def __init__(self):
# Initialize lock servo and button.
# self.servo = PWM.Servo()
RPIO.setup(23, RPIO.OUT)
RPIO.setup(config.BUTTON_PIN, RPIO.IN)
# Set initial box state.
self.button_state = RPIO.input(config.BUTTON_PIN)
self.is_locked = None
Code: Select all
def lock(self):
"""Lock the box."""
# self.servo.set_servo(config.LOCK_SERVO_PIN, config.LOCK_SERVO_LOCKED)
RPIO.output(23, RPIO.HIGH)
self.is_locked = True
def unlock(self):
"""Unlock the box."""
# self.servo.set_servo(config.LOCK_SERVO_PIN, config.LOCK_SERVO_UNLOCKED)
RPIO.output(23, RPIO.LOW)
self.is_locked = False
Code: Select all
"""Raspberry Pi Face Recognition Treasure Box
Treasure Box Script
Copyright 2013 Tony DiCola
"""
import cv2
import config
import face
import hardware
if __name__ == '__main__':
# Load training data into model
print 'Loading training data...'
model = cv2.createEigenFaceRecognizer()
model.load(config.TRAINING_FILE)
print 'Training data loaded!'
# Initialize camer and box.
camera = config.get_camera()
box = hardware.Box()
# 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.'
while True:
# Check if capture should be made.
# TODO: Check if button is pressed.
if box.is_button_up():
if not box.is_locked:
# Lock the box if it is unlocked
box.lock()
print 'Box is now locked.'
else:
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:
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 and resize image to face.
crop = face.resize(face.crop(image, x, y, w, h))
# Test face against model.
label, 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!'
Code: Select all
"""Raspberry Pi Face Recognition Treasure Box
Treasure Box Class
Copyright 2013 Tony DiCola
"""
import time
import cv2
import RPIO
from RPIO import PWM
import picam
import config
import face
class Box(object):
"""Class to represent the state and encapsulate access to the hardware of
the treasure box."""
def __init__(self):
# Initialize lock servo and button.
# self.servo = PWM.Servo()
RPIO.setup(23, RPIO.OUT)
RPIO.setup(config.BUTTON_PIN, RPIO.IN)
# Set initial box state.
self.button_state = RPIO.input(config.BUTTON_PIN)
self.is_locked = None
def lock(self):
"""Lock the box."""
# self.servo.set_servo(config.LOCK_SERVO_PIN, config.LOCK_SERVO_LOCKED)
RPIO.output(23, RPIO.HIGH)
self.is_locked = True
def unlock(self):
"""Unlock the box."""
# self.servo.set_servo(config.LOCK_SERVO_PIN, config.LOCK_SERVO_UNLOCKED)
RPIO.output(23, RPIO.LOW)
self.is_locked = False
def is_button_up(self):
"""Return True when the box button has transitioned from down to up (i.e.
the button was pressed)."""
old_state = self.button_state
self.button_state = RPIO.input(config.BUTTON_PIN)
# Check if transition from down to up
if old_state == config.BUTTON_DOWN and self.button_state == config.BUTTON_UP:
# Wait 20 milliseconds and measure again to debounce switch.
time.sleep(20.0/1000.0)
self.button_state = RPIO.input(config.BUTTON_PIN)
if self.button_state == config.BUTTON_UP:
return True
return False
Code: Select all
"""Raspberry Pi Face Recognition Treasure Box
Treasure Box Class
Copyright 2013 Tony DiCola
"""
import time
import cv2
import RPIO
from RPIO import PWM
import picam
import config
import face
class Box(object):
"""Class to represent the state and encapsulate access to the hardware of
the treasure box."""
def __init__(self):
# Initialize lock servo and button.
# self.servo = PWM.Servo()
RPIO.setup(23, RPIO.OUT)
RPIO.setup(config.BUTTON_PIN, RPIO.IN)
# Set initial box state.
self.button_state = RPIO.input(config.BUTTON_PIN)
self.is_locked = None
def lock(self):
"""Lock the box."""
# self.servo.set_servo(config.LOCK_SERVO_PIN, config.LOCK_SERVO_LOCKED)
RPIO.output(23, RPIO.HIGH)
self.is_locked = True
def unlock(self):
"""Unlock the box."""
# self.servo.set_servo(config.LOCK_SERVO_PIN, config.LOCK_SERVO_UNLOCKED)
RPIO.output(23, RPIO.LOW)
self.is_locked = False
def is_button_up(self):
"""Return True when the box button has transitioned from down to up (i.e.
the button was pressed)."""
old_state = self.button_state
self.button_state = RPIO.input(config.BUTTON_PIN)
# Check if transition from down to up
if old_state == config.BUTTON_DOWN and self.button_state == config.BUTTON_UP:
# Wait 20 milliseconds and measure again to debounce switch.
time.sleep(20.0/1000.0)
self.button_state = RPIO.input(config.BUTTON_PIN)
if self.button_state == config.BUTTON_UP:
return True
return False
Code: Select all
def lock(self):
"""Lock the box."""
# self.servo.set_servo(config.LOCK_SERVO_PIN, config.LOCK_SERVO_LOCKED)
RPIO.output(23, RPIO.HIGH)
self.is_locked = True