当前位置:   article > 正文

DeepStream--测试PCB-Defect-Detection_deepstream 制造 检测

deepstream 制造 检测

GitHub - clintonoduor/PCB-Defect-Detection-using-Deepstream: PCB defect detection using deepstream & YoloV5我参考了了这个代码,作者基于YoloV5,训练一个电路板检测的模型,训练数据集来自https://robotics.pkusz.edu.cn/resources/datasetENG/。模型支持以下6种残缺的检测:

Missing hole 漏孔
Mouse bite  鼠牙洞
Open circuit  开路
Short circuit 短路
Spurious copper 杂铜
spur 毛刺

但是这个代码有个问题,工程缺少custom_yolov5s.yaml 文件,这一步将失败,生成不了wts和cfg文件。

!python3 gen_wts_yoloV5.py -w /content/yolov5/runs/train/yolov5s_results/weights/best.pt -c /content/yolov5/models/custom_yolov5s.yaml

我按https://github.com/marcoslucianops/DeepStream-Yolo/blob/master/docs/YOLOv5.md 这里的步骤,将pt文件直接转成onnx模型。最终转的命令是:python3 export_yoloV5.py -w best.pt

config_infer_primary_yoloV5.txt的内容:

[property]
gpu-id=0
net-scale-factor=0.0039215697906911373
model-color-format=0
onnx-file=best.onnx
model-engine-file=best.onnx_b1_gpu0_fp32.engine
#int8-calib-file=calib.table
labelfile-path=labels.txt
batch-size=1
network-mode=0
num-detected-classes=6
interval=0
gie-unique-id=1
process-mode=1
network-type=0
cluster-mode=4
maintain-aspect-ratio=1
parse-bbox-func-name=NvDsInferParseYolo
custom-lib-path=nvdsinfer_custom_impl_Yolo/libnvdsinfer_custom_impl_Yolo.so
#engine-create-func-name=NvDsInferYoloCudaEngineGet

[class-attrs-all]
pre-cluster-threshold=0.2
 

后处理函数用NvDsInferParseCustomYolo。

  1. static NvDsInferParseObjectInfo
  2. convertBBox(const float& bx1, const float& by1, const float& bx2, const float& by2, const uint& netW, const uint& netH)
  3. {
  4. NvDsInferParseObjectInfo b;
  5. float x1 = bx1;
  6. float y1 = by1;
  7. float x2 = bx2;
  8. float y2 = by2;
  9. x1 = clamp(x1, 0, netW);
  10. y1 = clamp(y1, 0, netH);
  11. x2 = clamp(x2, 0, netW);
  12. y2 = clamp(y2, 0, netH);
  13. b.left = x1;
  14. b.width = clamp(x2 - x1, 0, netW);
  15. b.top = y1;
  16. b.height = clamp(y2 - y1, 0, netH);
  17. return b;
  18. }
  19. static void
  20. addBBoxProposal(const float bx1, const float by1, const float bx2, const float by2, const uint& netW, const uint& netH,
  21. const int maxIndex, const float maxProb, std::vector<NvDsInferParseObjectInfo>& binfo)
  22. {
  23. NvDsInferParseObjectInfo bbi = convertBBox(bx1, by1, bx2, by2, netW, netH);
  24. if (bbi.width < 1 || bbi.height < 1)
  25. return;
  26. bbi.detectionConfidence = maxProb;
  27. bbi.classId = maxIndex;
  28. binfo.push_back(bbi);
  29. }
  30. static std::vector<NvDsInferParseObjectInfo>
  31. decodeTensorYolo(const float* boxes, const float* scores, const float* classes, const uint& outputSize, const uint& netW,
  32. const uint& netH, const std::vector<float>& preclusterThreshold)
  33. {
  34. std::vector<NvDsInferParseObjectInfo> binfo;
  35. for (uint b = 0; b < outputSize; ++b) {
  36. float maxProb = scores[b];
  37. int maxIndex = (int) classes[b];
  38. if (maxProb < preclusterThreshold[maxIndex])
  39. continue;
  40. float bxc = boxes[b * 4 + 0];
  41. float byc = boxes[b * 4 + 1];
  42. float bw = boxes[b * 4 + 2];
  43. float bh = boxes[b * 4 + 3];
  44. float bx1 = bxc - bw / 2;
  45. float by1 = byc - bh / 2;
  46. float bx2 = bx1 + bw;
  47. float by2 = by1 + bh;
  48. addBBoxProposal(bx1, by1, bx2, by2, netW, netH, maxIndex, maxProb, binfo);
  49. }
  50. return binfo;
  51. }
  52. static bool
  53. NvDsInferParseCustomYolo(std::vector<NvDsInferLayerInfo> const& outputLayersInfo, NvDsInferNetworkInfo const& networkInfo,
  54. NvDsInferParseDetectionParams const& detectionParams, std::vector<NvDsInferParseObjectInfo>& objectList)
  55. {
  56. if (outputLayersInfo.empty()) {
  57. std::cerr << "ERROR: Could not find output layer in bbox parsing" << std::endl;
  58. return false;
  59. }
  60. std::vector<NvDsInferParseObjectInfo> objects;
  61. const NvDsInferLayerInfo& boxes = outputLayersInfo[0];
  62. const NvDsInferLayerInfo& scores = outputLayersInfo[1];
  63. const NvDsInferLayerInfo& classes = outputLayersInfo[2];
  64. const uint outputSize = boxes.inferDims.d[0];
  65. std::vector<NvDsInferParseObjectInfo> outObjs = decodeTensorYolo((const float*) (boxes.buffer),
  66. (const float*) (scores.buffer), (const float*) (classes.buffer), outputSize, networkInfo.width, networkInfo.height,
  67. detectionParams.perClassPreclusterThreshold);
  68. objects.insert(objects.end(), outObjs.begin(), outObjs.end());
  69. objectList = objects;
  70. return true;
  71. }

测试图片:

测试命令:

 gst-launch-1.0 filesrc location=a.JPG ! jpegdec ! videoconvert ! video/x-raw,format=I420 ! nvvideoconvert ! video/x-rawmemory:NVMM,format=NV12 ! mux.sink_0 nvstreammux name=mux batch-size=1 width=1280 height=720 ! nvinfer config-file-path=./config_infer_primary_yoloV5.txt ! nvvideoconvert ! video/x-rawmemory:NVMM,format=RGBA ! nvdsosd ! nvvideoconvert ! video/x-raw,format=I420 ! jpegenc ! filesink location=out.jpg

结果图片:

从结果来看,能识别部分点,但是有些分类是错误,都标成了"missing_hole"。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/749366
推荐阅读
相关标签
  

闽ICP备14008679号