
Advanced Face Recognition Using Python: Data Capture and Encoding
Explore the world of face recognition with Python through data collection, encoding, and image processing. Learn to integrate OpenCV and face recognition libraries to detect and encode faces, creating a powerful tool for identity verification and tracking. This comprehensive guide covers image manipulation, data storage, and face matching algorithms, offering a deep dive into the practical applications of facial recognition technology.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
2021 0817
Face_recognition faceR01.py faceR02.py faceR03.py webcam
Face_recognition faceR04.py array.bin faceR05.py faceR06.py webcam
Face_recognition data datac
# faceR04.py import cv2 import face_recognition import os import numpy as np path = 'datac' pic = 1 images = [] classNames = [] encodeListKnown=[] myList = os.listdir(path) print("myList=", myList) print(" data ") for cl in myList: curImg = cv2.imdecode(np.fromfile(file=f {path}/{cl} , dtype=np.uint8), cv2.IMREAD_COLOR) images.append(curImg) classNames.append(os.path.splitext(cl)[0]) # ( ) classNames print(" encodeListKnown ") for img in images: print(" "+str(pic)+" "+ classNames[pic-1]) pic +=1 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) encode = face_recognition.face_encodings(img, model="cnn")[0] encodeListKnown.append(encode)
# faceR04.py print(" ( ) classNamesFile.txt !!") with open("classNamesFile.txt", "w") as f: for i in range(len(images)): f.write(classNames[i]+'\n') print(" ( ) array.bin !!") with open("array.bin", "wb") as f: for i in range(len(encodeListKnown)): encodeListKnown[i].tofile(f) # print(encodeListKnown) # print('classNames = ', classNames) # print('# of imgs = ', len(images)) # print('people in encodeListKnown = ', len(encodeListKnown)) print(' ')
# faceR05.py import cv2 import face_recognition import numpy as np from PIL import Image, ImageDraw, ImageFont # classNamesFile.txt ( ) with open("classNamesFile.txt", "r") as f: classNames = f.read().splitlines() print('classNames = ', classNames) with open("array.bin", "rb") as f: # array.bin encodeListKnown = np.fromfile(f) encodeListKnown = encodeListKnown.reshape(-1, 128) print(' ', len(encodeListKnown),' ) image = cv2.imread("pin31.JPG") facesCurFrame = face_recognition.face_locations(image) # encodesCurFrame = face_recognition.face_encodings(image, facesCurFrame)
# faceR05.py for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame): matches = face_recognition.compare_faces(encodeListKnown, encodeFace) faceDis = face_recognition.face_distance(encodeListKnown, encodeFace) matchIndex = np.argmin(faceDis) # faceDis Index if matches[matchIndex]: # match name = classNames[matchIndex].upper() # index print("faceDis = ", faceDis) print("matchIndex = ", matchIndex) print(name) y1, x2, y2, x1 = faceLoc cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.rectangle(image, (x1, y2 + 25), (x2, y2), (0, 0, 255), cv2.FILLED) font = ImageFont.truetype("simsun.ttc", 20) # img_pil = Image.fromarray(image) # numpy array PIL draw = ImageDraw.Draw(img_pil) # # draw.text((x1 + 6, y2 + 2), name, font=font, fill=(255, 255, 255,1)) image = np.array(img_pil) cv2.imshow('MATCH', image) cv2.waitKey(0)
faceR06.py webcam
# faceR06.py import cv2 import face_recognition import numpy as np from PIL import Image, ImageDraw, ImageFont import time import codecs keyb = 0 # classNamesFile.txt ( ) with open("classNamesFile.txt", "r") as f: classNames = f.read().splitlines() print('classNames = ', classNames) with open("array.bin", "rb") as f: # array.bin encodeListKnown = np.fromfile(f) encodeListKnown = encodeListKnown.reshape(-1, 128) print(' ', len(encodeListKnown),' ) cap = cv2.VideoCapture(0)
while True: prev_time = time.time() success, frame = cap.read() frame = cv2.resize(frame,(800, 450)) facesCurFrame = face_recognition.face_locations(frame, model="cnn") encodesCurFrame = face_recognition.face_encodings(frame, facesCurFrame) for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame): matches = face_recognition.compare_faces(encodeListKnown, encodeFace) faceDis = face_recognition.face_distance(encodeListKnown, encodeFace) matchIndex = np.argmin(faceDis) if matches[matchIndex]: name = classNames[matchIndex].upper() print("matchIndex = ", matchIndex, name , "faceDis = ", faceDis) y1, x2, y2, x1 = faceLoc cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.rectangle(frame, (x1, y2 + 25), (x2, y2), (0, 0, 255), cv2.FILLED) font = ImageFont.truetype("simsun.ttc", 20) # img_pil = Image.fromarray(image) # PIL draw = ImageDraw.Draw(img_pil) # # draw.text((x1 + 6, y2 + 2), name, font=font, fill=(255, 255, 255,1)) frame = np.array(img_pil)
with codecs.open('report.txt', 'r+, encoding='utf-8') as f: myDataList = f.readlines() nameList = [] for line in myDataList: entry = line.split(',') nameList.append(entry[0]) now = datetime.now() dtString = now.strftime('%H:%M:%S') if (name not in nameList) or (keyb == ord('s')): f.writelines(f'\n{name},{dtString}') cv2.putText(frame, 'fps: ' + str(int(1 / (time.time() - prev_time))), (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1, cv2.LINE_AA) cv2.imshow('Webcam match', frame) keyb = cv2.waitKey(1) & 0xFF if keyb == 27: break
while True: prev_time = time.time() success, frame = cap.read() frame = cv2.resize(frame,(800, 450)) imgS = cv2.resize(frame, (0, 0), None, 0.25, 0.25) facesCurFrame = face_recognition.face_locations(imgS, model="cnn") encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame) for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame): matches = face_recognition.compare_faces(encodeListKnown, encodeFace) faceDis = face_recognition.face_distance(encodeListKnown, encodeFace) matchIndex = np.argmin(faceDis) if matches[matchIndex]: ssss name = classNames[matchIndex].upper() print("matchIndex = ", matchIndex, name , "faceDis = ", faceDis) y1, x2, y2, x1 = faceLoc y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4 cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2) cv2.rectangle(frame, (x1, y2 + 25), (x2, y2), (0, 0, 255), cv2.FILLED)
Dollar bill premium icon Binoculars free icon Employees premium icon Website premium icon Coffee premium icon Target premium icon Telescope premium icon Calculator premium icon Idea premium icon Basket premium icon Clock premium icon Smartphone premium icon Speedometer premium icon Mountain premium icon Money bag premium icon Megaphone premium icon Piggy bank premium icon Rocket free icon Brain premium icon Team premium icon Tie premium icon Glasses premium icon Flask free icon Trophy premium icon Lifebuoy premium icon Documentation premium icon Flag premium icon Magnifying glass premium icon Store free icon Paper plane free icon Organization premium icon Ufo premium icon Suitcase premium icon Employee premium icon Pointer premium icon Compass premium icon Diagram premium icon Diagram free icon Calendar free icon Cogwheel premium icon Coffee premium icon Chat premium icon Microscope premium icon Mortarboard premium icon Smartphone premium icon Coins premium icon Medal premium icon Horse free icon Form free icon
Stage 1 Mauris at risus neque. Praesent et leo id ligula rutrum ullamcorper. 2021 2023 2025 Stage 1 Mauris at risus neque. Praesent et leo id ligula rutrum ullamcorper. 2022 2024 2026