Face Detection and Recognition – รายวิชา Selected Topics

Face Detection and Recognition

รายวิชา Selected Topics in Information Technology

 

นายธิติ จิวะวิวัฒน์เสถียร และนางสาวศิรินารถ สีหาบุตร ได้ทำโปรเจครายวิชา Selected Topics in Information Technology โดยได้ทำเกี่ยวกับ Face Detection and Recognition ได้ใช้ Library ของ face_recognition มาช่วยในการค้นหาใบหน้า (Face Detection) และรู้จำใบหน้า (Face Recognition)

 

ติดตั้งโปรแกรม

pip install face_recognition
pip install cmake
pip install dlib
pip install opencv-python

 

  • ตัวอย่างรูปภาพที่เก็บไว้ในโฟลเดอร์เพื่อใช้สำหรับนำมาเปรียบเทียบ โดยรูปจะต้องประกอบด้วย ใบหน้า

  • import library ชื่อ face_recognition และ opencv เข้ามาเพื่อใช้งาน

import face_recognition
import cv2

  • นำรูปภาพที่จัดเก็บไว้ในโฟลเดอร์ (known.jpg และ known2.jpg) มาค้นหาใบหน้าและหาคุณลักษณะพิเศษ (Feature Extraction)

known_pic = face_recognition.load_image_file(“known.jpg”)
face_encoding = face_recognition.face_encodings(known_pic)[0]

known_pic2 = face_recognition.load_image_file(“known2.jpg”)
face_encoding2 = face_recognition.face_encodings(known_pic2)[0]

  • กำหนด Label ให้กับแต่ละบุคคล (ใบหน้า)

known_face_encodings = [
face_encoding,
face_encoding2,
]

known_face_names = [
“Boss”,
“Pin”,
]

  • ตัวอย่าง OpenCV เพื่อเรียกใช้ Web Camera

video_capture = cv2.VideoCapture(0)

ret, frame = video_capture.read()

  • ตัวอย่างการนำข้อมูลจาก frame มาใช้เพื่อค้นหาใบหน้า และคำนวณหาคุณลักษณะพิเศษ

small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

# แปลงสีภาพจาก BGR (ถูกใช้ใน OpenCV) เป็นสีแบบ RGB (ถูกใช้ใน face_recognition)
rgb_small_frame = small_frame[:, :, ::-1]

face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

  • นำใบหน้าที่ค้นหาได้ในแต่ละ frame (face_encodings) มาเปรียบเทียบกับภาพในโฟลเดอร์ (known_face_encodings) ด้วยคำสั่ง face_recognition.compare_faces และเก็บไว้ที่ตัวแปร matches
  • หากเปรียบเทียบแล้วเหมือนจะได้ค่า True และนำ index ที่ได้ไปตรวจสอบกับตัวแปร known_face_names

face_names = []for face_encoding in face_encodings:
     # ทำการเปรียบเทียบใบหน้าที่อยู่ในวีดีโอกับใบหน้าที่รู้จักในระบบ
     matches = face_recognition.compare_faces(known_face_encodings,  
                        face_encoding)
     name = “Unknown”

     # ถ้า encoding แล้วใบหน้าตรงกันก็จะแสดงข้อมูล
     if True in matches:
          first_match_index = matches.index(True)
          name = known_face_names[first_match_index]

     face_names.append(name)

  • จากนั้นทำการตีกรอบให้กับใบหน้าและแสดงชื่อ

 

Leave a Reply

Your email address will not be published. Required fields are marked *