diff --git "a/docs/models/\344\272\272\350\204\270\346\243\200\346\265\213\345\244\215\347\216\260\346\265\201\347\250\213.md" "b/docs/models/\344\272\272\350\204\270\346\243\200\346\265\213\345\244\215\347\216\260\346\265\201\347\250\213.md" new file mode 100644 index 0000000000000000000000000000000000000000..b5f7b432f02f9ff694a0a01ccdc9e5d7d8fccaff --- /dev/null +++ "b/docs/models/\344\272\272\350\204\270\346\243\200\346\265\213\345\244\215\347\216\260\346\265\201\347\250\213.md" @@ -0,0 +1,237 @@ +# 1 安装系统环境(ubuntu) +## 1.1 安装显卡驱动 + +- 打开software & updates ->选择**Additional Drivers** 安装一个驱动 +- 使用 nvidia-smi 查看显卡是否正常 +## 1.2 安装CUDA +1.2.1 下载CUDA + - i https://developer.nvidia.com/cuda-toolkit-archive + - 选择对应版本的CUDA + - 选择 -> Linux x86_64 -> Ubuntu-> version -> Runfile (local) +**下面这个是根据版本选择得到的** +wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run +sudo sh cuda_11.8.0_520.61.05_linux.run +等待一段时间,会弹出一个窗口,选择continue 回车 输入 **accept**->进入CUDA Installer界面 选择Driver 点击**空格**取消选择Driver +--> Install +安装完成后,需要进行环境变量配置 +输入 vim ~/.bashrc +添加以下内容: + - export PATH=/usr/local/cuda-11.8/bin${PATH:+:${PATH}} + - export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64\${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} + + - 输入**nvcc -V** 查看是否安装成功 + +## 1.3 安装cudnn +1.3.1 下载cudnn +点击下面的链接,进行对应版本的选择 + - https://developer.nvidia.com/rdp/cudnn-archive + - sudo apt-get install + +## 1.4 安装 Nvidia-docker +进入官网 +https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html +选择 install with apt + - Configure the production repository: + curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \ + && curl -s -L https://nvidia.github.io/libnvidia-container/stable/deb/nvidia-container-toolkit.list | \ + sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \ + sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list + - Update the packages list from the repository: + sudo apt-get update + - Install the NVIDIA Container Toolkit packages: + sudo apt-get install -y nvidia-container-toolkit + +# 2 配置训练环境 +## 2.1 拉取docker镜像 +- modelscope github 仓库地址 +- https://github.com/modelscope/modelscope + +GPU镜像 +# py37 +- registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.3.0-py37-torch1.11.0-tf1.15.5-1.6.1 + +# py38 +- registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.8.0-py38-torch2.0.1-tf2.13.0-1.9.5 + +我拉取的是 py38 版本 +- docker pull registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.8.0-py38-torch2.0.1-tf2.13.0-1.9.5 +查看镜像 +- docker images +启动镜像 -v /home/ubuntu/:/home/ubuntu/ 本地文件映射到容器内 +- docker run -it --gpus all -v /home/ubuntu/:/home/ubuntu/ registry.cn-hangzhou.aliyuncs.com/modelscope-repo/modelscope:ubuntu20.04-cuda11.8.0-py38-torch2.0.1-tf2.13.0-1.9.5 -n /bin/bash + +## 2.2 拉取训练模型 +- git clone https://github.com/biubug6/Pytorch_Retinaface.git + + +# 3 数据集制作 +## 3.1 数据集标注 +- 数据集标注使用LabelMe进行数据标注 +文件夹样式 +- Dataset + - annotations + - images + - flags.txt +- 目标数据格式 +```markdown + ./data/widerface/ + train/ + images/ + label.txt + val/ + images/ + wider_val.txt +``` +- 标注内容 + - 人脸边界框:上额头到下额头,左右脸边缘 + - 左眼、右眼、鼻子、左嘴角、右嘴角 +- 标签文件转换 +```python +import os +import json +import random # 导入random模块 + +def convert_json_to_format(json_data, folder_name): + # 提取图片路径 + image_path = json_data['imagePath'] + + # 获取图像尺寸 + image_width = json_data['imageWidth'] + image_height = json_data['imageHeight'] + + # 初始化输出字符串 + output = "" + + # 初始化各部位坐标值为 -1 + face_box_x, face_box_y, face_box_w, face_box_h = -1, -1, -1, -1 + left_eye_x, left_eye_y = -1, -1 + right_eye_x, right_eye_y = -1, -1 + nose_x, nose_y = -1, -1 + left_mouse_x, left_mouse_y = -1, -1 + right_mouse_x, right_mouse_y = -1, -1 + + # 处理形状数据 + shapes = json_data['shapes'] + + for shape in shapes: + label = shape['label'] + points = shape['points'] + + if label == 'face' and len(points) == 2: + # 转换矩形框坐标 + x1, y1 = points[0] + x2, y2 = points[1] + face_box_x = min(x1, x2) + face_box_y = min(y1, y2) + face_box_w = abs(x2 - x1) + face_box_h = abs(y2 - y1) + elif label == 'left_eye': + # 添加左眼位置信息 + left_eye_x, left_eye_y = points[0] + elif label == 'right_eye': + # 添加右眼位置信息 + right_eye_x, right_eye_y = points[0] + elif label == 'nose': + # 添加鼻子位置信息 + nose_x, nose_y = points[0] + elif label == 'left_mouse': + # 添加左嘴角位置信息 + left_mouse_x, left_mouse_y = points[0] + elif label == 'right_mouse': + # 添加右嘴角位置信息 + right_mouse_x, right_mouse_y = points[0] + + # 构建输出字符串 + output = f"{int(face_box_x)} {int(face_box_y)} {int(face_box_w)} {int(face_box_h)} " + output += f"{float(left_eye_x):.3f} {float(left_eye_y):.3f} 0.0 " + output += f"{float(right_eye_x):.3f} {float(right_eye_y):.3f} 0.0 " + output += f"{float(nose_x):.3f} {float(nose_y):.3f} 0.0 " + output += f"{float(left_mouse_x):.3f} {float(left_mouse_y):.3f} 0.0 " + output += f"{float(right_mouse_x):.3f} {float(right_mouse_y):.3f} 0.0 " + + # 添加随机置信度值,范围在0.8到0.99之间 + confidence = random.uniform(0.8, 0.99) + output += f"{confidence:.2f}" + + # 返回结果 + return f"# {folder_name}/{os.path.basename(image_path)}\n{output}\n" + +def process_folder(folder_path, output_file): + # 打开输出文件 + with open(output_file, 'w', encoding='utf-8') as out_file: + # 遍历文件夹中的所有文件 + for filename in os.listdir(folder_path): + if filename.endswith('.json'): + file_path = os.path.join(folder_path, filename) + with open(file_path, 'r', encoding='utf-8') as file: + json_data = json.load(file) + # 获取文件夹名 + folder_name = os.path.basename(os.path.dirname(file_path)) + result = convert_json_to_format(json_data, folder_name) + out_file.write(result) + +# 指定文件夹路径和输出文件路径 +folder_path = r'C:\Users\zhb20\Desktop\FaceData\train\images\face' +output_file = r'C:\Users\zhb20\Desktop\FaceData\train\label.txt' + +# 调用函数 +process_folder(folder_path, output_file) +``` +验证集标签文件制作 +```python +import os + +def save_folder_and_image_names(folder_path, output_file): + """ + 遍历指定文件夹中的所有图片,将文件夹名和图片名组合后保存到输出文件中。 + + :param folder_path: 包含图片的文件夹路径 + :param output_file: 输出文件的路径 + """ + # 检查文件夹是否存在 + if not os.path.isdir(folder_path): + print(f"错误:{folder_path} 不是一个有效的文件夹路径") + return + + # 图片文件可能的扩展名列表 + image_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff'] + + with open(output_file, 'w', encoding='utf-8') as file: + for root, dirs, files in os.walk(folder_path): + for name in files: + # 获取文件扩展名 + ext = os.path.splitext(name)[1].lower() + if ext in image_extensions: + # 组合文件夹名和图片名 + folder_name = os.path.basename(root) + full_name = f"{folder_name}/{name}" + file.write(full_name + '\n') + +if __name__ == "__main__": + # 修改这里的文件夹路径和输出文件路径 + folder_path = r'C:\Users\Administrator\Desktop\facedata\val\images\face' + output_file = r'C:\Users\Administrator\Desktop\facedata\val\wider_val.txt' + + save_folder_and_image_names(folder_path, output_file) + print(f"图片信息已保存至 {output_file}") +``` +# 4 windows 环境搭建 +## 4.1 conda安装 +进入网址 +- https://anaconda.org/anaconda/conda + 下载conda 默认步骤安装即可 +- conda create -n 环境名 python==版本号 -y +- conda activate 环境名 +- 配置国内镜像源地址 +- pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple some-package +- 安装 pytorch 2.0.1 +- [pytorch下载网址](https://pytorch.org/get-started/locally/) +- pillow==8.2.0 +- opencv-python==4.5.2.54 +- numpy==1.22.0 +- matplotlib==3.5.1 +- tensorboard==2.5.0 +-大概就可以使用了,如果出现版本不兼容,灵活调整 +**注** +- 如果要使用GPU训练,请先安装环境 +- 显卡驱动、CUDA、CUDNN \ No newline at end of file diff --git a/example/vision/face_detection/README.md b/example/vision/face_detection/README.md index 784ae33bd4fb8bb94b1cf924d092ce644e8cc0c1..427bd4694661a06bd07bda52b2627b0fbcdc28c8 100644 --- a/example/vision/face_detection/README.md +++ b/example/vision/face_detection/README.md @@ -33,7 +33,7 @@ ## 2 运行前的准备 -* 请确保你已经下载了 [凌智视觉模块人脸检测模型](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.0/LZ-RetinaFace.rknn) +* 请确保你已经下载了 [凌智视觉模块人脸检测模型](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.3/LZ-RetinaFace-2024-1121-1627.rknn) ## 3 在凌智视觉模块上部署模型 diff --git a/example/vision/face_detection/images/img-1.png b/example/vision/face_detection/images/img-1.png new file mode 100644 index 0000000000000000000000000000000000000000..f224ef199470251b43ef28220c2fd1a7a7448e04 Binary files /dev/null and b/example/vision/face_detection/images/img-1.png differ diff --git a/example/vision/face_detection/python/README.md b/example/vision/face_detection/python/README.md index 6495c8d14473e8db3ad10b9637a51eaccc372c66..1264cd36b67b1f018afc29bcdc55e0e1b4a7c955 100644 --- a/example/vision/face_detection/python/README.md +++ b/example/vision/face_detection/python/README.md @@ -278,7 +278,10 @@ python test_retina_face.py LZ-RetinaFace.rknn 连接凌智视觉模块图片传输助手[凌智视觉模块图片传输助手下载地址](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.0/LockzhinerVisionModuleImageFetcher.exe)后,选择连接设备 -![](../images/img.png) +![](../images/img-1.png) + + + 运行程序后,屏幕上开始打印矩形框信息和置信度,并在一段时间后输出 FPS 值 diff --git a/example/vision/face_detection/python/test_retina_face_latest.py b/example/vision/face_detection/python/test_retina_face_latest.py new file mode 100644 index 0000000000000000000000000000000000000000..a8865d4766d4febd3eedcbad6dd15d467e830661 --- /dev/null +++ b/example/vision/face_detection/python/test_retina_face_latest.py @@ -0,0 +1,78 @@ +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 + + +def predict_video(face_det_model, width, height): + edit = Edit() + edit.start_and_accept_connection() + + 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) + + 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 = 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 + score = result.score + print( + f"(x,y,w,h,score): [{box.x},{box.y},{box.width},{box.height},{score}]" + ) + vis_mat = visualize(mat, results) + edit.print(vis_mat) + 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