Click here to Skip to main content
14,643,391 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello Friends,

I'm messing about with a facial recognition door locking project but it unlocks the condition is met. I've tried a few solution but none were successful and i'm stumped.The power is regulated with a one-channel relay switch but when i run my program it unlocks immediately. the project is based of this How to Create a Facial Recognition Door Lock With Raspberry Pi | Raspberry Pi | Maker Pro[^]
i'm new enough to python and any advice would be greatly appreciated.

import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import numpy as np 
import pickle
import RPi.GPIO as GPIO
from time import sleep

relay_pin = [26]
GPIO.setup(relay_pin, GPIO.OUT)
GPIO.output(relay_pin, 0)

with open('labels', 'rb') as f:
	dict = pickle.load(f)

camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(640, 480))

faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
recognizer = cv2.face.LBPHFaceRecognizer_create()"trainer.yml")


for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
	frame = frame.array
	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.5, minNeighbors = 5)
	for (x, y, w, h) in faces:
		roiGray = gray[y:y+h, x:x+w]

		id_, conf = recognizer.predict(roiGray)

		for name, value in dict.items():
			if value == id_:

		if conf <= 70:
			GPIO.output(relay_pin, 1)
			cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
			cv2.putText(frame, name + str(conf), (x, y), font, 2, (0, 0 ,255), 2,cv2.LINE_AA)

			GPIO.output(relay_pin, 0)

	cv2.imshow('frame', frame)
	key = cv2.waitKey(1)


	if key == 27:


What I have tried:

Ive attempted putting in time stops to see if it was a code synchronization error and i've attempted booting the code at startup but none of these have seemed to work
Updated 4-May-20 20:56pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100