Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from inference_sdk import InferenceHTTPClient
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Initialize API client
|
| 8 |
+
CLIENT = InferenceHTTPClient(
|
| 9 |
+
api_url="https://outline.roboflow.com",
|
| 10 |
+
api_key="yu2FwTnkRok26cNiMo8e"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Predefined color options
|
| 14 |
+
color_options = {
|
| 15 |
+
"π΄ Red": (0, 0, 255),
|
| 16 |
+
"π΅ Blue": (255, 0, 0),
|
| 17 |
+
"π’ Green": (0, 255, 0),
|
| 18 |
+
"π Pink": (203, 192, 255),
|
| 19 |
+
"π Purple": (128, 0, 128),
|
| 20 |
+
"β« Black": (0, 0, 0),
|
| 21 |
+
"βͺ White": (255, 255, 255)
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
# Function to apply nail paint
|
| 25 |
+
def apply_nail_paint(image, selected_color):
|
| 26 |
+
image = np.array(image)
|
| 27 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 28 |
+
|
| 29 |
+
result = CLIENT.infer(image, model_id="seg_nail_test/1")
|
| 30 |
+
|
| 31 |
+
mask = np.zeros(image.shape[:2], dtype=np.uint8)
|
| 32 |
+
for pred in result['predictions']:
|
| 33 |
+
if 'points' in pred:
|
| 34 |
+
points = np.array([[p['x'], p['y']] for p in pred['points']], dtype=np.int32)
|
| 35 |
+
cv2.fillPoly(mask, [points], 255)
|
| 36 |
+
|
| 37 |
+
mask = cv2.GaussianBlur(mask, (7, 7), 5)
|
| 38 |
+
mask = cv2.erode(mask, None, iterations=1)
|
| 39 |
+
mask = cv2.dilate(mask, None, iterations=2)
|
| 40 |
+
mask_3d = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
|
| 41 |
+
|
| 42 |
+
color_bgr = color_options[selected_color]
|
| 43 |
+
color_nails = np.full_like(image, color_bgr, dtype=np.uint8)
|
| 44 |
+
colored_nails = cv2.addWeighted(image, 0.4, color_nails, 0.6, 0)
|
| 45 |
+
final_nails = np.where(mask_3d == 255, colored_nails, image)
|
| 46 |
+
|
| 47 |
+
glossy_nails = cv2.addWeighted(final_nails, 0.9, mask_3d, 0.1, 0)
|
| 48 |
+
glossy_nails = cv2.cvtColor(glossy_nails, cv2.COLOR_BGR2RGB)
|
| 49 |
+
|
| 50 |
+
return Image.fromarray(glossy_nails)
|
| 51 |
+
|
| 52 |
+
# Function to apply nail texture
|
| 53 |
+
def apply_nail_texture(image, texture):
|
| 54 |
+
image = np.array(image)
|
| 55 |
+
texture = np.array(texture)
|
| 56 |
+
|
| 57 |
+
result = CLIENT.infer(image, model_id="seg_nail_test/1")
|
| 58 |
+
|
| 59 |
+
mask = np.zeros(image.shape[:2], dtype=np.uint8)
|
| 60 |
+
for pred in result['predictions']:
|
| 61 |
+
if 'points' in pred:
|
| 62 |
+
points = np.array([[p['x'], p['y']] for p in pred['points']], dtype=np.int32)
|
| 63 |
+
cv2.fillPoly(mask, [points], 255)
|
| 64 |
+
|
| 65 |
+
mask = cv2.GaussianBlur(mask, (7, 7), 5)
|
| 66 |
+
mask = cv2.erode(mask, None, iterations=1)
|
| 67 |
+
mask = cv2.dilate(mask, None, iterations=2)
|
| 68 |
+
mask_3d = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
|
| 69 |
+
|
| 70 |
+
texture_resized = cv2.resize(texture, (image.shape[1], image.shape[0]))
|
| 71 |
+
textured_nails = np.where(mask_3d == 255, texture_resized, image)
|
| 72 |
+
# textured_nails = cv2.cvtColor(textured_nails, cv2.COLOR_BGR2RGB)
|
| 73 |
+
|
| 74 |
+
return Image.fromarray(textured_nails)
|
| 75 |
+
|
| 76 |
+
# Function to handle dropdown selection
|
| 77 |
+
def process_image(mode, image, selected_color=None, texture=None):
|
| 78 |
+
if mode == "Apply Paint":
|
| 79 |
+
return apply_nail_paint(image, selected_color)
|
| 80 |
+
elif mode == "Apply Texture":
|
| 81 |
+
return apply_nail_texture(image, texture)
|
| 82 |
+
|
| 83 |
+
# Function to clear all inputs/outputs
|
| 84 |
+
def clear_inputs():
|
| 85 |
+
return None, None, None, None
|
| 86 |
+
|
| 87 |
+
# Gradio UI
|
| 88 |
+
with gr.Blocks() as demo:
|
| 89 |
+
gr.Markdown("## Nail Paint & Texture Application")
|
| 90 |
+
|
| 91 |
+
mode = gr.Dropdown(
|
| 92 |
+
["Apply Paint", "Apply Texture"],
|
| 93 |
+
label="Select Mode",
|
| 94 |
+
value="Apply Paint"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
with gr.Row():
|
| 98 |
+
image_input = gr.Image(type="pil", label="Upload Hand Image")
|
| 99 |
+
color_input = gr.Radio(choices=list(color_options.keys()), label="Select Nail Color", visible=True)
|
| 100 |
+
texture_input = gr.Image(type="pil", label="Upload Texture Image", visible=False)
|
| 101 |
+
|
| 102 |
+
output_image = gr.Image(type="pil", label="Output Image")
|
| 103 |
+
|
| 104 |
+
def update_inputs(selected_mode):
|
| 105 |
+
if selected_mode == "Apply Paint":
|
| 106 |
+
return gr.update(visible=True), gr.update(visible=False)
|
| 107 |
+
else:
|
| 108 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 109 |
+
|
| 110 |
+
mode.change(update_inputs, inputs=[mode], outputs=[color_input, texture_input])
|
| 111 |
+
|
| 112 |
+
with gr.Row():
|
| 113 |
+
process_btn = gr.Button("Process")
|
| 114 |
+
clear_btn = gr.Button("Clear")
|
| 115 |
+
|
| 116 |
+
process_btn.click(process_image, inputs=[mode, image_input, color_input, texture_input], outputs=output_image)
|
| 117 |
+
clear_btn.click(clear_inputs, outputs=[image_input, color_input, texture_input, output_image])
|
| 118 |
+
|
| 119 |
+
demo.launch()
|