Shantanu Nath
Shantanu Nath

Reputation: 383

np.savetxt getting this (No such file or directory) Error

Here is my code:

import numpy as np
import os
import json
import xmltodict

from xml.dom import minidom

BASE_Path = 'E:/Personal Projects/ML/computer_vision/open_image_vehicle/'


for file in os.listdir(os.path.join(BASE_Path,'cng_resized')):
    if file.endswith(".xml"):
        mydoc = minidom.parse(file)
        li = []
        ob = mydoc.getElementsByTagName('object')
        image_width = 416.0
        image_height = 416.0
        
        # total amount of items
        for i in ob:
            bbox = i.getElementsByTagName("bndbox")
            #print(len(bbox))
            for child in bbox:
                xmin = float(child.getElementsByTagName("xmin")[0].childNodes[0].nodeValue)
                ymin = float(child.getElementsByTagName("ymin")[0].childNodes[0].nodeValue)
                xmax = float(child.getElementsByTagName("xmax")[0].childNodes[0].nodeValue)
                ymax = float(child.getElementsByTagName("ymax")[0].childNodes[0].nodeValue)
                
                bbox_w =(xmax - xmin)/image_width
                bbox_h = (ymax - ymin)/image_height        
                x_center = (xmin + bbox_w/2)/image_width 
                y_center = (ymin + bbox_h/2)/image_height
                
                li.append([5,x_center,y_center,bbox_w,bbox_h])
                li  = np.array(li)
            
            print(BASE_Path)
            
            np.savetxt(os.path.join(BASE_Path, "/cng/labels/"+file.rsplit('.', 1)[0]+".txt"),
                       li,
                       fmt = ["%d", "%f", "%f", "%f", "%f"]
                       )
        
        

Required Output: E:/Personal Projects/ML/computer_vision/open_image_vehicle/cng/labels/cng.txt

But Showing: No such file or directory: 'E:/cng/labels/CNG.txt'

Why os.path.join not joining the BASE_Path??

Upvotes: 0

Views: 647

Answers (1)

Stefan
Stefan

Reputation: 957

Removing the first / should work. So replace with "cng/labels/". Otherwise you're duplicating as the join function already includes one.

Upvotes: 1

Related Questions