OpenCV与AI深度学习 | 实战 | 使用OpenCV和Streamlit搭建虚拟化妆应用程序(附源码)

本文来源公众号“OpenCV与AI深度学习”,仅用于学术分享,侵权删,干货满满。

原文链接:实战 | 使用OpenCV和Streamlit搭建虚拟化妆应用程序(附源码)

现看看demo演示。

    本文将介绍如何使用Streamlit和OpenCV创建一个虚拟化妆应用程序。

    首先需要一个预先训练好的脸部解析模型,可以从这里下载:

https://github.com/Pavankunchala/Virtual_Makeup_Streamlit/blob/main/cp/79999_iter.pth

导入库

    我们使用Streamlit为该应用程序创建UI,并使用 OpenCV进行图像处理,可以使用以下代码通过 pip 安装它们:

pip install streamlit
pip install opencv-python 
pip install pillow

 导入需要的库

import cv2
import os
import numpy as np
from skimage.filters import gaussian
from test import evaluate
import streamlit as st
from PIL import Image, ImageColor

创建函数

    我们将创建一些函数来锐化图像以及解析头发:

import numpy as np
import cv2

def sharpen(img):
    img = img * 1.0
    gauss_out = cv2.GaussianBlur(img, (0, 0), sigmaX=5, sigmaY=5, borderType=cv2.BORDER_DEFAULT)
    alpha = 1.5
    img_out = (img - gauss_out) * alpha + img
    img_out = img_out / 255.0
    mask_1 = img_out < 0
    mask_2 = img_out > 1
    img_out = img_out * (1 - mask_1)
    img_out = img_out * (1 - mask_2) + mask_2
    img_out = np.clip(img_out, 0, 1)
    img_out = img_out * 255
    return np.array(img_out, dtype=np.uint8)

def hair(image, parsing, part=17, color=[230, 50, 20]):
    b, g, r = color
    tar_color = np.zeros_like(image)
    tar_color[:, :, 0] = b
    tar_color[:, :, 1] = g
    tar_color[:, :, 2] = r
    np.repeat(parsing[:, :, np.newaxis], 3, axis=2)
    image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    tar_hsv = cv2.cvtColor(tar_color, cv2.COLOR_BGR2HSV)
    if part == 12 or part == 13:
        image_hsv[:, :, 0:2] = tar_hsv[:, :, 0:2]
    else:
        image_hsv[:, :, 0:1] = tar_hsv[:, :, 0:1]
    changed = cv2.cvtColor(image_hsv, cv2.COLOR_HSV2BGR)
    if part == 17:
        changed = sharpen(changed)
    changed[parsing != part] = image[parsing != part]
    return changed

这段代码中使用了 cv2.GaussianBlur 函数来应用高斯模糊,并且假设 gaussian 函数已经被正确定义或者就是指 cv2.GaussianBlur。此外,cv2.cvtColor 用于在BGR和HSV色彩空间之间转换图像。这段代码的目的是修改图像中特定区域的颜色,并可选地对整个图像应用锐化效果。

标题和文件上传器

    那么让我们进入应用程序部分。我们使用 streamlit 的文件上传器来动态上传不同的图像进行测试:

import streamlit as st
from PIL import Image
import numpy as np

DEMO_IMAGE = 'imgs/116.jpg'
st.title('Virtual Makeup')
st.sidebar.title('Virtual Makeup')
st.sidebar.subheader('Parameters')
table = {
    'hair': 17,
    'upper_lip': 12,
    'lower_lip': 13,
}

img_file_buffer = st.sidebar.file_uploader("Upload an image", type=["jpg", "jpeg", 'png'])
if img_file_buffer is not None:
    image = np.array(Image.open(img_file_buffer))
    demo_image = img_file_buffer
else:
    demo_image = DEMO_IMAGE
    image = np.array(Image.open(demo_image))

    上面代码使用了 Streamlit 库来创建一个简单的用户界面,允许用户上传图片或使用默认图片。在上面的代码片段中,我首先为 Demo-Image 创建了一个变量,应用程序默认使用该变量。我使用st.title()和st.sidebar.title()将标题添加到应用程序。

    同时创建了一个名为 table 的字典,它将名字与人脸解析器中的数字进行匹配。如果你想查看主仓库以获得更好的理解,你也可以添加它并解析人脸。

显示和调整大小

new_image = image.copy()
st.subheader('Original Image')
st.image(image,use_column_width = True)

cp = 'cp/79999_iter.pth'
ori = image.copy()
h,w,_ = ori.shape
#print(h)
#print(w)
image = cv2.resize(image,(1024,1024))

    我们使用st.image()函数显示原始图像,并将其大小调整为 1024*1024,以使其与模型兼容。我们还创建了一个包含模型路径的变量。

评估并展示

import cv2
import streamlit as st
from PIL import Image, ImageColor
import numpy as np

# 假设 evaluate 函数和 hair 函数已经定义

parsing = evaluate(demo_image, cp)
parsing = cv2.resize(parsing, image.shape[0:2], interpolation=cv2.INTER_NEAREST)
parts = [table['hair'], table['upper_lip'], table['lower_lip']]

hair_color = st.sidebar.color_picker('Pick the Hair Color', '#000000')
hair_color = ImageColor.getcolor(hair_color, "RGB")
lip_color = st.sidebar.color_picker('Pick the Lip Color', '#edbad1')
lip_color = ImageColor.getcolor(lip_color, "RGB")

colors = [hair_color, lip_color, lip_color]

for part, color in zip(parts, colors):
    image = hair(image, parsing, part, color)

image = cv2.resize(image, (w, h))

st.subheader('Output Image')
st.image(image, use_column_width=True)

    我们创建了一个名为valuate()的函数,可以从这里的test.py文件中找到它.

    使用 streamlit 的color_picker()函数创建了一个颜色选择器,并使用 PIL 库使其兼容应用于图像。

    最后,我调整输出图像的大小,然后基于图像的虚拟化妆应用程序就准备好了。

    要运行应用程序,请在终端中输入以下内容:

streamlit run app.py

    只需将 app.py 替换为您的代码的文件名。

    以下是我们得到的一些结果:

完整项目代码:

https://github.com/Pavankunchala/Virtual_Makeup_Streamlit

THE END !

文章结束,感谢阅读。您的点赞,收藏,评论是我继续更新的动力。大家有推荐的公众号可以评论区留言,共同学习,一起进步。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值