diff --git a/tools/face_recognition_service/.gitignore b/tools/face_recognition_service/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..4ecf239680254297a58c484b548645123c373f5a --- /dev/null +++ b/tools/face_recognition_service/.gitignore @@ -0,0 +1,4 @@ +.idea/ +.vscode/ +__pycache__/ +*/__pycache__/ diff --git a/tools/face_recognition_service/Database/train/Aaron_Sorkin.jpg b/tools/face_recognition_service/Database/train/Aaron_Sorkin.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e6b1dfa6dd2b9046631cc2342bc043f05b871c7f Binary files /dev/null and b/tools/face_recognition_service/Database/train/Aaron_Sorkin.jpg differ diff --git a/tools/face_recognition_service/Database/train/Anwar_Ibrahim.jpg b/tools/face_recognition_service/Database/train/Anwar_Ibrahim.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4f1236fe8713e34ca3f0b695d48b82864d9b6f8d Binary files /dev/null and b/tools/face_recognition_service/Database/train/Anwar_Ibrahim.jpg differ diff --git a/tools/face_recognition_service/Database/train/Bob_Graham.jpg b/tools/face_recognition_service/Database/train/Bob_Graham.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2f03900854fedbf79e3229dcd940b2f0c7f53677 Binary files /dev/null and b/tools/face_recognition_service/Database/train/Bob_Graham.jpg differ diff --git a/tools/face_recognition_service/Database/train/Carlos_Ortega.jpg b/tools/face_recognition_service/Database/train/Carlos_Ortega.jpg new file mode 100644 index 0000000000000000000000000000000000000000..bfb1f9e3d318e80ee0578b791fed944b41be4936 Binary files /dev/null and b/tools/face_recognition_service/Database/train/Carlos_Ortega.jpg differ diff --git a/tools/face_recognition_service/Database/train/Cindy_Crawford.jpg b/tools/face_recognition_service/Database/train/Cindy_Crawford.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e788f7ff6879f809f3e3a93ebc2f2aafc7dca7e8 Binary files /dev/null and b/tools/face_recognition_service/Database/train/Cindy_Crawford.jpg differ diff --git a/tools/face_recognition_service/Database/train/Monica_Seles.jpg b/tools/face_recognition_service/Database/train/Monica_Seles.jpg new file mode 100644 index 0000000000000000000000000000000000000000..713019007834351024cfd33d572dd13fbc84be0a Binary files /dev/null and b/tools/face_recognition_service/Database/train/Monica_Seles.jpg differ diff --git a/tools/face_recognition_service/Database/train/Sally_Kirkland.jpg b/tools/face_recognition_service/Database/train/Sally_Kirkland.jpg new file mode 100644 index 0000000000000000000000000000000000000000..b3cbfe16a4ddf6b944953a5194ba0274a3da8d82 Binary files /dev/null and b/tools/face_recognition_service/Database/train/Sally_Kirkland.jpg differ diff --git a/tools/face_recognition_service/Database/train/Vanessa_Redgrave.jpg b/tools/face_recognition_service/Database/train/Vanessa_Redgrave.jpg new file mode 100644 index 0000000000000000000000000000000000000000..e2ea52babd45f30a59d7d7b5a8bc88725989b6e1 Binary files /dev/null and b/tools/face_recognition_service/Database/train/Vanessa_Redgrave.jpg differ diff --git a/tools/face_recognition_service/Database/val/Aaron_Sorkin.jpg b/tools/face_recognition_service/Database/val/Aaron_Sorkin.jpg new file mode 100644 index 0000000000000000000000000000000000000000..a7959c51ba1d9ff8b5e93b1106e91c72568c59a9 Binary files /dev/null and b/tools/face_recognition_service/Database/val/Aaron_Sorkin.jpg differ diff --git a/tools/face_recognition_service/Database/val/Anwar_Ibrahim.jpg b/tools/face_recognition_service/Database/val/Anwar_Ibrahim.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1f9369001d2b54808d9582b79a3a1314e8194690 Binary files /dev/null and b/tools/face_recognition_service/Database/val/Anwar_Ibrahim.jpg differ diff --git a/tools/face_recognition_service/Database/val/Bob_Graham.jpg b/tools/face_recognition_service/Database/val/Bob_Graham.jpg new file mode 100644 index 0000000000000000000000000000000000000000..dbb1be1dbccc189e07c0475bfe3b5930063eca6e Binary files /dev/null and b/tools/face_recognition_service/Database/val/Bob_Graham.jpg differ diff --git a/tools/face_recognition_service/Database/val/Carlos_Ortega.jpg b/tools/face_recognition_service/Database/val/Carlos_Ortega.jpg new file mode 100644 index 0000000000000000000000000000000000000000..4255a3a08f435e9f84b844b498210cd3c528b5a7 Binary files /dev/null and b/tools/face_recognition_service/Database/val/Carlos_Ortega.jpg differ diff --git a/tools/face_recognition_service/Database/val/Cindy_Crawford.jpg b/tools/face_recognition_service/Database/val/Cindy_Crawford.jpg new file mode 100644 index 0000000000000000000000000000000000000000..2502f6baaacc4a75085b635696ae2aec19202a21 Binary files /dev/null and b/tools/face_recognition_service/Database/val/Cindy_Crawford.jpg differ diff --git a/tools/face_recognition_service/Database/val/Monica_Seles.jpg b/tools/face_recognition_service/Database/val/Monica_Seles.jpg new file mode 100644 index 0000000000000000000000000000000000000000..38b2e685b1079467b8a01a6e0bff73b2bd9a92ba Binary files /dev/null and b/tools/face_recognition_service/Database/val/Monica_Seles.jpg differ diff --git a/tools/face_recognition_service/Database/val/Sally_Kirkland.jpg b/tools/face_recognition_service/Database/val/Sally_Kirkland.jpg new file mode 100644 index 0000000000000000000000000000000000000000..5201ead274f86dbcac944bfa65a1c286b21496b9 Binary files /dev/null and b/tools/face_recognition_service/Database/val/Sally_Kirkland.jpg differ diff --git a/tools/face_recognition_service/Database/val/Vanessa_Redgrave.jpg b/tools/face_recognition_service/Database/val/Vanessa_Redgrave.jpg new file mode 100644 index 0000000000000000000000000000000000000000..1087aade492ea0b009206a1506dba4246b1fdbc0 Binary files /dev/null and b/tools/face_recognition_service/Database/val/Vanessa_Redgrave.jpg differ diff --git a/tools/face_recognition_service/LICENSE b/tools/face_recognition_service/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..21b4770e5d2ad823be71e990f08fe4df0db5c159 --- /dev/null +++ b/tools/face_recognition_service/LICENSE @@ -0,0 +1,127 @@ + 木兰宽松许可证, 第2版 + + 木兰宽松许可证, 第2版 + 2020年1月 http://license.coscl.org.cn/MulanPSL2 + + + 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束: + + 0. 定义 + + “软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 + + “贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 + + “贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 + + “法人实体”是指提交贡献的机构及其“关联实体”。 + + “关联实体”是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 + + 1. 授予版权许可 + + 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。 + + 2. 授予专利许可 + + 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。 + + 3. 无商标许可 + + “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。 + + 4. 分发限制 + + 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 + + 5. 免责声明与责任限制 + + “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。 + + 6. 语言 + “本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。 + + 条款结束 + + 如何将木兰宽松许可证,第2版,应用到您的软件 + + 如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步: + + 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字; + + 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中; + + 3, 请将如下声明文本放入每个源文件的头部注释中。 + + Copyright (c) [Year] [name of copyright holder] + [Software Name] is licensed under Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. + + + Mulan Permissive Software License,Version 2 + + Mulan Permissive Software License,Version 2 (Mulan PSL v2) + January 2020 http://license.coscl.org.cn/MulanPSL2 + + Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions: + + 0. Definition + + Software means the program and related documents which are licensed under this License and comprise all Contribution(s). + + Contribution means the copyrightable work licensed by a particular Contributor under this License. + + Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. + + Legal Entity means the entity making a Contribution and all its Affiliates. + + Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. + + 1. Grant of Copyright License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. + + 2. Grant of Patent License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. + + 3. No Trademark License + + No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4. + + 4. Distribution Restriction + + You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. + + 5. Disclaimer of Warranty and Limitation of Liability + + THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + + 6. Language + + THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL. + + END OF THE TERMS AND CONDITIONS + + How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software + + To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps: + + i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; + + ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package; + + iii Attach the statement to the appropriate annotated syntax at the beginning of each source file. + + + Copyright (c) [Year] [name of copyright holder] + [Software Name] is licensed under Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. \ No newline at end of file diff --git a/tools/face_recognition_service/README.md b/tools/face_recognition_service/README.md new file mode 100644 index 0000000000000000000000000000000000000000..4d5dc64f91375e2ddcc87a00ccb73be493bbe083 --- /dev/null +++ b/tools/face_recognition_service/README.md @@ -0,0 +1,69 @@ +# 人脸识别项目 + +这个项目是一个用于人脸识别的Python应用程序。它利用计算机视觉技术和深度学习模型来识别人脸并与预先构建的数据库进行比较。您可以通过本地图像文件或相机捕获的实时图像来执行人脸识别。本项目可以在openEuler系统下正常使用。 + +## 依赖项 + +在运行该应用程序之前,请确保已安装以下依赖项: + +- OpenCV +- NumPy +- Faiss +- InsightFace + +您可以使用以下命令来安装这些依赖项: + +``` +pip install opencv-python numpy faiss-cpu insightface +``` + +## 使用方法 + +您可以使用以下命令行参数来运行该应用程序: + +- `--image_path`: 本地照片文件路径。使用此选项可以对单个本地图像进行人脸识别。 +- `--camera`: 使用相机识别。使用此选项可以打开摄像头并进行实时人脸识别。 +- `--build`: 建立数据库。使用此选项可以构建人脸数据库,需要提供包含用于建立数据库的图像的文件夹地址。 +- `--img_fold`: 构建数据库所使用的图像文件夹地址。在使用 `--build` 选项时,指定包含用于建立数据库的图像的文件夹。 +- `--save_dir`: 数据库保存地址。指定数据库文件的保存路径。 +- `--add`: 添加人脸数据。使用此选项可以将新的人脸数据添加到现有的数据库中,需要提供要添加的图像文件的路径。 + +### 示例用法 + +- 使用本地照片进行人脸识别: + +``` +python main.py --image_path /path/to/your/image.jpg +``` + +- 使用相机进行实时人脸识别: + +``` +python main.py --camera +``` + +- 建立人脸数据库: + +``` +python main.py --build --img_fold /path/to/database/images --save_dir /path/to/database/database.npz +``` + +- 添加人脸数据到数据库: + +``` +python main.py --add /path/to/new/face/image.jpg --save_dir /path/to/database/database.npz +``` + +## 注意事项 + +- 请确保图像文件的路径正确,并且文件存在。 +- 当使用 `--build` 选项构建数据库时,您需要提供包含足够多人脸图像的文件夹,以便构建一个具有良好识别性能的数据库。 +- 使用 `--add` 选项时,请确保提供的图像包含有效的人脸,并且已经在数据库中有相应的标签。 + +## 作者 + +- 朱一杰 + +## 许可证 + +此项目采用 [MulanPSL-2.0](http://license.coscl.org.cn/MulanPSL2) 许可。 \ No newline at end of file diff --git a/tools/face_recognition_service/SimHei.ttf b/tools/face_recognition_service/SimHei.ttf new file mode 100644 index 0000000000000000000000000000000000000000..60a4819db82ba2670bed0dec143b370346b7fb30 Binary files /dev/null and b/tools/face_recognition_service/SimHei.ttf differ diff --git a/tools/face_recognition_service/main.py b/tools/face_recognition_service/main.py new file mode 100644 index 0000000000000000000000000000000000000000..8b64512a0a45787a1d33c2493015a85f3fdd3969 --- /dev/null +++ b/tools/face_recognition_service/main.py @@ -0,0 +1,45 @@ +# -*- encoding=utf-8 -*- +""" +# ********************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# [oec-application] is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# Author: CoderJackZhu +# Create: 2023-10-27 +# Description: face recognition +# ********************************************************************************** +""" +import argparse +import warnings + +from src.add_data import add_database +from src.build_database import build_database +from src.camera import use_camera, no_camera + +# 关闭控制台所有的警告输出 +warnings.filterwarnings("ignore") + +parser = argparse.ArgumentParser(description="人脸识别程序") +parser.add_argument("--image_path", help="本地照片文件路径") +parser.add_argument("--camera", action="store_true", help="使用相机识别") +parser.add_argument("--build", action="store_true", help="建立database") +parser.add_argument("--img_fold", default="./Database/train", help="build所使用的图像文件夹地址") +parser.add_argument("--save_dir", default="./Database/database.npz", help="数据库保存地址") +parser.add_argument("--add", help="添加人脸数据,输入图像路径") + +args = parser.parse_args() + +if args.camera: + use_camera() +elif args.image_path: + no_camera(args.image_path) +elif args.build: + build_database(args.img_fold, args.save_dir) +elif args.add: + add_database(args.add, args.save_dir) diff --git a/tools/face_recognition_service/src/__init__.py b/tools/face_recognition_service/src/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tools/face_recognition_service/src/add_data.py b/tools/face_recognition_service/src/add_data.py new file mode 100644 index 0000000000000000000000000000000000000000..c56ec4b8c0894a676cd4afd373ccf7ed46731607 --- /dev/null +++ b/tools/face_recognition_service/src/add_data.py @@ -0,0 +1,79 @@ +# -*- encoding=utf-8 -*- +""" +# ********************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# [oec-application] is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# Author: CoderJackZhu +# Create: 2023-10-27 +# Description: face recognition +# ********************************************************************************** +""" +import base64 +import logging +import os + +import cv2 +import insightface + +from .cfg import cfg +from .face_detect import get_access_toke_from_client_infos, face_detection, FaceRecognitionDatabase +from .utils import crop_and_expand + + +# 先剪裁出人脸位置,再提特征,最后保存到数据库中 + +def add_database(image_path, save_dir): + # 获取access_token + token_info = get_access_toke_from_client_infos(cfg.CLIENT_ID, cfg.CLIENT_SECRET) + + # 准备FaceAnalysis模型 + model = insightface.app.FaceAnalysis() + model.prepare(ctx_id=0, det_thresh=0.3) + + # 声明一个数据库实例 + database = FaceRecognitionDatabase() + database.load_database("./Database/database.npz") + + # 读取图像 + image = cv2.imread(image_path) + + with open(image_path, 'rb') as f: + image_data = f.read() + image_base64 = base64.b64encode(image_data) + + # 利用获取的access_token调用推理接口 + if token_info: + access_token = token_info.get("access_token", None) + if access_token: + inference_ret = face_detection("face_detection", access_token, image_base64) + image = crop_and_expand(image, inference_ret) + else: + logging.error("Failed to obtain access") + + # 利用FaceAnalysis模型提取特征 + face_result = model.get(image) + + # 仅考虑检测到单张人脸的情况 + if len(face_result) == 1: + # 获取特征向量 + feature_vector = face_result[0].embedding + # 去除图像名称的后缀 + file_name = os.path.basename(image_path) + name = os.path.splitext(file_name)[0] + + # 将特征向量和名称添加到数据库中 + database.add_to_database(feature_vector, name) + else: + logging.error(f'未检测到人脸或有多张人脸,请重新拍照') + + # 保存数据库 + database.save_database(save_dir) + + logging.info("数据库添加完成") diff --git a/tools/face_recognition_service/src/build_database.py b/tools/face_recognition_service/src/build_database.py new file mode 100644 index 0000000000000000000000000000000000000000..b8357252de9d155cde2ad87c03da28c0f3971c67 --- /dev/null +++ b/tools/face_recognition_service/src/build_database.py @@ -0,0 +1,80 @@ +# -*- encoding=utf-8 -*- +""" +# ********************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# [oec-application] is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# Author: CoderJackZhu +# Create: 2023-10-27 +# Description: face recognition +# ********************************************************************************** +""" +import base64 +import logging +import os + +import cv2 +import insightface + +from src.cfg import cfg +from src.face_detect import get_access_toke_from_client_infos, FaceRecognitionDatabase, face_detection +from src.utils import crop_and_expand + + +# 先剪裁出人脸位置,再提特征,最后保存到数据库中 + +def build_database(image_folder_path, save_dir): + # 获取access_token + token_info = get_access_toke_from_client_infos(cfg.CLIENT_ID, cfg.CLIENT_SECRET) + + # 准备FaceAnalysis模型 + model = insightface.app.FaceAnalysis() + model.prepare(ctx_id=0, det_thresh=0.5) + + # 声明一个数据库实例 + database = FaceRecognitionDatabase() + + # 读取图片文件夹中的图片,并提取特征保存到数据库中 + for filename in os.listdir(image_folder_path): + # 读取图像 + image_path = os.path.join(image_folder_path, filename) + image = cv2.imread(image_path) + + with open(image_path, 'rb') as f: + image_data = f.read() + image_base64 = base64.b64encode(image_data) + + # 利用获取的access_token调用推理接口 + if token_info: + access_token = token_info.get("access_token", None) + if access_token: + inference_ret = face_detection("face_detection", access_token, image_base64) + image = crop_and_expand(image, inference_ret) + else: + logging.error("Failed to obtain access") + + # 利用FaceAnalysis模型提取特征 + face_result = model.get(image) + + # 去除图像名称的后缀 + name = os.path.splitext(filename)[0] + + # 仅考虑检测到单张人脸的情况 + if len(face_result) == 1: + # 获取特征向量 + feature_vector = face_result[0].embedding + # 将特征向量和名称添加到数据库中 + database.add_to_database(feature_vector, name) + else: + logging.warning(f'{name}未检测到人脸或有多张人脸,请重新拍照') + + # 保存数据库 + database.save_database(save_dir) + + logging.info("数据库建立完成") diff --git a/tools/face_recognition_service/src/camera.py b/tools/face_recognition_service/src/camera.py new file mode 100644 index 0000000000000000000000000000000000000000..fa653bb524ee3a201959af70c40471f5ce6a5436 --- /dev/null +++ b/tools/face_recognition_service/src/camera.py @@ -0,0 +1,158 @@ +# -*- encoding=utf-8 -*- +""" +# ********************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# [oec-application] is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# Author: CoderJackZhu +# Create: 2023-10-27 +# Description: face recognition +# ********************************************************************************** +""" +import base64 +import logging +import warnings + +import cv2 +import insightface + +from .cfg import cfg +from .face_detect import FaceRecognitionDatabase, get_access_toke_from_client_infos, face_detection +from .utils import crop_and_expand, draw_detection_boxes + +# 关闭控制台所有的警告输出 +warnings.filterwarnings("ignore") + + +def use_camera(): + """ + 调用摄像头,并使用接口进行人脸检测 + :return: + """ + # INFERENCE_TYPE = "face_detection" + + # 准备模型 + model = insightface.app.FaceAnalysis() + model.prepare(ctx_id=0, det_thresh=0.3) + + # 创建FaceRecognitionDatabase实例并加载数据库 + database = FaceRecognitionDatabase() + database.load_database("./Database/database.npz") + + cap = cv2.VideoCapture(0) + frame_count = 0 # 计数器,用于控制每10帧进行一次推理 + while True: + ret, frame = cap.read() + + if ret: + frame_count += 1 + if frame_count == 10: # 每10帧进行推理 + frame_count = 0 # 重置计数器 + + # 获取摄像头的一帧图像并编码 + img_data = cv2.imencode('.jpg', frame)[1] + image_base64 = base64.b64encode(img_data) + + # 获取access_token + token_info = get_access_toke_from_client_infos(cfg.CLIENT_ID, cfg.CLIENT_SECRET) + # 利用获取的access_token调用推理接口 + if token_info: + access_token = token_info.get("access_token", None) + if access_token: + inference_ret = face_detection("face_detection", access_token, image_base64) + image = crop_and_expand(image, inference_ret) + else: + logging.error("Failed to obtain access") + try: + logging.info(f"Inference result is: {inference_ret}") + # 提取特征 + res = model.get(image) + try: + emb1 = res[0].embedding + except Exception as err: + logging.error(err) + continue + if emb1 is not None: + # 使用数据库中的相似度查找函数 + db_result = database.search_similar_faces(emb1, cfg.cosine_similarity_threshold) + if db_result is not None: + name, _, similarity = db_result + logging.info(f"检测到人脸,人脸名称为:{name}, 相似度为:{similarity}") + flag = True + else: + flag = False + logging.info("未检测到人脸") + # 如果检测到人脸以及相应的位置,就在结果图上画框并显示出来 + if flag: + draw_detection_boxes(inference_ret, frame, name) + else: + draw_detection_boxes(inference_ret, frame, "None") + + except Exception as err: + logging.error(err) + break + else: + break + cap.release() + cv2.destroyAllWindows() + + +def no_camera(image_path): + # 准备模型 + model = insightface.app.FaceAnalysis() + model.prepare(ctx_id=0, det_thresh=0.3) + + # 创建FaceRecognitionDatabase实例并加载数据库 + database = FaceRecognitionDatabase() + database.load_database("../Database/database.npz") + + # 读取验证图像 + image = cv2.imread(image_path) + + with open(image_path, 'rb') as f: + image_data = f.read() + image_base64 = base64.b64encode(image_data) + + # 获取access_token + token_info = get_access_toke_from_client_infos(cfg.CLIENT_ID, cfg.CLIENT_SECRET) + # 利用获取的access_token调用推理接口 + if token_info: + access_token = token_info.get("access_token", None) + if access_token: + inference_ret = face_detection("face_detection", access_token, image_base64) + image = crop_and_expand(image, inference_ret) + else: + logging.error("Failed to obtain access") + try: + logging.info(f"Inference result is: {inference_ret}") + # 提取特征 + res = model.get(image) + try: + emb1 = res[0].embedding + except Exception as err: + logging.error(err) + if emb1 is not None: + # 使用数据库中的相似度查找函数 + db_result = database.search_similar_faces(emb1, cfg.cosine_similarity_threshold) + if db_result is not None: + name, _, _ = db_result + flag = True + else: + flag = False + if flag: + logging.info(f"检测到人脸,人脸名称为:{name}") + else: + logging.info("未检测到人脸") + + except Exception as err: + logging.error(err) + + +if __name__ == "__main__": + use_camera() diff --git a/tools/face_recognition_service/src/cfg.py b/tools/face_recognition_service/src/cfg.py new file mode 100644 index 0000000000000000000000000000000000000000..f2b33fea9e82d9c4e86c09fce818f587e83ca49d --- /dev/null +++ b/tools/face_recognition_service/src/cfg.py @@ -0,0 +1,25 @@ +# -*- encoding=utf-8 -*- +""" +# ********************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# [oec-application] is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# Author: CoderJackZhu +# Create: 2023-10-27 +# Description: face recognition +# ********************************************************************************** +""" +from easydict import EasyDict + +# 创建一个包含 CLIENT_ID 和 CLIENT_SECRET 的字典 +cfg = EasyDict({ + 'CLIENT_ID': '2b96bc92a1e5406cacfc26846245b717', + 'CLIENT_SECRET': 'cbec5898006947a73c8b4ef8e0067673729c280313581b56', + "cosine_similarity_threshold": 0.5 +}) diff --git a/tools/face_recognition_service/src/face_detect.py b/tools/face_recognition_service/src/face_detect.py new file mode 100644 index 0000000000000000000000000000000000000000..9c8092437049424b11b386e267dc72086795f6a7 --- /dev/null +++ b/tools/face_recognition_service/src/face_detect.py @@ -0,0 +1,124 @@ +# -*- encoding=utf-8 -*- +""" +# ********************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# [oec-application] is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# Author: CoderJackZhu +# Create: 2023-10-27 +# Description: face recognition +# ********************************************************************************** +""" +import json +import logging +from urllib import parse + +import faiss +import numpy as np +import requests + + +def get_access_toke_from_client_infos(client_id, client_secret): + try: + headers = { + "Content-Type": "application/x-www-form-urlencoded" + } + send_data = {"grant_type": 'client_credentials', + "client_id": client_id, + "client_secret": client_secret} + resp = requests.post(url="https://studio.e.huawei.com/baas/auth/v1.0/oauth2/token", + headers=headers, + data=parse.urlencode(send_data), + verify=False) + return resp.json() + except requests.exceptions.RequestException as err: + logging.error(f"Exception occurred in face_detection: {err}") + return {} + + +def face_detection(inference_type, access_token, image_base64): + # 请求body体 + send_data = { + "name": inference_type, + "dataInputs": {"images": [{"image": image_base64.decode(), "type": "base64"}]} + } + + # 请求header + headers = { + "access-Token": access_token, + "Content-Type": "application/json" + } + try: + resp = requests.post(url="https://studio.e.huawei.com/ai-enable/v1.0/services/invoke", + headers=headers, + data=json.dumps(send_data), + verify=False) + return resp.json() + except Exception as err: + logging.error(f"Exception occurred in face_detection: {err}") + return {} + + +class FaceRecognitionDatabase: + def __init__(self): + self.feature_matrix = np.empty((0, 512), dtype=np.float32) + self.face_names = [] + self.faiss_index = None + + def build_faiss_database(self): + if self.feature_matrix.shape[0] > 0: + d = self.feature_matrix.shape[1] + self.faiss_index = faiss.IndexFlatIP(d) + self.faiss_index.add(self.feature_matrix) + + def search_similar_faces(self, query_vector, cosine_similarity_threshold): + if self.faiss_index is None: + raise ValueError("Faiss index is not built. Please add vectors to the database and build the index.") + + k = 1 + _, indices = self.faiss_index.search(np.expand_dims(query_vector, axis=0), k) + + query_vector_norm = np.linalg.norm(query_vector) + feature_vector_norm = np.linalg.norm(self.feature_matrix[indices[0][0]]) + + # 添加除零保护 + q_f_norm = query_vector_norm * feature_vector_norm + if q_f_norm == 0: + cosine_similarity = 0 # 如果分母为0,将余弦相似度设置为0 + else: + cosine_similarity = np.dot(query_vector, self.feature_matrix[indices[0][0]]) / q_f_norm + + if cosine_similarity < cosine_similarity_threshold: + logging.info(f"相似度低于阈值,相似度为{cosine_similarity:.2f}") + flag = None + return flag + else: + similar_name = self.face_names[indices[0][0]] + similar_vector = self.feature_matrix[indices[0][0]] + return similar_name, similar_vector, cosine_similarity + + def add_to_database(self, new_feature_vector, face_name): + # assert new_feature_vector.shape[0] == self.feature_matrix.shape[1], "维度不匹配" + if self.feature_matrix.shape[0] != self.feature_matrix.shape[1]: + raise ValueError("维度不匹配") + self.feature_matrix = np.vstack([self.feature_matrix, new_feature_vector]) + self.face_names.append(face_name) + if self.faiss_index is None: + self.build_faiss_database() + else: + self.faiss_index.add(np.expand_dims(new_feature_vector, axis=0)) + + def save_database(self, save_dir): + np.savez(save_dir, feature_matrix=self.feature_matrix, face_names=self.face_names) + + def load_database(self, load_dir): + data = np.load(load_dir) + self.feature_matrix = data['feature_matrix'] + self.face_names = list(data['face_names']) + self.build_faiss_database() diff --git a/tools/face_recognition_service/src/utils.py b/tools/face_recognition_service/src/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..c70762f141337fd321d0fcb92682e4654af04d05 --- /dev/null +++ b/tools/face_recognition_service/src/utils.py @@ -0,0 +1,91 @@ +# -*- encoding=utf-8 -*- +""" +# ********************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# [oec-application] is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# Author: CoderJackZhu +# Create: 2023-10-27 +# Description: face recognition +# ********************************************************************************** +""" +import cv2 +import numpy as np + + +def cosine_similarity(vector_a, vector_b): + # 计算向量的余弦相似度 + dot_product = np.dot(vector_a, vector_b) # 计算点积 + norm_a = np.linalg.norm(vector_a) # 计算向量A的模长 + norm_b = np.linalg.norm(vector_b) # 计算向量B的模长 + # 添加除零保护 + norm_c = norm_a * norm_b + if norm_c == 0: + similarity = 0 # 如果分母为0,将相似度设置为0 + else: + similarity = dot_product / norm_c # 计算余弦相似度 + return similarity + + +def draw_detection_boxes(inference_ret, frame, name): + # 如果检测到人脸以及相应的位置,就在结果图上画框并显示出来 + if len(inference_ret["result"]) > 0: + for i in range(len(inference_ret["result"])): + for j in range(len(inference_ret["result"][i]["infos"])): + # 把一次检测出的多张人脸都画在一张图上,并显示出来 + location = inference_ret["result"][i]["infos"][j]["location"] + height, top_left_x, top_left_y, width = location["height"], location["top_left_x"], \ + location["top_left_y"], location["width"] + + # 绘制人脸框 + img = frame.copy() # 创建图像的副本以免影响原图像 + cv2.rectangle(img, (top_left_x, top_left_y), (top_left_x + width, top_left_y + height), (0, 0, 255), 2) + + # chinese_font_path = "../SimHei.ttf" + chinese_font = cv2.FONT_HERSHEY_SIMPLEX + text_position = (top_left_x, top_left_y - 10) # 文本位置稍微上移一些 + cv2.putText(img, name, text_position, chinese_font, 0.6, (0, 0, 255), 2) + + # 显示图像 + cv2.imshow('FaceDetect', img) + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + +def crop_and_expand(image, detection_result): + # 检查检测是否成功以及是否检测到任何人脸 + if "resCode" in detection_result and detection_result["resCode"] == "0" and "result" in detection_result: + faces = detection_result["result"] + if len(faces) > 0 and "infos" in faces[0] and len(faces[0]["infos"]) > 0: + face_info = faces[0]["infos"][0] + location = face_info["location"] + top_left_x = int(location["top_left_x"]) + top_left_y = int(location["top_left_y"]) + width = int(location["width"]) + height = int(location["height"]) + + # 计算扩展区域 + expand_width = width // 2 + expand_height = height // 2 + + # 确保扩展区域在图像边界内 + expanded_top_left_x = max(top_left_x - expand_width, 0) + expanded_top_left_y = max(top_left_y - expand_height, 0) + expanded_bottom_right_x = min(top_left_x + width + expand_width, image.shape[1]) + expanded_bottom_right_y = min(top_left_y + height + expand_height, image.shape[0]) + + # 裁剪扩展区域 + cropped_image = image[expanded_top_left_y:expanded_bottom_right_y, + expanded_top_left_x:expanded_bottom_right_x] + + return cropped_image + + # 如果未检测到人脸或检测失败,则返回None + return image diff --git a/tools/face_recognition_service/test/test.py b/tools/face_recognition_service/test/test.py new file mode 100644 index 0000000000000000000000000000000000000000..02ae1f015fd62fe08cff8cdffa957156cdd7c4e3 --- /dev/null +++ b/tools/face_recognition_service/test/test.py @@ -0,0 +1,89 @@ +# -*- encoding=utf-8 -*- +""" +# ********************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# [oec-application] is licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# Author: CoderJackZhu +# Create: 2023-10-27 +# Description: face recognition +# ********************************************************************************** +""" +import logging +import os +import warnings + +import cv2 +import insightface + +from src.build_database import build_database +from src.face_detect import FaceRecognitionDatabase + +# 关闭控制台所有的警告输出 +warnings.filterwarnings("ignore") + +# 建立数据库database +build_database("../Database/train", "../Database/database.npz") + +# 指定验证数据集的文件夹路径 +VAL_FOLDER = '../Database/val' # 请替换为您的验证数据集文件夹路径 + +# 创建FaceRecognitionDatabase实例并加载数据库 +database = FaceRecognitionDatabase() +database.load_database("../Database/database.npz") + +# 准备FaceAnalysis模型 +model = insightface.app.FaceAnalysis() +model.prepare(ctx_id=0, det_thresh=0.3) + +# 设置相似度阈值 +COSINE_SIMILARITY_THRESHOLD = 0.5 # 根据需要调整阈值 + +# 计数器,用于统计正确识别的人脸数 +CORRECT_COUNT = 0 +TOTAL_COUNT = 0 + +# 遍历验证数据集中的图像 +for filename in os.listdir(VAL_FOLDER): + image_path = os.path.join(VAL_FOLDER, filename) + + # 读取验证图像 + image = cv2.imread(image_path) + + # 提取特征 + res = model.get(image) + try: + emb1 = res[0].embedding + except Exception as err: + logging.error(err) + continue + + if emb1 is not None: + # 使用数据库中的相似度查找函数 + result = database.search_similar_faces(emb1, COSINE_SIMILARITY_THRESHOLD) + + if result is not None: + similar_name, _, _ = result + # 获取验证图像的文件名(去除后缀) + val_name = os.path.splitext(filename)[0] + + # 检查是否正确识别 + if similar_name == val_name: + CORRECT_COUNT += 1 + + TOTAL_COUNT += 1 + +# 计算正确率 +if TOTAL_COUNT == 0: + ACCURACY = 0 # 如果分母为0,将正确率设置为0 +else: + ACCURACY = CORRECT_COUNT / TOTAL_COUNT +logging.info(f"正确识别人脸数: {CORRECT_COUNT}") +logging.info(f"总人脸数: {TOTAL_COUNT}") +logging.info(f"识别准确率: {ACCURACY * 100:.2f}%")