Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am working on a face recognition project which will recognize the face and will send sms that who is at the door? So while running the program on terminal the CV2 frame is showing a wrong name and the command terminal recognizing the same face correctly. can't understand whats happening the terminal is giving the correct name and confidence while cV2 frame dosen't. Here is my Recognition Code


Python
import cv2
import boto3
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.setmode(GPIO.BCM)
GPIO.setup(relay_pin, GPIO.OUT)
GPIO.output(relay_pin, GPIO.LOW)
GPIO.output(relay_pin, GPIO.HIGH)
with open('labels', 'rb') as f:
    dict= pickle.load(f)
    f.close()
 
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(640, 480))
client = boto3.client(
    "sns",
    aws_access_key_id="",
    aws_secret_access_key="",
    region_name=""
)
# Load prebuilt model for Frontal Face
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# Create Local Binary Patterns Histograms for face recognization
recognizer = cv2.face.createLBPHFaceRecognizer()
# Load the trained mode
recognizer.load("trainer.yml")
 
font = cv2.FONT_HERSHEY_SIMPLEX
 
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    frame = frame.array
     # Convert the captured frame into grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.5, minNeighbors = 5)
    # For each face in faces
    for (x, y, w, h) in faces:
         # Create rectangle around the face
        roiGray = gray[y:y+h, x:x+w]
# Recognize the face belongs to which ID
        id_, conf = recognizer.predict(roiGray)
 
        for name, value in dict.items():
            if value == id_:
                print(name)
                print(conf)
#agar confidence <=70 hoga toh door open hoga wrna nhi
                     # Put text describe who is in the picture
        if conf <= 70:
            GPIO.output(26, GPIO.HIGH)
            cv2.rectangle(frame,(x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)
            client.publish(
    PhoneNumber="+",
    Message= name+"  is at the door"
    )
            cv2.putText(frame,name+ str(conf), (x,y-40), font, 1, (255,255,255), 3)
 
        else:
            GPIO.output(26, GPIO.LOW)
 # Display the video frame with the bounded rectangle
    cv2.imshow('frame', frame)
    key = cv2.waitKey(1)
 
    rawCapture.truncate(0)
#if cross button is pressed close the cam
    if key == 27:
        break
 
cv2.destroyAllWindows()


What I have tried:

I cant even understand why is it showing diffrent names on both windows?
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900