face_recognition

This commit is contained in:
Siwat Sirichai 2023-09-23 13:03:15 +07:00
parent 6c44c742e8
commit 5bd4b4c100
16 changed files with 9681 additions and 9417 deletions

BIN
face_recognition/boom.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

View file

@ -0,0 +1,15 @@
import cv2
cam = cv2.VideoCapture(0)
while True:
check, frame = cam.read()
cv2.imshow('video', frame)
key = cv2.waitKey(1)
if key == 27:
break
cam.release()
cv2.destroyAllWindows()

View file

@ -0,0 +1,102 @@
import face_recognition as face
import numpy as np
import cv2
#ORIGINAL_CODE_CREDIT: https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
#ดึงวิดีโอตัวอย่างเข้ามา, ถ้าต้องการใช้webcamให้ใส่เป็น
video_capture = cv2.VideoCapture(0)
#ใบหน้าคนที่ต้องการรู้จำเป็นreference #คนที่1
boom_image = face.load_image_file("boom.jpg")
boom_face_encoding = face.face_encodings(boom_image)[0]
tat_image = face.load_image_file("tat.jpg")
tat_face_encoding = face.face_encodings(tat_image)[0]
'''
#ใบหน้าคนที่ต้องการรู้จำเป็นreference #คนที่2
oat_image = face.load_image_file("oat.jpg")
oat_face_encoding = face.face_encodings(oat_image)[0]
'''
#ประกาศตัวแปร
face_locations = []
face_encodings = []
face_names = []
face_percent = []
#ตัวแปรนี้ใช้สำหรับคิดเฟรมเว้นเฟรมเพื่อเพิ่มfps
process_this_frame = True
known_face_encodings = [boom_face_encoding, tat_face_encoding]
known_face_names = ["BOOM", "TAT"]
#loopคำนวณแต่ละเฟรมของวิดีโอ
while True:
#อ่านค่าแต่ละเฟรมจากวิดีโอ
ret, frame = video_capture.read()
if ret:
#ลดขนาดสองเท่าเพื่อเพิ่มfps
small_frame = cv2.resize(frame, (0,0), fx=0.5,fy=0.5)
#เปลี่ยน bgrเป็น rgb
#rgb_small_frame = small_frame[:,:,::-1]
rgb_small_frame = np.ascontiguousarray(small_frame[:, :, ::-1])
face_names = []
face_percent = []
if process_this_frame:
#ค้นหาตำแหน่งใบหน้าในเฟรม
face_locations = face.face_locations(rgb_small_frame)
#นำใบหน้ามาหาfeaturesต่างๆที่เป็นเอกลักษณ์
face_encodings = face.face_encodings(rgb_small_frame, face_locations)
#เทียบแต่ละใบหน้า
for face_encoding in face_encodings:
face_distances = face.face_distance(known_face_encodings, face_encoding)
best = np.argmin(face_distances)
face_percent_value = 1-face_distances[best]
#กรองใบหน้าที่ความมั่นใจ50% ปล.สามารถลองเปลี่ยนได้
if face_percent_value >= 0.4:
name = known_face_names[best]
percent = round(face_percent_value*100,2)
face_percent.append(percent)
else:
name = "UNKNOWN"
face_percent.append(0)
face_names.append(name)
#วาดกล่องและtextเมื่อแสดงผลออกมาออกมา
for (top,right,bottom, left), name, percent in zip(face_locations, face_names, face_percent):
top*= 2
right*= 2
bottom*= 2
left*= 2
if name == "UNKNOWN":
color = [46,2,209]
else:
color = [255,102,51]
cv2.rectangle(frame, (left,top), (right,bottom), color, 2)
cv2.rectangle(frame, (left-1, top -30), (right+1,top), color, cv2.FILLED)
cv2.rectangle(frame, (left-1, bottom), (right+1,bottom+30), color, cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left+6, top-6), font, 0.6, (255,255,255), 1)
cv2.putText(frame, "MATCH: "+str(percent)+"%", (left+6, bottom+23), font, 0.6, (255,255,255), 1)
#สลับค่าเป็นค่าตรงข้ามเพื่อให้คิดเฟรมเว้นเฟรม
process_this_frame = not process_this_frame
#แสดงผลลัพท์ออกมา
cv2.imshow("Video", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#ล้างค่าต่างๆเมื่อปิดโปรแกรม
video_capture.release()
cv2.destroyAllWindows()

View file

@ -0,0 +1,105 @@
import face_recognition
import cv2
import numpy as np
import matplotlib.pyplot as plt
# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
# 1. Process each video frame at 1/4 resolution (though still display it at full resolution)
# 2. Only detect faces in every other frame of video.
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)
# Load a sample picture and learn how to recognize it.
PUN_image = face_recognition.load_image_file("PUN.jpg")
PUN_face_encoding = face_recognition.face_encodings(PUN_image)[0]
# Load a second sample picture and learn how to recognize it.
TAT_image = face_recognition.load_image_file("tat.jpg")
TAT_face_encoding = face_recognition.face_encodings(TAT_image)[0]
# Create arrays of known face encodings and their names
known_face_encodings = [
PUN_face_encoding,
TAT_face_encoding
]
known_face_names = [
"PUN",
"TAT"
]
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Only process every other frame of video to save time
if process_this_frame:
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = np.ascontiguousarray(small_frame[:, :, ::-1])
# Find all the faces and face encodings in the current frame of video
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:
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# # If a match was found in known_face_encodings, just use the first one.
# if True in matches:
# first_match_index = matches.index(True)
# name = known_face_names[first_match_index]
# Or instead, use the known face with the smallest distance to the new face
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = known_face_names[best_match_index]
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 box 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)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

BIN
face_recognition/tat.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB