【问题标题】:Get object from bounding box [Object Detection]从边界框获取对象[对象检测]
【发布时间】:2020-04-03 22:56:52
【问题描述】:

我有一个 .txt 文件,其中每一行都包含 path/to/image.jpg,xmin,ymin,xmax,ymax 和一个包含 jpg 图像的 img 文件夹。使用python提取每个文件坐标内的“对象”并查看边界框是否设置正确的最佳方法是什么?

谢谢!

【问题讨论】:

    标签: python object detection yolo vision


    【解决方案1】:

    您可以使用opencv-python 库:

    import cv2
    
    # Reading the file
    with open("filename.txt") as iostream:
        content = iostream.read()
    
    # Visualizing the data
    color = (0, 0, 255) # RED
    for line in content.split("\n"):
        image_path, xmin, ymin, xmax, ymax = line.split(",")
        image = cv2.imread(image_path)
        pt1 = (int(xmin), int(ymin))
        pt2 = (int(xmax), int(ymax))
        cv2.rectangle(image, pt1, pt2, color)
        cv2.imshow("Visualization bounding box", image)
        cv2.waitKey()
    

    此代码将显示每个图像,并带有一个红色矩形来显示边界框。如果你按任意键,它将切换到下一个。

    如果你想保存裁剪的图像,你可以使用这样的东西:

        outfile = image_path[:-4] + "_bbox.jpg"
        outimage = image[int(ymin):int(ymax), int(xmin):int(xmax)]
        cv2.imwrite(outfile, outimage)
    

    【讨论】:

      【解决方案2】:

      您的文本文件是 CSV(逗号分隔值),可以按原样加载。

      您可以简单地使用 PIL 将文件夹中的每个图像裁剪到边界框:

      import csv
      from PIL import Image
      
      f = open('test.csv')
      csv_f = csv.reader(f)
      
      #iterate through rows of your CSV
      for row in csv_f:
      
        #open image using PIL
        im = Image.open(row[0])
        #crop box format: xmin, ymin, xmax, ymax
        crop_box = (row[1], row[2], row[3], row[4])
        #convert values to int
        crop_box = map(int, crop_box)
        #crop using PIL
        im = im.crop((crop_box))
        #save the image to predetermined savepath
        im.save(save_name)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-20
        • 1970-01-01
        • 2015-03-14
        • 1970-01-01
        • 2018-08-01
        • 2019-09-18
        • 2014-08-02
        相关资源
        最近更新 更多