File size: 1,156 Bytes
25ab7f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2112a66
 
25ab7f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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