Niv Sardi commited on
Commit
8beee8d
1 Parent(s): f934e8e

python: introduce markers and crop scripts to help debug

Browse files
Files changed (2) hide show
  1. python/crop.py +26 -0
  2. python/markers.py +21 -0
python/crop.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import argparse
3
+ import imtool
4
+
5
+ parser = argparse.ArgumentParser(description='crop images to train YOLO on squares')
6
+ parser.add_argument('src', metavar='dir', type=str, nargs='+',
7
+ help='dir containing the images')
8
+ parser.add_argument('--dst', dest='dst', type=str, default='./data/squares',
9
+ help='dest dir')
10
+
11
+ args = parser.parse_args()
12
+
13
+ for d in args.src:
14
+ i = 0
15
+ with os.scandir(d) as it:
16
+ for e in it:
17
+ if e.name.endswith('.png') and e.is_file():
18
+ print(e.name)
19
+ label = e.path.replace('images', 'labels').replace('.png', '.txt')
20
+ try:
21
+ i+=1
22
+ bco, boxes = imtool.read_centroids(label)
23
+ imtool.crop(bco, e.path, boxes, args.dst)
24
+
25
+ except Exception as err:
26
+ print(err)
python/markers.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import argparse
3
+ import imtool
4
+
5
+ parser = argparse.ArgumentParser(description='shows YOLO markers')
6
+ parser.add_argument('pngs', metavar='img.png', type=str, nargs='+',
7
+ help='images to debug')
8
+ args = parser.parse_args()
9
+
10
+ for i in args.pngs:
11
+ im = cv2.imread(i)
12
+ label = i.replace('images', 'labels').replace('.png', '.txt').replace('.jpg', '.txt')
13
+ bco, ccs = imtool.read_centroids(label)
14
+ bbs = [c.to_bounding_box(im.shape) for c in ccs]
15
+ for i,b in enumerate(bbs):
16
+ c = (100, 255*i/len(bbs), 255*(1 - i/len(bbs)))
17
+ cv2.rectangle(im, b.start, b.end, c, 5)
18
+
19
+ cv2.imshow('result', im)
20
+ cv2.waitKey(0)
21
+ cv2.destroyAllWindows()