Nhận diện khuôn mặt với OpenCV Python

- Môi trường thử nghiệm

- Cài đặt thư viện trên pycharm (Terminal)  hoặc cmd (desktop) :
  1. - pip install numpy
  2.  - pip install opencv - python

Source Code :

# import thư viện vào

import numpy as np
import cv2

# Đầu vào của camera và file xml

face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eyeCascade = cv2.CascadeClassifier('haarcascade_eye.xml')
smile_cascade = cv2.CascadeClassifier('haarcascade_smile.xml')
cap = cv2.VideoCapture(0)

# Đây là đầu ra của video khi bạn thực hiện nhận diện xong

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

# Quá trình này dùng để tạo video thành màu xám, và các bước tính toán dữ liệu của khuôn mặt gồm có faces, eyes, smiles

while (True):
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray)
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = frame[y:y + h, x:x + w]

        eyes = eyeCascade.detectMultiScale(
            roi_gray,
            scaleFactor=1.5,
            minNeighbors=10,
            minSize=(5, 5),
        )

        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (255, 255, 0), 2)
        smiles = smile_cascade.detectMultiScale(roi_gray, 1.7, 20)
        for (sx, sy, sw, sh) in smiles:
            # Draw the rectangle around every eye
            cv2.rectangle(roi_color, (sx, sy), (sx + sw, sy + sh), (0, 0, 255), 2)

# Đầu ra của video được ghi lại

    out.write(frame)

# Toàn bộ trên đã được phác hoa ra màn hình của bạn

    cv2.imshow('Camera - Khai TK', frame)

#Kết thúc chương trình
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
out.release()
cv2.destroyAllWindows()






- Video đã được ghi lại quá trình này .







- Chúc các bạn thành công!
Previous Post Next Post