diff --git a/README.md b/README.md index 126e24cdff67895a70850f8a421467a603574265..94c7645a4219ecd24b254ef69252a54cfae5ddf0 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,13 @@ Lockzhiner Vision Module 拥有丰富的 IO 接口,其接口图片如下图所 这一部分教程将引导你从零开始训练并部署各种深度学习模型,包括分类模型、检测模型、分割模型和 OCR 模型。通过这些实战教程,你将学习到如何利用 Lockzhiner Vision Module 的高性能计算能力以及借助飞桨生态系统的丰富资源,轻松实现视觉任务的训练和部署。 +### 👍 OpenCV 案例 + +OpenCV 是一个开源的计算机视觉库,它提供了一组功能强大的函数,用于处理图像和视频,并进行各种图像处理和计算机视觉任务。它由一系列C函数和少量C++类构成,并提供了Python接口。 + +* [凌智视觉模块 OpenCV 圆形检测部署指南](example/opencv_example/opencv_circle_detection) +* [凌智视觉模块 OpenCV 边缘检测案例](example/opencv_example/opencv_edge_detection) + ### 👍 目标分类案例 目标分类(Object Classification)是深度学习中的一项核心任务,旨在为输入图像分配一个或多个类别标签。这是计算机视觉的基础问题之一,主要用于识别和区分图像中的主要物体类别,而不关心物体的位置或数量。一般来说,目标分类任务的标注过程比较轻松,适合不需要知道目标位置的场景。 @@ -89,6 +96,7 @@ Lockzhiner Vision Module 拥有丰富的 IO 接口,其接口图片如下图所 * [凌智视觉模块猫狗分类部署指南](example/special/cat_and_dog_classification) * [凌智视觉模块花卉分类部署指南](example/special/flower_classfication/) * [凌智视觉模块口罩佩戴分类部署指南](example/special/maskwear_classfication) + ### 👍 目标检测案例 目标检测(Object Detection)是深度学习中计算机视觉领域的重要任务之一,旨在识别图像或视频中所有感兴趣的物体,并准确地定位这些物体的边界框(Bounding Box)。与目标分类不同,目标检测不仅需要预测物体的类别,还需要标注它们在图像中的位置。一般来说,目标检测任务的标注过程比较复杂,适合既需要对目标进行分类,有需要对目标进行定位的场景。 @@ -108,6 +116,7 @@ Lockzhiner Vision Module 拥有丰富的 IO 接口,其接口图片如下图所 * [凌智视觉模块人脸检测部署指南](example/vision/face_detection) * [凌智视觉模块人脸识别部署指南](example/vision/face_recognition) +* [凌智视觉模块人脸识别系统部署指南](example/special/face_system) ## 🐛 Bug反馈 diff --git a/example/opencv_example/opencv_circle_detection/README.md b/example/opencv_example/opencv_circle_detection/README.md new file mode 100644 index 0000000000000000000000000000000000000000..70a3705221e9006eda84de2d3502529de3662cb1 --- /dev/null +++ b/example/opencv_example/opencv_circle_detection/README.md @@ -0,0 +1,40 @@ +

凌智视觉模块 OpenCV 圆形检测部署指南

+ +发布版本:V0.0.0 + +日期:2024-11-29 + +文件密级:□绝密 □秘密 □内部资料 ■公开 + +--- + +**免责声明** + +本文档按**现状**提供,福州凌睿智捷电子有限公司(以下简称**本公司**)不对本文档中的任何陈述、信息和内容的准确性、可靠性、完整性、适销性、适用性及非侵权性提供任何明示或暗示的声明或保证。本文档仅作为使用指导的参考。 + +由于产品版本升级或其他原因,本文档可能在未经任何通知的情况下不定期更新或修改。 + +**读者对象** + +本教程适用于以下工程师: + +- 技术支持工程师 +- 软件开发工程师 + +**修订记录** + +| **日期** | **版本** | **作者** | **修改说明** | +|:-----------| -------- |--------| ------------ | +| 2024/11/29 | 0.0.0 | 钟海滨 | 初始版本 | + +## 1 简介 + +在计算机视觉领域,圆形检测是一项常见的任务,广泛应用于工业检测、医疗影像分析、自动驾驶等多个领域。准确地检测圆形不仅可以提高系统的自动化程度,还能显著提升工作效率。本文将详细介绍如何使用 OpenCV 库在凌智视觉模块上实现高效的圆形检测功能。 + +## 2 在凌智视觉模块上部署圆形检测案例 + +下载模型后,请参考以下教程使用 Python 在凌智视觉模块上部署分类模型例程: + +- [凌智视觉模块 OpenCV 圆形检测 Python 部署指南](./python) + + diff --git a/example/opencv_example/opencv_circle_detection/python/README.md b/example/opencv_example/opencv_circle_detection/python/README.md new file mode 100644 index 0000000000000000000000000000000000000000..1b2fb227dd234f104f8876c442d8e389d4104541 --- /dev/null +++ b/example/opencv_example/opencv_circle_detection/python/README.md @@ -0,0 +1,171 @@ +

凌智视觉模块 OpenCV 圆形检测 Python 部署指南

+ +发布版本:V0.0.0 + +日期:2024-11-29 + +文件密级:□绝密 □秘密 □内部资料 ■公开 + +--- + +**免责声明** + +本文档按**现状**提供,福州凌睿智捷电子有限公司(以下简称**本公司**)不对本文档中的任何陈述、信息和内容的准确性、可靠性、完整性、适销性、适用性及非侵权性提供任何明示或暗示的声明或保证。本文档仅作为使用指导的参考。 + +由于产品版本升级或其他原因,本文档可能在未经任何通知的情况下不定期更新或修改。 + +**读者对象** + +本教程适用于以下工程师: + +- 技术支持工程师 +- 软件开发工程师 + +**修订记录** + +| **日期** | **版本** | **作者** | **修改说明** | +|:-----------| -------- |--------| ------------ | +| 2024/11/29 | 0.0.0 | 钟海滨 | 初始版本 | + +## 1 简介 + +在计算机视觉领域,圆形检测是一项常见的任务,广泛应用于工业检测、医疗影像分析、自动驾驶等多个领域。准确地检测圆形不仅可以提高系统的自动化程度,还能显著提升工作效率。 本文将详细介绍如何使用 OpenCV 库在凌智视觉模块上实现高效的圆形检测功能。 + + +## 2 Python API 文档 + +```markdown +def cvtColor(src, code, dstCn=0): + """ + 转换图像颜色空间。 + 参数: + - src: 输入图像。 + - code: 颜色空间转换代码。 + - dstCn: 目标图像的通道数,如果为0则根据code自动确定。 + 返回: + - 转换后的图像。 + """ + return cv2.cvtColor(src, code, dstCn) + +def GaussianBlur(src, ksize, sigmaX, sigmaY=0, borderType=cv2.BORDER_DEFAULT): + """ + 使用高斯滤波模糊图像。 + 参数: + - src: 输入图像。 + - ksize: 滤波器的大小,必须是正奇数。 + - sigmaX: 水平方向的标准差。 + - sigmaY: 垂直方向的标准差,如果为0则与sigmaX相同。 + - borderType: 边界处理方式,默认为cv2.BORDER_DEFAULT。 + 返回: + - 模糊后的图像。 + """ + temp_ksize = convert2size(ksize) + return cv2.GaussianBlur(src, temp_ksize, sigmaX, sigmaY, borderType) + +def HoughCircles(image, method, dp, minDist, param1, param2, minRadius, maxRadius): + """ + 使用霍夫变换检测图像中的圆。 + 参数: + - image: 输入图像,应为8位单通道(灰度)图像。 + - method: 检测方法,通常为cv2.HOUGH_GRADIENT。 + - dp: 累加器分辨率与图像分辨率的反比。 + - minDist: 检测到的圆的中心之间的最小距离。 + - param1: 梯度值阈值。 + - param2: 累加器阈值。 + - minRadius: 最小圆半径。 + - maxRadius: 最大圆半径。 + 返回: + - 检测到的圆的数组,每个圆包含三个值:圆心坐标(x, y)和半径。 + """ + return [cv2.HoughCircles( + image, method, dp, minDist, param1, param2, minRadius, maxRadius + )] + +def circle(img, center, radius, color, thickness=1, lineType=8, shift=0): + """ + 在图像上绘制圆。 + 参数: + - img: 输入图像。 + - center: 圆心坐标。 + - radius: 圆的半径。 + - color: 圆的颜色,为一个包含BGR值的元组。 + - thickness: 圆轮廓的厚度,正值表示轮廓厚度,负值表示填充圆。 + - lineType: 线条类型,默认为8连接线。 + - shift: 圆心坐标和半径的缩放比例,默认为0表示无缩放。 + """ + temp_center = convert2point(center) + temp_color = convert2scalar(color) + cv2.circle(img, temp_center, radius, temp_color, thickness, lineType, shift) + +``` + +## 3 在凌智视觉模块上进行边缘检测案例 + +为了方便大家入手,我们提供了 OpenCV 圆形检测的 Python 例程。该程序可以使用摄像头进行端到端推理。 +**测试图片下载链接**:[圆形检测图片](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.4/circle.png) + +```python +import lockzhiner_vision_module.cv2 as cv2 +# 读取图像 +image_path = 'circle.png' +img = cv2.imread(image_path) + +if img is None: + print("Error: Image not loaded.") +else: + # 转换为灰度图像 + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + + # 使用高斯模糊减少噪声 + blurred = cv2.GaussianBlur(gray, (9, 9), 2) + + # 使用 HoughCircles 检测圆形 + circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, dp=1, minDist=50, + param1=50, param2=30, minRadius=0, maxRadius=0) + if circles is not None: + # 在图像上绘制检测到的圆 + for i in circles[0]: + center = (int(i[0]), int(i[1])) + radius = int(i[2]) + + # 绘制圆心 + cv2.circle(img, center, 1, (0, 100, 100), 3) + + # 绘制圆 + cv2.circle(img, center, radius, (0, 255, 0), 3) + + # # 保存结果图像 + cv2.imwrite('img_circles.png', img) + +``` +## 4 上传并测试 Python 程序 + +参考 [连接设备指南](../../../../docs/introductory_tutorial/connect_device_using_ssh.md) 正确连接 Lockzhiner Vision Module 设备。 + +![](../../../../docs/introductory_tutorial/images/connect_device_using_ssh/ssh_success.png) + +请使用 Electerm Sftp 依次上传以下文件: + +- 进入存放 **test_hough_circle.py** 脚本文件的目录,将 **test_hough_circle.py** 上传到 Lockzhiner Vision Module +- 进入存放 **待检测图片** 存放的目录,将 **待检测图片** 上传到 Lockzhiner Vision Module + +上传文件 +![](./images/ssh.png) + +请使用 Electerm Ssh 并在命令行中执行以下命令: + +```bash +python test_hough_circle.py +``` + +运行程序后,屏幕上输出 +![](./images/img.png) +下载结果 +![](./images/result.png) +圆形检测原图 +![](./images/circle.png) +圆形检测结果图片 +![](./images/img_circles.png) + + + diff --git a/example/opencv_example/opencv_circle_detection/python/images/circle.png b/example/opencv_example/opencv_circle_detection/python/images/circle.png new file mode 100644 index 0000000000000000000000000000000000000000..bc0b5c320099a6fa8ba58004efad59a10825c87a Binary files /dev/null and b/example/opencv_example/opencv_circle_detection/python/images/circle.png differ diff --git a/example/opencv_example/opencv_circle_detection/python/images/img.png b/example/opencv_example/opencv_circle_detection/python/images/img.png new file mode 100644 index 0000000000000000000000000000000000000000..2c047655beddc6f7f822bdf096761cc9353210c8 Binary files /dev/null and b/example/opencv_example/opencv_circle_detection/python/images/img.png differ diff --git a/example/opencv_example/opencv_circle_detection/python/images/img_circles.png b/example/opencv_example/opencv_circle_detection/python/images/img_circles.png new file mode 100644 index 0000000000000000000000000000000000000000..577048a4321ec150540646bcc400407827218cfb Binary files /dev/null and b/example/opencv_example/opencv_circle_detection/python/images/img_circles.png differ diff --git a/example/opencv_example/opencv_circle_detection/python/images/result.png b/example/opencv_example/opencv_circle_detection/python/images/result.png new file mode 100644 index 0000000000000000000000000000000000000000..d4b20f7159e447e5acab52add3f6b8727afe5f00 Binary files /dev/null and b/example/opencv_example/opencv_circle_detection/python/images/result.png differ diff --git a/example/opencv_example/opencv_circle_detection/python/images/ssh.png b/example/opencv_example/opencv_circle_detection/python/images/ssh.png new file mode 100644 index 0000000000000000000000000000000000000000..15072073dc5d93e2cd87451ff75c008bad07e311 Binary files /dev/null and b/example/opencv_example/opencv_circle_detection/python/images/ssh.png differ diff --git a/example/opencv_example/opencv_circle_detection/python/test_hough_circle.py b/example/opencv_example/opencv_circle_detection/python/test_hough_circle.py new file mode 100644 index 0000000000000000000000000000000000000000..72e452899562192a40c22f29d3358f35832ceb18 --- /dev/null +++ b/example/opencv_example/opencv_circle_detection/python/test_hough_circle.py @@ -0,0 +1,25 @@ +import lockzhiner_vision_module.cv2 as cv2 +# 读取图像 +image_path = 'circle.png' +img = cv2.imread(image_path) +if img is None: + print("Error: Image not loaded.") +else: + # 转换为灰度图像 + gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + # 使用高斯模糊减少噪声 + blurred = cv2.GaussianBlur(gray, (9, 9), 2) + # 使用 HoughCircles 检测圆形 + circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, dp=1, minDist=50, + param1=50, param2=30, minRadius=0, maxRadius=0) + if circles is not None: + # 在图像上绘制检测到的圆 + for i in circles[0]: + center = (int(i[0]), int(i[1])) + radius = int(i[2]) + # 绘制圆心 + cv2.circle(img, center, 1, (0, 100, 100), 3) + # 绘制圆 + cv2.circle(img, center, radius, (0, 255, 0), 3) + # # 保存结果图像 + cv2.imwrite('img_circles.png', img) diff --git a/example/opencv_example/opencv_edge_detection/README.md b/example/opencv_example/opencv_edge_detection/README.md new file mode 100644 index 0000000000000000000000000000000000000000..623dca5727b54466dad4a1c89129779a95a9e7a4 --- /dev/null +++ b/example/opencv_example/opencv_edge_detection/README.md @@ -0,0 +1,39 @@ +

凌智视觉模块 OpenCV 边缘检测部署指南

+ +发布版本:V0.0.0 + +日期:2024-11-29 + +文件密级:□绝密 □秘密 □内部资料 ■公开 + +--- + +**免责声明** + +本文档按**现状**提供,福州凌睿智捷电子有限公司(以下简称**本公司**)不对本文档中的任何陈述、信息和内容的准确性、可靠性、完整性、适销性、适用性及非侵权性提供任何明示或暗示的声明或保证。本文档仅作为使用指导的参考。 + +由于产品版本升级或其他原因,本文档可能在未经任何通知的情况下不定期更新或修改。 + +**读者对象** + +本教程适用于以下工程师: + +- 技术支持工程师 +- 软件开发工程师 + +**修订记录** + +| **日期** | **版本** | **作者** | **修改说明** | +|:-----------| -------- |--------| ------------ | +| 2024/11/29 | 0.0.0 | 钟海滨 | 初始版本 | + +## 1 简介 + +在现代计算机视觉应用中,边缘检测是一项基础而重要的任务,它能够帮助系统理解和解析图像中的关键结构信息。边缘检测技术广泛应用于图像分割、目标识别、特征提取等多个领域。 +本指南将详细介绍如何使用OpenCV库在凌智视觉模块上部署高效的边缘检测算法,旨在为开发者提供一个清晰、实用的操作步骤。 + +## 2 在凌智视觉模块上部署边缘检测案例 + +- [凌智视觉模块 OpenCV 边缘检测 Python 部署指南](./python/README.md) + + diff --git a/example/opencv_example/opencv_edge_detection/python/README.md b/example/opencv_example/opencv_edge_detection/python/README.md new file mode 100644 index 0000000000000000000000000000000000000000..efaa3afaaaba4c88aada9ef1c17c3b96315f1279 --- /dev/null +++ b/example/opencv_example/opencv_edge_detection/python/README.md @@ -0,0 +1,145 @@ +

