This is how to develop a Python face recognition programme that can identify human face in videos, images

To create a Python face recognition program that can identify human faces in videos and images, you can use libraries like OpenCV and face_recognition. Here's a basic outline:




  1. Install necessary libraries:

pip install opencv-python-headless numpy face_recognition

2.Develop the Python code:

import cv2 import face_recognition # Load a sample image and learn how to recognize it known_image = face_recognition.load_image_file("known_image.jpg") known_encoding = face_recognition.face_encodings(known_image)[0] # Initialize some variables face_locations = [] face_encodings = [] face_names = [] process_this_frame = True # Capture video from your webcam or load a video file cap = cv2.VideoCapture(0) # Change 0 to your desired video file path while cap.isOpened(): ret, frame = cap.read() # Resize frame for faster face recognition processing small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) # Convert the image from BGR color (OpenCV) to RGB color (face_recognition) rgb_small_frame = cv2.cvtColor(small_frame, cv2.COLOR_BGR2RGB) # Process every other frame to save time if process_this_frame: # Find all face locations and face encodings in the current frame face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) face_names = [] for face_encoding in face_encodings: # Compare each face found with the known face and check if it matches matches = face_recognition.compare_faces([known_encoding], face_encoding) name = "Unknown" if True in matches: name = "Known Person" face_names.append(name) process_this_frame = not process_this_frame # Display the results for (top, right, bottom, left), name in zip(face_locations, face_names): # Scale back up face locations since the frame we detected in was scaled to 1/4 size top *= 4 right *= 4 bottom *= 4 left *= 4 # Draw a rectangle around the face cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # Draw a label with a name below the face cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1) # Display the resulting image cv2.imshow('Video', frame) # Break the loop if 'q' is pressed if cv2.waitKey(1) & 0xFF == ord('q'): break # Release the video capture object and close all windows cap.release() cv2.destroyAllWindows()


Then replace "known_image.jpg" with the path to an image containing the known face you want to recognize. This code captures video from your webcam, detects faces, and compares them with the known face. If a match is found, it labels the face as "Known Person" in the video and if you find this programme useful do share with others to stay updated also check out my YouTube channel by clicking on the link below and subscribe to my YouTube channel:👇👇👇

https://youtube.com/@ugxcode?si=5rnqWpQK9qzFJMHX


Comments

  1. I like this programme you developed and I am to use it in my project I am developing

    ReplyDelete
  2. This is cool, you have given me a hint of how I should structure my project

    ReplyDelete
  3. Gratitude and stay updated everyday for new tutorials

    ReplyDelete

Post a Comment

Popular posts from this blog

Fetching Data from an API in React Native with Expo

State Management in React Native with Expo: A Beginner’s Guide

How to Set Up React Native with Expo 🚀