0

About my project I'm trying to train images for text detection using python and tensorflow on pycharm MacOS

now I'm working on splitting the data into training. There are 2 files inside of TrainingData file. Each image folder has 4 images.(I know it's quiet few, but I'll add more images later)


path = 'TrainingData'

images = []
classNo = []
myList = os.listdir(path)
print('Total No of Classes Detected...', len(myList))
noOfClasses = len(myList)
print('Importing Classes...')


for x in range(0, noOfClasses):
    myPicList = os.listdir(path+'/'+str(x))
    for y in myPicList:
        curImg = cv2.imread(path+'/'+str(x)+'/'+y)
        classNo.append(x)
    print(x, end=' ')
print(' ')

However, the result returns

 File "/Users/myname/PycharmProjects/TextDetection/Playernames.py",line 37, in <module>
    myPicList = os.listdir(path+"/"+str(x))
FileNotFoundError: [Errno 2] No such file or directory: 'TrainingData/0'
Total No of Classes Detected... 2
Importing Classes...

What I tried to solve the problem

1.Checked if the path exists

path = 'TrainingData'
if os.path.isfile(path):
    print('File exists')
else:
    print('Failed')

result shows Failed so I think the path doesn't exist in the file.

2: Checked the tree of the files

tree of TrainingDAta↓

/Users/myname/PycharmProjects/TextDetection/TrainingData
├── File Name 1
│   ├── IMG_1754.jpg
│   ├── IMG_1755.jpg
│   ├── IMG_1756.jpg
│   └── IMG_1757.jpg
└── File Name 2
    ├── IMG_1751.jpg
    ├── IMG_1752.jpg
    ├── IMG_1753.jpg
    └── IMG_1758.jpg

2 directories, 8 files

Tree of the project file itself

/Users/myname/PycharmProjects/TextDetection/Playernames.py [error opening dir]

0 directories, 0 files

What I wanna know

According to the tree, I guess I need to change the location of the TrainingData, because the project shows it doesn't have any directories or files. And I wanna know how to change that.

I'm a beginner so I'm not sure if my attempt is right or wrong.

wasabi
  • 21
  • 6
  • 1
    Welcome to Stack Overflow @Wasabi. I would recommend using [`pathlib`](https://docs.python.org/3/library/pathlib.html) for this, it's the modern alternative to `os.path`. If you want a functional example see the 2 sections called *"Using pathlib from Python 3.4"* and *"Use glob method in pathlib.Path()"* in [this post](https://stackoverflow.com/a/41447012). If you want to narrow your search [try a tag combination like this](https://stackoverflow.com/questions/tagged/python%2bpathlib?tab=Votes) – bad_coder Jan 24 '21 at 17:39

1 Answers1

1

When you define the path to the images you have to include the full file path.

If you change your code to the following it should work!

path = '/Users/myname/PycharmProjects/TextDetection/TrainingData'

images = []
classNo = []
myList = os.listdir(path)
print('Total No of Classes Detected...', len(myList))
noOfClasses = len(myList)
print('Importing Classes...')


for x in range(0, noOfClasses):
    myPicList = os.listdir(path+'/'+str(x))
    for y in myPicList:
        curImg = cv2.imread(path+'/'+str(x)+'/'+y)
        classNo.append(x)
    print(x, end=' ')
print(' ')

Let me know if this works!

BoomBoxBoy
  • 1,770
  • 1
  • 5
  • 23
  • Thank you for your advice! I didn't know I had to include full file path when it comes to images. However, it still returns the same error. – wasabi Jan 25 '21 at 08:03
  • What are the specific names of the two folders that contain the images? – BoomBoxBoy Jan 25 '21 at 17:08
  • folder1 is cv19 and folder2 is KOEM – wasabi Jan 25 '21 at 18:11
  • @wasabi I think your best bet is to look at some other questions regarding loading images with os. Follow the code in this question, and if you still have trouble let me know. https://stackoverflow.com/questions/46739945/reading-images-in-python-using-opencv-and-os-libraries – BoomBoxBoy Jan 25 '21 at 19:17
  • 1
    Hi. I've tried several solutions and it seems like the problem is with my image file.(TrainingData) The program works with no error when I use a different sample file of images from the internet. – wasabi Jan 28 '21 at 17:28