【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测

【VisionFive 2 Lite 单板计算机】边缘AI视觉应用部署:缺陷检测

本文介绍了昉·星光 VisionFive2 Lite 单板计算机结合 OpenCV 实现缺陷检测的板端推理的项目设计。

项目介绍

VisionFive 2 Lite 结合 OpenCV 与缺陷检测模型实现缺陷检测。

  • 准备工作:硬件连接、系统安装、软件更新、OpenCV 部署、Qt 平台完善等;
  • 缺陷检测模型:包括流程图、代码、效果演示等;

准备工作

包括硬件连接、OpenCV 部署及测试等。

硬件连接

包括供电、联网、显示、鼠标键盘输入等。

系统安装

安装和烧录 VisionFive2 Lite 官方 Ubuntu 操作系统。

详见:VF2 Lite 系统安装

软件更新

更新软件包

wget https://github.com/starfive-tech/Debian/releases/download/v0.15.0-engineering-release-wayland/install_full.sh
sudo chmod +x install_full.sh
sudo ./install_full.sh

详见: StarFive-Tech | Debian .

OpenCV

包括 OpenCV 安装及版本检测流程。

  • 安装 OpenCV

    sudo apt update
    sudo apt install python3-opencv
    
  • 安装缺失的 libopencv 包

sudo apt install -y libopencv-dev
  • 验证安装结果
python3 -c "import cv2,sys;print(cv2.__version__,sys.version)"
  • 输出 OpenCV 版本号

至此,完成 OpenCV 的板端本地部署。

Qt 平台

完整 Qt 环境部署,以便 SSH 正常弹窗显示结果

sudo apt-get install -y libxcb-xinerama0 libxcb-xinput0 libxcb-cursor0
sudo apt-get install -y libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0
sudo apt-get install -y libxcb-render-util0 libxcb-shape0 libxcb-sync1 libxcb-xfixes0
sudo apt-get install -y libxcb-xkb1 libxkbcommon-x11-0

# 安装Qt相关的库
sudo apt-get install -y libqt5gui5 libqt5widgets5 libqt5core5a

工程代码

终端执行 touch dd_cpp.py 新建程序文件,并添加如下代码

#!/usr/bin/env python
'''
This sample demonstrates defect detection by image processing.

Usage:
  defect_detection.py

'''
import cv2 as cv
import numpy as np
import argparse

def main():
  img = cv.imread('/usr/share/opencv4/defect_detection/defect.png')
  img2 = cv.resize(img, (600,400))
  cv.imshow('0.Resize', img2)
  gray_mat = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
  # cv.imshow('1.Grey-scale Map', gray_mat)
  _retval, bin_mat = cv.threshold(gray_mat, 128, 255, cv.THRESH_BINARY)
  # cv.imshow('2.Binary Image', bin_mat)
  inv_mat = 255 - bin_mat
  # cv.imshow('3.Color Flip', inv_mat)
  element = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
  erode_mat = cv.erode(inv_mat, element)
  # cv.imshow("04.Erode", erode_mat);

  # Find the outline from erode_mat,and save the result to contours
  contours, hierarchy = cv.findContours(erode_mat, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
  #drawing = np.zeros((erode_mat.shape[0], erode_mat.shape[1], 3), dtype=np.uint8)

  # Iterate over all contours and highline the defect contours.
  for i, c in enumerate(contours):
      aero = cv.contourArea(c)
      if  aero > 1000:
        cv.drawContours(img2, contours, i, (0, 0, 255), -1, 8, hierarchy)
        print('aero:', aero)

  cv.imshow("05.Contours Image", img2);
  cv.waitKey(0)
  print('Done')

if __name__ == '__main__':
    print(__doc__)
    main()
    cv.destroyAllWindows()

保存代码。

效果演示

  • 终端执行 python dd_cpp.py 指令运行程序;

  • 终端打印缺陷检测结果,包括缺陷位置信息等;

弹窗显示,对比检测结果

更多测试效果

实际应用需根据特定场景进行数据训练,或采用 YOLOv8 图像分割算法进一步提高缺陷检测的精度。

总结

本文介绍了昉·星光 VisionFive2 Lite 单板计算机结合 OpenCV 与缺陷检测模型实现缺陷检测板端推理的项目设计,包括 OpenCV 部署、Qt 平台部署、代码及效果演示等,为相关产品在边缘 AI 视觉、工业检测分析等领域的快速开发与应用设计提供了参考。

2 Likes