当前位置:网站首页>使用U2-Net深层网络实现——证件照生成程序
使用U2-Net深层网络实现——证件照生成程序
2022-07-07 08:15:00 【快乐边城】
效果预览:http://map.gnnu.work/rm21/qy/profilepicture
使用到:
1.pytorch 加载分割模型
2.PIL 更加方便地操作图像
2.U2-net 网络将人像的轮廓从图像中分割出来
具体实现步骤如下:
1.原始图

原始图
2.从原始图片获得Alpha图
# 从原始图片获得Alpha图 org:原始图的路径 返回得到的Alpha图
def seg_trimap(org):
image = Image.open(org)
img = np.array(image)
net = pre_net()
inputs_test = pre_test_data(img)
d1, d2, d3, d4, d5, d6, d7 = net(inputs_test)
# normalization
pred = d1[:, 0, :, :]
pred = normPRED(pred)
# 将数据转换成图片
im = get_im(pred)
# im.save(alpha)
sp = image.size
# 根据原始图片调整尺寸
imo = im.resize((sp[0], sp[1]), resample=Image.BILINEAR)
imo.save("alpha_resize111.jpg")
return imo
return imo

Alpha图
3.将Alpha图转成trimap图
def to_standard_trimap(alpha):
# Alpha图生成 trimap
# image = Image.open(alpha)
image = alpha;
# image = image.convert("P")
# image_file.save('resize_trimap.png')
sp = image.size
width = sp[0]
height = sp[1]
for yh in range(height):
for xw in range(width):
dot = (xw, yh)
color_d_arr = image.getpixel(dot)
color_d=color_d_arr[0]
if 0 < color_d <= 60:
image.putpixel(dot, (0,0,0))
if 60 < color_d <= 200:
image.putpixel(dot, (128,128,128))
if 200 < color_d <= 255:
image.putpixel(dot, (255,255,255))
image.save("trimap1111.jpg")
return image

trimap图
4.通过原始图和trimap图结合背景色生成最终结果
def to_background(org, resize_trimap, id_image, color):
"""
org:原始图片
resize_trimap:trimap
id_image:新图片
color: 背景颜色
"""
scale = 1.0
image = load_image(org, "RGB", scale, "box")
trimap = load_image(resize_trimap, "GRAY", scale, "nearest")
im = Image.open(org)
# estimate alpha from image and trimap
try:
alpha = estimate_alpha_cf(image, trimap)
except:
print("异常")
print(org)
a = "-1"
return a
new_background = Image.new('RGB', im.size, color_dict[color])
new_background.save("bj.png")
# load new background
new_background = load_image("bj.png", "RGB", scale, "box")
# estimate foreground from image and alpha
foreground, background = estimate_foreground_ml(image, alpha, return_background=True)
# blend foreground with background and alpha
new_image = blend(foreground, new_background, alpha)
save_image(id_image, new_image)
return id_image

结果图
至此,生成纯色证件照的功能已实现。
接下来使用Vue写程序界面以提供用户交互,由于证件照有特定的尺寸,此处借助vue-croppe实现让用户在前端将纯色证件照剪切成一寸或者两寸。
代码较多不进行展示,有需要跳转到文末获取源代码。
界面实现效果如下:

源码如下:
后端:https://github.com/QiuYang01/profilepicture-server
前端:https://github.com/QiuYang01/profilepicture-client
边栏推荐
- 2022.7.6DAY598
- 2022.7.3DAY595
- 2022.7.5DAY597
- MCU与MPU的区别
- Postman interface test V
- Methods of adding centerlines and centerlines in SolidWorks drawings
- ORM -- query type, association query
- This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
- Guid primary key
- Programming features of ISP, IAP, ICP, JTAG and SWD
猜你喜欢

求方程ax^2+bx+c=0的根(C语言)

Inno Setup 打包及签名指南

Es classes and objects, prototypes

555电路详解

ORM -- query type, association query

【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码

Fiddler simulates the interface test

ArcGIS operation: batch modify attribute table

SQLyog数据库怎么取消自动保存更改

Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
随机推荐
[email protected]能帮助我们快速拿到日志对象
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
Postman interface test V
Kotlin实现微信界面切换(Fragment练习)
对存储过程进行加密和解密(SQL 2008/SQL 2012)
【二开】【JeecgBoot】修改分页参数
基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS
mysql插入数据创建触发器填充uuid字段值
电表远程抄表拉合闸操作命令指令
P1223 排队接水/1319:【例6.1】排队接水
HAL库配置通用定时器TIM触发ADC采样,然后DMA搬运到内存空间。
1324:【例6.6】整数区间
Embedded background - chip
JMeter about setting thread group and time
Apprentissage avancé des fonctions en es6
Factorial implementation of large integer classes
STM32 ADC and DMA
Guid primary key
【剑指Offer】42. 栈的压入、弹出序列
The Hal library is configured with a general timer Tim to trigger ADC sampling, and then DMA is moved to the memory space.