Facial Landmarks and Face Detection in Python with OpenCV
“Deteksi wajah adalah teknologi komputer yang digunakan dalam berbagai aplikasi yang mengidentifikasi wajah manusia dalam gambar digital. Deteksi wajah juga mengacu pada proses psikologis di mana manusia menemukan dan memperhatikan wajah dalam adegan visual.” — Wikipedia
Deteksi landmark wajah adalah proses mendeteksi landmark atau area yang diminati (poin-poin) pada wajah seperti siluet Alis, Mata, Hidung, Mulut dan Rahang.
Beberapa aplikasi deteksi landmark wajah adalah pertukaran wajah, deteksi pose kepala, mendeteksi gerakan wajah, arah tatapan, dll.
Langkah Awal:
- Buat Folder : facial_landmark, kemudian buat folder data dalam folder facial_landmark

- Kemudian download file pada link ini dan ekstrak ke dalam folder facial_landmark (hanya files: haarcascade_frontalface_alt2) dan di folder data (files: haarcascade_frontalface_alt2 dan LFBmodel) :

Instalasi dan Unduhan
Proyek ini dilakukan pada PC Core i5-8xx, RAM 16Gb, Windows 11
Sebelum memulai, Anda harus menginstal yang berikut ini di komputer Anda:
- Python, tersedia di sini pilih versi 3.7 atau yang lebih baru.
- PIP. Pip adalah penginstal paket untuk Python. Anda dapat menggunakan pip untuk menginstal paket dari Indeks Paket Python dan indeks lainnya.
Paket berikut diperlukan, Anda dapat menginstalnya dengan pip dari baris perintah Anda:
OpenCV adalah singkatan dari Open Source Computer Vision Library, untuk membaca dan memproses gambar kita.
pip install opencv-python // or this pip install opencv-python — user
Untuk penanganan permintaan URL, install:
pip install urllib3 // or this pip install urllib3 — user
Dan matplotlib untuk memplot gambar kita:
pip install matplotlib // or this pip install matplotlib — user
Penggunaan deteksi wajah à haarcascade face classifier, mengunduh the classifier (< 1MB) pada link diatas dan ekstrak pada folder utama facial.
Penggunaan deteksi landmark the LBFmodel, mengunduh the model (< 54 MB). Dapat pada link diatas sebelumnya dan ekstrak ke folder data. Kemudian copy coding ini simpan dengan nama facial.py :
#Facial Landmark Detection in Python with OpenCV
#Detection from web cam
# Import Packages
import cv2
import os
import urllib.request as urlreq
import numpy as np
# save face detection algorithm's url in haarcascade_url variable
haarcascade_url = "https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_alt2.xml"
# save face detection algorithm's name as haarcascade
haarcascade = "haarcascade_frontalface_alt2.xml"
haarcascade_clf = "data/" + haarcascade
# check if data folder is in working directory
if (os.path.isdir('data')):
# check if haarcascade is in data directory
if (haarcascade in os.listdir('data')):
print("File exists")
else:
# download file from url and save locally as haarcascade_frontalface_alt2.xml
urlreq.urlretrieve(haarcascade_url, haarcascade_clf)
print("File downloaded")
else:
# create data folder in current directory
os.mkdir('data')
# download haarcascade to data folder
urlreq.urlretrieve(haarcascade_url, haarcascade_clf)
print("File downloaded")
# create an instance of the Face Detection Cascade Classifier
detector = cv2.CascadeClassifier(haarcascade_clf)
# save facial landmark detection model's url in LBFmodel_url variable
LBFmodel_url = "https://github.com/kurnianggoro/GSOC2017/raw/master/data/lbfmodel.yaml"
# save facial landmark detection model's name as LBFmodel
LBFmodel = "LFBmodel.yaml"
LBFmodel_file = "data/" + LBFmodel
# check if data folder is in working directory
if (os.path.isdir('data')):
# check if Landmark detection model is in data directory
if (LBFmodel in os.listdir('data')):
print("File exists")
else:
# download file from url and save locally as haarcascade_frontalface_alt2.xml
urlreq.urlretrieve(LBFmodel_url, LBFmodel_file)
print("File downloaded")
else:
# create data folder in current directory
os.mkdir('data')
# download Landmark detection model to data folder
urlreq.urlretrieve(LBFmodel_url, LBFmodel_file)
print("File downloaded")
# create an instance of the Facial landmark Detector with the model
landmark_detector = cv2.face.createFacemarkLBF()
landmark_detector.loadModel(LBFmodel_file)
# get image from webcam
print ("checking webcam for connection ...")
webcam_cap = cv2.VideoCapture(0)
while(True):
# read webcam
_, frame = webcam_cap.read()
# convert frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces using the haarcascade classifier on the "grayscale image"
faces = detector.detectMultiScale(gray)
for (x,y,w,d) in faces:
# Detect landmarks on "gray"
_, landmarks = landmark_detector.fit(gray, np.array(faces))
for landmark in landmarks:
for x,y in landmark[0]:
# display landmarks on "frame/image,"
# with blue colour in BGR and thickness 2
cv2.circle(frame, (int(x), int(y)), 1, (255, 0, 0), 2)
# save last instance of detected image
cv2.imwrite('face-detect.jpg', frame)
# Show image
cv2.imshow("frame", frame)
# terminate the capture window
if cv2.waitKey(20) & 0xFF == ord('q'):
webcam_cap.release()
cv2.destroyAllWindows()
break
Maka kan menghasilkan seperti gambar dibawah ini:

Silahkan Ikutin Tutorial nya dan jadikan Laporan ke 4, dengan judul pada Title Tutorial ini: