-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcreate_samples.py
52 lines (44 loc) · 2.1 KB
/
create_samples.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
"""create_samples.py
Calls opencv_createsamples with defaults from the output of annotations.txt.
You can optionally specify the values.
Usage:
create_samples.py [--dry-run] [options]
Options:
--help, -h Print this help message.
--dry-run, -d Prints what it would do instead of executing.
--positives=<file>, -p <file> [Default: annotations.txt] The annotations file for positive images.
--negatives=<file>, -n <file> [Default: negatives.txt] The annotations file for negative images.
--output-file=<file, -o <file> [Default: vec.bin] The samples binary output file.
--num=<num>, -N <num> Specify the number of positive bounding boxes to use from the --positives file.
If unspecified, the total number will be automatically determined and used
as a default value.
--width=<px>, -w <px> [Default: 24] Width in pixels of the output samples. Used in training step.
--height=<px>, -H <px> [Default: 24] Height in pixels of the output samples. Used in training step.
--show, -s Shows each image processed.
"""
import docopt
import os
import subprocess
import settings
if __name__ == "__main__":
args = docopt.docopt(__doc__)
dry = args["--dry-run"]
if args["--num"] is None:
args["--num"] = 0
with open(args["--positives"], "r") as f:
for line in f:
args["--num"] += int(line.split(" ")[1])
cmd = [
os.path.join(settings.OPENCV_BIN_DIR, "opencv_createsamples"),
"-vec " + os.path.abspath(args["--output-file"]),
"-info " + os.path.abspath(args["--positives"]),
"-bg " + os.path.abspath(args["--negatives"]),
"-num " + str(args["--num"]),
"-w " + args["--width"],
"-h " + args["--height"],
"-show" if args["--show"] else "",
]
if dry:
print("Would run {}".format(" ".join(cmd)))
else:
subprocess.call(" ".join(cmd), shell=True)