110 lines
4.0 KiB
Python
110 lines
4.0 KiB
Python
import numpy as np
|
|
import cv2
|
|
import os
|
|
from skimage.feature import local_binary_pattern
|
|
import joblib
|
|
|
|
def extract_lbp_features(image, radius=1, n_points=8, method='uniform'):
|
|
lbp = local_binary_pattern(image, n_points, radius, method)
|
|
(hist, _) = np.histogram(lbp.ravel(), bins=np.arange(0, n_points + 3), range=(0, n_points + 2))
|
|
# Normalize the histogram
|
|
hist = hist.astype("float")
|
|
hist /= (hist.sum() + 1e-7)
|
|
return hist
|
|
|
|
|
|
# Fungsi untuk deteksi wajah dan cropping
|
|
def detect_and_crop_faces(image_path):
|
|
# Baca gambar
|
|
original_image = cv2.imread(image_path)
|
|
gray_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
|
|
|
|
# Peningkatan kontras
|
|
gray_image = cv2.equalizeHist(gray_image)
|
|
|
|
# Muat pre-trained Haar Cascade untuk deteksi wajah
|
|
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
|
|
|
|
# Deteksi wajah pada gambar dengan parameter yang disesuaikan
|
|
faces = face_cascade.detectMultiScale(gray_image, scaleFactor=1.2, minNeighbors=10, minSize=(50, 50))
|
|
|
|
# Cek jumlah wajah yang terdeteksi
|
|
print(f'{image_path}: {len(faces)} face(s) detected')
|
|
|
|
# Loop melalui setiap wajah yang terdeteksi, potong, dan tampilkan
|
|
for i, (x, y, w, h) in enumerate(faces, 1):
|
|
# Pemotongan (Cropping) wajah
|
|
cropped_face = original_image[y:y + h, x:x + w]
|
|
|
|
# # Tampilkan gambar wajah yang terpotong
|
|
# cv2.imshow(f'Cropped Face {i}', cropped_face)
|
|
#
|
|
# # Tunggu 1 detik sebelum menampilkan gambar berikutnya
|
|
# cv2.waitKey(1000)
|
|
|
|
return faces
|
|
|
|
|
|
# Load the trained model from file
|
|
knn_model = joblib.load('facemodel.joblib')
|
|
|
|
# Load test data
|
|
X_test = [] # Menampung fitur-fitur ekstraksi
|
|
y_test = [] # Menampung label kelas
|
|
|
|
# Path ke folder data uji
|
|
test_root_folder = 'images/testface/'
|
|
|
|
# Iterasi melalui setiap folder dalam folder data uji
|
|
for person_folder in os.listdir(test_root_folder):
|
|
person_folder_path = os.path.join(test_root_folder, person_folder)
|
|
|
|
# Pastikan path adalah direktori
|
|
if os.path.isdir(person_folder_path):
|
|
# Iterasi melalui setiap gambar dalam folder
|
|
for image_name in os.listdir(person_folder_path):
|
|
image_path = os.path.join(person_folder_path, image_name)
|
|
|
|
# Pastikan path adalah file gambar
|
|
if os.path.isfile(image_path):
|
|
# Deteksi wajah dan cropping
|
|
faces = detect_and_crop_faces(image_path)
|
|
|
|
# Baca kembali gambar setelah deteksi wajah
|
|
original_image = cv2.imread(image_path)
|
|
|
|
# Loop melalui setiap wajah yang terdeteksi, potong, dan ekstraksi LBP
|
|
for i, (x, y, w, h) in enumerate(faces, 1):
|
|
# Pemotongan (Cropping) wajah
|
|
cropped_face = original_image[y:y + h, x:x + w]
|
|
|
|
# Resize gambar menjadi 100x100 piksel
|
|
resized_face = cv2.resize(cropped_face, (100, 100))
|
|
|
|
# Konversi gambar resized ke grayscale
|
|
gray_resized_face = cv2.cvtColor(resized_face, cv2.COLOR_BGR2GRAY)
|
|
|
|
# Normalisasi intensitas
|
|
normalized_face = cv2.normalize(gray_resized_face, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
|
|
|
|
# Ekstraksi fitur LBP
|
|
lbp_features = extract_lbp_features(normalized_face, radius=1, n_points=8, method='uniform')
|
|
|
|
# Tampilkan nilai ekstraksi LBP ke konsol
|
|
print(f'LBP Features for face {i}: {lbp_features}')
|
|
|
|
# Tambahkan fitur dan label ke data uji
|
|
X_test.append(lbp_features)
|
|
y_test.append(person_folder) # Label kelas adalah nama folder
|
|
|
|
# Prediksi kelas menggunakan model K-NN pada data uji
|
|
predicted_classes = knn_model.predict(X_test)
|
|
|
|
# Hitung jumlah prediksi yang benar
|
|
correct_predictions = sum(predicted_classes == y_test)
|
|
|
|
# Hitung akurasi
|
|
accuracy = correct_predictions / len(y_test)
|
|
|
|
print(f'Accuracy: {accuracy:.2f}')
|