forked from foamliu/Deep-Image-Matting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre_process.py
133 lines (103 loc) · 4.28 KB
/
pre_process.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
import os
import shutil
import zipfile
import tarfile
from Combined_Dataset.Training_set.Composition_code_revised import do_composite
from Combined_Dataset.Test_set.Composition_code_revised import do_composite_test
if __name__ == '__main__':
# path to provided foreground images
fg_path = 'data/fg/'
# path to provided alpha mattes
a_path = 'data/mask/'
# Path to background images (MSCOCO)
bg_path = 'data/bg/'
# Path to folder where you want the composited images to go
out_path = 'data/merged/'
train_folder = 'data/Combined_Dataset/Training_set/'
# if not os.path.exists('Combined_Dataset'):
zip_file = 'data/Adobe_Deep_Matting_Dataset.zip'
print('Extracting {}...'.format(zip_file))
zip_ref = zipfile.ZipFile(zip_file, 'r')
zip_ref.extractall('data')
zip_ref.close()
if not os.path.exists(bg_path):
zip_file = 'data/train2014.zip'
print('Extracting {}...'.format(zip_file))
zip_ref = zipfile.ZipFile(zip_file, 'r')
zip_ref.extractall('data')
zip_ref.close()
with open(os.path.join(train_folder, 'training_bg_names.txt')) as f:
training_bg_names = f.read().splitlines()
os.makedirs(bg_path)
for bg_name in training_bg_names:
src_path = os.path.join('data/train2014', bg_name)
dest_path = os.path.join(bg_path, bg_name)
shutil.move(src_path, dest_path)
if not os.path.exists(fg_path):
os.makedirs(fg_path)
for old_folder in [train_folder + 'Adobe-licensed images/fg', train_folder + 'Other/fg']:
fg_files = os.listdir(old_folder)
for fg_file in fg_files:
src_path = os.path.join(old_folder, fg_file)
dest_path = os.path.join(fg_path, fg_file)
shutil.move(src_path, dest_path)
if not os.path.exists(a_path):
os.makedirs(a_path)
for old_folder in [train_folder + 'Adobe-licensed images/alpha', train_folder + 'Other/alpha']:
a_files = os.listdir(old_folder)
for a_file in a_files:
src_path = os.path.join(old_folder, a_file)
dest_path = os.path.join(a_path, a_file)
shutil.move(src_path, dest_path)
if not os.path.exists(out_path):
os.makedirs(out_path)
# do_composite()
# path to provided foreground images
fg_test_path = 'data/fg_test/'
# path to provided alpha mattes
a_test_path = 'data/mask_test/'
# Path to background images (PASCAL VOC)
bg_test_path = 'data/bg_test/'
# Path to folder where you want the composited images to go
out_test_path = 'data/merged_test/'
# test data gen
test_folder = 'data/Combined_Dataset/Test_set/'
if not os.path.exists(bg_test_path):
os.makedirs(bg_test_path)
tar_file = 'data/VOCtrainval_14-Jul-2008.tar'
print('Extracting {}...'.format(tar_file))
tar = tarfile.open(tar_file)
tar.extractall('data')
tar.close()
tar_file = 'data/VOC2008test.tar'
print('Extracting {}...'.format(tar_file))
tar = tarfile.open(tar_file)
tar.extractall('data')
tar.close()
with open(os.path.join(test_folder, 'test_bg_names.txt')) as f:
test_bg_names = f.read().splitlines()
for bg_name in test_bg_names:
tokens = bg_name.split('_')
src_path = os.path.join('data/VOCdevkit/VOC2008/JPEGImages', bg_name)
dest_path = os.path.join(bg_test_path, bg_name)
shutil.move(src_path, dest_path)
if not os.path.exists(fg_test_path):
os.makedirs(fg_test_path)
for old_folder in [test_folder + 'Adobe-licensed images/fg']:
fg_files = os.listdir(old_folder)
for fg_file in fg_files:
src_path = os.path.join(old_folder, fg_file)
dest_path = os.path.join(fg_test_path, fg_file)
shutil.move(src_path, dest_path)
if not os.path.exists(a_test_path):
os.makedirs(a_test_path)
for old_folder in [test_folder + 'Adobe-licensed images/alpha']:
a_files = os.listdir(old_folder)
for a_file in a_files:
src_path = os.path.join(old_folder, a_file)
dest_path = os.path.join(a_test_path, a_file)
shutil.move(src_path, dest_path)
if not os.path.exists(out_test_path):
os.makedirs(out_test_path)
do_composite_test()