Spaces:
Running
Running
paresh95
commited on
Commit
·
e5ce3a7
1
Parent(s):
9ef22b3
PS|Added face texture module
Browse files- app.py +6 -6
- cv_utils/facial_texture.py +0 -77
- utils/cv_utils.py +11 -0
- utils/face_texture.py +63 -0
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
def identity_function(input_image):
|
5 |
-
return input_image
|
6 |
|
7 |
iface = gr.Interface(
|
8 |
-
fn=
|
9 |
inputs=gr.inputs.Image(type="pil"),
|
10 |
-
outputs=gr.outputs.Image(type="pil")
|
|
|
|
|
|
|
11 |
)
|
12 |
|
13 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from utils.face_texture import GetFaceTexture
|
|
|
|
|
|
|
3 |
|
4 |
iface = gr.Interface(
|
5 |
+
fn=GetFaceTexture().main,
|
6 |
inputs=gr.inputs.Image(type="pil"),
|
7 |
+
outputs=[gr.outputs.Image(type="pil"),
|
8 |
+
gr.outputs.Image(type="pil"),
|
9 |
+
"text"
|
10 |
+
]
|
11 |
)
|
12 |
|
13 |
iface.launch()
|
cv_utils/facial_texture.py
DELETED
@@ -1,77 +0,0 @@
|
|
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))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utils/cv_utils.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
|
5 |
+
def get_image(image_input) -> np.array:
|
6 |
+
"""Outputs numpy array of image given a string filepath or PIL image"""
|
7 |
+
if type(image_input) == str:
|
8 |
+
image = cv2.imread(image_input) # OpenCV uses BGR
|
9 |
+
else:
|
10 |
+
image = cv2.cvtColor(np.array(image_input), cv2.COLOR_RGB2BGR) # PIL uses RGB
|
11 |
+
return image
|
utils/face_texture.py
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
from utils.cv_utils import get_image
|
10 |
+
from typing import Tuple
|
11 |
+
|
12 |
+
|
13 |
+
#TODO: face texture class - face detector and output face
|
14 |
+
#TODO: create YAML file to point towards static parameters
|
15 |
+
#TODO: Test main output and app
|
16 |
+
#TODO: Consider using other method for face detector - this one not as reliable
|
17 |
+
#TODO: Text output showing other examples - celeb, child, gender
|
18 |
+
|
19 |
+
|
20 |
+
class GetFaceTexture:
|
21 |
+
def __init__(self) -> None:
|
22 |
+
pass
|
23 |
+
|
24 |
+
def preprocess_image(self, image) -> np.array:
|
25 |
+
image = imutils.resize(image, width=800)
|
26 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
27 |
+
return gray_image
|
28 |
+
|
29 |
+
def get_face(self, gray_image: np.array) -> np.array:
|
30 |
+
detector = dlib.get_frontal_face_detector()
|
31 |
+
faces = detector(gray_image, 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_image = gray_image[y:y+h, x:x+w]
|
37 |
+
return face_image
|
38 |
+
|
39 |
+
def get_face_texture(self, face_image: np.array) -> Tuple[np.array, float]:
|
40 |
+
radius = 1
|
41 |
+
n_points = 8 * radius
|
42 |
+
lbp = local_binary_pattern(face_image, n_points, radius, method="uniform")
|
43 |
+
hist, _ = np.histogram(lbp.ravel(), bins=np.arange(0, n_points + 3), range=(0, n_points + 2))
|
44 |
+
variance = np.var(hist)
|
45 |
+
std = np.sqrt(variance)
|
46 |
+
return lbp, std
|
47 |
+
|
48 |
+
def postprocess_image(self, lbp: np.array) -> Image:
|
49 |
+
lbp = (lbp * 255).astype(np.uint8)
|
50 |
+
return Image.fromarray(lbp)
|
51 |
+
|
52 |
+
def main(self, image_input) -> Image:
|
53 |
+
image = get_image(image_input)
|
54 |
+
gray_image = self.preprocess_image(image)
|
55 |
+
face_image = self.get_face(gray_image)
|
56 |
+
lbp, std = self.get_face_texture(face_image)
|
57 |
+
face_texture_image = self.postprocess_image(lbp)
|
58 |
+
return face_texture_image, face_image, std
|
59 |
+
|
60 |
+
|
61 |
+
if __name__ == "__main__":
|
62 |
+
image_path = 'data/images_symmetry/gigi_hadid.webp'
|
63 |
+
print(GetFaceTexture().main(image_path))
|