diff --git a/README.md b/README.md index 94c7645a4219ecd24b254ef69252a54cfae5ddf0..b60c36f1486b1e6c9812e6fa7e8e8bcc9c7c6d10 100644 --- a/README.md +++ b/README.md @@ -82,10 +82,13 @@ Lockzhiner Vision Module 拥有丰富的 IO 接口,其接口图片如下图所 ### 👍 OpenCV 案例 -OpenCV 是一个开源的计算机视觉库,它提供了一组功能强大的函数,用于处理图像和视频,并进行各种图像处理和计算机视觉任务。它由一系列C函数和少量C++类构成,并提供了Python接口。 +OpenCV 是一个开源的计算机视觉库,它提供了一组功能强大的函数,用于处理图像和视频,并进行各种图像处理和计算机视觉任务。它由一系列 C++ 类构成,并提供了 Python 接口。 -* [凌智视觉模块 OpenCV 圆形检测部署指南](example/opencv_example/opencv_circle_detection) * [凌智视觉模块 OpenCV 边缘检测案例](example/opencv_example/opencv_edge_detection) +* [凌智视觉模块 OpenCV 直线检测部署指南](example/opencv_example/opencv_line_detection) +* [凌智视觉模块 OpenCV 三角形检测部署指南](example/opencv_example/opencv_triangle_detection) +* [凌智视觉模块 OpenCV 矩形检测部署指南](example/opencv_example/opencv_rectangle_detection) +* [凌智视觉模块 OpenCV 圆形检测部署指南](example/opencv_example/opencv_circle_detection) ### 👍 目标分类案例 diff --git a/example/opencv_example/opencv_circle_detection/python/README.md b/example/opencv_example/opencv_circle_detection/python/README.md index 1b2fb227dd234f104f8876c442d8e389d4104541..7cf822d27ab8e5188a4d0cfde01d8e63b942c036 100644 --- a/example/opencv_example/opencv_circle_detection/python/README.md +++ b/example/opencv_example/opencv_circle_detection/python/README.md @@ -34,7 +34,8 @@ ## 2 Python API 文档 -```markdown +```python + def cvtColor(src, code, dstCn=0): """ 转换图像颜色空间。 @@ -45,7 +46,6 @@ def cvtColor(src, code, dstCn=0): 返回: - 转换后的图像。 """ - return cv2.cvtColor(src, code, dstCn) def GaussianBlur(src, ksize, sigmaX, sigmaY=0, borderType=cv2.BORDER_DEFAULT): """ @@ -59,8 +59,6 @@ def GaussianBlur(src, ksize, sigmaX, sigmaY=0, 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): """ @@ -77,9 +75,6 @@ def HoughCircles(image, method, dp, minDist, param1, param2, minRadius, maxRadiu 返回: - 检测到的圆的数组,每个圆包含三个值:圆心坐标(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): """ @@ -93,49 +88,73 @@ def circle(img, center, radius, color, thickness=1, lineType=8, shift=0): - 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 +from lockzhiner_vision_module.cv2 import VideoCapture 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) +from lockzhiner_vision_module.edit import Edit +import time +import sys +pi = 3.14159265358979323846 +if __name__ == "__main__": + args = sys.argv + if len(args) != 3: + print("Need model path. Example: python test_capture.py width height") + exit(1) + + edit = Edit() + edit.start_and_accept_connection() + + video_capture = VideoCapture() + video_capture.set_width(int(args[1])) + video_capture.set_height(int(args[2])) + 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): + start_time = time.time() + ret, mat = video_capture.read() + if ret is False: + continue + end_time = time.time() + # 转换为灰度图像 + # 转换为灰度图像 + gray = cv2.cvtColor(mat, 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=120, maxRadius=250) + # print(circles) + if circles is not None: + # 在图像上绘制检测到的圆 + for i in circles[0]: + center = (int(i[0]), int(i[1])) + radius = int(i[2]) + cv2.putText(mat, "circle", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + # 绘制圆心 + cv2.circle(mat, center, 1, (0, 100, 100), 3) + + # 绘制圆 + cv2.circle(mat, center, radius, (0, 255, 0), 3) + + edit.print(mat) + total_time_ms += end_time - start_time + read_index += 1 + print(f"FPS is {1.0 / (total_time_ms/read_index)}") ``` ## 4 上传并测试 Python 程序 @@ -147,25 +166,24 @@ else: 请使用 Electerm Sftp 依次上传以下文件: - 进入存放 **test_hough_circle.py** 脚本文件的目录,将 **test_hough_circle.py** 上传到 Lockzhiner Vision Module -- 进入存放 **待检测图片** 存放的目录,将 **待检测图片** 上传到 Lockzhiner Vision Module 上传文件 -![](./images/ssh.png) +![](./images/img_2.png) 请使用 Electerm Ssh 并在命令行中执行以下命令: ```bash -python test_hough_circle.py +python test_hough_circle.py 640 480 ``` 运行程序后,屏幕上输出 -![](./images/img.png) -下载结果 -![](./images/result.png) -圆形检测原图 -![](./images/circle.png) + +![](./images/img_3.png) + + 圆形检测结果图片 -![](./images/img_circles.png) + +![](./images/circle_img.png) diff --git a/example/opencv_example/opencv_circle_detection/python/images/circle_img.png b/example/opencv_example/opencv_circle_detection/python/images/circle_img.png new file mode 100644 index 0000000000000000000000000000000000000000..71b2d9f5326aa9e384a406631cfedf352061d999 Binary files /dev/null and b/example/opencv_example/opencv_circle_detection/python/images/circle_img.png differ diff --git a/example/opencv_example/opencv_circle_detection/python/images/img_1.png b/example/opencv_example/opencv_circle_detection/python/images/img_1.png new file mode 100644 index 0000000000000000000000000000000000000000..f74688e562892755003e0b90412ad2c6d69c1119 Binary files /dev/null and b/example/opencv_example/opencv_circle_detection/python/images/img_1.png differ diff --git a/example/opencv_example/opencv_circle_detection/python/images/img_2.png b/example/opencv_example/opencv_circle_detection/python/images/img_2.png new file mode 100644 index 0000000000000000000000000000000000000000..19415f3d71c88ac9cdd2b2a7ea271449d90d748c Binary files /dev/null and b/example/opencv_example/opencv_circle_detection/python/images/img_2.png differ diff --git a/example/opencv_example/opencv_circle_detection/python/images/img_3.png b/example/opencv_example/opencv_circle_detection/python/images/img_3.png new file mode 100644 index 0000000000000000000000000000000000000000..7c2616b9a277eb2c88fd8881aff99655f17e20d6 Binary files /dev/null and b/example/opencv_example/opencv_circle_detection/python/images/img_3.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 index 72e452899562192a40c22f29d3358f35832ceb18..bf8b9b1c5b51371ecd789a1a29c1c98e88edf2ba 100644 --- a/example/opencv_example/opencv_circle_detection/python/test_hough_circle.py +++ b/example/opencv_example/opencv_circle_detection/python/test_hough_circle.py @@ -1,25 +1,58 @@ +from lockzhiner_vision_module.cv2 import VideoCapture 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) +from lockzhiner_vision_module.edit import Edit +import time +import sys +pi = 3.14159265358979323846 +if __name__ == "__main__": + args = sys.argv + if len(args) != 3: + print("Need model path. Example: python test_capture.py width height") + exit(1) + + edit = Edit() + edit.start_and_accept_connection() + + video_capture = VideoCapture() + video_capture.set_width(int(args[1])) + video_capture.set_height(int(args[2])) + 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): + start_time = time.time() + ret, mat = video_capture.read() + if ret is False: + continue + end_time = time.time() + # 转换为灰度图像 + # 转换为灰度图像 + gray = cv2.cvtColor(mat, 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=120, maxRadius=250) + # print(circles) + if circles is not None: + # 在图像上绘制检测到的圆 + for i in circles[0]: + center = (int(i[0]), int(i[1])) + radius = int(i[2]) + cv2.putText(mat, "circle", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + # 绘制圆心 + cv2.circle(mat, center, 1, (0, 100, 100), 3) + + # 绘制圆 + cv2.circle(mat, center, radius, (0, 255, 0), 3) + + edit.print(mat) + total_time_ms += end_time - start_time + read_index += 1 + print(f"FPS is {1.0 / (total_time_ms/read_index)}") \ No newline at end of file diff --git a/example/opencv_example/opencv_edge_detection/README.md b/example/opencv_example/opencv_edge_detection/README.md index 623dca5727b54466dad4a1c89129779a95a9e7a4..7f5f99273a1c6632f1881d83bd4856f08502a131 100644 --- a/example/opencv_example/opencv_edge_detection/README.md +++ b/example/opencv_example/opencv_edge_detection/README.md @@ -30,7 +30,7 @@ ## 1 简介 在现代计算机视觉应用中,边缘检测是一项基础而重要的任务,它能够帮助系统理解和解析图像中的关键结构信息。边缘检测技术广泛应用于图像分割、目标识别、特征提取等多个领域。 -本指南将详细介绍如何使用OpenCV库在凌智视觉模块上部署高效的边缘检测算法,旨在为开发者提供一个清晰、实用的操作步骤。 +本指南将详细介绍如何使用 OpenCV 库在凌智视觉模块上部署高效的边缘检测算法,旨在为开发者提供一个清晰、实用的操作步骤。 ## 2 在凌智视觉模块上部署边缘检测案例 diff --git a/example/opencv_example/opencv_edge_detection/python/README.md b/example/opencv_example/opencv_edge_detection/python/README.md index efaa3afaaaba4c88aada9ef1c17c3b96315f1279..b6bf2dd07e40f60a81d34bde1a18a59a3f623e1f 100644 --- a/example/opencv_example/opencv_edge_detection/python/README.md +++ b/example/opencv_example/opencv_edge_detection/python/README.md @@ -30,12 +30,12 @@ ## 1 简介 在现代计算机视觉应用中,边缘检测是一项基础而重要的任务,它能够帮助系统理解和解析图像中的关键结构信息。边缘检测技术广泛应用于图像分割、目标识别、特征提取等多个领域。 -本指南将详细介绍如何使用OpenCV库在凌智视觉模块上部署高效的边缘检测算法,旨在为开发者提供一个清晰、实用的操作步骤。 +本指南将详细介绍如何使用 OpenCV 库在凌智视觉模块上部署高效的边缘检测算法,旨在为开发者提供一个清晰、实用的操作步骤。 ## 2 Python API 文档 -```markdown +```python def GaussianBlur(src, ksize, sigmaX, sigmaY=0, borderType=cv2.BORDER_DEFAULT): """ 对图像进行高斯模糊处理。 @@ -48,10 +48,6 @@ def GaussianBlur(src, ksize, sigmaX, sigmaY=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): """ @@ -65,10 +61,7 @@ def threshold(src, thresh, maxval, 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算法检测图像中的边缘。 @@ -81,16 +74,14 @@ def Canny(img, threshold1, threshold2, apertureSize=3, L2gradient=False): 返回: - 边缘检测后的图像。 """ - # 使用OpenCV的Canny函数进行边缘检测 - return cv2.Canny(img, threshold1, threshold2, apertureSize, L2gradient) - ``` ## 3 在凌智视觉模块上进行边缘检测案例 为了快速上手,我们提供了边缘检测案例 -**边缘检测图片下载链接:**[边缘检测图片](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.4/car.png) +**测试图片下载链接:**[边缘检测图片](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.4/car.png) + ```python import lockzhiner_vision_module.cv2 as cv2 # 读取图片 @@ -110,11 +101,12 @@ 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) +![](../../../../docs/introductory_tutorial/images/connect_device_using_ssh/ssh_success.png) 请使用 Electerm Sftp 依次上传以下文件: @@ -122,6 +114,7 @@ cv2.imwrite('edges.png', edges) - 进入存放 **待检测图片** 存放的目录,将 **待检测图片** 上传到 Lockzhiner Vision Module 上传文件 + ![](./images/img.png) 请使用 Electerm Ssh 并在命令行中执行以下命令: @@ -131,15 +124,21 @@ 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_line_detection/README.md b/example/opencv_example/opencv_line_detection/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e2a532499b34f75e002ae75ce384dd78dfa0794d --- /dev/null +++ b/example/opencv_example/opencv_line_detection/README.md @@ -0,0 +1,38 @@ +

凌智视觉模块 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_line_detection/python/README.md b/example/opencv_example/opencv_line_detection/python/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8eab3ba0d3e5f441486601dc55df5e46493acc3b --- /dev/null +++ b/example/opencv_example/opencv_line_detection/python/README.md @@ -0,0 +1,180 @@ +

凌智视觉模块 OpenCV 直线检测 Python 部署指南

+ +发布版本:V0.0.0 + +日期:2024-11-29 + +文件密级:□绝密 □秘密 □内部资料 ■公开 + +--- + +**免责声明** + +本文档按**现状**提供,福州凌睿智捷电子有限公司(以下简称**本公司**)不对本文档中的任何陈述、信息和内容的准确性、可靠性、完整性、适销性、适用性及非侵权性提供任何明示或暗示的声明或保证。本文档仅作为使用指导的参考。 + +由于产品版本升级或其他原因,本文档可能在未经任何通知的情况下不定期更新或修改。 + +**读者对象** + +本教程适用于以下工程师: + +- 技术支持工程师 +- 软件开发工程师 + +**修订记录** + +| **日期** | **版本** | **作者** | **修改说明** | +|:-----------| -------- |--------| ------------ | +| 2024/11/29 | 0.0.0 | 钟海滨 | 初始版本 | + +## 1 简介 + +直线检测是计算机视觉中的一个基本任务,广泛应用于图像处理、机器视觉和自动驾驶等领域。通过检测图像中的直线,可以实现道路边缘识别、物体边界检测等多种应用场景。本指南将详细介绍如何使用 OpenCV 库在凌智视觉模块上部署直线检测功能。 + +## 2 Python API 文档 + +```python +def cvtColor(src, code, dstCn=0): + """ + 转换图像的颜色空间。 + 参数: + - src: 输入图像。 + - code: 颜色空间转换的代码。 + - dstCn: 目标图像的通道数,默认为0。 + 返回: + - 颜色空间转换后的图像。 + """ + +def GaussianBlur(src, ksize, sigmaX, sigmaY=0, borderType=cv2.BORDER_DEFAULT): + """ + 对图像进行高斯模糊处理。 + 参数: + - src: 输入图像。 + - ksize: 高斯核的大小,可以是一个整数或两个整数的元组。 + - sigmaX: 高斯核在X方向上的标准差。 + - sigmaY: 高斯核在Y方向上的标准差,默认为0。 + - borderType: 图像边界的处理方式,默认为cv2.BORDER_DEFAULT。 + 返回: + - 模糊处理后的图像。 + """ + +def Canny(img, threshold1, threshold2, apertureSize=3, L2gradient=False): + """ + 使用Canny算法检测图像中的边缘。 + 参数: + - img: 输入图像。 + - threshold1: 第一个阈值,用于直线检测。 + - threshold2: 第二个阈值,用于直线检测。 + - apertureSize: Sobel算子的孔径大小,默认为3。 + - L2gradient: 一个布尔值,表示是否使用更精确的L2范数进行梯度计算,默认为False。 + 返回: + - 直线检测后的图像。 + """ + +def HoughLinesP(image, rho, theta, threshold, minLineLength=0, maxLineGap=0): + """ + 使用霍夫变换检测图像中的线段。 + 参数: + - image: 输入图像。 + - rho: 距离的分辨率。 + - theta: 角度的分辨率。 + - threshold: 累计器的阈值。 + - minLineLength: 最小线段长度,默认为0。 + - maxLineGap: 线段之间的最大间隔,默认为0。 + 返回: + - 检测到的线段信息列表。 + """ + + +``` + + +## 3 在凌智视觉模块上进行直线检测案例 + +为了快速上手,我们提供了直线检测案例 + +```python +from lockzhiner_vision_module.cv2 import VideoCapture +import lockzhiner_vision_module.cv2 as cv2 +from lockzhiner_vision_module.edit import Edit +import time +import sys +pi = 3.14159265358979323846 +if __name__ == "__main__": + args = sys.argv + if len(args) != 3: + print("Need model path. Example: python test_capture.py width height") + exit(1) + + edit = Edit() + edit.start_and_accept_connection() + + video_capture = VideoCapture() + video_capture.set_width(int(args[1])) + video_capture.set_height(int(args[2])) + 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): + start_time = time.time() + ret, mat = video_capture.read() + if ret is False: + continue + end_time = time.time() + # 转换为灰度图像 + gray = cv2.cvtColor(mat, cv2.COLOR_BGR2GRAY) + # 使用高斯模糊减少噪声 + blurred = cv2.GaussianBlur(gray, (5, 5), 1) + # 使用 Canny 边缘检测 + edges = cv2.Canny(gray, 50, 150) + # 使用霍夫变换检测直线 + lines = cv2.HoughLinesP(edges, 1, pi / 180, threshold=100, minLineLength=40, maxLineGap=40) + # 绘制检测到的直线 + if lines is not None: + for line in lines[0]: + x1, y1, x2, y2 = line + cv2.putText(mat, "line", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + cv2.line(mat, (x1, y1), (x2, y2), (0, 255, 0), 2) + + edit.print(mat) + total_time_ms += end_time - start_time + read_index += 1 + print(f"FPS is {1.0 / (total_time_ms/read_index)}") + +``` + +## 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_line.py** 脚本文件的目录,将 **test_line.py** 上传到 Lockzhiner Vision Module + +上传文件 + +![](./images/upload.png) + +请使用 Electerm Ssh 并在命令行中执行以下命令: + +```bash +python test_line.py 640 480 +``` + +运行程序后,屏幕上输出 + +![](./images/img.png) + +直线检测结果图片 + +![](./images/line_results.png) + + + + diff --git a/example/opencv_example/opencv_line_detection/python/images/dowmresults.png b/example/opencv_example/opencv_line_detection/python/images/dowmresults.png new file mode 100644 index 0000000000000000000000000000000000000000..603b895fbb50ff23a58f8aefc4f44f7c8f64d91d Binary files /dev/null and b/example/opencv_example/opencv_line_detection/python/images/dowmresults.png differ diff --git a/example/opencv_example/opencv_line_detection/python/images/img.png b/example/opencv_example/opencv_line_detection/python/images/img.png new file mode 100644 index 0000000000000000000000000000000000000000..90373fe2a10c15cba6a197774b4415e480662e3e Binary files /dev/null and b/example/opencv_example/opencv_line_detection/python/images/img.png differ diff --git a/example/opencv_example/opencv_line_detection/python/images/img_detected.png b/example/opencv_example/opencv_line_detection/python/images/img_detected.png new file mode 100644 index 0000000000000000000000000000000000000000..8211a5ef610e253dcbad7fe1aa92141ebf32a379 Binary files /dev/null and b/example/opencv_example/opencv_line_detection/python/images/img_detected.png differ diff --git a/example/opencv_example/opencv_line_detection/python/images/line.png b/example/opencv_example/opencv_line_detection/python/images/line.png index fc2d752cca85df63e1364bd71d214f1b26b9b232..dc3473432771503de73ffd3480d8c063d9e531a0 100644 Binary files a/example/opencv_example/opencv_line_detection/python/images/line.png 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 deleted file mode 100644 index f7e3df3ac65acf8df4d59f0b50271b928f452225..0000000000000000000000000000000000000000 Binary files a/example/opencv_example/opencv_line_detection/python/images/line_detected.png and /dev/null differ diff --git a/example/opencv_example/opencv_line_detection/python/images/line_results.png b/example/opencv_example/opencv_line_detection/python/images/line_results.png new file mode 100644 index 0000000000000000000000000000000000000000..992d1b12e7adab0b3dc84713a1e9a3965fae7a38 Binary files /dev/null and b/example/opencv_example/opencv_line_detection/python/images/line_results.png differ diff --git a/example/opencv_example/opencv_line_detection/python/images/run.png b/example/opencv_example/opencv_line_detection/python/images/run.png new file mode 100644 index 0000000000000000000000000000000000000000..4ee4df22882f1a9c26145945cb5e5d01fceb4447 Binary files /dev/null and b/example/opencv_example/opencv_line_detection/python/images/run.png differ diff --git a/example/opencv_example/opencv_line_detection/python/images/upload.png b/example/opencv_example/opencv_line_detection/python/images/upload.png new file mode 100644 index 0000000000000000000000000000000000000000..0413baa83f822f61009f7e82d8956f6eae69e3fb Binary files /dev/null and b/example/opencv_example/opencv_line_detection/python/images/upload.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 index c679edfa3b4b749b2a30a19d45e777de7ac908ae..a8911f355df4939c4ef1444791206a0b2da4a953 100644 --- a/example/opencv_example/opencv_line_detection/python/test_line.py +++ b/example/opencv_example/opencv_line_detection/python/test_line.py @@ -1,72 +1,50 @@ -# 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) +from lockzhiner_vision_module.cv2 import VideoCapture import lockzhiner_vision_module.cv2 as cv2 -# import cv2 +from lockzhiner_vision_module.edit import Edit +import time +import sys +pi = 3.14159265358979323846 +if __name__ == "__main__": + args = sys.argv + if len(args) != 3: + print("Need model path. Example: python test_capture.py width height") + exit(1) -# import cv2 -pi = 3.1415926535897932384626433832795 -# 读取图片 -image = cv2.imread('img.png') + edit = Edit() + edit.start_and_accept_connection() -# 检查图片是否成功读 + video_capture = VideoCapture() + video_capture.set_width(int(args[1])) + video_capture.set_height(int(args[2])) + if video_capture.open(0) is False: + print("Failed to open capture") + exit(1) -# 转换为灰度图像 -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) + while True: + read_index = 0 + total_time_ms = 0 + for i in range(30): + start_time = time.time() + ret, mat = video_capture.read() + if ret is False: + continue + end_time = time.time() + # 转换为灰度图像 + gray = cv2.cvtColor(mat, cv2.COLOR_BGR2GRAY) + # 使用高斯模糊减少噪声 + blurred = cv2.GaussianBlur(gray, (5, 5), 1) + # 使用 Canny 边缘检测 + edges = cv2.Canny(gray, 50, 150) + # 使用霍夫变换检测直线 + lines = cv2.HoughLinesP(edges, 1, pi / 180, threshold=100, minLineLength=40, maxLineGap=40) + # 绘制检测到的直线 + if lines is not None: + for line in lines[0]: + x1, y1, x2, y2 = line + cv2.putText(mat, "line", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + cv2.line(mat, (x1, y1), (x2, y2), (0, 255, 0), 2) -cv2.imwrite('line_detected.png', image) + edit.print(mat) + total_time_ms += end_time - start_time + read_index += 1 + print(f"FPS is {1.0 / (total_time_ms/read_index)}") \ No newline at end of file diff --git a/example/opencv_example/opencv_rectangle_detection/README.md b/example/opencv_example/opencv_rectangle_detection/README.md index 8290a5126be6fb173e30c928794267edd4307e25..2d9b97f15b1343e5973e37c33f41b14ad4c9cc9b 100644 --- a/example/opencv_example/opencv_rectangle_detection/README.md +++ b/example/opencv_example/opencv_rectangle_detection/README.md @@ -29,7 +29,8 @@ ## 1 简介 -在现代计算机视觉应用中,矩形检测是一项基本但重要的任务,广泛应用于物体识别、场景理解、文档扫描等领域。本指南将详细介绍如何使用OpenCV库在凌智视觉模块上部署高效的矩形检测算法。 +在现代计算机视觉应用中,矩形检测是一项基本但重要的任务,广泛应用于物体识别、场景理解、文档扫描等领域。本指南将详细介绍如何使用 OpenCV 库在凌智视觉模块上部署高效的矩形检测算法。 + ## 2 在凌智视觉模块上部署矩形检测案例 下载模型后,请参考以下教程使用 Python 在凌智视觉模块上部署分类模型例程: diff --git a/example/opencv_example/opencv_rectangle_detection/python/README.md b/example/opencv_example/opencv_rectangle_detection/python/README.md index b736f036f56832ed593aa90f2669d2806cb43cfc..6ec4037c86edf9a4d90e4c7ae5fa0e249a0a7f16 100644 --- a/example/opencv_example/opencv_rectangle_detection/python/README.md +++ b/example/opencv_example/opencv_rectangle_detection/python/README.md @@ -29,12 +29,12 @@ ## 1 简介 -在现代计算机视觉应用中,矩形检测是一项基本但重要的任务,广泛应用于物体识别、场景理解、文档扫描等领域。本指南将详细介绍如何使用OpenCV库在凌智视觉模块上部署高效的矩形检测算法。 本文将详细介绍如何使用 OpenCV 库在凌智视觉模块上实现高效的矩形检测功能。 +在现代计算机视觉应用中,矩形检测是一项基本但重要的任务,广泛应用于物体识别、场景理解、文档扫描等领域。本指南将详细介绍如何使用 OpenCV 库在凌智视觉模块上部署高效的矩形检测算法。 本文将详细介绍如何使用 OpenCV 库在凌智视觉模块上实现高效的矩形检测功能。 ## 2 Python API 文档 -```markdown +```python def cvtColor(src, code, dstCn=0): """ @@ -46,7 +46,6 @@ def cvtColor(src, code, dstCn=0): 返回: - 转换后的图像。 """ - return cv2.cvtColor(src, code, dstCn) def GaussianBlur(src, ksize, sigmaX, sigmaY=0, borderType=cv2.BORDER_DEFAULT): """ @@ -60,8 +59,6 @@ def GaussianBlur(src, ksize, sigmaX, sigmaY=0, borderType=cv2.BORDER_DEFAULT): 返回: - 模糊后的图像。 """ - temp_ksize = convert2size(ksize) - return cv2.GaussianBlur(src, temp_ksize, sigmaX, sigmaY, borderType) def threshold(src, thresh, maxval, type): """ @@ -75,8 +72,6 @@ def threshold(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): """ @@ -90,9 +85,7 @@ def drawContours(img, contours, contourIdx, color, thickness=1, lineType=8, shif - lineType: 轮廓线的类型。 - shift: 轮廓坐标的小数位数。 """ - temp_color = convert2scalar(color) - cv2.drawContours(img, contours, contourIdx, temp_color, thickness, lineType, shift) - + def findContours(image, mode, method, point=cv2.Point()): """ 在二值图像中检测轮廓。 @@ -105,9 +98,7 @@ def findContours(image, mode, method, point=cv2.Point()): - 检测到的轮廓列表。 - 轮廓的层次结构。 """ - contours, hierarchy = cv2.findContours(image, mode, method, point) - return contours, hierarchy - + def arcLength(curve, closed): """ 计算曲线的长度。 @@ -117,8 +108,6 @@ def arcLength(curve, closed): 返回: - 曲线的长度。 """ - return cv2.arcLength(curve, closed) - def approxPolyDP(curve, epsilon, closed): """ @@ -130,36 +119,71 @@ def approxPolyDP(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 +from lockzhiner_vision_module.cv2 import VideoCapture 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) +from lockzhiner_vision_module.edit import Edit +import time +import sys + +pi = 3.14159265358979323846 +if __name__ == "__main__": + args = sys.argv + if len(args) != 3: + print("Need model path. Example: python test_capture.py width height") + exit(1) + + edit = Edit() + edit.start_and_accept_connection() + + video_capture = VideoCapture() + video_capture.set_width(int(args[1])) + video_capture.set_height(int(args[2])) + 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): + start_time = time.time() + ret, mat = video_capture.read() + if ret is False: + continue + end_time = time.time() + # 转换为灰度图像 + gray = cv2.cvtColor(mat, 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) + # 将轮廓近似为多边形 + if epsilon < 15: + continue + approx = cv2.approxPolyDP(contour, epsilon, True) + # 如果近似轮廓有4个顶点,则认为是矩形 + if len(approx) == 4: + cv2.putText(mat, "Rectangle", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + cv2.drawContours(mat, [approx], -1, (0, 255, 0), 2) + edit.print(mat) + total_time_ms += end_time - start_time + read_index += 1 + print(f"FPS is {1.0 / (total_time_ms / read_index)}") + ``` ## 4 上传并测试 Python 程序 @@ -171,25 +195,23 @@ cv2.imwrite('img_rectangle.png', image) 请使用 Electerm Sftp 依次上传以下文件: - 进入存放 **test_rectangle.py** 脚本文件的目录,将 **test_rectangle.py** 上传到 Lockzhiner Vision Module -- 进入存放 **待检测图片** 存放的目录,将 **待检测图片** 上传到 Lockzhiner Vision Module 上传文件 + ![](./images/sftp.png) 请使用 Electerm Ssh 并在命令行中执行以下命令: ```bash -python test_rectangle.py +python test_rectangle.py 640 480 ``` - 运行程序后,屏幕上输出 -![](./images/result.png) -下载结果 -![](./images/dowm_result.png) -矩形检测原图 -![](./images/rectangle.png) -矩形检测结果图片 + ![](./images/results.png) +矩形检测结果图片 + +![](./images/rectangle_result.png) + diff --git a/example/opencv_example/opencv_rectangle_detection/python/images/img_rectangle.png b/example/opencv_example/opencv_rectangle_detection/python/images/img_rectangle.png new file mode 100644 index 0000000000000000000000000000000000000000..443bec4a97a237159ada9e228acbef120e2986ad Binary files /dev/null and b/example/opencv_example/opencv_rectangle_detection/python/images/img_rectangle.png differ diff --git a/example/opencv_example/opencv_rectangle_detection/python/images/rectangle_result.png b/example/opencv_example/opencv_rectangle_detection/python/images/rectangle_result.png new file mode 100644 index 0000000000000000000000000000000000000000..3e7591e73bd2835af642adcb30d642e19de9c60e Binary files /dev/null and b/example/opencv_example/opencv_rectangle_detection/python/images/rectangle_result.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 deleted file mode 100644 index 85313072b0fa83e8bd9b0f1627930182c0a45a9f..0000000000000000000000000000000000000000 Binary files a/example/opencv_example/opencv_rectangle_detection/python/images/result.png and /dev/null 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 index cddd3669c6373f8b4590235112835c80f1246173..97837e5d4fcf75bb6c8bd56f3ed5b8d14bb1d31f 100644 Binary files a/example/opencv_example/opencv_rectangle_detection/python/images/results.png and b/example/opencv_example/opencv_rectangle_detection/python/images/results.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 index 076800f1aad341e6d1cb777cbbc6539365f67409..79a7a9fa70e44427205c9a991b8bc43889da3783 100644 --- a/example/opencv_example/opencv_rectangle_detection/python/test_rectangle.py +++ b/example/opencv_example/opencv_rectangle_detection/python/test_rectangle.py @@ -1,20 +1,56 @@ +from lockzhiner_vision_module.cv2 import VideoCapture 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 +from lockzhiner_vision_module.edit import Edit +import time +import sys + +pi = 3.14159265358979323846 +if __name__ == "__main__": + args = sys.argv + if len(args) != 3: + print("Need model path. Example: python test_capture.py width height") + exit(1) + + edit = Edit() + edit.start_and_accept_connection() + + video_capture = VideoCapture() + video_capture.set_width(int(args[1])) + video_capture.set_height(int(args[2])) + 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): + start_time = time.time() + ret, mat = video_capture.read() + if ret is False: + continue + end_time = time.time() + # 转换为灰度图像 + gray = cv2.cvtColor(mat, 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) + # 将轮廓近似为多边形 + if epsilon < 15: + continue + approx = cv2.approxPolyDP(contour, epsilon, True) + # 如果近似轮廓有4个顶点,则认为是矩形 + if len(approx) == 4: + cv2.putText(mat, "Rectangle", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) + cv2.drawContours(mat, [approx], -1, (0, 255, 0), 2) + edit.print(mat) + total_time_ms += end_time - start_time + read_index += 1 + print(f"FPS is {1.0 / (total_time_ms / read_index)}") diff --git a/example/opencv_example/opencv_triangle_detection/README.md b/example/opencv_example/opencv_triangle_detection/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9a31085e533f91b923ab19a1da19ba81230740af --- /dev/null +++ b/example/opencv_example/opencv_triangle_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/README.md) + + diff --git a/example/opencv_example/opencv_triangle_detection/python/README.md b/example/opencv_example/opencv_triangle_detection/python/README.md new file mode 100644 index 0000000000000000000000000000000000000000..d2df83faaf163849728193085cb02e1675844ff9 --- /dev/null +++ b/example/opencv_example/opencv_triangle_detection/python/README.md @@ -0,0 +1,221 @@ +

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

+ +发布版本:V0.0.0 + +日期:2024-11-29 + +文件密级:□绝密 □秘密 □内部资料 ■公开 + +--- + +**免责声明** + +本文档按**现状**提供,福州凌睿智捷电子有限公司(以下简称**本公司**)不对本文档中的任何陈述、信息和内容的准确性、可靠性、完整性、适销性、适用性及非侵权性提供任何明示或暗示的声明或保证。本文档仅作为使用指导的参考。 + +由于产品版本升级或其他原因,本文档可能在未经任何通知的情况下不定期更新或修改。 + +**读者对象** + +本教程适用于以下工程师: + +- 技术支持工程师 +- 软件开发工程师 + +**修订记录** + +| **日期** | **版本** | **作者** | **修改说明** | +|:-----------| -------- |--------| ------------ | +| 2024/11/29 | 0.0.0 | 钟海滨 | 初始版本 | + +## 1 简介 + +在现代计算机视觉应用中,三角形检测是一项基本但重要的任务,广泛应用于物体识别、场景理解、文档扫描等领域。本指南将详细介绍如何使用 OpenCV 库在凌智视觉模块上部署高效的三角形检测算法。 本文将详细介绍如何使用 OpenCV 库在凌智视觉模块上实现高效的三角形检测功能。 + +## 2 Python API 文档 + +```python + +def cvtColor(src, code, dstCn=0): + """ + 转换图像颜色空间。 + 参数: + - src: 输入图像。 + - code: 颜色空间转换代码。 + - dstCn: 目标图像的通道数,如果为0则根据code自动确定。 + 返回: + - 转换后的图像。 + """ + +def GaussianBlur(src, ksize, sigmaX, sigmaY=0, borderType=cv2.BORDER_DEFAULT): + """ + 使用高斯滤波模糊图像。 + 参数: + - src: 输入图像。 + - ksize: 高斯核的大小,必须是奇数。 + - sigmaX: 高斯核在X方向的标准差。 + - sigmaY: 高斯核在Y方向的标准差,如果为0则与sigmaX相同。 + - borderType: 图像边界的处理方式,默认为cv2.BORDER_DEFAULT。 + 返回: + - 模糊后的图像。 + """ + +def threshold(src, thresh, maxval, type): + """ + 对图像应用阈值处理。 + 参数: + - src: 输入图像。 + - thresh: 阈值。 + - maxval: 阈值处理后的最大值。 + - type: 阈值处理的类型。 + 返回: + - 计算出的阈值。 + - 阈值处理后的图像。 + """ + +def drawContours(img, contours, contourIdx, color, thickness=1, lineType=8, shift=0): + """ + 在图像上绘制轮廓。 + 参数: + - img: 输入图像。 + - contours: 要绘制的轮廓列表。 + - contourIdx: 要绘制的轮廓索引,-1表示绘制所有轮廓。 + - color: 轮廓的颜色。 + - thickness: 轮廓线的厚度。 + - lineType: 轮廓线的类型。 + - shift: 轮廓坐标的小数位数。 + """ + +def findContours(image, mode, method, point=cv2.Point()): + """ + 在二值图像中检测轮廓。 + 参数: + - image: 输入的二值图像。 + - mode: 轮廓的检索模式。 + - method: 轮廓的近似方法。 + - point: 轮廓检索的起始点。 + 返回: + - 检测到的轮廓列表。 + - 轮廓的层次结构。 + """ + +def arcLength(curve, closed): + """ + 计算曲线的长度。 + 参数: + - curve: 输入的曲线,由点序列组成。 + - closed: 曲线是否封闭。 + 返回: + - 曲线的长度。 + """ + +def approxPolyDP(curve, epsilon, closed): + """ + 使用Douglas-Peucker算法近似曲线。 + 参数: + - curve: 输入的曲线,由点序列组成。 + - epsilon: 近似精度,通常为原始曲线长度的百分之一。 + - closed: 曲线是否封闭。 + 返回: + - 近似后的曲线。 + """ + +``` + +## 3 在凌智视觉模块上进行边缘检测案例 + +为了方便大家入手,我们提供了 OpenCV 三角形检测的 Python 例程。该程序可以使用摄像头进行端到端推理。 +**测试图片下载链接:**[三角形检测图片](https://gitee.com/LockzhinerAI/LockzhinerVisionModule/releases/download/v0.0.4/triangle.png) + +```python +from lockzhiner_vision_module.cv2 import VideoCapture +import lockzhiner_vision_module.cv2 as cv2 +from lockzhiner_vision_module.edit import Edit +import time +import sys + +pi = 3.14159265358979323846 +if __name__ == "__main__": + args = sys.argv + if len(args) != 3: + print("Need model path. Example: python test_capture.py width height") + exit(1) + + edit = Edit() + edit.start_and_accept_connection() + + video_capture = VideoCapture() + video_capture.set_width(int(args[1])) + video_capture.set_height(int(args[2])) + 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): + start_time = time.time() + ret, mat = video_capture.read() + if ret is False: + continue + end_time = time.time() + # 转换为灰度图像 + # 转换为灰度图像 + gray = cv2.cvtColor(mat, cv2.COLOR_BGR2GRAY) + blurred = cv2.GaussianBlur(gray, (3, 3), 0) + _, thresh = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY) + edges = cv2.Canny(thresh, 50, 150) + + # 3. 查找轮廓 + contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) + print(f"len:{len(contours)}") + # 4. 筛选轮廓 + for contour in contours: + # 计算轮廓的周长 + perimeter = cv2.arcLength(contour, True) + if perimeter<15: + continue + # 近似轮廓 + approx = cv2.approxPolyDP(contour, 0.04 * perimeter, True) + + # 如果近似轮廓有3个顶点,则认为它是三角形 + if len(approx) == 3: + # 5. 绘制结果 + cv2.putText(mat, "Triangle", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), ) + cv2.drawContours(mat, [approx], -1, (0, 255, 0), 2) + # cv2.imwrite("blurred.png", blurred) + edit.print(mat) + total_time_ms += end_time - start_time + read_index += 1 + print(f"FPS is {1.0 / (total_time_ms / read_index)}") + + +``` +## 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 + +上传文件 + +![](./images/uploads.png) + +请使用 Electerm Ssh 并在命令行中执行以下命令: + +```bash +python test_trangle.py 640 480 +``` + +运行程序后,屏幕上输出 + +三角形检测结果图片 + +![](./images/triangle-rest.png) + + + diff --git a/example/opencv_example/opencv_triangle_detection/python/images/dowmresult.png b/example/opencv_example/opencv_triangle_detection/python/images/dowmresult.png new file mode 100644 index 0000000000000000000000000000000000000000..c6f5c90339fa3cc57b69bdf55715098ae505812a Binary files /dev/null and b/example/opencv_example/opencv_triangle_detection/python/images/dowmresult.png differ diff --git a/example/opencv_example/opencv_triangle_detection/python/images/result_triangle.png b/example/opencv_example/opencv_triangle_detection/python/images/result_triangle.png new file mode 100644 index 0000000000000000000000000000000000000000..f7b1796a89a9fccf6d76a38687abe81c4a431588 Binary files /dev/null and b/example/opencv_example/opencv_triangle_detection/python/images/result_triangle.png differ diff --git a/example/opencv_example/opencv_triangle_detection/python/images/run.png b/example/opencv_example/opencv_triangle_detection/python/images/run.png new file mode 100644 index 0000000000000000000000000000000000000000..70858cfa5787e74d4f0d08d4bb6a9b561918ce68 Binary files /dev/null and b/example/opencv_example/opencv_triangle_detection/python/images/run.png differ diff --git a/example/opencv_example/opencv_triangle_detection/python/images/triangle-rest.png b/example/opencv_example/opencv_triangle_detection/python/images/triangle-rest.png new file mode 100644 index 0000000000000000000000000000000000000000..309318bf15533ea22fd624c6f3b64477ea3a87d5 Binary files /dev/null and b/example/opencv_example/opencv_triangle_detection/python/images/triangle-rest.png differ diff --git a/example/opencv_example/opencv_triangle_detection/python/images/triangle.png b/example/opencv_example/opencv_triangle_detection/python/images/triangle.png new file mode 100644 index 0000000000000000000000000000000000000000..17cb98478ed1d1490ee6e3e6e6fbccbd49d7b814 Binary files /dev/null and b/example/opencv_example/opencv_triangle_detection/python/images/triangle.png differ diff --git a/example/opencv_example/opencv_triangle_detection/python/images/uploads.png b/example/opencv_example/opencv_triangle_detection/python/images/uploads.png new file mode 100644 index 0000000000000000000000000000000000000000..1c9e33335e3bb772527bbd6da5ec50d58c9429b2 Binary files /dev/null and b/example/opencv_example/opencv_triangle_detection/python/images/uploads.png differ diff --git a/example/opencv_example/opencv_triangle_detection/python/test_trangle.py b/example/opencv_example/opencv_triangle_detection/python/test_trangle.py new file mode 100644 index 0000000000000000000000000000000000000000..cbfd2db679edac753917256d9f8c54fbf347c971 --- /dev/null +++ b/example/opencv_example/opencv_triangle_detection/python/test_trangle.py @@ -0,0 +1,60 @@ +from lockzhiner_vision_module.cv2 import VideoCapture +import lockzhiner_vision_module.cv2 as cv2 +from lockzhiner_vision_module.edit import Edit +import time +import sys + +pi = 3.14159265358979323846 +if __name__ == "__main__": + args = sys.argv + if len(args) != 3: + print("Need model path. Example: python test_capture.py width height") + exit(1) + + edit = Edit() + edit.start_and_accept_connection() + + video_capture = VideoCapture() + video_capture.set_width(int(args[1])) + video_capture.set_height(int(args[2])) + 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): + start_time = time.time() + ret, mat = video_capture.read() + if ret is False: + continue + end_time = time.time() + # 转换为灰度图像 + gray = cv2.cvtColor(mat, cv2.COLOR_BGR2GRAY) + blurred = cv2.GaussianBlur(gray, (3, 3), 0) + _, thresh = cv2.threshold(blurred, 127, 255, cv2.THRESH_BINARY) + edges = cv2.Canny(thresh, 50, 150) + + # 3. 查找轮廓 + contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE) + print(f"len:{len(contours)}") + # 4. 筛选轮廓 + for contour in contours: + # 计算轮廓的周长 + perimeter = cv2.arcLength(contour, True) + if perimeter<15: + continue + # 近似轮廓 + approx = cv2.approxPolyDP(contour, 0.04 * perimeter, True) + + # 如果近似轮廓有3个顶点,则认为它是三角形 + if len(approx) == 3: + # 5. 绘制结果 + cv2.putText(mat, "Triangle", (50,50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), ) + cv2.drawContours(mat, [approx], -1, (0, 255, 0), 2) + # cv2.imwrite("blurred.png", blurred) + edit.print(mat) + total_time_ms += end_time - start_time + read_index += 1 + print(f"FPS is {1.0 / (total_time_ms / read_index)}") diff --git a/example/special/face_system/README.md b/example/special/face_system/README.md index 587436ea37e9f86f5c0f5aab75918b649522d571..acaa9877bb40d69d236e3b3ef10e4fb8d855b8ac 100644 --- a/example/special/face_system/README.md +++ b/example/special/face_system/README.md @@ -41,7 +41,7 @@ 训练完模型后,请参考以下教程在凌智视觉模块上部署检测模型例程: -* [凌智视觉模块 人脸识别系统 Python 部署指南](./python/README.md) +* [凌智视觉模块人脸识别系统 Python 部署指南](./python/README.md) ## 4 各模型性能指标 @@ -49,4 +49,4 @@ | 人脸识别系统 | FPS(帧/s) | 精度(%) | |:------------------:|:--------:|:----:| -| LZ-Face、LZ-Arcface | 23 | - | \ No newline at end of file +| LZ-Face+LZ-Arcface | 23 | - | \ No newline at end of file diff --git a/example/special/face_system/python/README.md b/example/special/face_system/python/README.md index be081bb224e15a98400263c2f8dce9ded682535b..363f9b4e96baa56db14352eeecd491c7747e0c41 100644 --- a/example/special/face_system/python/README.md +++ b/example/special/face_system/python/README.md @@ -1,4 +1,4 @@ -

