Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
from skimage.filters import gaussian
|
| 5 |
+
from test import evaluate
|
| 6 |
+
import streamlit as st
|
| 7 |
+
from PIL import Image, ImageColor
|
| 8 |
+
|
| 9 |
+
def sharpen(img):
|
| 10 |
+
img = img * 1.0
|
| 11 |
+
gauss_out = gaussian(img, sigma=5, multichannel=True)
|
| 12 |
+
|
| 13 |
+
alpha = 1.5
|
| 14 |
+
img_out = (img - gauss_out) * alpha + img
|
| 15 |
+
|
| 16 |
+
img_out = img_out / 255.0
|
| 17 |
+
|
| 18 |
+
mask_1 = img_out < 0
|
| 19 |
+
mask_2 = img_out > 1
|
| 20 |
+
|
| 21 |
+
img_out = img_out * (1 - mask_1)
|
| 22 |
+
img_out = img_out * (1 - mask_2) + mask_2
|
| 23 |
+
img_out = np.clip(img_out, 0, 1)
|
| 24 |
+
img_out = img_out * 255
|
| 25 |
+
return np.array(img_out, dtype=np.uint8)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def hair(image, parsing, part=17, color=[230, 50, 20]):
|
| 29 |
+
b, g, r = color #[10, 50, 250] # [10, 250, 10]
|
| 30 |
+
tar_color = np.zeros_like(image)
|
| 31 |
+
tar_color[:, :, 0] = b
|
| 32 |
+
tar_color[:, :, 1] = g
|
| 33 |
+
tar_color[:, :, 2] = r
|
| 34 |
+
np.repeat(parsing[:, :, np.newaxis], 3, axis=2)
|
| 35 |
+
|
| 36 |
+
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
|
| 37 |
+
tar_hsv = cv2.cvtColor(tar_color, cv2.COLOR_BGR2HSV)
|
| 38 |
+
|
| 39 |
+
if part == 12 or part == 13:
|
| 40 |
+
image_hsv[:, :, 0:2] = tar_hsv[:, :, 0:2]
|
| 41 |
+
else:
|
| 42 |
+
image_hsv[:, :, 0:1] = tar_hsv[:, :, 0:1]
|
| 43 |
+
|
| 44 |
+
changed = cv2.cvtColor(image_hsv, cv2.COLOR_HSV2BGR)
|
| 45 |
+
|
| 46 |
+
if part == 17:
|
| 47 |
+
changed = sharpen(changed)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
changed[parsing != part] = image[parsing != part]
|
| 51 |
+
return changed
|
| 52 |
+
|
| 53 |
+
DEMO_IMAGE = 'imgs/116.jpg'
|
| 54 |
+
|
| 55 |
+
st.title('Virtual Makeup')
|
| 56 |
+
|
| 57 |
+
st.sidebar.title('Virtual Makeup')
|
| 58 |
+
st.sidebar.subheader('Parameters')
|
| 59 |
+
|
| 60 |
+
table = {
|
| 61 |
+
'hair': 17,
|
| 62 |
+
'upper_lip': 12,
|
| 63 |
+
'lower_lip': 13,
|
| 64 |
+
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
img_file_buffer = st.sidebar.file_uploader("Upload an image", type=[ "jpg", "jpeg",'png'])
|
| 68 |
+
|
| 69 |
+
if img_file_buffer is not None:
|
| 70 |
+
image = np.array(Image.open(img_file_buffer))
|
| 71 |
+
demo_image = img_file_buffer
|
| 72 |
+
|
| 73 |
+
else:
|
| 74 |
+
demo_image = DEMO_IMAGE
|
| 75 |
+
image = np.array(Image.open(demo_image))
|
| 76 |
+
|
| 77 |
+
#st.set_option('deprecation.showfileUploaderEncoding', False)
|
| 78 |
+
|
| 79 |
+
new_image = image.copy()
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
st.subheader('Original Image')
|
| 86 |
+
|
| 87 |
+
st.image(image,use_column_width = True)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
cp = 'cp/79999_iter.pth'
|
| 91 |
+
ori = image.copy()
|
| 92 |
+
h,w,_ = ori.shape
|
| 93 |
+
|
| 94 |
+
#print(h)
|
| 95 |
+
#print(w)
|
| 96 |
+
image = cv2.resize(image,(1024,1024))
|
| 97 |
+
|
| 98 |
+
parsing = evaluate(demo_image, cp)
|
| 99 |
+
parsing = cv2.resize(parsing, image.shape[0:2], interpolation=cv2.INTER_NEAREST)
|
| 100 |
+
|
| 101 |
+
parts = [table['hair'], table['upper_lip'], table['lower_lip']]
|
| 102 |
+
|
| 103 |
+
hair_color = st.sidebar.color_picker('Pick the Hair Color', '#000')
|
| 104 |
+
hair_color = ImageColor.getcolor(hair_color, "RGB")
|
| 105 |
+
|
| 106 |
+
lip_color = st.sidebar.color_picker('Pick the Lip Color', '#edbad1')
|
| 107 |
+
|
| 108 |
+
lip_color = ImageColor.getcolor(lip_color, "RGB")
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
colors = [hair_color, lip_color, lip_color]
|
| 113 |
+
|
| 114 |
+
for part, color in zip(parts, colors):
|
| 115 |
+
image = hair(image, parsing, part, color)
|
| 116 |
+
|
| 117 |
+
image = cv2.resize(image,(w,h))
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
st.subheader('Output Image')
|
| 121 |
+
|
| 122 |
+
st.image(image,use_column_width = True)
|
| 123 |
+
|