当前位置:网站首页>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
边栏推荐
- Prototype object in ES6
- 555电路详解
- Vs code specifies the extension installation location
- The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
- 搭建物联网硬件通信技术几种方案
- @Transcation的配置,使用,原理注意事项:
- [STM32] solution to the problem that SWD cannot recognize devices after STM32 burning program
- XML configuration file parsing and modeling
- Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
- The method of word automatically generating directory
猜你喜欢
ArcGIS operation: batch modify attribute table
IO model review
Use of JSON extractor originals in JMeter
leetcode-560:和为 K 的子数组
1324:【例6.6】整数区间
JMeter loop controller and CSV data file settings are used together
SQLyog数据库怎么取消自动保存更改
Remote meter reading, switching on and off operation command
【acwing】786. Number k
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
随机推荐
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
求方程ax^2+bx+c=0的根(C语言)
P1031 [NOIP2002 提高组] 均分纸牌
PDF文档签名指南
The method of word automatically generating directory
1324:【例6.6】整数区间
555电路详解
反射效率为什么低?
ORM -- database addition, deletion, modification and query operation logic
根据设备信息进行页面跳转至移动端页面或者PC端页面
Postman interface test II
【acwing】789. 数的范围(二分基础)
ArcGIS operation: batch modify attribute table
STM32 product introduction
宁愿把简单的问题说一百遍,也不把复杂的问题做一遍
STM32 ADC and DMA
Use the fetch statement to obtain the repetition of the last row of cursor data
学习记录——高精度加法和乘法
Slurm资源管理与作业调度系统安装配置
IO model review