Python 在每个文件写入完毕之前打开文件

问题描述 投票:0回答:1

我正在编写一个程序,将 Dota 格式转换为 Yolo 格式(了解格式的详细信息并不重要)。

需要从目录中读取很多文件(Dota 文件)。每个文件的每一行都会被读取和操作。输出是一个新创建的文件,具有相同的名称和相同的行数(减去每个 Dota 文件开头用于元数据的 2 行,这些行被删除),其中点数小于 1。

示例:

P0000.txt(Dota文件,输入)

imagesource:GoogleEarth                     //metadata
gsd:0.146343590398                          //metadata
2753 2408 2861 2385 2888 2468 2805 2502 plane 0
3445 3391 3484 3409 3478 3422 3437 3402 large-vehicle 0
3185 4158 3195 4161 3175 4204 3164 4199 large-vehicle 0
2870 4250 2916 4268 2912 4283 2866 4263 large-vehicle 0
630 1674 628 1666 640 1654 644 1666 small-vehicle 0
636 1713 633 1706 646 1698 650 1706 small-vehicle 0
717 76 726 78 722 95 714 90 small-vehicle 0
737 82 744 84 739 101 731 98 small-vehicle 0

P0000.txt(Yolo 文件,正确的预期输出)

0 0.7278709677419355 0.4441112322791712 0.03483870967741936 0.021264994547437296
0 0.8930322580645161 0.6191384950926936 0.012129032258064517 0.0056343147946201376
0 0.8205161290322581 0.7599054889131225 0.008 0.008360596146855689
0 0.7460645161290322 0.7754452926208651 0.012903225806451613 0.0059978189749182115
0 0.16412903225806452 0.3024354780079971 0.004129032258064516 0.0036350418029807343
0 0.1655483870967742 0.3099781897491821 0.004387096774193549 0.0027262813522355507
0 0.18580645161290324 0.015539803707742638 0.0030967741935483874 0.0034532897128316973
0 0.1903225806451613 0.016630316248636857 0.0033548387096774194 0.0034532897128316973

问题是该程序仅适用于 1 个文件。如果我在目录中放置超过 1 个输入文件,则只有第一个输出文件是正确的,其余文件的行数大于其输入文件的等效行数,并且某些坐标(数字)>1。

我认为发生这种情况是因为目录中的所有输入文件都会立即打开,而不是一次打开 1 个文件,而等效的输出文件已完成写入。


textDir = "./inputData"
coordinatesList = list()

with open(textPath, "r") as f:
                    
    contents = f.read()
    contentsSplitLine = contents.splitlines()[2:]

    # print(contents)

    for i in range(len(contentsSplitLine)):
        splitLine = ' '.join(contentsSplitLine[i].rsplit(' ', 2)[:-2]).split()
        coordinatesList.append(splitLine)       

    imageHeight, imageWidth, channels = img.shape
    print(f"{imageWidth}x{imageHeight}")

    for coordinates in coordinatesList:

        coordinates = [eval(i) for i in coordinates]
        coordsX = coordinates[::2]  # get every other element starting from the first (x coordinates)
        coordsY = coordinates[1::2]  # get every other element starting from the second (y coordinates)

        minX = min(coordsX)
        maxX = max(coordsX)

        minY = min(coordsY)
        maxY = max(coordsY)
                        
        centerX = ((maxX + minX)/2) * (1/imageWidth)
        centerY = ((maxY + minY)/2) * (1/imageHeight)

        boundingWidth = (maxX - minX) * (1/imageWidth)
        boundingHeight = (maxY - minY) * (1/imageHeight)

        out = (f"0 {centerX} {centerY} {boundingWidth} {boundingHeight}\n")
        print(out)

        f = open(f"./outputData/{textFile[:-4]}.txt", "a")
        f.write(out)
        f.close()
else:
   continue

剩余的脚本(不完整)

python file artificial-intelligence yolo
1个回答
0
投票

清除

coordinatesList

...
if imageFile[:-4] == textFile[:-4]:

      imagePath = os.path.join(imageDir, imageFile)
      textPath = os.path.join(textDir, textFile)
      img = cv2.imread(imagePath) 

      coordinatesList = []
...
© www.soinside.com 2019 - 2024. All rights reserved.