克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0
hivision_logo

HivisionIDPhoto

English / 中文 / 日本語 / 한국어


相关项目

  • SwanLab:训练人像抠图模型全程用它来分析和监控,以及和实验室同学协作交流,大幅提升了训练效率。

目录


🤩 最近更新

  • 在线体验: SwanHub DemoSpaces

  • 2024.09.25: 增加五寸相纸JPEG下载选项|默认照片下载支持300DPI

  • 2024.09.24: API接口增加base64图像传入选项 | Gradio Demo增加排版照裁剪线功能

  • 2024.09.22: Gradio Demo增加野兽模式,可设置内存加载策略 | API接口增加dpi、face_alignment参数

  • 2024.09.18: Gradio Demo增加分享模版照功能、增加美式证件照背景选项

  • 2024.09.17: Gradio Demo增加自定义底色-HEX输入功能 | (社区贡献)C++版本 - HivisionIDPhotos-cpp 贡献 by zjkhahah

  • 2024.09.16: Gradio Demo增加人脸旋转对齐功能,自定义尺寸输入支持毫米单位

  • 2024.09.14: Gradio Demo增加自定义DPI功能,增加日语和韩语支持,增加调整亮度、对比度、锐度功能

  • 2024.09.12: Gradio Demo增加美白功能 | API接口增加加水印设置照片KB值大小证件照裁切


项目简介

🚀 谢谢你对我们的工作感兴趣。您可能还想查看我们在图像领域的其他成果,欢迎来信:zeyi.lin@swanhub.co.

HivisionIDPhoto 旨在开发一种实用、系统性的证件照智能制作算法。

它利用一套完善的AI模型工作流程,实现对多种用户拍照场景的识别、抠图与证件照生成。

HivisionIDPhoto 可以做到:

  1. 轻量级抠图(纯离线,仅需 CPU 即可快速推理)
  2. 根据不同尺寸规格生成不同的标准证件照、六寸排版照
  3. 支持 纯离线 或 端云 推理
  4. 美颜
  5. 智能换正装(waiting)

如果 HivisionIDPhoto 对你有帮助,请 star 这个 repo 或推荐给你的朋友,解决证件照应急制作问题!


🏠 社区

我们分享了一些由社区构建的HivisionIDPhotos的有趣应用和扩展:

HivisionIDPhotos-ComfyUI HivisionIDPhotos-wechat-weapp
ComfyUI workflow ComfyUI workflow
ComfyUI证件照处理工作流 证件照微信小程序(JAVA后端+原生前端)
HivisionIDPhotos-Uniapp HivisionIDPhotos-web
HivisionIDPhotos-uniapp HivisionIDPhotos-uniapp
证件照微信小程序(uniapp) 证件照应用网页版

🔧 准备工作

环境安装与依赖:

  • Python >= 3.7(项目主要测试在 python 3.10)
  • OS: Linux, Windows, MacOS

1. 克隆项目

git clone https://github.com/Zeyi-Lin/HivisionIDPhotos.git
cd  HivisionIDPhotos

2. 安装依赖环境

建议 conda 创建一个 python3.10 虚拟环境后,执行以下命令

pip install -r requirements.txt
pip install -r requirements-app.txt

3. 下载人像抠图模型权重文件

方式一:脚本下载

python scripts/download_model.py --models all
# 如需指定下载某个模型
# python scripts/download_model.py --models modnet_photographic_portrait_matting

方式二:直接下载

模型均存到项目的hivision/creator/weights目录下:

人像抠图模型 介绍 下载
MODNet MODNet官方权重 下载(24.7MB)
hivision_modnet 对纯色换底适配性更好的抠图模型 下载(24.7MB)
rmbg-1.4 BRIA AI 开源的抠图模型 下载(176.2MB)后重命名为rmbg-1.4.onnx
birefnet-v1-lite ZhengPeng7 开源的抠图模型,拥有最好的分割精度 下载(224MB)后重命名为birefnet-v1-lite.onnx