凌智视觉模块 人脸识别系统 Python 部署指南

+

凌智视觉模块人脸识别系统 Python 部署指南

发布版本:V0.0.0 @@ -96,7 +96,7 @@ class FaceRecognitionSystem: input_mat (cv2.Mat): 输入的图像数据,通常是一个 cv2.Mat 变量。 Returns: - FaceFeatureResult: 识别到的用户,余弦相似度分数,检测到的目标框 + FaceFeatureResults: 识别到的用户,余弦相似度分数,检测到的目标框 """ return self.model.predict(input_mat) @@ -121,7 +121,7 @@ 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") + print("Need model path. Example: python face_system.py LZ-Face.rknn LZ-ArcFace.rknn baseDataset crop_root") exit(1) # 初始化面部识别系统 @@ -175,28 +175,30 @@ if __name__ == '__main__': 请使用 Electerm Sftp 依次上传以下文件: -- 进入存放 **test_arc_face[face_system.py](face_system.py).py** 脚本文件的目录,将 **[face_system.py](face_system.py).py** 上传到 Lockzhiner Vision Module +- 进入存放 **face_system.py** 脚本文件的目录,将 **face_system.py** 上传到 Lockzhiner Vision Module - 进入存放 **LZ-ArcFace.rknn(也可能是其他模型)** 模型存放的目录(模型存放在训练模型后下载的 output 文件夹内),将 **LZ-ArcFace.rknn** 上传到 Lockzhiner Vision Module - 进入存放 **LZ-Face.rknn(也可能是其他模型)** 模型存放的目录(模型存放在训练模型后下载的 output 文件夹内),将 **LZ-Face.rknn** 上传到 Lockzhiner Vision Module -- 上传构建人脸数据库的文件夹 - - 文件夹结构: - - 主目录 - - 人脸数据库文件夹(即为用户名) - - 人脸图片 + +人脸数据库文件夹结构示意图 + +![](./images/img_1.png) + 上传文件 + ![](./images/connect.png) -文件夹结构示意图 -![](./images/img.png) + + 请使用 Electerm Ssh 并在命令行中执行以下命令: ```bash -python face_system.py LZ-Face.rknn LZ-ArcFace.rknn baseDataset_root crop_root +python face_system.py LZ-Face.rknn LZ-ArcFace.rknn BaseDataset crop_root ``` 运行程序后,屏幕上开始打印数据库构建的相关信息,并在一段时间后输出 ![alt text](./images/build_database.png) + ![alt text](./images/fps.png) diff --git a/example/special/face_system/python/images/img_1.png b/example/special/face_system/python/images/img_1.png new file mode 100644 index 0000000000000000000000000000000000000000..415ca050420d563faf4b332120d1f6ec49ebec73 Binary files /dev/null and b/example/special/face_system/python/images/img_1.png differ diff --git a/example/special/face_system/python/test_face_system.py b/example/special/face_system/python/test_face_system.py index 9c1ff03bc0be5caed08f33090c95661116291c56..7c29e3914324824478bf9900a467378f3ec4bc69 100644 --- a/example/special/face_system/python/test_face_system.py +++ b/example/special/face_system/python/test_face_system.py @@ -18,9 +18,6 @@ def predict_face(det_model, rec_model, face_mat): det_result = det_results[0] face_roi = det_result.box - # print( - # f"(x,y,w,h,score): [{face_roi.x},{face_roi.y},{face_roi.width},{face_roi.height},{det_result.score}]" - # ) crop_mat = input_mat.crop(face_roi) if crop_mat.empty(): return None, None @@ -55,16 +52,12 @@ if __name__ == '__main__': os.makedirs(crop_dataset_path) # 构建数据库 # 临时存储特征向量的字典 - - # c++的字典使用,map(std::string,vectoor,rec.result) - 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): @@ -99,7 +92,7 @@ if __name__ == '__main__': 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.")