import cv2
import numpy as np
import time
from pad4pi import rpi_gpio
# Set up keypad
KEYPAD = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
['*', 0, '#']
]
ROW_PINS = [5, 6, 13, 19] # BCM numbering
COL_PINS = [21, 20, 16] # BCM numbering
factory = rpi_gpio.KeypadFactory()
keypad = factory.create_keypad(keypad=KEYPAD, row_pins=ROW_PINS, col_pins=COL_PINS)
# Set up servo
SERVO_PIN = 17
SERVO_MIN_DUTY = 3
SERVO_MAX_DUTY = 14
# Set up face recognition model (Assuming you have pre-trained model)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Set up webcam
cap = cv2.VideoCapture(0)
# Function to recognize faces
def recognize_faces():
# Your face recognition code here
pass
# Function to control servo
def control_servo(angle):
# Calculate duty cycle from angle
duty = SERVO_MIN_DUTY + (angle / 180) * (SERVO_MAX_DUTY - SERVO_MIN_DUTY)
# Your servo control code here
pass
# Function to handle keypad input
def handle_keypad_input(key):
# Your keypad input handling code here
pass
# Main loop
while True:
ret, frame = cap.read()
if not ret:
continue
# Convert frame to grayscale for face detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the grayscale frame
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
# Draw rectangle around the face
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.imshow('frame', frame)
# Check for keypad input
keypad.registerKeyPressHandler(handle_keypad_input)
# Exit if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()