Samuel Schmidt
Update src/LBP.py
2a0f77b
raw
history blame
1.75 kB
import cv2
from skimage import feature
import matplotlib.pyplot as plt
import numpy as np
class LBPImageEncoder:
def __init__(self, numPoints, radius):
self.numPoints = numPoints
self.radius = radius
def describe(self, image, eps=1e-7):
lbp = feature.local_binary_pattern(image, self.numPoints, self.radius)
hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, self.numPoints + 3), range=(0, self.numPoints + 2))
hist = hist.astype("float")
hist /= (hist.sum() + eps)
return hist
def face_detection(self, image):
cascadePath = "haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(cascadePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=10, minSize=(30, 30),flags=cv2.CASCADE_SCALE_IMAGE)
return rects
def preprocess_img(self, image):
img = np.array(image)
rects = self.face_detection(img)
feature_vector = np.zeros((self.numPoints + 2) * 3)
for (x, y, w, h) in rects:
face = img[y:y + h, x:x + w]
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
lbp = self.describe(face)
feature_vector[(self.numPoints + 2) * 0: (self.numPoints + 2) * 1] += lbp
# Process green channel
lbp = self.describe(face[:, :, 1])
feature_vector[(self.numPoints + 2) * 1: (self.numPoints + 2) * 2] += lbp
# Process blue channel
lbp = self.describe(face[:, :, 2])
feature_vector[(self.numPoints + 2) * 2: (self.numPoints + 2) * 3] += lbp
feature_vector /= len(rects)
return feature_vector