如果下载网速不顺利:前往SwanHub下载。

4. 人脸检测模型配置(可选)

拓展人脸检测模型 介绍 使用文档
MTCNN 离线人脸检测模型,高性能CPU推理(毫秒级),为默认模型,检测精度较低 Clone此项目后直接使用
RetinaFace 离线人脸检测模型,CPU推理速度中等(秒级),精度较高 下载后放到hivision/creator/retinaface/weights目录下
Face++ 旷视推出的在线人脸检测API,检测精度较高,官方文档 使用文档

5. 性能参考

测试环境为Mac M1 Max 64GB,非GPU加速,测试图片分辨率为 512x715(1) 与 764×1146(2)。

模型组合 内存占用 推理时长(1) 推理时长(2)
MODNet + mtcnn 410MB 0.207s 0.246s
MODNet + retinaface 405MB 0.571s 0.971s
birefnet-v1-lite + retinaface 6.20GB 7.063s 7.128s

6. GPU推理加速(可选)

在当前版本,可被英伟达GPU加速的模型为birefnet-v1-lite,并请确保你有16GB左右的显存。

如需使用英伟达GPU加速推理,在确保你已经安装CUDAcuDNN后,根据onnxruntime-gpu文档找到对应的onnxruntime-gpu版本安装,以及根据pytorch官网找到对应的torch版本安装。

# 假如你的电脑安装的是CUDA 12.x, cuDNN 8
# 安装torch是可选的,如果你始终配置不好cuDNN,那么试试安装torch
pip install onnxruntime-gpu==1.18.0
pip install torch --index-url https://download.pytorch.org/whl/cu121

完成安装后,调用birefnet-v1-lite模型即可利用GPU加速推理。

TIPS: CUDA 支持向下兼容。比如你的 CUDA 版本为 12.6,torch 官方目前支持的最高版本为 12.4(<12.6),torch仍可以正常使用CUDA。


⚡️ 运行 Gradio Demo

python app.py

运行程序将生成一个本地 Web 页面,在页面中可完成证件照的操作与交互。


🚀 Python 推理

核心参数:

  • -i: 输入图像路径
  • -o: 保存图像路径
  • -t: 推理类型,有idphoto、human_matting、add_background、generate_layout_photos可选
  • --matting_model: 人像抠图模型权重选择
  • --face_detect_model: 人脸检测模型选择

更多参数可通过python inference.py --help查看

1. 证件照制作

输入 1 张照片,获得 1 张标准证件照和 1 张高清证件照的 4 通道透明 png

python inference.py -i demo/images/test0.jpg -o ./idphoto.png --height 413 --width 295

2. 人像抠图

输入 1 张照片,获得 1张 4 通道透明 png

python inference.py -t human_matting -i demo/images/test0.jpg -o ./idphoto_matting.png --matting_model hivision_modnet

3. 透明图增加底色

输入 1 张 4 通道透明 png,获得 1 张增加了底色的 3通道图像

python inference.py -t add_background -i ./idphoto.png -o ./idphoto_ab.jpg  -c 4f83ce -k 30 -r 1

4. 得到六寸排版照

输入 1 张 3 通道照片,获得 1 张六寸排版照

python inference.py -t generate_layout_photos -i ./idphoto_ab.jpg -o ./idphoto_layout.jpg  --height 413 --width 295 -k 200

5. 证件照裁剪

输入 1 张 4 通道照片(抠图好的图像),获得 1 张标准证件照和 1 张高清证件照的 4 通道透明 png

python inference.py -t idphoto_crop -i ./idphoto_matting.png -o ./idphoto_crop.png --height 413 --width 295

⚡️ 部署 API 服务

启动后端

python deploy_api.py

请求 API 服务

详细请求方式请参考 API 文档,包含以下请求示例:


🐳 Docker 部署

1. 拉取或构建镜像

以下方式三选一

方式一:拉取最新镜像:

docker pull linzeyi/hivision_idphotos

