
บทเรียนต่อไปนี้คือเทคนิคการทำ Face Detection ด้วย Python และ OpenCV ในการจับใบหน้าคนที่ปรากฏบนไฟล์ Video
บทเรียนก่อนหน้านี้:
- การประมวลผลรู้จำใบหน้า Face Recognition ด้วย Python
- Face Recognition เปรียบเทียบข้อมูลใบหน้าแบบ Real-Time ด้วย Python และ OpenCV
- Python กับ OpenCV ดึงฐานข้อมูล Face Recognition ผ่านระบบ Firebase
- Review: บริการ AI for Thai มาทดสอบระบบการระบุตำแหน่งบุคคลในภาพ (Person Detection) ด้วย Python กัน
สำหรับบทเรียนนี้นอกจากการจับภาพจาก WebCam หรือ USB Camera ที่เรานิยมใช้กับแบบ Real-Time แล้วจะเป็นการสอนการจับภาพ หรือประมวลผลใบหน้าจากไฟล์ Video นามสกุล mp4 เป็นหลัก โดยไฟล์ Video ที่เราจะใช้นั้นให้เซฟ แล้วดาวน์โหลดไว้ที่ โฟลเดอร์ images ใน Path เดียวกับ Source ของ Python ที่เราจะเขียน
ผมจะใช้ไฟล์ movie.mp4 ในโฟลเดอร์ images เป็น video ที่จะนำมาทดสอบ เรียก Header มาใช้เลยคือ:
1 2 3 4 |
import cv2 import face_recognition cap = cv2.VideoCapture( r'images/movie.mp4',0) |
เรียก face_recognition มาใช้งานหลังจากนั้นสร้าง Landmark ของใบหน้าไว้เป็น Array สำหรับวนหาใบหน้า
1 |
face_locations = [] |
หลังจากนั้นให้เราเขียนคำสั่งต่อไปนี้:
1 2 3 4 5 6 7 8 9 |
while True: ret, frame = cap.read() rgb_frame = frame[:, :, ::-1] face_locations = face_recognition.face_locations(rgb_frame) for top, right, bottom, left in face_locations: cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 4) frame = cv2.resize(frame, (600, 340)) cv2.imshow('Video', frame) |
การทำงานคือ จับ Frame ของ Video ทีละ Frame โดย
1 |
ret, frame = cap.read() |
หลังจากนั้นแปลงภาพจาก Frame ที่เป็น OpenCV ประมวลผลแบบ BGR Color ให้เป็น RGB
1 |
rgb_frame = frame[:, :, ::-1] |
ในแต่ละ Frame จะทำการค้นหาใบหน้าทั้งหมดที่ปรากฏในภาพ Frame
1 |
face_locations = face_recognition.face_locations(rgb_frame) |
ทำการวาดสี่เหลี่ยมสีเขียวลงบนหน้าที่ตรวจจับได้:
1 2 |
for top, right, bottom, left in face_locations: cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 4) |
ทำการย่อขนาดของหน้าต่าง video ตัว output แล้วแสดงผลลัพธ์ออกมา
1 2 |
frame = cv2.resize(frame, (600, 340)) cv2.imshow('Video', frame) |
เขียนคำสั่งด Enter แล้วปิดหน้าต่าง:
1 2 |
if cv2.waitKey(25) == 13: break |
และสุดท้ายถ้าประมวลผลเสร็จสิ้นให้ทิ้ง Task ที่กำลังทำงานอยู่ออกไปให้หมด
1 2 |
cap.release() cv2.destroyAllWindows() |
จะเห็นว่าไฟล์ Python ของเราจะเป็นดังนี้
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import cv2 import face_recognition cap = cv2.VideoCapture( r'images/movie.mp4',0) face_locations = [] while True: ret, frame = cap.read() rgb_frame = frame[:, :, ::-1] face_locations = face_recognition.face_locations(rgb_frame) for top, right, bottom, left in face_locations: cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 4) frame = cv2.resize(frame, (600, 340)) cv2.imshow('Video', frame) if cv2.waitKey(25) == 13: break cap.release() cv2.destroyAllWindows() |
ทดสอบก็จะได้ผลลัพธ์ตามนี้:
ทีนี้ลองเอา ระบบ Face Reconition มาใช้แต่ต้องเปลี่ยนวีดีโอเล็กน้อยที่คมชัดเพราะวีดีโอข้างต้นมันขึ้น Unknown หมดเลยทุกคน
ใส่ code นี้ไปเลย:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import cv2 import face_recognition cap = cv2.VideoCapture( r'images/movie.mp4',0) database_image = face_recognition.load_image_file("images/banyapon.jpg") data_base_encoding = face_recognition.face_encodings(database_image)[0] person_face_encodings = [data_base_encoding] person_face_names = ["BANYAPON"] data_locations = [] data_encodings = [] data_names = [] frameProcess = True while True: ret, frame = cap.read() rgb_frame = frame[:, :, ::-1] data_locations = face_recognition.face_locations(rgb_frame) for top, right, bottom, left in data_locations: cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 4) data_encodings = face_recognition.face_encodings(rgb_frame, data_locations) data_names = [] for dc in data_encodings: matches = face_recognition.compare_faces(person_face_encodings, dc) name = "UNKNOWN" if True in matches: first_match_index = matches.index(True) name = person_face_names[first_match_index] data_names.append(name) cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (26, 174, 10), cv2.FILLED) font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1) frame = cv2.resize(frame, (600, 340)) cv2.imshow('Video', frame) if cv2.waitKey(25) == 13: break cap.release() cv2.destroyAllWindows() |
ทดสอบไฟล์ mp4 แบบใบหน้าเดียว
ทดสอบไฟล์ mp4 แบบหลายๆ ใบหน้า ก็ไม่ค่อยแม่นเท่าไรอ่ะนะจะจับได้บางมุมอยู่ที่ความชัดของ Video นั่นแหละครับ
One Comment