-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompose.py
49 lines (37 loc) · 1.36 KB
/
compose.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
import tensorflow as tf
import torchvision.transforms.functional as TF
from PIL import Image
import numpy as np
import yaml
import os
from src.tensortree import buildTree, rebuildImg
with open('./config.yml', 'r') as stream:
meta = yaml.load(stream)
sdir = meta['crop']['dest']
mod = meta['crop']['mode']
rec = meta['crop']['recpath']
leafs = np.array([int(meta['split']['left']), int(meta['split']['middle']), int(meta['split']['right'])])
nimg = len([file for file in os.listdir(sdir) if os.path.isfile(os.path.join(sdir, file))]) // np.sum(leafs)
if not os.path.exists(rec):
os.makedirs(rec)
else: print('path already exists')
patches = list()
seen = []
has = 0
def save(tensor, name):
tf.keras.utils.save_img(rec+name+'.png', tensor, data_format="channels_first", file_format='png')
for file in sorted(os.listdir(sdir)):
im = Image.open(sdir + '/' + file)
im = TF.to_tensor(im)
if str(os.path.splitext(os.path.basename(file))[0][0:3]) not in seen:
seen.append(os.path.splitext(os.path.basename(file))[0][0:3])
else:
has+=1
patches.append(im)
if has==np.sum(leafs)-1:
tree = buildTree(patches, leafs)
reb = rebuildImg(tree, leafs)
save(reb, str(os.path.splitext(os.path.basename(file))[0][0:3]))
patches.clear()
del tree
has = 0