Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import cv2
|
6 |
+
import torch
|
7 |
+
import facer
|
8 |
+
from typing import Tuple
|
9 |
+
|
10 |
+
def process_image(input_image: np.ndarray) -> np.ndarray:
|
11 |
+
"""
|
12 |
+
Process the input image to apply face smoothing effect.
|
13 |
+
|
14 |
+
Args:
|
15 |
+
input_image (np.ndarray): Input image in numpy array format
|
16 |
+
|
17 |
+
Returns:
|
18 |
+
np.ndarray: Processed image with smoothing effect applied to face
|
19 |
+
"""
|
20 |
+
device = 'cpu'
|
21 |
+
|
22 |
+
# Convert numpy array to PIL Image and back to ensure correct format
|
23 |
+
input_pil = Image.fromarray(input_image)
|
24 |
+
|
25 |
+
# Convert image to format expected by facer
|
26 |
+
image = facer.hwc2bchw(np.array(input_pil)).to(device=device)
|
27 |
+
|
28 |
+
# Initialize face detector
|
29 |
+
face_detector = facer.face_detector('retinaface/mobilenet', device=device)
|
30 |
+
|
31 |
+
# Detect faces
|
32 |
+
with torch.inference_mode():
|
33 |
+
faces = face_detector(image)
|
34 |
+
|
35 |
+
if len(faces['bbox']) == 0:
|
36 |
+
raise ValueError("No faces detected in the image!")
|
37 |
+
|
38 |
+
# Initialize face parser
|
39 |
+
face_parser = facer.face_parser('farl/lapa/448', device=device)
|
40 |
+
|
41 |
+
# Parse face features
|
42 |
+
with torch.inference_mode():
|
43 |
+
faces = face_parser(image, faces)
|
44 |
+
|
45 |
+
# Process nose segment
|
46 |
+
nose_array = np.array(faces['seg']['logits'][0][6])
|
47 |
+
nose_array = np.where(nose_array > 0, 1, 0)
|
48 |
+
|
49 |
+
# Process face segment
|
50 |
+
face_array = np.array(faces['seg']['logits'][0][1])
|
51 |
+
face_array = np.where(face_array > 0, 1, 0)
|
52 |
+
|
53 |
+
# Combine face and nose arrays
|
54 |
+
face_array = np.clip(face_array + nose_array, 0, 1)
|
55 |
+
|
56 |
+
# Apply bilateral filter for smoothing
|
57 |
+
smooth_img = cv2.bilateralFilter(input_image, 30, 75, 75)
|
58 |
+
|
59 |
+
# Apply smoothing only to face region
|
60 |
+
smooth_img[face_array == 0] = input_image[face_array == 0]
|
61 |
+
|
62 |
+
return smooth_img
|
63 |
+
|
64 |
+
def smooth_face(input_img) -> Tuple[np.ndarray, str]:
|
65 |
+
"""
|
66 |
+
Gradio interface function to process the image and handle errors.
|
67 |
+
|
68 |
+
Args:
|
69 |
+
input_img: Input image from Gradio interface
|
70 |
+
|
71 |
+
Returns:
|
72 |
+
Tuple[np.ndarray, str]: Processed image and status message
|
73 |
+
"""
|
74 |
+
try:
|
75 |
+
processed_img = process_image(input_img)
|
76 |
+
return processed_img, "Face smoothing applied successfully!"
|
77 |
+
except ValueError as e:
|
78 |
+
return input_img, str(e)
|
79 |
+
except Exception as e:
|
80 |
+
return input_img, f"Error processing image: {str(e)}"
|
81 |
+
|
82 |
+
# Create Gradio interface
|
83 |
+
iface = gr.Interface(
|
84 |
+
fn=smooth_face,
|
85 |
+
inputs=gr.Image(type="numpy"),
|
86 |
+
outputs=[
|
87 |
+
gr.Image(type="numpy", label="Processed Image"),
|
88 |
+
gr.Textbox(label="Status")
|
89 |
+
],
|
90 |
+
title="Face Smoothing App",
|
91 |
+
description="Upload an image to apply face smoothing effect. The app will detect faces and apply smoothing only to the face region.",
|
92 |
+
examples=["face-4.jpg"] # Add example images here if you have any
|
93 |
+
)
|
94 |
+
|
95 |
+
# Launch the app
|
96 |
+
if __name__ == "__main__":
|
97 |
+
iface.launch()
|