Giter Site home page Giter Site logo

face-recognition-train-yml-python's Introduction

Face-Recognition-Train-YML-Python

In this project you can have face detection using 'haarcascade_frontalface_default.xml' file python code. you can run 'face.py' access faces thurough USB camera.

To do face Recognition part first you have to create 'train.yml' file it's like a 'haarcascade_frontalface_default.xml' file.In that file have specific faces data.After generate 'train.yml' file you can run 'recognizer.py' file, in this file can do recognition part.

face-recognition-train-yml-python's People

Contributors

asankad7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

face-recognition-train-yml-python's Issues

cv2.error: OpenCV(4.9.0) D:\a\opencv-python\opencv-python\opencv_contrib\modules\face\src\lbph_faces.cpp:406: error: (-5:Bad argument) This LBPH model is not computed yet. Did you call the train method? in function 'cv::face::LBPH::predict

import re

from sys import path
from tkinter import*
from tkinter import ttk
from PIL import Image,ImageTk
import os
import mysql.connector
import cv2
import numpy as np
from tkinter import messagebox
from time import strftime
from datetime import datetime
class Face_Recognition:

def __init__(self,root):
    self.root=root
    self.root.geometry("1366x768+0+0")
    self.root.title("Face Recognition Pannel")

    # This part is image labels setting start 
    # first header image  
    img=Image.open(r"C:\Users\mc882\Videos\Captures\OneDrive\Desktop\Python-FYP-Face-Recognition-Attendence-System-master\Images_GUI\banner.jpg")
    img=img.resize((1366,130),Image.Resampling.LANCZOS)
    self.photoimg=ImageTk.PhotoImage(img)

    # set image as lable
    f_lb1 = Label(self.root,image=self.photoimg)
    f_lb1.place(x=0,y=0,width=1366,height=130)

    # backgorund image 
    bg1=Image.open(r"C:\Users\mc882\Videos\Captures\OneDrive\Desktop\Python-FYP-Face-Recognition-Attendence-System-master\Images_GUI\bg2.jpg")
    bg1=bg1.resize((1366,768),Image.Resampling.LANCZOS)
    self.photobg1=ImageTk.PhotoImage(bg1)

    # set image as lable
    bg_img = Label(self.root,image=self.photobg1)
    bg_img.place(x=0,y=130,width=1366,height=768)


    #title section
    title_lb1 = Label(bg_img,text="Welcome to Face Recognition Pannel",font=("verdana",30,"bold"),bg="white",fg="navyblue")
    title_lb1.place(x=0,y=0,width=1366,height=45)

    # Create buttons below the section 
    # ------------------------------------------------------------------------------------------------------------------- 
    # Training button 1
    std_img_btn=Image.open(r"C:\Users\mc882\Videos\Captures\OneDrive\Desktop\Python-FYP-Face-Recognition-Attendence-System-master\Images_GUI\f_det.jpg")
    std_img_btn=std_img_btn.resize((180,180),Image.Resampling.LANCZOS)
    self.std_img1=ImageTk.PhotoImage(std_img_btn)

    std_b1 = Button(bg_img,command=self.face_recog,image=self.std_img1,cursor="hand2")
    std_b1.place(x=600,y=170,width=180,height=180)

    std_b1_1 = Button(bg_img,command=self.face_recog,text="Face Detector",cursor="hand2",font=("tahoma",15,"bold"),bg="white",fg="navyblue")
    std_b1_1.place(x=600,y=350,width=180,height=45)
#=====================Attendance===================

def mark_attendance(self,i,r,n):
    with open("attendance.csv","r+",newline="\n") as f:
        myDatalist=f.readlines()
        name_list=[]
        for line in myDatalist:
            entry=line.split((","))
            name_list.append(entry[0])

        if((i not in name_list)) and ((r not in name_list)) and ((n not in name_list)):
            now=datetime.now()
            d1=now.strftime("%d/%m/%Y")
            dtString=now.strftime("%H:%M:%S")
            f.writelines(f"\n{i}, {r}, {n}, {dtString}, {d1}, Present")


