-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.py
60 lines (54 loc) · 1.72 KB
/
rename.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
from os.path import join
import argparse
import random
parser = argparse.ArgumentParser()
parser.add_argument('--image_dir',
dest='image_dir',
type=str,
required=True,
help='generae list of all images in the dir')
args = parser.parse_args()
image_dir = os.path.abspath(args.image_dir)
tmp_list = os.listdir(args.image_dir)
txt_dir = image_dir.replace('JPEGImages', 'labels').replace('images','labels')
xml_dir = image_dir.replace('JPEGImages', 'annotations').replace('images','annotations')
if not os.path.isdir(xml_dir):
xml_dir.replace('annotations', 'Annotations')
if not os.path.isdir(xml_dir):
xml_dir = None
if not os.path.isdir(txt_dir):
txt_dir = None
cnt = -1
tmp_list.sort()
image_list = []
for img in tmp_list:
if 'image_' in img:
cnt = int(img.split('.')[0].split('_')[-1])
else:
image_list.append(img)
cnt += 1
print('Index start from : {}'.format(cnt))
for img in image_list:
print('Renaming: '+os.path.join(image_dir, img))
prefix = img.rsplit('.')[0]
new = 'image_{0:05}'.format(cnt)
# Rename image
src = os.path.join(image_dir, img)
dst = os.path.join(image_dir, new+'.'+img.split('.')[-1])
os.rename(src, dst)
# Rename txt
if txt_dir:
src = os.path.join(txt_dir, prefix+'.txt')
dst = os.path.join(txt_dir, new+'.txt')
if os.path.isfile(src):
print('Renaming: '+src)
os.rename(src, dst)
# Rename xml
if xml_dir:
src = os.path.join(xml_dir, prefix+'.xml')
dst = os.path.join(xml_dir, new+'.xml')
if os.path.isfile(src):
print('Renaming: '+src)
os.rename(src, dst)
cnt+=1