forked from kirill-sidorchuk/lego_detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrepareTestSet.py
64 lines (45 loc) · 1.64 KB
/
PrepareTestSet.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
import argparse
import os
from multiprocessing.pool import Pool
import cv2
from Finetune import IMAGE_WIDTH
from ImageUtils import resize_to_resolution
def prepare_image(src_file, dst_file):
img = cv2.imread(src_file)
if img is None:
return os.path.split(src_file)[1], False
img = resize_to_resolution(img, IMAGE_WIDTH * 2)
# cropping central part
center = (img.shape[1]//2, img.shape[0]//2)
crop_w = img.shape[1]//2
crop_h = crop_w
x0 = (img.shape[1] - crop_w)//2
y0 = (img.shape[0] - crop_h)//2
crop = img[y0:y0 + crop_h, x0:x0 + crop_w]
cv2.imwrite(dst_file, crop)
return os.path.split(src_file)[1], True
def prepare(args):
image_files = os.listdir(args.raw_dir)
print("%d images found" % len(image_files))
out_dir = os.path.join(args.raw_dir, "prepared")
if not os.path.exists(out_dir):
os.mkdir(out_dir)
pool = Pool(4)
futures = []
for img_file in image_files:
full_img_path = os.path.join(args.raw_dir, img_file)
if os.path.isdir(full_img_path):
continue
futures.append(pool.apply_async(prepare_image, (full_img_path, os.path.join(out_dir, img_file))))
# waiting for results
for future in futures:
try:
name, success = future.get()
print("%s : %s" % (name, "ok" if success else "failed"))
except Exception as e:
print("Exception in prepare_image: " + str(e))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Prepare dataset')
parser.add_argument("raw_dir", type=str, help="raw image dir")
_args = parser.parse_args()
prepare(_args)