凌智视觉模块 OpenCV 边缘检测 Python 部署指南

+ +发布版本:V0.0.0 + +日期:2024-11-29 + +文件密级:□绝密 □秘密 □内部资料 ■公开 + +--- + +**免责声明** + +本文档按**现状**提供,福州凌睿智捷电子有限公司(以下简称**本公司**)不对本文档中的任何陈述、信息和内容的准确性、可靠性、完整性、适销性、适用性及非侵权性提供任何明示或暗示的声明或保证。本文档仅作为使用指导的参考。 + +由于产品版本升级或其他原因,本文档可能在未经任何通知的情况下不定期更新或修改。 + +**读者对象** + +本教程适用于以下工程师: + +- 技术支持工程师 +- 软件开发工程师 + +**修订记录** + +| **日期** | **版本** | **作者** | **修改说明** | +|:-----------| -------- |--------| ------------ | +| 2024/11/29 | 0.0.0 | 钟海滨 | 初始版本 | + +## 1 简介 + +在现代计算机视觉应用中,边缘检测是一项基础而重要的任务,它能够帮助系统理解和解析图像中的关键结构信息。边缘检测技术广泛应用于图像分割、目标识别、特征提取等多个领域。 +本指南将详细介绍如何使用OpenCV库在凌智视觉模块上部署高效的边缘检测算法,旨在为开发者提供一个清晰、实用的操作步骤。 + + +## 2 Python API 文档 + +```markdown +def GaussianBlur(src, ksize, sigmaX, sigmaY=0, borderType=cv2.BORDER_DEFAULT): + """ + 对图像进行高斯模糊处理。 + 参数: + - src: 输入图像。 + - ksize: 高斯核的大小,可以是一个整数或两个整数的元组。 + - sigmaX: 高斯核在X方向上的标准差。 + - sigmaY: 高斯核在Y方向上的标准差,默认为0。 + - borderType: 图像边界的处理方式,默认为cv2.BORDER_DEFAULT。 + 返回: + - 模糊处理后的图像。 + """ + # 将ksize转换为两个整数的元组 + temp_ksize = convert2size(ksize) + # 使用OpenCV的GaussianBlur函数进行高斯模糊处理 + return cv2.GaussianBlur(src, temp_ksize, sigmaX, sigmaY, borderType) + +def threshold(src, thresh, maxval, type): + """ + 对图像进行阈值处理。 + 参数: + - src: 输入图像。 + - thresh: 阈值。 + - maxval: 最大值,应用于type所指定的阈值类型。 + - type: 阈值处理的类型。 + 返回: + - 计算得到的阈值。 + - 阈值处理后的图像。 + """ + # 使用OpenCV的threshold函数进行阈值处理 + computed_threshold, dst = cv2.threshold(src, thresh, maxval, type) + return computed_threshold, dst + +def Canny(img, threshold1, threshold2, apertureSize=3, L2gradient=False): + """ + 使用Canny算法检测图像中的边缘。 + 参数: + - img: 输入图像。 + - threshold1: 第一个阈值,用于边缘检测。 + - threshold2: 第二个阈值,用于边缘检测。 + - apertureSize: Sobel算子的孔径大小,默认为3。 + - L2gradient: 一个布尔值,表示是否使用更精确的L2范数进行梯度计算,默认为False。 + 返回: + - 边缘检测后的图像。 + """ + # 使用OpenCV的Canny函数进行边缘检测 + return cv2.Canny(img, threshold1, threshold2, apertureSize, L2gradient) + +``` + + +## 3 在凌智视觉模块上进行边缘检测案例 + +为了快速上手,我们提供了边缘检测案例 +**边缘检测图片下载链接:**[边缘检测图片](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.4/car.png) +```python +import lockzhiner_vision_module.cv2 as cv2 +# 读取图片 +image = cv2.imread('car.png') +# 检查图像是否成功读取 +if image is None: + print("Error: Unable to load image.") + exit() +# 转换为灰度图像 +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) +# 高斯模糊 +blurred = cv2.GaussianBlur(gray, (3, 3), 0) +# 阈值操作 +_, thresholded = cv2.threshold(blurred, 100, 255, cv2.THRESH_BINARY) +# 使用 Canny 边缘检测 +edges = cv2.Canny(thresholded, 20, 10) +# 保存边缘检测结果 +cv2.imwrite('edges.png', edges) +``` +## 4 上传并测试 Python 程序 + +参考 [连接设备指南](../../../docs/introductory_tutorial/connect_device_using_ssh.md) 正确连接 Lockzhiner Vision Module 设备。 + +![](../../../docs/introductory_tutorial/images/connect_device_using_ssh/ssh_success.png) + +请使用 Electerm Sftp 依次上传以下文件: + +- 进入存放 **test_canny.py** 脚本文件的目录,将 **test_canny.py** 上传到 Lockzhiner Vision Module +- 进入存放 **待检测图片** 存放的目录,将 **待检测图片** 上传到 Lockzhiner Vision Module + +上传文件 +![](./images/img.png) + +请使用 Electerm Ssh 并在命令行中执行以下命令: + +```bash +python test_canny.py +``` + +运行程序后,屏幕上输出 +![](./images/img_2.png) +下载结果 +![](./images/img_1.png) +边缘检测原图 +![](./images/car.png) +边缘检测结果图片 +![](./images/edges.png) +阈值操作结果图片 +![](./images/thresholded.png) + + + diff --git a/example/opencv_example/opencv_edge_detection/python/images/car.png b/example/opencv_example/opencv_edge_detection/python/images/car.png new file mode 100644 index 0000000000000000000000000000000000000000..1362b71e8bad37acb9b208d13b8e280bbff8d52d Binary files /dev/null and b/example/opencv_example/opencv_edge_detection/python/images/car.png differ diff --git a/example/opencv_example/opencv_edge_detection/python/images/edges.png b/example/opencv_example/opencv_edge_detection/python/images/edges.png new file mode 100644 index 0000000000000000000000000000000000000000..c28c472fa658836151e169f4b1e8332e47e6490f Binary files /dev/null and b/example/opencv_example/opencv_edge_detection/python/images/edges.png differ diff --git a/example/opencv_example/opencv_edge_detection/python/images/img.png b/example/opencv_example/opencv_edge_detection/python/images/img.png new file mode 100644 index 0000000000000000000000000000000000000000..bf6d465d1122afe0bfe758a70dc1b6d4f81caf01 Binary files /dev/null and b/example/opencv_example/opencv_edge_detection/python/images/img.png differ diff --git a/example/opencv_example/opencv_edge_detection/python/images/img_1.png b/example/opencv_example/opencv_edge_detection/python/images/img_1.png new file mode 100644 index 0000000000000000000000000000000000000000..a82d7a8d8d73fa414fbc3d3fff02011942e11367 Binary files /dev/null and b/example/opencv_example/opencv_edge_detection/python/images/img_1.png differ diff --git a/example/opencv_example/opencv_edge_detection/python/images/img_2.png b/example/opencv_example/opencv_edge_detection/python/images/img_2.png new file mode 100644 index 0000000000000000000000000000000000000000..7a672eb61b8b075f4465fb3316004127c95a1999 Binary files /dev/null and b/example/opencv_example/opencv_edge_detection/python/images/img_2.png differ diff --git a/example/opencv_example/opencv_edge_detection/python/images/thresholded.png b/example/opencv_example/opencv_edge_detection/python/images/thresholded.png new file mode 100644 index 0000000000000000000000000000000000000000..673fcf9354f60cb872e79c4b2cbb98f96f3d7c5c Binary files /dev/null and b/example/opencv_example/opencv_edge_detection/python/images/thresholded.png differ diff --git a/example/opencv_example/opencv_edge_detection/python/test_canny.py b/example/opencv_example/opencv_edge_detection/python/test_canny.py new file mode 100644 index 0000000000000000000000000000000000000000..c3b35382ab098335a1275ec7c6e1270361f76e0e --- /dev/null +++ b/example/opencv_example/opencv_edge_detection/python/test_canny.py @@ -0,0 +1,17 @@ +import lockzhiner_vision_module.cv2 as cv2 +# 读取图片 +image = cv2.imread('car.png') +# 检查图像是否成功读取 +if image is None: + print("Error: Unable to load image.") + exit() +# 转换为灰度图像 +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) +# 高斯模糊 +blurred = cv2.GaussianBlur(gray, (3, 3), 0) +# 阈值操作 +_, thresholded = cv2.threshold(blurred, 100, 255, cv2.THRESH_BINARY) +# 使用 Canny 边缘检测 +edges = cv2.Canny(thresholded, 20, 10) +# 保存边缘检测结果 +cv2.imwrite('edges.png', edges) diff --git a/example/opencv_example/opencv_line_detection/python/images/line.png b/example/opencv_example/opencv_line_detection/python/images/line.png new file mode 100644 index 0000000000000000000000000000000000000000..fc2d752cca85df63e1364bd71d214f1b26b9b232 Binary files /dev/null and b/example/opencv_example/opencv_line_detection/python/images/line.png differ diff --git a/example/opencv_example/opencv_line_detection/python/images/line_detected.png b/example/opencv_example/opencv_line_detection/python/images/line_detected.png new file mode 100644 index 0000000000000000000000000000000000000000..f7e3df3ac65acf8df4d59f0b50271b928f452225 Binary files /dev/null and b/example/opencv_example/opencv_line_detection/python/images/line_detected.png differ diff --git a/example/opencv_example/opencv_line_detection/python/test_line.py b/example/opencv_example/opencv_line_detection/python/test_line.py new file mode 100644 index 0000000000000000000000000000000000000000..c679edfa3b4b749b2a30a19d45e777de7ac908ae --- /dev/null +++ b/example/opencv_example/opencv_line_detection/python/test_line.py @@ -0,0 +1,72 @@ +# import cv2 +# +# # import lockzhiner_vision_module.cv2 as cv2 +# # 定义π +# pi = 3.14159265358979323846 +# +# # 读取图片 +# image = cv2.imread('img.png') +# +# # 转换为灰度图像 +# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) +# print("转换为灰度图像") +# +# # 高斯模糊 +# blurred = cv2.GaussianBlur(gray, (3, 3), 0) +# +# # 使用 Canny 边缘检测 +# edges = cv2.Canny(blurred, 30, 80) +# print("使用Canny边缘检测") +# cv2.imwrite('edges.png', edges) +# +# # 使用霍夫变换检测直线 +# # 注意这里使用的是 cv2.HoughLinesP 而不是 cv2.HoughLines +# lines = cv2.HoughLinesP(edges, 1, pi / 180, threshold=90, minLineLength=30, maxLineGap=10) +# print("使用霍夫变换检测直线") +# print(lines.shape) +# # 绘制检测到的直线 +# if lines is not None: +# for line in lines: +# # 注意这里的line格式,它是一个列表,包含了两个点坐标[x1, y1, x2, y2] +# x1, y1, x2, y2 = line[0] +# cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2) +# +# # 显示原始边缘检测图像和带有检测直线的图像 +# cv2.imshow('Edges', edges) +# cv2.imshow('Line Detection', image) +# cv2.waitKey(0) +# cv2.destroyAllWindows() +# +# # 保存带有检测直线的图片 +# cv2.imwrite('line_detected.png', image) +import lockzhiner_vision_module.cv2 as cv2 +# import cv2 + +# import cv2 +pi = 3.1415926535897932384626433832795 +# 读取图片 +image = cv2.imread('img.png') + +# 检查图片是否成功读 + +# 转换为灰度图像 +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) +print("转换为灰度图像") +# 使用高斯模糊减少噪声 +# blurred = cv2.GaussianBlur(gray, (5, 5), 0.5) +print("高斯滤波") +# 使用 Canny 边缘检测 +edges = cv2.Canny(gray, 50, 150) +print("使用Canny边缘检测") +# 使用霍夫变换检测直线 +lines = cv2.HoughLinesP(edges, 1, pi / 180, threshold=100, minLineLength=50, maxLineGap=20) +print("使用霍夫变换检测直线") +# 绘制检测到的直线 +if lines is not None: + # print(lines.tolist()) + for line in lines[0]: + # print(type(line)) + x1, y1, x2, y2 = line + cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2) + +cv2.imwrite('line_detected.png', image) diff --git a/example/opencv_example/opencv_rectangle_detection/README.md b/example/opencv_example/opencv_rectangle_detection/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8290a5126be6fb173e30c928794267edd4307e25 --- /dev/null +++ b/example/opencv_example/opencv_rectangle_detection/README.md @@ -0,0 +1,39 @@ +