方式二:Dockrfile 直接构建镜像:

在确保将至少一个抠图模型权重文件放到hivision/creator/weights下后,在项目根目录执行:

docker build -t linzeyi/hivision_idphotos .

方式三:Docker compose 构建:

在确保将至少一个抠图模型权重文件放到hivision/creator/weights下后,在项目根目录下执行:

docker compose build

2. 运行服务

启动 Gradio Demo 服务

运行下面的命令,在你的本地访问 http://127.0.0.1:7860 即可使用。

docker run -d -p 7860:7860 linzeyi/hivision_idphotos

启动 API 后端服务

docker run -d -p 8080:8080 linzeyi/hivision_idphotos python3 deploy_api.py

两个服务同时启动

docker compose up -d

环境变量

本项目提供了一些额外的配置项,使用环境变量进行设置:

环境变量 类型 描述 示例
FACE_PLUS_API_KEY 可选 这是你在 Face++ 控制台申请的 API 密钥 7-fZStDJ····
FACE_PLUS_API_SECRET 可选 Face++ API密钥对应的Secret VTee824E····
RUN_MODE 可选 运行模式,可选值为beast(野兽模式)。野兽模式下人脸检测和抠图模型将不释放内存,从而获得更快的二次推理速度。建议内存16GB以上尝试。 beast
DEFAULT_LANG 可选 Gradio Demo启动时的默认语言 en

docker使用环境变量示例:

docker run  -d -p 7860:7860 \
    -e FACE_PLUS_API_KEY=7-fZStDJ···· \
    -e FACE_PLUS_API_SECRET=VTee824E···· \
    -e RUN_MODE=beast \
    -e DEFAULT_LANG=en \
    linzeyi/hivision_idphotos  

FAQ

1. 如何修改预设尺寸和颜色?

  • 尺寸:修改size_list_CN.csv后再次运行 app.py 即可,其中第一列为尺寸名,第二列为高度,第三列为宽度。
  • 颜色:修改color_list_CN.csv后再次运行 app.py 即可,其中第一列为颜色名,第二列为Hex值。

2. 如何修改水印字体?

  1. 将字体文件放到hivision/plugin/font文件夹下
  2. 修改hivision/plugin/watermark.pyfont_file参数值为字体文件名

3. 如何添加社交媒体模板照?

  1. 将模板图片放到hivision/plugin/template/assets文件夹下。模板图片是一个4通道的透明png。
  2. hivision/plugin/template/assets/template_config.json文件中添加最新的模板信息,其中width为模板图宽度(px),height为模板图高度(px),anchor_points为模板中透明区域的四个角的坐标(px);rotation为透明区域相对于垂直方向的旋转角度,>0为逆时针,<0为顺时针。
  3. demo/processor.py_generate_image_template函数中的TEMPLATE_NAME_LIST变量添加最新的模板名

4. 如何修改Gradio Demo的顶部导航栏?

  • 修改demo/assets/title.md

📧 联系我们

如果您有任何问题,请发邮件至 zeyi.lin@swanhub.co


🙏 感谢支持

Stargazers repo roster for @Zeyi-Lin/HivisionIDPhotos

Forkers repo roster for @Zeyi-Lin/HivisionIDPhotos

Star History Chart

贡献者们:

Zeyi-LinSAKURA-CATFeudalmanswpfYKaikaikaifangShaohonChenKashiwaByte


📜 Lincese

This repository is licensed under the Apache-2.0 License.


📚 引用

如果您在研究或项目中使用了HivisionIDPhotos,请考虑引用我们的工作。您可以使用以下BibTeX条目:

@misc{hivisionidphotos,
      title={{HivisionIDPhotos: A Lightweight and Efficient AI ID Photos Tool}},
      author={Zeyi Lin and SwanLab Team},
      year={2024},
      publisher={GitHub},
      url = {\url{https://github.com/Zeyi-Lin/HivisionIDPhotos}},
}
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

暂无描述 展开 收起
Python 等 3 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化