Spaces:
Running
Running
paresh95
commited on
Commit
·
7372611
1
Parent(s):
8811e6c
PS|WIP-facial texture
Browse files- app.py +2 -1
- cv_utils/facial_texture.py +77 -0
- data/images_symmetry/gigi_hadid.webp +0 -0
app.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
def identity_function(input_image):
|
4 |
return input_image
|
5 |
|
6 |
iface = gr.Interface(
|
7 |
-
fn=
|
8 |
inputs=gr.inputs.Image(type="pil"),
|
9 |
outputs=gr.outputs.Image(type="pil")
|
10 |
)
|
|
|
1 |
import gradio as gr
|
2 |
+
from cv_utils.facial_texture import compute_face_simplicity
|
3 |
|
4 |
def identity_function(input_image):
|
5 |
return input_image
|
6 |
|
7 |
iface = gr.Interface(
|
8 |
+
fn=compute_face_simplicity,
|
9 |
inputs=gr.inputs.Image(type="pil"),
|
10 |
outputs=gr.outputs.Image(type="pil")
|
11 |
)
|
cv_utils/facial_texture.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from skimage.feature import local_binary_pattern
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import dlib
|
6 |
+
import imutils
|
7 |
+
import os
|
8 |
+
from PIL import Image
|
9 |
+
|
10 |
+
|
11 |
+
def compute_face_simplicity(image):
|
12 |
+
|
13 |
+
######## create if or depending on input - filepath or PIL file
|
14 |
+
# Load the image from a filepath
|
15 |
+
# image = cv2.imread(image_path)
|
16 |
+
|
17 |
+
# Convert RGB to BGR format (OpenCV uses BGR while PIL uses RGB)
|
18 |
+
image_np = np.array(image)
|
19 |
+
image = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
20 |
+
|
21 |
+
# Resize the image
|
22 |
+
image = imutils.resize(image, width=800)
|
23 |
+
|
24 |
+
# Convert to grayscale
|
25 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
26 |
+
|
27 |
+
detector = dlib.get_frontal_face_detector()
|
28 |
+
predictor = dlib.shape_predictor("models/face_alignment/shape_predictor_68_face_landmarks.dat")
|
29 |
+
|
30 |
+
# Detect the face in the image
|
31 |
+
faces = detector(gray, 1)
|
32 |
+
if len(faces) == 0:
|
33 |
+
return "No face detected."
|
34 |
+
|
35 |
+
x, y, w, h = (faces[0].left(), faces[0].top(), faces[0].width(), faces[0].height())
|
36 |
+
face_img = gray[y:y+h, x:x+w]
|
37 |
+
|
38 |
+
|
39 |
+
# Parameters for LBP
|
40 |
+
radius = 1
|
41 |
+
n_points = 8 * radius
|
42 |
+
|
43 |
+
# Apply LBP to the face region
|
44 |
+
lbp = local_binary_pattern(face_img, n_points, radius, method="uniform")
|
45 |
+
|
46 |
+
# Compute the histogram of the LBP
|
47 |
+
hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, n_points + 3), range=(0, n_points + 2))
|
48 |
+
|
49 |
+
# Measure the variance of the histogram
|
50 |
+
variance = np.var(hist)
|
51 |
+
std = np.sqrt(variance)
|
52 |
+
print(std)
|
53 |
+
|
54 |
+
# A hypothetical threshold - needs calibration
|
55 |
+
threshold = 10000
|
56 |
+
|
57 |
+
if std < threshold:
|
58 |
+
simplicity = "Simple"
|
59 |
+
else:
|
60 |
+
simplicity = "Complex"
|
61 |
+
|
62 |
+
# Visualizing the LBP pattern on the detected face
|
63 |
+
# plt.imshow(lbp)
|
64 |
+
lbp = (lbp * 255).astype(np.uint8)
|
65 |
+
lbp = Image.fromarray(lbp)
|
66 |
+
|
67 |
+
return lbp #, simplicity
|
68 |
+
|
69 |
+
|
70 |
+
if __name__ == "__main__":
|
71 |
+
print(os.getcwd())
|
72 |
+
detector = dlib.get_frontal_face_detector()
|
73 |
+
predictor = dlib.shape_predictor("models/face_alignment/shape_predictor_68_face_landmarks.dat")
|
74 |
+
print(predictor)
|
75 |
+
|
76 |
+
image_path = 'data/images_symmetry/gigi_hadid.webp'
|
77 |
+
print(compute_face_simplicity(image_path))
|
data/images_symmetry/gigi_hadid.webp
ADDED