凌智视觉模块 OpenCV 矩形检测部署指南

+ +发布版本:V0.0.0 + +日期:2024-11-29 + +文件密级:□绝密 □秘密 □内部资料 ■公开 + +--- + +**免责声明** + +本文档按**现状**提供,福州凌睿智捷电子有限公司(以下简称**本公司**)不对本文档中的任何陈述、信息和内容的准确性、可靠性、完整性、适销性、适用性及非侵权性提供任何明示或暗示的声明或保证。本文档仅作为使用指导的参考。 + +由于产品版本升级或其他原因,本文档可能在未经任何通知的情况下不定期更新或修改。 + +**读者对象** + +本教程适用于以下工程师: + +- 技术支持工程师 +- 软件开发工程师 + +**修订记录** + +| **日期** | **版本** | **作者** | **修改说明** | +|:-----------| -------- |--------| ------------ | +| 2024/11/29 | 0.0.0 | 钟海滨 | 初始版本 | + +## 1 简介 + +在现代计算机视觉应用中,矩形检测是一项基本但重要的任务,广泛应用于物体识别、场景理解、文档扫描等领域。本指南将详细介绍如何使用OpenCV库在凌智视觉模块上部署高效的矩形检测算法。 +## 2 在凌智视觉模块上部署矩形检测案例 + +下载模型后,请参考以下教程使用 Python 在凌智视觉模块上部署分类模型例程: + +- [凌智视觉模块 OpenCV 矩形检测 Python 部署指南](./python/README.md) + + diff --git a/example/opencv_example/opencv_rectangle_detection/python/README.md b/example/opencv_example/opencv_rectangle_detection/python/README.md new file mode 100644 index 0000000000000000000000000000000000000000..b736f036f56832ed593aa90f2669d2806cb43cfc --- /dev/null +++ b/example/opencv_example/opencv_rectangle_detection/python/README.md @@ -0,0 +1,195 @@ +

