Spaces:
Runtime error
Runtime error
File size: 1,746 Bytes
25ab7f8 654f874 25ab7f8 934a274 25ab7f8 934a274 25ab7f8 934a274 25ab7f8 2a0f77b 25ab7f8 1bcb537 25ab7f8 934a274 25ab7f8 934a274 2112a66 934a274 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
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
|