File size: 1,645 Bytes
8beee8d
 
 
1a24a58
 
 
 
 
8beee8d
 
 
 
 
 
1a24a58
 
 
8beee8d
 
 
1a24a58
 
 
 
 
 
 
 
 
 
 
8beee8d
 
1a24a58
 
 
 
8beee8d
1a24a58
 
 
 
 
8beee8d
1a24a58
 
 
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
import os
import argparse
import imtool
from progress.bar import ChargingBar
import concurrent.futures

PARALLEL = 30
print("🖼 croping augmented data")

parser = argparse.ArgumentParser(description='crop images to train YOLO on squares')
parser.add_argument('src', metavar='dir', type=str, nargs='+',
                    help='dir containing the images')
parser.add_argument('--dst', dest='dst', type=str, default='./data/squares',
                    help='dest dir')
parser.add_argument('--parallel', metavar='parallel', type=int,
                    default=PARALLEL,
                    help='number of concurrent jobs')

args = parser.parse_args()

def process(e):
    if e.name.endswith('.png') and e.is_file():
        # print(e.name)
        label = e.path.replace('images', 'labels').replace('.png', '.txt')
        try:
            id, boxes = imtool.read_centroids(label)
            imtool.crop(id, e.path, boxes, args.dst)

        except Exception as err:
            print(err)

for d in args.src:
    with os.scandir(d) as it:
        with concurrent.futures.ThreadPoolExecutor(max_workers = args.parallel) as executor:
            futures = {executor.submit(process, e): e for e in it}
            count = len(futures.keys())
            bar = ChargingBar('crop', max=count)

            print('waiting for futures')
            for f in concurrent.futures.as_completed(futures):
                e = futures[f]
                try:
                    f.result()
                except Exception as err:
                    print(f'{a}({e}) generated an exception: {err}')
                bar.next()
            bar.finish()