I tried making a "face recognition login system" with Python [OpenCV x face detection]

programming

It would be convenient if you could log in with your face...

For those who have such concerns, I will use Python and OpenCV.Your own face recognition login systemWe will show you how to make it.

Although it may look difficult, the code is actually very simple.

So that anyone can make it easily,Necessary preparations, code, and the final productAll of this is explained in detail.

Recommended for those who want to strengthen security or create useful tools using Python.

What is the facial recognition login system?

conclusion

Facial recognition login is a system that allows you to log in by using your face to verify your identity.

reason

In recent years, password leaks and theft have become a problem.

Therefore,Biometric authentication technology (face, fingerprint, voice, etc.)is attracting attention.

Facial recognition has become so common that it is even used on smartphones.

Examples

For example, face unlock on your smartphone.

You can easily create something similar to this in Python.

point

• All you need is a camera (no high-end camera required)

• Use the free library "OpenCV"

• Less than 100 lines of code

Basic preparation of Python and OpenCV

conclusion

To implement the face recognition system, you will need to prepare "Python" and "OpenCV".

reason

Image processing technology is essential for facial recognition processing.

OpenCV is a free library specialized for image analysis and has a proven track record.

What you need

• Python (recommended: 3.9 or higher)

• OpenCV (cv2)

• Cascade file for face recognition (haarcascade_frontalface_default.xml)

How to install

1
pip install opencv-python

Download the Cascade file here:

OpenCV GitHub

How to register and save face images

conclusion

First, you register your face and save it as data.

reason

This is because when you log in, something to compare it to is needed to determine who you match.

Implementation flow

• Start the camera

• Detect faces

• Stores captured images (in place of a database)

1
import cv2 cap = cv2.VideoCapture(0) face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') while True: ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray) for (x, y, w, h) in faces: face = frame[y:y+h, x:x+w] cv2.imwrite("face.jpg", face) cv2.rectangle(frame, (x,y), (x+w,y+h), (255,0,0), 2) cv2.imshow('Register Face', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()

How to implement face recognition (login determination)

conclusion

If the registered face is similar to the current face, the login is determined to be successful.

reason

By comparing the similarity of facial images, it is possible to determine whether the person is the real person.

Implementation Steps

• Load the training image

• Load the current face image

• Matching using image similarity (histogram comparison)

1
def is_match(img1_path, img2_path): img1 = cv2.imread(img1_path) img2 = cv2.imread(img2_path) img1 = cv2.resize(img1, (100, 100)) img2 = cv2.resize(img2, (100, 100)) hist1 = cv2.calcHist([img1], [0], None, [256], [0,256]) hist2 = cv2.calcHist([img2], [0], None, [256], [0,256]) score = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL) return score > 0.9 # A match of 0.9 or higher is considered a match.

Completed code for face recognition login

Below is the face recognition login system.Completed Codeis.

1
import cv2 def is_match(img1_path, img2_path): img1 = cv2.imread(img1_path) img2 = cv2.imread(img2_path) img1 = cv2.resize(img1, (100, 100)) img2 = cv2.resize(img2, (100, 100)) hist1 = cv2.calcHist([img1], [0], None, [256], [0,256]) hist2 = cv2.calcHist([img2], [0], None, [256], [0,256]) score = cv2.compareHist(hist1, hist2, cv2.HISTCMP_CORREL) return score > 0.9 cap = cv2.VideoCapture(0) face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') while True: ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray) for (x, y, w, h) in faces: face = frame[y:y+h, x:x+w] cv2.imwrite("login_face.jpg", face) result = is_match("face.jpg", "login_face.jpg") print("Login successful" if result else "Login failed") cv2.imshow('Login Check', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()

Key security measures

• Ideally, facial images should be stored encrypted.

• Similarity score threshold is adjustable

• To prevent unauthorized access by othersAdjusting lighting and facial expressionsAlso necessary

Related links

summary

Face recognition login systemSurprisingly simple structureYou can make it with:

With Python and OpenCV,Dozens of lines of codeThis completes the creation of a full-fledged identity verification tool.

If you want to improve security or login convenience, please refer to this article.Create your own face recognition system.

Copied title and URL