diff --git a/example/special/face_system/python/README.md b/example/special/face_system/python/README.md index 363f9b4e96baa56db14352eeecd491c7747e0c41..e6b90f877fd3db04d508927c15604541857adf60 100644 --- a/example/special/face_system/python/README.md +++ b/example/special/face_system/python/README.md @@ -193,7 +193,7 @@ if __name__ == '__main__': 请使用 Electerm Ssh 并在命令行中执行以下命令: ```bash -python face_system.py LZ-Face.rknn LZ-ArcFace.rknn BaseDataset crop_root +python test_face_system.py LZ-Face.rknn LZ-ArcFace.rknn BaseDataset crop_root ``` 运行程序后,屏幕上开始打印数据库构建的相关信息,并在一段时间后输出 diff --git a/example/special/face_system/python/face_system.py b/example/special/face_system/python/face_system.py deleted file mode 100644 index 8c5b2def46b465a609443b9e961d27262428e592..0000000000000000000000000000000000000000 --- a/example/special/face_system/python/face_system.py +++ /dev/null @@ -1,57 +0,0 @@ -import sys -import time - -from lockzhiner_vision_module.cv2 import VideoCapture, imread, imwrite -from lockzhiner_vision_module.vision import FaceRecognitionSystem, visualize -from lockzhiner_vision_module.edit import Edit - - -# 主程序入口 -if __name__ == '__main__': - # 获取命令行参数 - args = sys.argv - # 确保提供了足够的参数 - if len(args) != 5: - print("Need model path. Example: python face_system.py LZ-Face.rknn LZ-ArcFace.rknn baseDataset_root crop_root") - exit(1) - - # 初始化面部识别系统 - face_system = FaceRecognitionSystem() - # 验证面部识别系统的初始化是否成功 - if face_system.initialize(args[1], args[2]) is False: - print("Failed to initialize face_system model") - exit(1) - # 构建面部数据库 - face_system.buildDatabase(args[3],args[4]) - - # 初始化图像编辑处理对象 - edit = Edit() - # 开始并接受连接,准备进行图像编辑处理 - edit.start_and_accept_connection() - # 初始化视频捕捉对象 - video_capture = VideoCapture() - # 验证摄像头是否成功打开 - if video_capture.open(0) is False: - print("Failed to open capture") - exit(1) - # 主循环,用于持续捕捉和处理视频帧 - while True: - read_index = 0 - total_time_ms = 0 - for i in range(100): - # 从摄像头读取帧 - ret, input_mat = video_capture.read() - # 如果读取失败,则继续循环直到成功读取 - if ret is False: - continue - # 预测调用predict方法 - start_time = time.time() - results = face_system.predict(input_mat) - end_time = time.time() - total_time_ms += end_time - start_time - read_index += 1 - # 可视化处理结果 - vis_mat = visualize(input_mat, results) - # 打印处理后的图像 - edit.print(vis_mat) - print(f"FPS is {1.0 / (total_time_ms / read_index)}") \ No newline at end of file diff --git a/example/special/face_system/python/test_face_system.py b/example/special/face_system/python/test_face_system.py index 7c29e3914324824478bf9900a467378f3ec4bc69..8c5b2def46b465a609443b9e961d27262428e592 100644 --- a/example/special/face_system/python/test_face_system.py +++ b/example/special/face_system/python/test_face_system.py @@ -1,120 +1,57 @@ -import os import sys -from lockzhiner_vision_module.cv2 import VideoCapture,imread,imwrite -from lockzhiner_vision_module.vision import PaddleDet, ArcFace, visualize, cosine_similarity -from lockzhiner_vision_module.edit import Edit import time -import sys - -def predict_face(det_model, rec_model, face_mat): - if face_mat.empty(): - return None, None - - det_results = det_model.predict(face_mat) - result_size = len(det_results) - if result_size != 1: - print(f"Failed to find face. The size of faces is {result_size}, but need 1.") - return None, None - det_result = det_results[0] - face_roi = det_result.box - crop_mat = input_mat.crop(face_roi) - if crop_mat.empty(): - return None, None - - rec_result = rec_model.predict(crop_mat) - return det_results, rec_result +from lockzhiner_vision_module.cv2 import VideoCapture, imread, imwrite +from lockzhiner_vision_module.vision import FaceRecognitionSystem, visualize +from lockzhiner_vision_module.edit import Edit +# 主程序入口 if __name__ == '__main__': + # 获取命令行参数 args = sys.argv - if len(args) != 3: - print("Need model path. Example: python face_system.py LZ-Face.rknn LZ-ArcFace.rknn") - exit(1) - - face_det = PaddleDet() - if face_det.initialize(args[1]) is False: - print("Failed to initialize face_det model") + # 确保提供了足够的参数 + if len(args) != 5: + print("Need model path. Example: python face_system.py LZ-Face.rknn LZ-ArcFace.rknn baseDataset_root crop_root") exit(1) - face_rec = ArcFace() - if face_rec.initialize(args[2]) is False: - print("Failed to initialize face_rec model") + # 初始化面部识别系统 + face_system = FaceRecognitionSystem() + # 验证面部识别系统的初始化是否成功 + if face_system.initialize(args[1], args[2]) is False: + print("Failed to initialize face_system model") exit(1) + # 构建面部数据库 + face_system.buildDatabase(args[3],args[4]) - # 原始数据路径 - base_dataset_path = 'BaseDataset' - # 裁剪后的数据路径 - crop_dataset_path = 'CropDataset' - - # 如果裁剪文件夹不存在,则创建 - if not os.path.exists(crop_dataset_path): - os.makedirs(crop_dataset_path) - # 构建数据库 - # 临时存储特征向量的字典 - face_dict = {} - # 遍历BaseDataset中的每个文件夹 - for user_folder in os.listdir(base_dataset_path): - user_path = os.path.join(base_dataset_path, user_folder) - if os.path.isdir(user_path) is False: - continue - # 创建对应用户的裁剪文件夹 - user_crop_path = os.path.join(crop_dataset_path, user_folder) - if not os.path.exists(user_crop_path): - os.makedirs(user_crop_path) - # 初始化用户的特征向量列表 - face_dict[user_folder] = [] - for img_name in os.listdir(user_path): - img_path = os.path.join(user_path, img_name) - print(img_path) - - # 读取图片并检测图片是否为空 - input_mat = imread(img_path) - if input_mat.empty(): - print(f"{img_path} is empty.") - continue - - _, feature_result = predict_face(face_det, face_rec, input_mat) - if feature_result is None: - print(f"Failed to find face in {img_path}.") - continue - face_dict[user_folder].append(feature_result) - + # 初始化图像编辑处理对象 edit = Edit() + # 开始并接受连接,准备进行图像编辑处理 edit.start_and_accept_connection() - + # 初始化视频捕捉对象 video_capture = VideoCapture() + # 验证摄像头是否成功打开 if video_capture.open(0) is False: print("Failed to open capture") exit(1) - + # 主循环,用于持续捕捉和处理视频帧 while True: - ret, input_mat = video_capture.read() - if ret is False: - continue - # 预测 - result_0, result_1 = predict_face(face_det, face_rec, input_mat) - if result_0 is None or result_1 is None: - print(f"Failed to find face on video.") - continue - vis_mat = visualize(input_mat, result_0) - edit.print(vis_mat) - - max_name = "None" - max_similarity = 0.0 - for name, feature_results in face_dict.items(): - for feature_result in feature_results: - similarity = cosine_similarity(result_1, feature_result) - if max_similarity < similarity: - max_similarity = similarity - max_name = name - print(f"User: {max_name}, score: {max_similarity}") - - - - - - - - - + read_index = 0 + total_time_ms = 0 + for i in range(100): + # 从摄像头读取帧 + ret, input_mat = video_capture.read() + # 如果读取失败,则继续循环直到成功读取 + if ret is False: + continue + # 预测调用predict方法 + start_time = time.time() + results = face_system.predict(input_mat) + end_time = time.time() + total_time_ms += end_time - start_time + read_index += 1 + # 可视化处理结果 + vis_mat = visualize(input_mat, results) + # 打印处理后的图像 + edit.print(vis_mat) + print(f"FPS is {1.0 / (total_time_ms / read_index)}") \ No newline at end of file diff --git a/example/vision/face_detection/README.md b/example/vision/face_detection/README.md index fe24056cd32d5aaf2f32810f5b0c3883d51ec552..b4af46db6a4910afd3f19336766c2f51d7c01167 100644 --- a/example/vision/face_detection/README.md +++ b/example/vision/face_detection/README.md @@ -35,6 +35,7 @@ ## 2 运行前的准备 * 请确保你已经下载了 [凌智视觉模块人脸检测模型](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.3/LZ-Face.rknn) +* 请确保你已经下载了 [凌智视觉模块图片传输助手下载地址](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.0/LockzhinerVisionModuleImageFetcher.exe) ## 3 在凌智视觉模块上部署模型 diff --git a/example/vision/face_detection/python/README.md b/example/vision/face_detection/python/README.md index a16416606f8776cd080b4ec43876b118c4fa6982..8a25d7d25b4718de127ae09c62fdeeffac3fda28 100644 --- a/example/vision/face_detection/python/README.md +++ b/example/vision/face_detection/python/README.md @@ -34,6 +34,7 @@ - 请确保你已经参考 [凌智视觉模块人脸检测模型部署指南](../README.md) 对模型进行了充分训练。 - 请确保你已经按照 [开发环境搭建指南](../../../../docs/introductory_tutorial/python_development_environment.md) 正确配置了开发环境。 + ## 2 Python API 文档 ```python @@ -192,7 +193,7 @@ import sys if __name__ == "__main__": args = sys.argv if len(args) != 2: - print("Need model path. Example: python test_face_detection.py LZ-Face.rknn") + print("Need model path. Example: python test_picodet.py LZ-Face.rknn") exit(1) edit = Edit() @@ -243,19 +244,19 @@ if __name__ == "__main__": 请使用 Electerm Sftp 依次上传以下两个文件: -- 进入存放 **test_face_detection.py** 脚本文件的目录,将 **test_face_detection.py** 上传到 Lockzhiner Vision Module -- 进入存放 **LZ-Face.rknn(也可能是其他模型)** 模型存放的目录(模型存放在训练模型后下载的 output 文件夹内),将 **LZ-Face.rknn** 上传到 Lockzhiner Vision Module +- 进入存放 **test_picodet.py** 脚本文件的目录,将 **test_picodet.py** 上传到 Lockzhiner Vision Module +- 进入存放 **LZ-Face.rknn** 模型存放的目录(模型存放在训练模型后下载的 output 文件夹内),将 **LZ-Face.rknn** 上传到 Lockzhiner Vision Module ![](../images/stfp.png) 请使用 Electerm Ssh 并在命令行中执行以下命令: ```bash -python test_face_detection.py LZ-Face.rknn +python test_picodet.py LZ-Face.rknn ``` ![](../images/connect.png) -连接凌智视觉模块图片传输助手[凌智视觉模块图片传输助手下载地址](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.0/LockzhinerVisionModuleImageFetcher.exe)后,选择连接设备 +连接凌智视觉模块图片传输助手后,选择连接设备 ![](../images/result1.png) 运行程序后,屏幕上开始打印矩形框信息,标签信息和置信度,并在一段时间后输出 FPS 值 diff --git a/example/vision/face_detection/python/test_face_detection.py b/example/vision/face_detection/python/test_face_detection.py deleted file mode 100644 index 6c35fa9698263a67e3ea6ae9c0c24191aea4abe6..0000000000000000000000000000000000000000 --- a/example/vision/face_detection/python/test_face_detection.py +++ /dev/null @@ -1,50 +0,0 @@ -from lockzhiner_vision_module.cv2 import VideoCapture -from lockzhiner_vision_module.vision import PaddleDet, visualize -from lockzhiner_vision_module.edit import Edit -import time -import sys - -if __name__ == "__main__": - args = sys.argv - if len(args) != 2: - print("Need model path. Example: python test_face_detection.py LZ-Face.rknn") - exit(1) - - edit = Edit() - edit.start_and_accept_connection() - - model = PaddleDet() - if model.initialize(args[1]) is False: - print("Failed to initialize PaddleDet") - exit(1) - - video_capture = VideoCapture() - if video_capture.open(0) is False: - print("Failed to open capture") - exit(1) - - while True: - read_index = 0 - total_time_ms = 0 - for i in range(30): - ret, mat = video_capture.read() - if ret is False: - continue - - start_time = time.time() - results = model.predict(mat) - end_time = time.time() - total_time_ms += end_time - start_time - read_index += 1 - - print(f"result size is {len(results)}") - for result in results: - box = result.box - score = result.score - label_id = result.label_id - print( - f"(x,y,w,h,score,label_id): [{box.x},{box.y},{box.width},{box.height},{score},{label_id}]" - ) - vis_mat = visualize(mat, results) - edit.print(vis_mat) - print(f"FPS is {1.0 / (total_time_ms/read_index)}") \ No newline at end of file diff --git a/example/vision/face_detection/python/test_retina_face_latest.py b/example/vision/face_detection/python/test_picodet.py similarity index 93% rename from example/vision/face_detection/python/test_retina_face_latest.py rename to example/vision/face_detection/python/test_picodet.py index a8865d4766d4febd3eedcbad6dd15d467e830661..2bbd633ed3e10073f2560c0a8f5a9f53c5131551 100644 --- a/example/vision/face_detection/python/test_retina_face_latest.py +++ b/example/vision/face_detection/python/test_picodet.py @@ -1,10 +1,9 @@ from lockzhiner_vision_module.cv2 import imread, imwrite, VideoCapture -from lockzhiner_vision_module.vision import RetinaFace, visualize +from lockzhiner_vision_module.vision import PaddleDet, visualize from lockzhiner_vision_module.edit import Edit import time import sys - def predict_video(face_det_model, width, height): edit = Edit() edit.start_and_accept_connection() @@ -53,7 +52,7 @@ def predict_image(face_det_model, image_path): f"(x,y,w,h,score): [{box.x},{box.y},{box.width},{box.height},{score}]" ) imwrite("face_det.png", vis_mat) - + if __name__ == "__main__": args = sys.argv @@ -61,9 +60,9 @@ if __name__ == "__main__": print("Need model path. Example: python test_retina_face.py LZ-RetinaFace.rknn width height") exit(1) - model = RetinaFace() + model = PaddleDet() if model.initialize(args[1]) is False: - print("Failed to initialize RetinaFace") + print("Failed to initialize PaddleDet") exit(1) video_width = 640 diff --git a/example/vision/face_detection/python/test_retina_face.py b/example/vision/face_detection/python/test_retina_face.py index bd027eb746b4b6f72101a0ee896e792fe91dde4b..a8865d4766d4febd3eedcbad6dd15d467e830661 100644 --- a/example/vision/face_detection/python/test_retina_face.py +++ b/example/vision/face_detection/python/test_retina_face.py @@ -1,24 +1,17 @@ -from lockzhiner_vision_module.cv2 import VideoCapture +from lockzhiner_vision_module.cv2 import imread, imwrite, VideoCapture from lockzhiner_vision_module.vision import RetinaFace, visualize from lockzhiner_vision_module.edit import Edit import time import sys -if __name__ == "__main__": - args = sys.argv - if len(args) != 2: - print("Need model path. Example: python test_retina_face.py LZ-RetinaFace.rknn") - exit(1) - + +def predict_video(face_det_model, width, height): edit = Edit() edit.start_and_accept_connection() - model = RetinaFace() - if model.initialize(args[1]) is False: - print("Failed to initialize RetinaFace") - exit(1) - video_capture = VideoCapture() + video_capture.set_width(width) + video_capture.set_height(height) if video_capture.open(0) is False: print("Failed to open capture") exit(1) @@ -32,11 +25,10 @@ if __name__ == "__main__": continue start_time = time.time() - results = model.predict(mat) + results = face_det_model.predict(mat) end_time = time.time() total_time_ms += end_time - start_time read_index += 1 - print(f"result size is {len(results)}") for result in results: box = result.box @@ -46,4 +38,41 @@ if __name__ == "__main__": ) vis_mat = visualize(mat, results) edit.print(vis_mat) - print(f"FPS is {1.0 / (total_time_ms/read_index)}") \ No newline at end of file + print(f"FPS is {1.0 / (total_time_ms/read_index)}") + + +def predict_image(face_det_model, image_path): + image = imread(image_path) + results = face_det_model.predict(image) + vis_mat = visualize(image, results) + print(f"result size is {len(results)}") + for result in results: + box = result.box + score = result.score + print( + f"(x,y,w,h,score): [{box.x},{box.y},{box.width},{box.height},{score}]" + ) + imwrite("face_det.png", vis_mat) + + +if __name__ == "__main__": + args = sys.argv + if len(args) < 2: + print("Need model path. Example: python test_retina_face.py LZ-RetinaFace.rknn width height") + exit(1) + + model = RetinaFace() + if model.initialize(args[1]) is False: + print("Failed to initialize RetinaFace") + exit(1) + + video_width = 640 + video_height = 480 + if len(args) == 5: + video_width = int(args[3]) + video_height = int(args[4]) + + if len(args) == 2: + predict_video(model, video_width, video_height) + elif len(args) == 3: + predict_image(model, args[2]) \ No newline at end of file