Spaces:
Running
Running
File size: 2,215 Bytes
7372611 |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
import cv2
import numpy as np
from skimage.feature import local_binary_pattern
import matplotlib.pyplot as plt
import dlib
import imutils
import os
from PIL import Image
def compute_face_simplicity(image):
######## create if or depending on input - filepath or PIL file
# Load the image from a filepath
# image = cv2.imread(image_path)
# Convert RGB to BGR format (OpenCV uses BGR while PIL uses RGB)
image_np = np.array(image)
image = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
# Resize the image
image = imutils.resize(image, width=800)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("models/face_alignment/shape_predictor_68_face_landmarks.dat")
# Detect the face in the image
faces = detector(gray, 1)
if len(faces) == 0:
return "No face detected."
x, y, w, h = (faces[0].left(), faces[0].top(), faces[0].width(), faces[0].height())
face_img = gray[y:y+h, x:x+w]
# Parameters for LBP
radius = 1
n_points = 8 * radius
# Apply LBP to the face region
lbp = local_binary_pattern(face_img, n_points, radius, method="uniform")
# Compute the histogram of the LBP
hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, n_points + 3), range=(0, n_points + 2))
# Measure the variance of the histogram
variance = np.var(hist)
std = np.sqrt(variance)
print(std)
# A hypothetical threshold - needs calibration
threshold = 10000
if std < threshold:
simplicity = "Simple"
else:
simplicity = "Complex"
# Visualizing the LBP pattern on the detected face
# plt.imshow(lbp)
lbp = (lbp * 255).astype(np.uint8)
lbp = Image.fromarray(lbp)
return lbp #, simplicity
if __name__ == "__main__":
print(os.getcwd())
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("models/face_alignment/shape_predictor_68_face_landmarks.dat")
print(predictor)
image_path = 'data/images_symmetry/gigi_hadid.webp'
print(compute_face_simplicity(image_path))
|