【问题标题】:Bounding boxes for yoloyolo 的边界框
【发布时间】:2018-08-13 19:27:03
【问题描述】:

我正在使用 YOLO 进行机器学习项目。我正在按照找到的指南 here(在 如何训练(检测您的自定义对象)部分)创建自己的数据集。对于边界框,我需要知道我想在给定图片中训练 YOLO 的每个对象的 [x] [y] [width] [height]。到目前为止,我一直在手动找到它,但它变得非常耗时。我希望得到一些帮助,编写一个可以为我计算这个的脚本。我知道 opencv 有一些很棒的图像处理工具,但不知道从哪里开始查找对象坐标。

【问题讨论】:

    标签: opencv bounding-box yolo


    【解决方案1】:

    在您提到的页面中有一个部分包含指向执行这些框的工具的链接:

    如何标记对象的边界框并创建注释文件:

    在这里您可以找到带有 GUI 软件的存储库,用于标记对象的边界框并为 Yolo v2 生成注释文件:https://github.com/AlexeyAB/Yolo_mark

    【讨论】:

      【解决方案2】:

      我也面临同样的问题,但在我的情况下,数据是视频,背景是一样的,所以我做了背景减法 您可以通过调整一些阈值来尝试此代码可能是您可以得到您想要的

      import cv2
      import numpy as np 
      
      # read and scale down image
      # wget https://bigsnarf.files.wordpress.com/2017/05/hammer.png
      img = cv2.pyrDown(cv2.imread('hammer.png', cv2.IMREAD_UNCHANGED))
      
      # threshold image
      ret, threshed_img = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
                      127, 255, cv2.THRESH_BINARY)
      # find contours and get the external one
      image, contours, hier = cv2.findContours(threshed_img, cv2.RETR_TREE,
                      cv2.CHAIN_APPROX_SIMPLE)
      
      # with each contour, draw boundingRect in green
      # a minAreaRect in red and
      # a minEnclosingCircle in blue
      for c in contours:
          # get the bounding rect
          x, y, w, h = cv2.boundingRect(c)
          # draw a green rectangle to visualize the bounding rect
          cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
      
          # get the min area rect
          rect = cv2.minAreaRect(c)
          box = cv2.boxPoints(rect)
          # convert all coordinates floating point values to int
          box = np.int0(box)
          # draw a red 'nghien' rectangle
          cv2.drawContours(img, [box], 0, (0, 0, 255))
      
          # finally, get the min enclosing circle
          (x, y), radius = cv2.minEnclosingCircle(c)
          # convert all values to int
          center = (int(x), int(y))
          radius = int(radius)
          # and draw the circle in blue
          img = cv2.circle(img, center, radius, (255, 0, 0), 2)
      
      print(len(contours))
      cv2.drawContours(img, contours, -1, (255, 255, 0), 1)
      
      cv2.imshow("contours", img)
      
      ESC = 27
      while True:
          keycode = cv2.waitKey()
          if keycode != -1:
              keycode &= 0xFF
              if keycode == ESC:
                  break
      cv2.destroyAllWindows()
      

      【讨论】:

        【解决方案3】:

        这里有一部分来自Yolo-mark-pwa的源代码,你可以看到,它比原来的Yolo_mark更具可读性(点击右上角的github图标,然后检查src/utils/createExportCord.tssrc/utils/readExportCord.ts)。 naturalWidthnaturalWidth 是图像大小,heightwidth 是蓝色矩形大小。

        namespace mark {
        
          export namespace utils {
        
            export const createExportCord = ({
              name, height, width, top, left, naturalHeight, naturalWidth
            }) => {
              console.log({name, height, width, top, left, naturalHeight, naturalWidth});
        
              const x = (left + (width/2)) / naturalWidth;
              const y = (top + (height/2)) / naturalHeight;
              const w = width / naturalWidth;
              const h = height / naturalHeight;
        
              return [name, x, y, w, h].join(' ');
            }
        
          } // namespace utils
        
        } // namespace mark
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-04
          • 2018-10-31
          • 2020-08-28
          • 2023-03-07
          • 1970-01-01
          • 2021-04-04
          • 2020-04-21
          • 2019-12-09
          相关资源
          最近更新 更多