凌智视觉模块 OpenCV 矩形检测 Python 部署指南

+ +发布版本:V0.0.0 + +日期:2024-11-29 + +文件密级:□绝密 □秘密 □内部资料 ■公开 + +--- + +**免责声明** + +本文档按**现状**提供,福州凌睿智捷电子有限公司(以下简称**本公司**)不对本文档中的任何陈述、信息和内容的准确性、可靠性、完整性、适销性、适用性及非侵权性提供任何明示或暗示的声明或保证。本文档仅作为使用指导的参考。 + +由于产品版本升级或其他原因,本文档可能在未经任何通知的情况下不定期更新或修改。 + +**读者对象** + +本教程适用于以下工程师: + +- 技术支持工程师 +- 软件开发工程师 + +**修订记录** + +| **日期** | **版本** | **作者** | **修改说明** | +|:-----------| -------- |--------| ------------ | +| 2024/11/29 | 0.0.0 | 钟海滨 | 初始版本 | + +## 1 简介 + +在现代计算机视觉应用中,矩形检测是一项基本但重要的任务,广泛应用于物体识别、场景理解、文档扫描等领域。本指南将详细介绍如何使用OpenCV库在凌智视觉模块上部署高效的矩形检测算法。 本文将详细介绍如何使用 OpenCV 库在凌智视觉模块上实现高效的矩形检测功能。 + + +## 2 Python API 文档 + +```markdown + +def cvtColor(src, code, dstCn=0): + """ + 转换图像颜色空间。 + 参数: + - src: 输入图像。 + - code: 颜色空间转换代码。 + - dstCn: 目标图像的通道数,如果为0则根据code自动确定。 + 返回: + - 转换后的图像。 + """ + return cv2.cvtColor(src, code, dstCn) + +def GaussianBlur(src, ksize, sigmaX, sigmaY=0, borderType=cv2.BORDER_DEFAULT): + """ + 使用高斯滤波模糊图像。 + 参数: + - src: 输入图像。 + - ksize: 高斯核的大小,必须是奇数。 + - sigmaX: 高斯核在X方向的标准差。 + - sigmaY: 高斯核在Y方向的标准差,如果为0则与sigmaX相同。 + - borderType: 图像边界的处理方式,默认为cv2.BORDER_DEFAULT。 + 返回: + - 模糊后的图像。 + """ + temp_ksize = convert2size(ksize) + return cv2.GaussianBlur(src, temp_ksize, sigmaX, sigmaY, borderType) + +def threshold(src, thresh, maxval, type): + """ + 对图像应用阈值处理。 + 参数: + - src: 输入图像。 + - thresh: 阈值。 + - maxval: 阈值处理后的最大值。 + - type: 阈值处理的类型。 + 返回: + - 计算出的阈值。 + - 阈值处理后的图像。 + """ + computed_threshold, dst = cv2.threshold(src, thresh, maxval, type) + return computed_threshold, dst + +def drawContours(img, contours, contourIdx, color, thickness=1, lineType=8, shift=0): + """ + 在图像上绘制轮廓。 + 参数: + - img: 输入图像。 + - contours: 要绘制的轮廓列表。 + - contourIdx: 要绘制的轮廓索引,-1表示绘制所有轮廓。 + - color: 轮廓的颜色。 + - thickness: 轮廓线的厚度。 + - lineType: 轮廓线的类型。 + - shift: 轮廓坐标的小数位数。 + """ + temp_color = convert2scalar(color) + cv2.drawContours(img, contours, contourIdx, temp_color, thickness, lineType, shift) + +def findContours(image, mode, method, point=cv2.Point()): + """ + 在二值图像中检测轮廓。 + 参数: + - image: 输入的二值图像。 + - mode: 轮廓的检索模式。 + - method: 轮廓的近似方法。 + - point: 轮廓检索的起始点。 + 返回: + - 检测到的轮廓列表。 + - 轮廓的层次结构。 + """ + contours, hierarchy = cv2.findContours(image, mode, method, point) + return contours, hierarchy + +def arcLength(curve, closed): + """ + 计算曲线的长度。 + 参数: + - curve: 输入的曲线,由点序列组成。 + - closed: 曲线是否封闭。 + 返回: + - 曲线的长度。 + """ + return cv2.arcLength(curve, closed) + + +def approxPolyDP(curve, epsilon, closed): + """ + 使用Douglas-Peucker算法近似曲线。 + 参数: + - curve: 输入的曲线,由点序列组成。 + - epsilon: 近似精度,通常为原始曲线长度的百分之一。 + - closed: 曲线是否封闭。 + 返回: + - 近似后的曲线。 + """ + return cv2.approxPolyDP(curve, epsilon, closed) + +``` + +## 3 在凌智视觉模块上进行边缘检测案例 + +为了方便大家入手,我们提供了 OpenCV 矩形检测的 Python 例程。该程序可以使用摄像头进行端到端推理。 +**测试图片下载链接:**[矩形检测图片](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.4/rectangle.png) + +```python +import lockzhiner_vision_module.cv2 as cv2 +# 读取图像 +image = cv2.imread('rectangle.png') +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) +# 高斯模糊 +blurred = cv2.GaussianBlur(gray, (5, 5), 0) +# 二值化 +_, binary = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY_INV) +edges = cv2.Canny(binary, 30, 200) +contours, _ = cv2.findContours(edges.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) +for contour in contours: + # 近似轮廓 + # 计算轮廓周长 + epsilon = 0.02 * cv2.arcLength(contour, True) + # 将轮廓近似为多边形 + approx = cv2.approxPolyDP(contour, epsilon, True) + # 如果近似轮廓有4个顶点,则认为是矩形 + if len(approx) == 4: + cv2.drawContours(image, [approx], -1, (0, 255, 0), 2) +cv2.imwrite('img_rectangle.png', image) + +``` +## 4 上传并测试 Python 程序 + +参考 [连接设备指南](../../../../docs/introductory_tutorial/connect_device_using_ssh.md) 正确连接 Lockzhiner Vision Module 设备。 + +![](../../../../docs/introductory_tutorial/images/connect_device_using_ssh/ssh_success.png) + +请使用 Electerm Sftp 依次上传以下文件: + +- 进入存放 **test_rectangle.py** 脚本文件的目录,将 **test_rectangle.py** 上传到 Lockzhiner Vision Module +- 进入存放 **待检测图片** 存放的目录,将 **待检测图片** 上传到 Lockzhiner Vision Module + +上传文件 +![](./images/sftp.png) + +请使用 Electerm Ssh 并在命令行中执行以下命令: + +```bash +python test_rectangle.py +``` + +运行程序后,屏幕上输出 +![](./images/result.png) +下载结果 +![](./images/dowm_result.png) +矩形检测原图 +![](./images/rectangle.png) +矩形检测结果图片 +![](./images/results.png) + + + diff --git a/example/opencv_example/opencv_rectangle_detection/python/images/dowm_result.png b/example/opencv_example/opencv_rectangle_detection/python/images/dowm_result.png new file mode 100644 index 0000000000000000000000000000000000000000..7c86faea28d6b49a1d8e7d58718c04427f686869 Binary files /dev/null and b/example/opencv_example/opencv_rectangle_detection/python/images/dowm_result.png differ diff --git a/example/opencv_example/opencv_rectangle_detection/python/images/rectangle.png b/example/opencv_example/opencv_rectangle_detection/python/images/rectangle.png new file mode 100644 index 0000000000000000000000000000000000000000..6fe90303778242710cecf1e5734fe04b419589eb Binary files /dev/null and b/example/opencv_example/opencv_rectangle_detection/python/images/rectangle.png differ diff --git a/example/opencv_example/opencv_rectangle_detection/python/images/result.png b/example/opencv_example/opencv_rectangle_detection/python/images/result.png new file mode 100644 index 0000000000000000000000000000000000000000..85313072b0fa83e8bd9b0f1627930182c0a45a9f Binary files /dev/null and b/example/opencv_example/opencv_rectangle_detection/python/images/result.png differ diff --git a/example/opencv_example/opencv_rectangle_detection/python/images/results.png b/example/opencv_example/opencv_rectangle_detection/python/images/results.png new file mode 100644 index 0000000000000000000000000000000000000000..cddd3669c6373f8b4590235112835c80f1246173 Binary files /dev/null and b/example/opencv_example/opencv_rectangle_detection/python/images/results.png differ diff --git a/example/opencv_example/opencv_rectangle_detection/python/images/sftp.png b/example/opencv_example/opencv_rectangle_detection/python/images/sftp.png new file mode 100644 index 0000000000000000000000000000000000000000..ef67eccfed06a391f760556cd3d33a9ddc2c24c1 Binary files /dev/null and b/example/opencv_example/opencv_rectangle_detection/python/images/sftp.png differ diff --git a/example/opencv_example/opencv_rectangle_detection/python/test_rectangle.py b/example/opencv_example/opencv_rectangle_detection/python/test_rectangle.py new file mode 100644 index 0000000000000000000000000000000000000000..076800f1aad341e6d1cb777cbbc6539365f67409 --- /dev/null +++ b/example/opencv_example/opencv_rectangle_detection/python/test_rectangle.py @@ -0,0 +1,20 @@ +import lockzhiner_vision_module.cv2 as cv2 +# 读取图像 +image = cv2.imread('rectangle.png') +gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) +# 高斯模糊 +blurred = cv2.GaussianBlur(gray, (5, 5), 0) +# 二值化 +_, binary = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY_INV) +edges = cv2.Canny(binary, 30, 200) +contours, _ = cv2.findContours(edges.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) +for contour in contours: + # 近似轮廓 + # 计算轮廓周长 + epsilon = 0.02 * cv2.arcLength(contour, True) + # 将轮廓近似为多边形 + approx = cv2.approxPolyDP(contour, epsilon, True) + # 如果近似轮廓有4个顶点,则认为是矩形 + if len(approx) == 4: + cv2.drawContours(image, [approx], -1, (0, 255, 0), 2) +cv2.imwrite('img_rectangle.png', image) \ No newline at end of file diff --git a/example/special/qr_code_recognition/README.md b/example/special/qr_code_recognition/README.md index bf0e834f4264df1c2aff472ae5451627c83f4bb2..99e1bc756df26cbf491b3cc00c689526d1026aa7 100644 --- a/example/special/qr_code_recognition/README.md +++ b/example/special/qr_code_recognition/README.md @@ -37,7 +37,7 @@ - [凌智视觉模块二维码识别 Python 部署指南](./python) -## 4 模型性能指标 +## 3 模型性能指标 以下测试数据为模型执行 Predict 函数运行 1000 次耗时的平均时间