forked from murphypei/create-pascal-voc-dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_JPEGImages.py
84 lines (56 loc) · 2.03 KB
/
create_JPEGImages.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding:utf-8 -*-
__auth__ = 'peic'
'''
读取图片,重命名图片,读取gt,生成output文件
'''
import re
import os
from PIL import Image
# 图片名称起始编号
name_number = 1
# 图片名称编号长度
name_length = 6
# Train annotations 位置
TRAIN_ANNO = r'G:\pycharm\INRIA2VOC\INRIADATA\original_images\Train\annotations'
# Test annotations 位置
TEST_ANNO = r'G:\pycharm\INRIA2VOC\INRIADATA\original_images\Test\annotations'
# 图片所在位置
ORIGIN_IMAGES = r'G:\pycharm\INRIA2VOC\INRIADATA\original_images'
# 输出文件
fout = open('output.txt', 'w')
# 创建另存为的文件夹
if not os.path.exists('JPEGImages'):
os.mkdir('JPEGImages')
print "mkdir donw"
# 定义处理函数
def process(anno_path):
train_anno_files = os.listdir(anno_path)
for anno in train_anno_files:
with open(os.path.join(anno_path, anno), 'r') as f:
for line in f:
line = line.strip()
# 匹配图片名称
sg = re.search(r'Image filename : "([\w\d/_.]+)"', line)
if sg:
image_filename = sg.group(1)
#print image_filename
# 构建新的图片名称
global name_number
new_name_str = (name_length - len(str(name_number))) * '0' + str(name_number) + ".jpg"
image = Image.open(os.path.join(ORIGIN_IMAGES, image_filename))
image.save(os.path.join("JPEGImages", new_name_str), 'jpeg')
name_number += 1
# 匹配bbox坐标
sg = re.search('\((\d+), (\d+)\) - \((\d+), (\d+)\)$', line)
if sg:
bbox = [sg.group(i) for i in range(1,5)]
if new_name_str:
fout.write(new_name_str + ' ' + ' '.join(bbox) + '\n')
print "process \n{} \ndone".format(anno_path)
# 处理Train
process(TRAIN_ANNO)
print name_number
# 处理Test
process(TEST_ANNO)
print name_number
fout.close()