-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_txt_with_no_image.py
42 lines (35 loc) · 1.02 KB
/
remove_txt_with_no_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os, glob
path = input("Images directory: ")
files = glob.glob(os.path.join(path, "*"))
IMAGETYPES = [".jpg", ".JPG", ".jpeg", ".JPEG"]
LABELED = {}
IMAGES = []
TXTs = []
for file in files:
fileType = os.path.splitext(os.path.basename(file))[1]
if fileType in IMAGETYPES:
IMAGES.append(file)
elif fileType == ".txt":
TXTs.append(file)
test = []
for txt in TXTs:
txtFileName = os.path.splitext(os.path.basename(txt))[0]
for image in IMAGES:
imageFileName = os.path.splitext(os.path.basename(image))[0]
if imageFileName == txtFileName:
LABELED[image] = txt
break
for image in IMAGES:
try:
for txt in TXTs:
if LABELED[image] == txt:
TXTs.remove(txt)
print(f"[REMOVE]: {os.path.basename(txt)}")
except:
print("Failed!")
for txt in TXTs:
txtFileName = os.path.splitext(os.path.basename(txt))[0]
if txtFileName == "classes":
TXTs.remove(txt)
for txt in TXTs:
print(txt)