├── LICENSE ├── README.md ├── detect_face_image.py ├── detect_face_video.py ├── haarcascade_frontalface_default.xml └── requirements.txt /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 karthikeyanAI 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Face-identifier 2 | The Face Recognition Code In Python Using OpenCV was developed using Python Programming, this Face Detection In Python using Haar cascades is a machine learning based approach where a cascade function is trained with a set of input data. OpenCV already contains many pre-trained classifiers for face, eyes, smiles, etc.. 3 | 4 | Today we will be using the face classifier. You can experiment with other classifiers as well. 5 | About The Project 6 | A Face Recognition Code in Python can detect faces of people in an images, faces contains a list of coordinates for the rectangular regions where faces were found. 7 | We use these coordinates to draw the rectangles in our image. And also the Face Detection Using Python Project can detect also real time faces in a camera on your laptop or pc. 8 | Face Recognition Code In Python Using OpenCV with Source Code 9 | Similarly, the Face Detection In Python Using Opencv can detect faces in videos. As you know videos are basically made up of frames, which are still images. So we perform the face detection for each frame in a video. 10 | The only difference here is that we use an infinite loop to loop through each frame in the video. We use cap.read() to read each frame. 11 | The first value returned is a flag that indicates if the frame was read correctly or not. We don’t need it. The second value returned is the still frame on which we will be performing the 12 | 13 | Steps on How To Build Face Recognition Code In Python Using OpenCV 14 | 15 | 16 | These are the steps on how to install face recognition in python 17 | 18 | Step 1: Download and unzip the zip file. 19 | First, download the source code given below and unzip the zip file. 20 | 21 | The project folder contains 4 files: 22 | 23 | 1.) detect_face_image.py – a python file for detecting face in an image. 24 | 2.) detect_face_video.py – a python file for detecting face in a camera. 25 | 3.) haarcascade_frontalface_default_default.xml – a file that load cascade from image. 26 | 4.) sample.jpg – sample image for experimenting in detecting face. 27 | Step 3: Creating Code for detecting faces in a video. 28 | Next, in the same process in step 2, we want to create a code for detecting faces in a video. 29 | Step 4: Run detect_face_image python file. 30 | The beginner Python project is now complete, you can run the Python file from the command prompt. Make sure to give an image path using ‘-i’ argument. 31 | Step 5: Run detect_face_video python file. 32 | The beginner Python project is now complete, you can run the Python file from the command prompt. 33 | -------------------------------------------------------------------------------- /detect_face_image.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | # Load the cascade 4 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 5 | 6 | # Read the input image 7 | img = cv2.imread('sample.jpg') 8 | 9 | # Convert into grayscale 10 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 11 | 12 | # Detect faces 13 | faces = face_cascade.detectMultiScale(gray, 1.1, 4) 14 | 15 | # Draw rectangle around the faces 16 | for (x, y, w, h) in faces: 17 | cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) 18 | 19 | # Display the output 20 | cv2.imshow('img', img) 21 | cv2.waitKey() 22 | 23 | -------------------------------------------------------------------------------- /detect_face_video.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | # Load the cascade 4 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 5 | # To capture video from webcam. 6 | cap = cv2.VideoCapture(0) 7 | # To use a video file as input 8 | # cap = cv2.VideoCapture('filename.mp4') 9 | while True: 10 | # Read the frame 11 | _, img = cap.read() 12 | # Convert to grayscale 13 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 14 | # Detect the faces 15 | faces = face_cascade.detectMultiScale(gray, 1.1, 4) 16 | # Draw the rectangle around each face 17 | for (x, y, w, h) in faces: 18 | cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) 19 | # Display 20 | cv2.imshow('img', img) 21 | # Stop if escape key is pressed 22 | k = cv2.waitKey(30) & 0xff 23 | if k==27: 24 | break 25 | # Release the VideoCapture object 26 | cap.release() -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.15.4 2 | opencv-python==3.4.3.18 3 | --------------------------------------------------------------------------------