当前位置:网站首页>Using U2 net deep network to realize -- certificate photo generation program
Using U2 net deep network to realize -- certificate photo generation program
2022-07-07 10:29:00 【Happy border town】
Results the preview :http://map.gnnu.work/rm21/qy/profilepicture
Use to :
1.pytorch Load split model
2.PIL More convenient to operate images
2.U2-net The network separates the contour of the portrait from the image
The specific implementation steps are as follows :
1. The original picture
The original picture
2. Get from the original picture Alpha chart
# Get from the original picture Alpha chart org: Path of original graph Return what you get Alpha chart
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)
# Convert data into pictures
im = get_im(pred)
# im.save(alpha)
sp = image.size
# Adjust the size according to the original picture
imo = im.resize((sp[0], sp[1]), resample=Image.BILINEAR)
imo.save("alpha_resize111.jpg")
return imo
return imo
Alpha chart
3. take Alpha Turn the picture into trimap chart
def to_standard_trimap(alpha):
# Alpha Graph generation 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 chart
4. Through the original figure and trimap Figure combines the background color to generate the final result
def to_background(org, resize_trimap, id_image, color):
"""
org: Original picture
resize_trimap:trimap
id_image: New picture
color: The background 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(" abnormal ")
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
Result chart
thus , The function of generating solid color ID photos has been realized .
Next use Vue Write a program interface to provide user interaction , Because the ID photo has a specific size , With the help of vue-croppe It allows users to cut the solid color certificate photo into one inch or two inches at the front end .
There are too many codes to show , You need to jump to the end of the text to get the source code .
The interface implementation effect is as follows :
Source code is as follows :
Back end :https://github.com/QiuYang01/profilepicture-server
front end :https://github.com/QiuYang01/profilepicture-client
边栏推荐
- Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
- mysql插入数据创建触发器填充uuid字段值
- C#记录日志方法
- SolidWorks工程图中添加中心线和中心符号线的办法
- Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
- CONDA creates virtual environment offline
- 【acwing】786. 第k个数
- Vs code specifies the extension installation location
- 施努卡:机器视觉定位技术 机器视觉定位原理
- Jump to the mobile terminal page or PC terminal page according to the device information
猜你喜欢
Some properties of leetcode139 Yang Hui triangle
Yarn的基础介绍以及job的提交流程
JMeter about setting thread group and time
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
MySQL insert data create trigger fill UUID field value
leetcode-304:二维区域和检索 - 矩阵不可变
1323:【例6.5】活动选择
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Appx代碼簽名指南
Elegant controller layer code
随机推荐
.NET配置系统
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
2022.7.3DAY595
JMeter loop controller and CSV data file settings are used together
Learning records - high precision addition and multiplication
【acwing】786. Number k
【acwing】789. Range of numbers (binary basis)
Smart city construction based on GIS 3D visualization technology
Can I open a stock trading account online? Is it safe
php \n 换行无法输出
Postman interface test III
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
OpenGL glLightfv 函数的应用以及光源的相关知识
Easyexcel read write simple to use
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
[sword finger offer] 42 Stack push in and pop-up sequence
Mongodb creates an implicit database as an exercise
High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors
【acwing】786. 第k个数