#================face recognition==================
def face_recog(self):
    def draw_boundray(img,classifier,scaleFactor,minNeighbors,color,text,clf):
       

        coord=[]
        
        for (x,y,w,h) in featuers:
            cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
            id,predict=clf.predict(gray_image[y:y+h,x:x+w])

            confidence=int((100*(1-predict/300)))

            conn = mysql.connector.connect(user='root', password='Azsxdc!#312',host='127.0.0.1',database='face_recognition',port=3306)
            cursor = conn.cursor()

            cursor.execute("select Name from student where Student_ID="+str(id))
            n=cursor.fetchone()
            n="+".join(n)

            cursor.execute("select Roll_No from student where Student_ID="+str(id))
            r=cursor.fetchone()
            r="+".join(r)

            cursor.execute("select Student_ID from student where Student_ID="+str(id))
            i=cursor.fetchone()
            i="+".join(i)


            if confidence > 77:
                cv2.putText(img,f"Student_ID:{i}",(x,y-80),cv2.FONT_HERSHEY_COMPLEX,0.8,(64,15,223),2)
                cv2.putText(img,f"Name:{n}",(x,y-55),cv2.FONT_HERSHEY_COMPLEX,0.8,(64,15,223),2)
                cv2.putText(img,f"Roll-No:{r}",(x,y-30),cv2.FONT_HERSHEY_COMPLEX,0.8,(64,15,223),2)
                self.mark_attendance(i,r,n)
            else:
                cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),3)
                cv2.putText(img,"Unknown Face",(x,y-5),cv2.FONT_HERSHEY_COMPLEX,0.8,(255,255,0),3)    

            coord=[x,y,w,y]
        
        return coord    


    #==========
    def recognize(img,clf,faceCascade):
        coord=draw_boundray(img,faceCascade,1.1,10,(255,25,255),"Face",clf)
        return img
    
    faceCascade=cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
    clf=cv2.face.LBPHFaceRecognizer_create()
    clf.read(r"C:\Users\mc882\Videos\Captures\OneDrive\Desktop\Python-FYP-Face-Recognition-Attendence-System-master\clf.xml")
    def recognize(img,clf,faceCascade):
        coord=draw_boundray(img,faceCascade,1.1,10,(255,25,255),"Face",clf)
        return img
    videoCap=cv2.VideoCapture(0)

    while True:
        ret,img=videoCap.read()
        gray_image=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        featuers=faceCascade.detectMultiScale(gray_image,1.3,5)
        img=recognize(img,clf,faceCascade)
        cv2.imshow("Face Detector",img)

        if cv2.waitKey(1) == 13:
            break
    videoCap.release()
    cv2.destroyAllWindows()

if name == "main":
root=Tk()
obj=Face_Recognition(root)
root.mainloop()

C:\ci\opencv_1512688052760\work\opencv_contrib-3.3.1\modules\face\src\lbph_faces.cpp:400: error: (-5) This LBPH model is not computed yet. Did you call the train method? in function cv::face::LBPH::predict

i get an error
Here is my code

import cv2
import numpy as np
faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam=cv2.VideoCapture(0);
rec=cv2.face.LBPHFaceRecognizer_create();
rec.read("recognizer/trainningData.yml")
id=0
font = cv2.FONT_HERSHEY_SIMPLEX
while(True):
    ret,img=cam.read();
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    faces=faceDetect.detectMultiScale(gray,1.3,5);
    for(x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
        id,conf=rec.predict(gray[y:y+h,x:x+w])
        cv2.putText(img,str(id),(x,y+h),font,1,255)
        
    cv2.imshow("Face",img);
    if(cv2.waitKey(1)==ord('q')):
        break;
cam.release()
cv2.destroyAllWindows()

can you help me ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.