import cv2 from skimage import feature import matplotlib.pyplot as plt 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 = plt.hist(lbp.ravel()) return lbp, hist def face_detection(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, imagePath): img = cv2.imread(imagePath) rects = self.face_detection(img) for (x, y, w, h) in rects: face = img[y:y + h, x:x + w] face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) # plt.imshow(face , cmap="gray") # print(face.shape) # face = np.array(face) lbp, hist = self.describe(face) return hist