Samuel Schmidt commited on
Commit
934a274
·
1 Parent(s): 6d2b087

Update src/LBP.py

Browse files
Files changed (1) hide show
  1. src/LBP.py +21 -8
src/LBP.py CHANGED
@@ -2,6 +2,7 @@ import cv2
2
  from skimage import feature
3
  import matplotlib.pyplot as plt
4
 
 
5
  class LBPImageEncoder:
6
  def __init__(self, numPoints, radius):
7
  self.numPoints = numPoints
@@ -9,10 +10,12 @@ class LBPImageEncoder:
9
 
10
  def describe(self, image, eps=1e-7):
11
  lbp = feature.local_binary_pattern(image, self.numPoints, self.radius)
12
- hist = plt.hist(lbp.ravel())
13
- return lbp, hist
 
 
14
 
15
- def face_detection(image):
16
  cascadePath = "haarcascade_frontalface_default.xml"
17
  detector = cv2.CascadeClassifier(cascadePath)
18
 
@@ -23,14 +26,24 @@ class LBPImageEncoder:
23
  def preprocess_img(self, imagePath):
24
  img = cv2.imread(imagePath)
25
  rects = self.face_detection(img)
 
 
26
  for (x, y, w, h) in rects:
27
  face = img[y:y + h, x:x + w]
28
  face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
29
- # plt.imshow(face , cmap="gray")
30
- # print(face.shape)
31
- # face = np.array(face)
32
 
33
- lbp, hist = self.describe(face)
 
34
 
35
- return hist
 
 
 
 
 
 
 
 
 
 
36
 
 
2
  from skimage import feature
3
  import matplotlib.pyplot as plt
4
 
5
+
6
  class LBPImageEncoder:
7
  def __init__(self, numPoints, radius):
8
  self.numPoints = numPoints
 
10
 
11
  def describe(self, image, eps=1e-7):
12
  lbp = feature.local_binary_pattern(image, self.numPoints, self.radius)
13
+ hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, self.numPoints + 3), range=(0, self.numPoints + 2))
14
+ hist = hist.astype("float")
15
+ hist /= (hist.sum() + eps)
16
+ return hist
17
 
18
+ def face_detection(self, image):
19
  cascadePath = "haarcascade_frontalface_default.xml"
20
  detector = cv2.CascadeClassifier(cascadePath)
21
 
 
26
  def preprocess_img(self, imagePath):
27
  img = cv2.imread(imagePath)
28
  rects = self.face_detection(img)
29
+ feature_vector = np.zeros((self.numPoints + 2) * 3)
30
+
31
  for (x, y, w, h) in rects:
32
  face = img[y:y + h, x:x + w]
33
  face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
 
 
 
34
 
35
+ lbp = self.describe(face)
36
+ feature_vector[(self.numPoints + 2) * 0: (self.numPoints + 2) * 1] += lbp
37
 
38
+ # Process green channel
39
+ lbp = self.describe(face[:, :, 1])
40
+ feature_vector[(self.numPoints + 2) * 1: (self.numPoints + 2) * 2] += lbp
41
+
42
+ # Process blue channel
43
+ lbp = self.describe(face[:, :, 2])
44
+ feature_vector[(self.numPoints + 2) * 2: (self.numPoints + 2) * 3] += lbp
45
+
46
+ feature_vector /= len(rects)
47
+
48
+ return feature_vector
49