新建一个web服务器,用于接收照片,生成证件照

This commit is contained in:
larry 2020-07-26 22:45:38 +08:00
parent 13728384b7
commit 69c07df8eb
6 changed files with 106 additions and 2 deletions

View File

@ -3,6 +3,8 @@
# aiphoto
最近要去办事情很多地方都需要证件照最近刚好在看AI人脸识别图形识别相关的知识就打算利用这些技术开发一个证件照功能
# 效果图
# 文档
通过文档可以快速上手和了解项目。

BIN
m_web/bj.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

BIN
m_web/cs.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

73
m_web/upload.py Normal file
View File

@ -0,0 +1,73 @@
import os
import tornado.web
import shortuuid
from u_2_net import my_u2net_test
from to_background import to_background
from to_background import to_standard_trimap
from m_dlib import ai_crop
# import PILImageMy as mypil
class UploadHandler(tornado.web.RequestHandler):
def post(self, *args, **kwargs):
filename=shortuuid.uuid()
print(os.path.dirname(__file__))
parent_path = os.path.dirname(os.path.dirname(__file__))
filePath =""
# 查看上传文件的完整格式files以字典形式返回
print(self.request.files)
# {'file1':
# [{'filename': '新建文本文档.txt', 'body': b'61 60 -83\r\n-445 64 -259', 'content_type': 'text/plain'}],
# 'file2':
filesDict = self.request.files
for inputname in filesDict:
# 第一层循环取出最外层信息即input标签传回的name值
# 用过filename键值对对应取出对应的上传文件的真实属性
http_file = filesDict[inputname]
for fileObj in http_file:
# 第二层循环取出完整的对象
# 取得当前路径下的 upfiles 文件夹+上fileObj.filename属性(即真实文件名)
filePath = os.path.join(parent_path, "static", filename+".jpg")
print(filePath)
with open(filePath, 'wb') as f:
f.write(fileObj.body)
org_img = filePath
id_image = os.path.join(parent_path, "static", filename+"_meinv_id.png")
# 20200719
# 通过识别人脸关键点,裁剪图像
ai_crop.crop_photo(org_img,id_image )
print(org_img)
alpha_img = os.path.join(parent_path, "static", filename+"_alpha.png")
print(alpha_img)
alpha_resize_img = os.path.join(parent_path, "static", filename+"_alpha_resize.png")
print(alpha_resize_img)
#
# 通过u_2_net 获取 alpha
my_u2net_test.test_seg_trimap(id_image, alpha_img, alpha_resize_img)
#
# # 通过alpha 获取 trimap
trimap = os.path.join(parent_path, "static", filename+"_trimap_resize.png")
to_standard_trimap.to_standard_trimap(alpha_resize_img, trimap)
print(trimap)
id_image_org = os.path.join(parent_path, "static", filename+"_meinv_id_2in.png")
#
# 证件照添加蓝底纯色背景//"..\\aiphoto\\img\\meinv_trimap_resize.png"
# to_standard_trimap.to_standard_trimap(alpha_resize_img, trimap)
to_background.to_background(id_image, trimap, id_image_org, "blue")
print(id_image_org)
self.write( "static/"+filename+"_meinv_id_2in.png")

26
m_web/web.py Normal file
View File

@ -0,0 +1,26 @@
import tornado.ioloop
import tornado.web
import m_web.upload as upload
import os
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/eam/fileLocal/upload", upload.UploadHandler),
(r"/eam/fileLocal/static", tornado.web.StaticFileHandler, {"path": "/static"})
],
static_path=os.path.dirname(os.path.dirname(__file__))+"/static"
)
if __name__ == "__main__":
current_path = os.path.dirname(__file__)
app = make_app()
app.listen(8013)
tornado.ioloop.IOLoop.current().start()

View File

@ -7,7 +7,7 @@ from u_2_net.data_loader import RescaleT
from u_2_net.data_loader import ToTensorLab
from u_2_net.model import U2NET # full size version 173.6 MB
from PIL import Image
import os
# normalize the predicted SOD probability map
def normPRED(d):
@ -44,7 +44,10 @@ def preprocess(image):
def pre_net():
# 采用n2net 模型数据
model_name = 'u2net'
model_dir = '..\\aiphoto\\u_2_net\\saved_models/'+ model_name + '/' + model_name + '.pth'
path = os.path.dirname(__file__)
print(path)
model_dir = path+'/saved_models/'+ model_name + '/' + model_name + '.pth'
print(model_dir)
print("...load U2NET---173.6 MB")
net = U2NET(3,1)
# 指定cpu