├── makefile
├── logo.png
├── requirements.txt
├── image-examples
├── 63001.jpg
├── 63002.jpg
├── 63003.jpg
├── 63004.jpg
├── 63005.jpg
├── 63006.jpg
├── 63007.jpg
├── 63008.jpg
├── 63009.jpg
├── 63010.jpg
├── 63011.jpg
├── 63012.jpg
├── 63014.jpg
├── 63015.jpg
├── 63016.jpg
├── 63017.jpg
├── 63019.jpg
├── 63020.jpg
├── 63021.jpg
├── 63022.jpg
├── 63023.jpg
├── 63024.jpg
├── 63025.jpg
├── 63026.jpg
├── 63027.jpg
├── 63029.jpg
├── 63030.jpg
├── 63031.jpg
├── 63032.jpg
├── 63033.jpg
├── 63034.jpg
├── 63035.jpg
├── 63036.jpg
├── 63037.jpg
├── 63038.jpg
├── 63039.jpg
├── 63042.jpg
├── 63043.jpg
├── 63045.jpg
├── 63046.jpg
├── 63048.jpg
├── 63049.jpg
├── 63050.jpg
├── 63051.jpg
├── 63052.jpg
├── 63054.jpg
├── 63056.jpg
├── 63057.jpg
├── 63058.jpg
├── 63061.jpg
├── 63062.jpg
├── 63063.jpg
├── 63064.jpg
├── 63065.jpg
├── 63067.jpg
├── 63069.jpg
├── 63070.jpg
└── 63071.jpg
├── license-images
├── shape_tester.py
├── detector_tester.py
├── preprocessing.py
├── prediction.py
├── .gitignore
├── detector_trainer.py
├── shape_trainer.py
├── landmark-examples
├── csv-example.csv
└── tps-example.tps
├── README.md
├── utils.py
└── LICENSE
/makefile:
--------------------------------------------------------------------------------
1 | all:
2 | pip3 install -r requirements.txt;
3 |
--------------------------------------------------------------------------------
/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/logo.png
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | pandas>=0.22.0
2 | numpy>=1.13.3
3 | opencv-python>=3.4.0.12
4 | dlib>=19.7.0
5 |
--------------------------------------------------------------------------------
/image-examples/63001.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63001.jpg
--------------------------------------------------------------------------------
/image-examples/63002.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63002.jpg
--------------------------------------------------------------------------------
/image-examples/63003.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63003.jpg
--------------------------------------------------------------------------------
/image-examples/63004.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63004.jpg
--------------------------------------------------------------------------------
/image-examples/63005.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63005.jpg
--------------------------------------------------------------------------------
/image-examples/63006.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63006.jpg
--------------------------------------------------------------------------------
/image-examples/63007.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63007.jpg
--------------------------------------------------------------------------------
/image-examples/63008.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63008.jpg
--------------------------------------------------------------------------------
/image-examples/63009.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63009.jpg
--------------------------------------------------------------------------------
/image-examples/63010.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63010.jpg
--------------------------------------------------------------------------------
/image-examples/63011.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63011.jpg
--------------------------------------------------------------------------------
/image-examples/63012.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63012.jpg
--------------------------------------------------------------------------------
/image-examples/63014.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63014.jpg
--------------------------------------------------------------------------------
/image-examples/63015.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63015.jpg
--------------------------------------------------------------------------------
/image-examples/63016.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63016.jpg
--------------------------------------------------------------------------------
/image-examples/63017.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63017.jpg
--------------------------------------------------------------------------------
/image-examples/63019.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63019.jpg
--------------------------------------------------------------------------------
/image-examples/63020.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63020.jpg
--------------------------------------------------------------------------------
/image-examples/63021.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63021.jpg
--------------------------------------------------------------------------------
/image-examples/63022.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63022.jpg
--------------------------------------------------------------------------------
/image-examples/63023.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63023.jpg
--------------------------------------------------------------------------------
/image-examples/63024.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63024.jpg
--------------------------------------------------------------------------------
/image-examples/63025.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63025.jpg
--------------------------------------------------------------------------------
/image-examples/63026.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63026.jpg
--------------------------------------------------------------------------------
/image-examples/63027.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63027.jpg
--------------------------------------------------------------------------------
/image-examples/63029.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63029.jpg
--------------------------------------------------------------------------------
/image-examples/63030.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63030.jpg
--------------------------------------------------------------------------------
/image-examples/63031.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63031.jpg
--------------------------------------------------------------------------------
/image-examples/63032.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63032.jpg
--------------------------------------------------------------------------------
/image-examples/63033.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63033.jpg
--------------------------------------------------------------------------------
/image-examples/63034.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63034.jpg
--------------------------------------------------------------------------------
/image-examples/63035.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63035.jpg
--------------------------------------------------------------------------------
/image-examples/63036.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63036.jpg
--------------------------------------------------------------------------------
/image-examples/63037.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63037.jpg
--------------------------------------------------------------------------------
/image-examples/63038.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63038.jpg
--------------------------------------------------------------------------------
/image-examples/63039.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63039.jpg
--------------------------------------------------------------------------------
/image-examples/63042.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63042.jpg
--------------------------------------------------------------------------------
/image-examples/63043.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63043.jpg
--------------------------------------------------------------------------------
/image-examples/63045.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63045.jpg
--------------------------------------------------------------------------------
/image-examples/63046.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63046.jpg
--------------------------------------------------------------------------------
/image-examples/63048.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63048.jpg
--------------------------------------------------------------------------------
/image-examples/63049.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63049.jpg
--------------------------------------------------------------------------------
/image-examples/63050.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63050.jpg
--------------------------------------------------------------------------------
/image-examples/63051.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63051.jpg
--------------------------------------------------------------------------------
/image-examples/63052.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63052.jpg
--------------------------------------------------------------------------------
/image-examples/63054.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63054.jpg
--------------------------------------------------------------------------------
/image-examples/63056.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63056.jpg
--------------------------------------------------------------------------------
/image-examples/63057.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63057.jpg
--------------------------------------------------------------------------------
/image-examples/63058.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63058.jpg
--------------------------------------------------------------------------------
/image-examples/63061.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63061.jpg
--------------------------------------------------------------------------------
/image-examples/63062.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63062.jpg
--------------------------------------------------------------------------------
/image-examples/63063.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63063.jpg
--------------------------------------------------------------------------------
/image-examples/63064.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63064.jpg
--------------------------------------------------------------------------------
/image-examples/63065.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63065.jpg
--------------------------------------------------------------------------------
/image-examples/63067.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63067.jpg
--------------------------------------------------------------------------------
/image-examples/63069.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63069.jpg
--------------------------------------------------------------------------------
/image-examples/63070.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63070.jpg
--------------------------------------------------------------------------------
/image-examples/63071.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/agporto/ml-morph/HEAD/image-examples/63071.jpg
--------------------------------------------------------------------------------
/license-images:
--------------------------------------------------------------------------------
1 | All images were downloaded from Morphbank (www.morphbank.net/) and have been submitted by David Houle (http://www.morphbank.net/?id=23).
2 | These images are licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
3 | To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/us/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
4 |
5 |
6 |
--------------------------------------------------------------------------------
/shape_tester.py:
--------------------------------------------------------------------------------
1 | # Part of the standard library
2 | import os
3 | import sys
4 | import glob
5 | import argparse
6 | # Not part of the standard library
7 | import dlib
8 |
9 | #Parsing arguments
10 | ap = argparse.ArgumentParser()
11 | ap.add_argument("-t", "--test", type=str, default='test.xml',
12 | help="test data: xml filename", metavar='')
13 | ap.add_argument("-p", "--predictor", type=str, default='predictor.dat',
14 | help="trained shape predictor", metavar='')
15 |
16 | args = vars(ap.parse_args())
17 |
18 |
19 | test_path = os.path.join('./', args['test'])
20 | print("Testing error (mean pixel deviation): {}".format(
21 | dlib.test_shape_predictor(test_path, args['predictor'])))
--------------------------------------------------------------------------------
/detector_tester.py:
--------------------------------------------------------------------------------
1 | # Part of the standard library
2 | import os
3 | import sys
4 | import glob
5 | import argparse
6 | # Not part of the standard library
7 | import dlib
8 |
9 | #Parsing arguments
10 | ap = argparse.ArgumentParser()
11 | ap.add_argument("-t", "--test", type=str, default='test.xml',
12 | help="test data (default=test.xml)", metavar='')
13 | ap.add_argument("-d", "--detector", type=str, default='detector.svm',
14 | help="trained object detector (default=detector.svm)", metavar='')
15 |
16 | args = vars(ap.parse_args())
17 |
18 |
19 | test_path = os.path.join('./', args['test'])
20 | print("Testing - {}".format(
21 | dlib.test_simple_object_detector(test_path, args['detector'])))
22 |
--------------------------------------------------------------------------------
/preprocessing.py:
--------------------------------------------------------------------------------
1 | import argparse
2 | import os
3 | import utils
4 |
5 |
6 | ap = argparse.ArgumentParser()
7 | ap.add_argument('-i','--input-dir', type=str, default='images', help="input directory containing image files (default = images)", metavar='')
8 | ap.add_argument('-c','--csv-file', type=str, default=None, help="(optional) XY coordinate file in csv format", metavar='')
9 | ap.add_argument('-t','--tps-file', type=str, default=None, help="(optional) tps coordinate file", metavar='')
10 |
11 |
12 |
13 | args = vars(ap.parse_args())
14 |
15 | assert os.path.isdir(args['input_dir']), "Could not find the folder {}".format(args['input_dir'])
16 |
17 | file_sizes=utils.split_train_test(args['input_dir'])
18 |
19 | if args['csv_file'] is not None:
20 | dict_csv=utils.read_csv(args['csv_file'])
21 | utils.generate_dlib_xml(dict_csv,file_sizes['train'],folder='train',out_file='train.xml')
22 | utils.generate_dlib_xml(dict_csv,file_sizes['test'],folder='test',out_file='test.xml')
23 |
24 | if args['tps_file'] is not None:
25 | dict_tps=utils.read_tps(args['tps_file'])
26 | utils.generate_dlib_xml(dict_tps,file_sizes['train'],folder='train',out_file='train.xml')
27 | utils.generate_dlib_xml(dict_tps,file_sizes['test'],folder='test',out_file='test.xml')
28 |
--------------------------------------------------------------------------------
/prediction.py:
--------------------------------------------------------------------------------
1 | import argparse
2 | import os
3 | import utils
4 |
5 |
6 | ap = argparse.ArgumentParser()
7 | ap.add_argument('-i','--input-dir', type=str, default='pred', help="input directory (default = pred)", metavar='')
8 | ap.add_argument('-d','--detector', type=str, default='detector.svm', help="trained object detection model (default = detector.svm)", metavar='')
9 | ap.add_argument('-p','--predictor', type=str, default='predictor.dat', help="trained shape prediction model (default = predictor.dat)", metavar='')
10 | ap.add_argument('-o','--out-file', type=str, default='output.xml', help="output file name (default = output.xml)", metavar='')
11 | ap.add_argument('-u','--upsample-limit', type=int, default=0, help="upsample limit (default= 0 ; max = 2)", metavar='')
12 | ap.add_argument('-t','--threshold', type=float, default=0, help="detector's confidence threshold for outputting an object (default= 0)", metavar='')
13 | ap.add_argument('-l','--ignore-list', nargs='*', type=int, default=None, help=" (optional) prevents landmarks of choice from being output", metavar='')
14 |
15 |
16 |
17 |
18 |
19 |
20 | args = vars(ap.parse_args())
21 |
22 | utils.predictions_to_xml(args['detector'],args['predictor'], dir=args['input_dir'],upsample=args['upsample_limit'],threshold=args['threshold'],ignore=args['ignore_list'],out_file=args['out_file'])
23 |
24 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | *.egg-info/
24 | .installed.cfg
25 | *.egg
26 | MANIFEST
27 |
28 | # PyInstaller
29 | # Usually these files are written by a python script from a template
30 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
31 | *.manifest
32 | *.spec
33 |
34 | # Installer logs
35 | pip-log.txt
36 | pip-delete-this-directory.txt
37 |
38 | # Unit test / coverage reports
39 | htmlcov/
40 | .tox/
41 | .coverage
42 | .coverage.*
43 | .cache
44 | nosetests.xml
45 | coverage.xml
46 | *.cover
47 | .hypothesis/
48 | .pytest_cache/
49 |
50 | # Translations
51 | *.mo
52 | *.pot
53 |
54 | # Django stuff:
55 | *.log
56 | local_settings.py
57 | db.sqlite3
58 |
59 | # Flask stuff:
60 | instance/
61 | .webassets-cache
62 |
63 | # Scrapy stuff:
64 | .scrapy
65 |
66 | # Sphinx documentation
67 | docs/_build/
68 |
69 | # PyBuilder
70 | target/
71 |
72 | # Jupyter Notebook
73 | .ipynb_checkpoints
74 |
75 | # pyenv
76 | .python-version
77 |
78 | # celery beat schedule file
79 | celerybeat-schedule
80 |
81 | # SageMath parsed files
82 | *.sage.py
83 |
84 | # Environments
85 | .env
86 | .venv
87 | env/
88 | venv/
89 | ENV/
90 | env.bak/
91 | venv.bak/
92 |
93 | # Spyder project settings
94 | .spyderproject
95 | .spyproject
96 |
97 | # Rope project settings
98 | .ropeproject
99 |
100 | # mkdocs documentation
101 | /site
102 |
103 | # mypy
104 | .mypy_cache/
105 |
--------------------------------------------------------------------------------
/detector_trainer.py:
--------------------------------------------------------------------------------
1 | # Part of the standard library
2 | import os
3 | import sys
4 | import glob
5 | import argparse
6 | # Not part of the standard library
7 | import dlib
8 |
9 | #Parsing arguments
10 | ap = argparse.ArgumentParser()
11 | ap.add_argument("-d", "--dataset", type=str, default='train.xml',
12 | help="training data (default = train.xml)", metavar='')
13 | ap.add_argument("-t", "--test", type=str, default=None,
14 | help="(optional) test data. if not provided, the model is not tested", metavar='')
15 | ap.add_argument("-o", "--out", type=str, default='detector',
16 | help="output filename (default = detector)", metavar='')
17 | ap.add_argument("-n", "--n-threads", type=int, default=1,
18 | help="number of threads to be used (default = 1)", metavar='')
19 | ap.add_argument("-s", "--symmetrical", type=bool, default=False,
20 | help="(True/False) indicating whether objects are bilaterally symmetrical (default = False)", metavar='')
21 | ap.add_argument("-e", "--epsilon", type=float, default=0.01,
22 | help="insensitivity parameter (default = 0.01)", metavar='')
23 | ap.add_argument("-c", "--c-param", type=float, default=5,
24 | help="soft margin parameter C (default =5)", metavar='')
25 | ap.add_argument("-u", "--upsample", type=int, default=0,
26 | help="upsample limit (default = 0)", metavar='')
27 | ap.add_argument("-w", "--window-size", type=int, default=None,
28 | help="(optional) detection window size", metavar='')
29 | args = vars(ap.parse_args())
30 |
31 | #Setting up the training parameters
32 | options = dlib.simple_object_detector_training_options()
33 | options.add_left_right_image_flips = args['symmetrical']
34 | options.C = args['c_param']
35 | options.num_threads = args['n_threads']
36 | options.be_verbose = True
37 | options.epsilon=args['epsilon']
38 | options.upsample_limit=args['upsample']
39 | if args['window_size'] is not None:
40 | options.detection_window_size=args['window_size']
41 |
42 | #Training the model
43 | train_path = os.path.join('./', args['dataset'])
44 | dlib.train_simple_object_detector(train_path, args['out']+".svm", options)
45 | print("Training - {}".format(
46 | dlib.test_simple_object_detector(train_path, args['out']+".svm")))
47 |
48 | #Testing the model (if test data was provided)
49 | if args['test'] is not None:
50 | test_path = os.path.join('./', args['test'])
51 | print("Testing - {}".format(
52 | dlib.test_simple_object_detector(test_path, args['out']+".svm")))
53 |
--------------------------------------------------------------------------------
/shape_trainer.py:
--------------------------------------------------------------------------------
1 | # Part of the standard library
2 | import os
3 | import sys
4 | import glob
5 | import argparse
6 | # Not part of the standard library
7 | import dlib
8 |
9 | #Parsing arguments
10 | ap = argparse.ArgumentParser()
11 | ap.add_argument("-d", "--dataset", type=str, default='train.xml',
12 | help="training data (default = train.xml)", metavar='')
13 | ap.add_argument("-t", "--test", type=str, default=None,
14 | help="test data (default = test.xml).if not provided, no testing is done", metavar='')
15 | ap.add_argument("-o", "--out", type=str, default='predictor',
16 | help="output filename (default = predictor)", metavar='')
17 | ap.add_argument("-th", "--threads", type=int, default=1,
18 | help="number of threads to be used (default = 1)", metavar='')
19 | ap.add_argument("-dp", "--tree-depth", type=int, default=4,
20 | help="choice of tree depth (default = 4)", metavar='')
21 | ap.add_argument("-c", "--cascade-depth", type=int, default=15,
22 | help="choice of cascade depth (default = 15)", metavar='')
23 | ap.add_argument("-nu", "--nu", type=float, default=0.1,
24 | help="regularization parameter (default = 0.1)", metavar='')
25 | ap.add_argument("-os", "--oversampling", type=int, default=10,
26 | help="oversampling amount (default = 10)", metavar='')
27 | ap.add_argument("-s", "--test-splits", type=int, default=20,
28 | help="number of test splits (default = 20)", metavar='')
29 | ap.add_argument("-f", "--feature-pool-size", type=int, default=500,
30 | help="choice of feature pool size (default = 500)", metavar='')
31 | ap.add_argument("-n", "--num-trees", type=int, default=500,
32 | help="number of regression trees (default = 500)", metavar='')
33 | args = vars(ap.parse_args())
34 |
35 | #Setting up the training parameters
36 | options = dlib.shape_predictor_training_options()
37 | options.num_trees_per_cascade_level=args['num_trees']
38 | options.nu = args['nu']
39 | options.num_threads=args['threads']
40 | options.tree_depth = args['tree_depth']
41 | options.cascade_depth = args['cascade_depth']
42 | options.feature_pool_size = args['feature_pool_size']
43 | options.num_test_splits = args['test_splits']
44 | options.oversampling_amount = args['oversampling']
45 | options.be_verbose = True
46 |
47 | #Training the model
48 | train_path = os.path.join('./', args['dataset'])
49 | dlib.train_shape_predictor(train_path, args['out']+".dat", options)
50 | print("Training error (average pixel deviation): {}".format(
51 | dlib.test_shape_predictor(train_path, args['out']+".dat")))
52 |
53 | #Testing the model (if test data was provided)
54 | if args['test'] is not None:
55 | test_path = os.path.join('./', args['test'])
56 | print("Testing error (average pixel deviation): {}".format(
57 | dlib.test_shape_predictor(test_path, args['out']+".dat")))
--------------------------------------------------------------------------------
/landmark-examples/csv-example.csv:
--------------------------------------------------------------------------------
1 | id,X1,Y1,X2,Y2,X3,Y3,X4,Y4,X5,Y5,X6,Y6,X7,Y7,X8,Y8,X9,Y9,X10,Y10,X11,Y11,X12,Y12
2 | 63001.jpg,290,108,100,179,73,229,158,320,476,310,572,301,352,169,345,211,445,242,441,257,510,269,534,230
3 | 63002.jpg,284,76,55,156,24,210,120,313,487,323,602,324,353,157,343,203,454,246,447,261,529,279,560,238
4 | 63003.jpg,278,64,51,159,30,208,124,308,494,301,608,297,351,138,348,188,457,225,452,244,539,258,560,211
5 | 63004.jpg,283,152,68,236,46,284,136,387,488,392,597,393,356,229,344,276,453,316,448,332,534,348,558,311
6 | 63005.jpg,269,123,59,216,36,268,129,365,481,353,601,346,334,193,320,242,435,276,432,294,520,304,549,263
7 | 63006.jpg,246,129,37,219,19,270,107,363,460,357,574,350,316,196,310,246,424,281,417,296,506,308,530,266
8 | 63007.jpg,257,107,46,194,23,243,112,339,464,336,582,335,325,177,319,226,428,261,426,278,514,291,535,250
9 | 63008.jpg,275,126,94,204,73,250,157,342,468,330,563,324,332,191,326,232,431,264,428,279,502,288,523,251
10 | 63009.jpg,276,114,94,187,73,235,154,322,461,316,566,308,342,180,334,219,434,249,430,263,495,275,517,238
11 | 63010.jpg,282,130,106,219,87,268,177,356,484,321,585,307,349,192,346,231,445,256,441,271,519,277,537,234
12 | 63011.jpg,274,113,101,189,79,239,165,327,469,317,567,305,344,179,336,217,438,251,436,265,509,272,526,232
13 | 63012.jpg,276,135,93,216,72,266,171,355,469,330,567,323,343,202,336,239,440,267,436,280,504,289,527,251
14 | 63014.jpg,248,118,31,214,10,265,119,362,472,341,589,327,317,190,305,234,431,269,427,284,507,293,539,248
15 | 63015.jpg,250,111,36,209,15,263,115,357,488,331,606,316,323,178,319,227,448,257,446,275,527,280,558,237
16 | 63016.jpg,248,103,33,207,14,256,117,352,467,332,585,321,316,173,310,225,426,253,421,271,502,284,533,237
17 | 63017.jpg,248,105,40,194,16,248,109,340,468,329,576,327,321,173,310,219,431,255,425,274,512,282,537,240
18 | 63019.jpg,284,144,104,220,77,275,169,361,473,342,572,330,349,209,344,247,443,275,437,291,507,298,529,265
19 | 63020.jpg,299,140,114,217,90,264,180,355,478,346,573,341,360,207,349,249,447,280,441,293,510,306,530,273
20 | 63021.jpg,281,137,94,213,70,259,157,352,465,343,565,340,347,203,336,241,438,276,433,291,506,303,528,265
21 | 63022.jpg,281,135,102,216,81,271,169,359,474,330,570,320,353,202,345,240,436,265,432,283,502,289,526,250
22 | 63023.jpg,287,128,103,203,77,258,163,348,480,334,584,332,349,192,342,235,439,267,434,284,509,295,538,253
23 | 63024.jpg,273,123,84,205,64,256,153,345,465,330,569,322,340,189,332,229,430,260,426,274,495,284,524,244
24 | 63025.jpg,253,108,39,204,19,260,122,358,482,332,597,326,321,175,318,224,436,260,430,279,514,287,545,237
25 | 63026.jpg,251,106,36,197,13,253,112,352,472,336,585,329,315,173,308,223,425,258,420,277,500,284,533,239
26 | 63027.jpg,262,95,36,187,15,245,115,344,484,332,600,322,325,164,318,219,442,254,436,272,519,281,551,236
27 | 63029.jpg,251,87,46,175,19,226,113,315,465,314,575,305,321,162,312,202,424,240,421,255,499,267,531,221
28 | 63030.jpg,254,113,38,201,16,250,114,345,461,348,571,341,323,186,316,230,420,270,418,284,498,300,529,260
29 | 63031.jpg,294,123,114,201,92,246,182,329,476,314,577,298,348,180,344,218,444,246,440,260,506,266,530,229
30 | 63032.jpg,284,119,99,197,80,246,164,339,469,328,566,317,352,190,342,226,438,259,435,273,503,286,529,248
31 | 63033.jpg,315,110,130,183,104,233,192,319,496,305,594,298,369,172,366,207,464,240,461,256,523,265,553,227
32 | 63034.jpg,286,108,100,189,78,230,181,319,476,296,569,287,347,170,342,207,436,234,435,250,498,256,527,222
33 | 63035.jpg,288,125,103,212,81,255,191,342,472,312,566,299,352,184,347,222,442,246,440,261,498,269,526,227
34 | 63036.jpg,314,146,141,228,128,271,233,347,489,325,581,312,374,202,367,237,454,264,453,278,520,284,542,247
35 | 63037.jpg,243,138,31,256,17,311,123,393,480,339,589,317,316,200,316,247,427,269,422,286,516,287,538,244
36 | 63038.jpg,270,91,37,197,19,246,128,339,490,312,594,305,335,162,328,210,442,240,440,259,521,268,552,222
37 | 63039.jpg,263,117,36,219,20,273,127,360,486,336,596,329,332,183,329,230,443,264,440,280,525,290,556,251
38 | 63042.jpg,259,97,33,193,12,243,119,335,477,323,588,320,322,161,311,210,431,247,426,264,512,277,544,230
39 | 63043.jpg,290,113,101,191,78,246,162,337,476,320,574,310,349,180,343,223,438,252,433,267,504,277,532,236
40 | 63045.jpg,257,96,42,187,19,241,120,339,472,321,581,319,317,160,311,216,421,246,418,263,504,274,532,233
41 | 63046.jpg,254,114,44,206,21,257,109,351,480,350,595,347,321,188,316,236,443,273,436,291,521,304,548,260
42 | 63048.jpg,244,105,32,201,15,255,110,346,466,327,573,317,312,172,310,220,422,252,418,269,503,280,524,234
43 | 63049.jpg,282,104,94,184,71,232,156,322,471,317,571,311,343,174,338,211,442,247,438,262,509,274,530,234
44 | 63050.jpg,279,122,94,200,74,244,168,337,470,323,565,313,342,183,331,222,442,254,439,268,511,279,524,238
45 | 63051.jpg,283,137,108,214,88,257,173,350,470,335,566,328,347,199,339,236,434,269,432,284,505,292,525,255
46 | 63052.jpg,264,136,94,214,73,261,154,350,463,326,559,315,333,198,332,237,426,263,424,277,498,283,514,245
47 | 63054.jpg,279,131,93,201,71,247,155,342,465,332,562,327,345,196,338,236,431,263,428,280,498,293,523,254
48 | 63056.jpg,247,107,36,199,12,248,106,344,471,329,572,324,315,174,311,224,424,256,420,274,504,282,531,239
49 | 63057.jpg,263,111,46,202,19,251,113,355,474,343,579,337,321,181,310,233,427,268,424,285,520,297,536,251
50 | 63058.jpg,248,86,44,174,17,222,106,316,479,308,596,304,323,154,319,198,432,234,427,250,519,264,548,219
51 | 63061.jpg,298,129,121,213,101,264,188,342,480,319,574,308,355,189,350,226,451,254,447,267,512,275,534,238
52 | 63062.jpg,270,121,93,202,71,248,151,341,465,325,563,316,336,188,332,228,432,259,430,273,499,284,522,245
53 | 63063.jpg,276,118,88,202,68,247,162,339,474,323,571,311,340,183,334,222,440,251,435,267,505,276,530,240
54 | 63064.jpg,310,114,115,198,92,251,190,340,493,322,592,316,364,181,355,222,462,254,459,270,526,279,552,242
55 | 63065.jpg,282,108,89,189,70,246,171,334,465,302,568,293,337,168,333,208,433,236,431,251,502,260,527,220
56 | 63067.jpg,274,116,47,219,27,275,137,368,491,339,607,334,332,180,325,234,455,266,450,285,535,293,563,250
57 | 63069.jpg,258,89,35,180,11,235,119,334,469,320,582,314,321,154,312,207,435,242,432,259,518,273,542,231
58 | 63070.jpg,252,98,42,188,21,237,113,337,466,327,580,326,311,165,310,218,424,251,420,270,512,282,535,241
59 | 63071.jpg,257,90,39,187,15,243,115,339,477,324,583,319,322,159,322,210,439,245,436,262,514,276,542,231
60 |
--------------------------------------------------------------------------------
/landmark-examples/tps-example.tps:
--------------------------------------------------------------------------------
1 | LM=12
2 | 290. 108.
3 | 100. 179.
4 | 73. 229.
5 | 158. 320.
6 | 476. 310.
7 | 572. 301.
8 | 352. 169.
9 | 345. 211.
10 | 445. 242.
11 | 441. 257.
12 | 510. 269.
13 | 534. 230.
14 | IMAGE=63001.jpg
15 | ID=0
16 | LM=12
17 | 284. 76.
18 | 55. 156.
19 | 24. 210.
20 | 120. 313.
21 | 487. 323.
22 | 602. 324.
23 | 353. 157.
24 | 343. 203.
25 | 454. 246.
26 | 447. 261.
27 | 529. 279.
28 | 560. 238.
29 | IMAGE=63002.jpg
30 | ID=1
31 | LM=12
32 | 278. 64.
33 | 51. 159.
34 | 30. 208.
35 | 124. 308.
36 | 494. 301.
37 | 608. 297.
38 | 351. 138.
39 | 348. 188.
40 | 457. 225.
41 | 452. 244.
42 | 539. 258.
43 | 560. 211.
44 | IMAGE=63003.jpg
45 | ID=2
46 | LM=12
47 | 283. 152.
48 | 68. 236.
49 | 46. 284.
50 | 136. 387.
51 | 488. 392.
52 | 597. 393.
53 | 356. 229.
54 | 344. 276.
55 | 453. 316.
56 | 448. 332.
57 | 534. 348.
58 | 558. 311.
59 | IMAGE=63004.jpg
60 | ID=3
61 | LM=12
62 | 269. 123.
63 | 59. 216.
64 | 36. 268.
65 | 129. 365.
66 | 481. 353.
67 | 601. 346.
68 | 334. 193.
69 | 320. 242.
70 | 435. 276.
71 | 432. 294.
72 | 520. 304.
73 | 549. 263.
74 | IMAGE=63005.jpg
75 | ID=4
76 | LM=12
77 | 246. 129.
78 | 37. 219.
79 | 19. 270.
80 | 107. 363.
81 | 460. 357.
82 | 574. 350.
83 | 316. 196.
84 | 310. 246.
85 | 424. 281.
86 | 417. 296.
87 | 506. 308.
88 | 530. 266.
89 | IMAGE=63006.jpg
90 | ID=5
91 | LM=12
92 | 257. 107.
93 | 46. 194.
94 | 23. 243.
95 | 112. 339.
96 | 464. 336.
97 | 582. 335.
98 | 325. 177.
99 | 319. 226.
100 | 428. 261.
101 | 426. 278.
102 | 514. 291.
103 | 535. 250.
104 | IMAGE=63007.jpg
105 | ID=6
106 | LM=12
107 | 275. 126.
108 | 94. 204.
109 | 73. 250.
110 | 157. 342.
111 | 468. 330.
112 | 563. 324.
113 | 332. 191.
114 | 326. 232.
115 | 431. 264.
116 | 428. 279.
117 | 502. 288.
118 | 523. 251.
119 | IMAGE=63008.jpg
120 | ID=7
121 | LM=12
122 | 276. 114.
123 | 94. 187.
124 | 73. 235.
125 | 154. 322.
126 | 461. 316.
127 | 566. 308.
128 | 342. 180.
129 | 334. 219.
130 | 434. 249.
131 | 430. 263.
132 | 495. 275.
133 | 517. 238.
134 | IMAGE=63009.jpg
135 | ID=8
136 | LM=12
137 | 282. 130.
138 | 106. 219.
139 | 87. 268.
140 | 177. 356.
141 | 484. 321.
142 | 585. 307.
143 | 349. 192.
144 | 346. 231.
145 | 445. 256.
146 | 441. 271.
147 | 519. 277.
148 | 537. 234.
149 | IMAGE=63010.jpg
150 | ID=9
151 | LM=12
152 | 274. 113.
153 | 101. 189.
154 | 79. 239.
155 | 165. 327.
156 | 469. 317.
157 | 567. 305.
158 | 344. 179.
159 | 336. 217.
160 | 438. 251.
161 | 436. 265.
162 | 509. 272.
163 | 526. 232.
164 | IMAGE=63011.jpg
165 | ID=10
166 | LM=12
167 | 276. 135.
168 | 93. 216.
169 | 72. 266.
170 | 171. 355.
171 | 469. 330.
172 | 567. 323.
173 | 343. 202.
174 | 336. 239.
175 | 440. 267.
176 | 436. 280.
177 | 504. 289.
178 | 527. 251.
179 | IMAGE=63012.jpg
180 | ID=11
181 | LM=12
182 | 248. 118.
183 | 31. 214.
184 | 10. 265.
185 | 119. 362.
186 | 472. 341.
187 | 589. 327.
188 | 317. 190.
189 | 305. 234.
190 | 431. 269.
191 | 427. 284.
192 | 507. 293.
193 | 539. 248.
194 | IMAGE=63014.jpg
195 | ID=12
196 | LM=12
197 | 250. 111.
198 | 36. 209.
199 | 15. 263.
200 | 115. 357.
201 | 488. 331.
202 | 606. 316.
203 | 323. 178.
204 | 319. 227.
205 | 448. 257.
206 | 446. 275.
207 | 527. 280.
208 | 558. 237.
209 | IMAGE=63015.jpg
210 | ID=13
211 | LM=12
212 | 248. 103.
213 | 33. 207.
214 | 14. 256.
215 | 117. 352.
216 | 467. 332.
217 | 585. 321.
218 | 316. 173.
219 | 310. 225.
220 | 426. 253.
221 | 421. 271.
222 | 502. 284.
223 | 533. 237.
224 | IMAGE=63016.jpg
225 | ID=14
226 | LM=12
227 | 248. 105.
228 | 40. 194.
229 | 16. 248.
230 | 109. 340.
231 | 468. 329.
232 | 576. 327.
233 | 321. 173.
234 | 310. 219.
235 | 431. 255.
236 | 425. 274.
237 | 512. 282.
238 | 537. 240.
239 | IMAGE=63017.jpg
240 | ID=15
241 | LM=12
242 | 284. 144.
243 | 104. 220.
244 | 77. 275.
245 | 169. 361.
246 | 473. 342.
247 | 572. 330.
248 | 349. 209.
249 | 344. 247.
250 | 443. 275.
251 | 437. 291.
252 | 507. 298.
253 | 529. 265.
254 | IMAGE=63019.jpg
255 | ID=16
256 | LM=12
257 | 299. 140.
258 | 114. 217.
259 | 90. 264.
260 | 180. 355.
261 | 478. 346.
262 | 573. 341.
263 | 360. 207.
264 | 349. 249.
265 | 447. 280.
266 | 441. 293.
267 | 510. 306.
268 | 530. 273.
269 | IMAGE=63020.jpg
270 | ID=17
271 | LM=12
272 | 281. 137.
273 | 94. 213.
274 | 70. 259.
275 | 157. 352.
276 | 465. 343.
277 | 565. 340.
278 | 347. 203.
279 | 336. 241.
280 | 438. 276.
281 | 433. 291.
282 | 506. 303.
283 | 528. 265.
284 | IMAGE=63021.jpg
285 | ID=18
286 | LM=12
287 | 281. 135.
288 | 102. 216.
289 | 81. 271.
290 | 169. 359.
291 | 474. 330.
292 | 570. 320.
293 | 353. 202.
294 | 345. 240.
295 | 436. 265.
296 | 432. 283.
297 | 502. 289.
298 | 526. 250.
299 | IMAGE=63022.jpg
300 | ID=19
301 | LM=12
302 | 287. 128.
303 | 103. 203.
304 | 77. 258.
305 | 163. 348.
306 | 480. 334.
307 | 584. 332.
308 | 349. 192.
309 | 342. 235.
310 | 439. 267.
311 | 434. 284.
312 | 509. 295.
313 | 538. 253.
314 | IMAGE=63023.jpg
315 | ID=20
316 | LM=12
317 | 273. 123.
318 | 84. 205.
319 | 64. 256.
320 | 153. 345.
321 | 465. 330.
322 | 569. 322.
323 | 340. 189.
324 | 332. 229.
325 | 430. 260.
326 | 426. 274.
327 | 495. 284.
328 | 524. 244.
329 | IMAGE=63024.jpg
330 | ID=21
331 | LM=12
332 | 253. 108.
333 | 39. 204.
334 | 19. 260.
335 | 122. 358.
336 | 482. 332.
337 | 597. 326.
338 | 321. 175.
339 | 318. 224.
340 | 436. 260.
341 | 430. 279.
342 | 514. 287.
343 | 545. 237.
344 | IMAGE=63025.jpg
345 | ID=22
346 | LM=12
347 | 251. 106.
348 | 36. 197.
349 | 13. 253.
350 | 112. 352.
351 | 472. 336.
352 | 585. 329.
353 | 315. 173.
354 | 308. 223.
355 | 425. 258.
356 | 420. 277.
357 | 500. 284.
358 | 533. 239.
359 | IMAGE=63026.jpg
360 | ID=23
361 | LM=12
362 | 262. 95.
363 | 36. 187.
364 | 15. 245.
365 | 115. 344.
366 | 484. 332.
367 | 600. 322.
368 | 325. 164.
369 | 318. 219.
370 | 442. 254.
371 | 436. 272.
372 | 519. 281.
373 | 551. 236.
374 | IMAGE=63027.jpg
375 | ID=24
376 | LM=12
377 | 251. 87.
378 | 46. 175.
379 | 19. 226.
380 | 113. 315.
381 | 465. 314.
382 | 575. 305.
383 | 321. 162.
384 | 312. 202.
385 | 424. 240.
386 | 421. 255.
387 | 499. 267.
388 | 531. 221.
389 | IMAGE=63029.jpg
390 | ID=25
391 | LM=12
392 | 254. 113.
393 | 38. 201.
394 | 16. 250.
395 | 114. 345.
396 | 461. 348.
397 | 571. 341.
398 | 323. 186.
399 | 316. 230.
400 | 420. 270.
401 | 418. 284.
402 | 498. 300.
403 | 529. 260.
404 | IMAGE=63030.jpg
405 | ID=26
406 | LM=12
407 | 294. 123.
408 | 114. 201.
409 | 92. 246.
410 | 182. 329.
411 | 476. 314.
412 | 577. 298.
413 | 348. 180.
414 | 344. 218.
415 | 444. 246.
416 | 440. 260.
417 | 506. 266.
418 | 530. 229.
419 | IMAGE=63031.jpg
420 | ID=27
421 | LM=12
422 | 284. 119.
423 | 99. 197.
424 | 80. 246.
425 | 164. 339.
426 | 469. 328.
427 | 566. 317.
428 | 352. 190.
429 | 342. 226.
430 | 438. 259.
431 | 435. 273.
432 | 503. 286.
433 | 529. 248.
434 | IMAGE=63032.jpg
435 | ID=28
436 | LM=12
437 | 315. 110.
438 | 130. 183.
439 | 104. 233.
440 | 192. 319.
441 | 496. 305.
442 | 594. 298.
443 | 369. 172.
444 | 366. 207.
445 | 464. 240.
446 | 461. 256.
447 | 523. 265.
448 | 553. 227.
449 | IMAGE=63033.jpg
450 | ID=29
451 | LM=12
452 | 286. 108.
453 | 100. 189.
454 | 78. 230.
455 | 181. 319.
456 | 476. 296.
457 | 569. 287.
458 | 347. 170.
459 | 342. 207.
460 | 436. 234.
461 | 435. 250.
462 | 498. 256.
463 | 527. 222.
464 | IMAGE=63034.jpg
465 | ID=30
466 | LM=12
467 | 288. 125.
468 | 103. 212.
469 | 81. 255.
470 | 191. 342.
471 | 472. 312.
472 | 566. 299.
473 | 352. 184.
474 | 347. 222.
475 | 442. 246.
476 | 440. 261.
477 | 498. 269.
478 | 526. 227.
479 | IMAGE=63035.jpg
480 | ID=31
481 | LM=12
482 | 314. 146.
483 | 141. 228.
484 | 128. 271.
485 | 233. 347.
486 | 489. 325.
487 | 581. 312.
488 | 374. 202.
489 | 367. 237.
490 | 454. 264.
491 | 453. 278.
492 | 520. 284.
493 | 542. 247.
494 | IMAGE=63036.jpg
495 | ID=32
496 | LM=12
497 | 243. 138.
498 | 31. 256.
499 | 17. 311.
500 | 123. 393.
501 | 480. 339.
502 | 589. 317.
503 | 316. 200.
504 | 316. 247.
505 | 427. 269.
506 | 422. 286.
507 | 516. 287.
508 | 538. 244.
509 | IMAGE=63037.jpg
510 | ID=33
511 | LM=12
512 | 270. 91.
513 | 37. 197.
514 | 19. 246.
515 | 128. 339.
516 | 490. 312.
517 | 594. 305.
518 | 335. 162.
519 | 328. 210.
520 | 442. 240.
521 | 440. 259.
522 | 521. 268.
523 | 552. 222.
524 | IMAGE=63038.jpg
525 | ID=34
526 | LM=12
527 | 263. 117.
528 | 36. 219.
529 | 20. 273.
530 | 127. 360.
531 | 486. 336.
532 | 596. 329.
533 | 332. 183.
534 | 329. 230.
535 | 443. 264.
536 | 440. 280.
537 | 525. 290.
538 | 556. 251.
539 | IMAGE=63039.jpg
540 | ID=35
541 | LM=12
542 | 259. 97.
543 | 33. 193.
544 | 12. 243.
545 | 119. 335.
546 | 477. 323.
547 | 588. 320.
548 | 322. 161.
549 | 311. 210.
550 | 431. 247.
551 | 426. 264.
552 | 512. 277.
553 | 544. 230.
554 | IMAGE=63042.jpg
555 | ID=36
556 | LM=12
557 | 290. 113.
558 | 101. 191.
559 | 78. 246.
560 | 162. 337.
561 | 476. 320.
562 | 574. 310.
563 | 349. 180.
564 | 343. 223.
565 | 438. 252.
566 | 433. 267.
567 | 504. 277.
568 | 532. 236.
569 | IMAGE=63043.jpg
570 | ID=37
571 | LM=12
572 | 257. 96.
573 | 42. 187.
574 | 19. 241.
575 | 120. 339.
576 | 472. 321.
577 | 581. 319.
578 | 317. 160.
579 | 311. 216.
580 | 421. 246.
581 | 418. 263.
582 | 504. 274.
583 | 532. 233.
584 | IMAGE=63045.jpg
585 | ID=38
586 | LM=12
587 | 254. 114.
588 | 44. 206.
589 | 21. 257.
590 | 109. 351.
591 | 480. 350.
592 | 595. 347.
593 | 321. 188.
594 | 316. 236.
595 | 443. 273.
596 | 436. 291.
597 | 521. 304.
598 | 548. 260.
599 | IMAGE=63046.jpg
600 | ID=39
601 | LM=12
602 | 244. 105.
603 | 32. 201.
604 | 15. 255.
605 | 110. 346.
606 | 466. 327.
607 | 573. 317.
608 | 312. 172.
609 | 310. 220.
610 | 422. 252.
611 | 418. 269.
612 | 503. 280.
613 | 524. 234.
614 | IMAGE=63048.jpg
615 | ID=40
616 | LM=12
617 | 282. 104.
618 | 94. 184.
619 | 71. 232.
620 | 156. 322.
621 | 471. 317.
622 | 571. 311.
623 | 343. 174.
624 | 338. 211.
625 | 442. 247.
626 | 438. 262.
627 | 509. 274.
628 | 530. 234.
629 | IMAGE=63049.jpg
630 | ID=41
631 | LM=12
632 | 279. 122.
633 | 94. 200.
634 | 74. 244.
635 | 168. 337.
636 | 470. 323.
637 | 565. 313.
638 | 342. 183.
639 | 331. 222.
640 | 442. 254.
641 | 439. 268.
642 | 511. 279.
643 | 524. 238.
644 | IMAGE=63050.jpg
645 | ID=42
646 | LM=12
647 | 283. 137.
648 | 108. 214.
649 | 88. 257.
650 | 173. 350.
651 | 470. 335.
652 | 566. 328.
653 | 347. 199.
654 | 339. 236.
655 | 434. 269.
656 | 432. 284.
657 | 505. 292.
658 | 525. 255.
659 | IMAGE=63051.jpg
660 | ID=43
661 | LM=12
662 | 264. 136.
663 | 94. 214.
664 | 73. 261.
665 | 154. 350.
666 | 463. 326.
667 | 559. 315.
668 | 333. 198.
669 | 332. 237.
670 | 426. 263.
671 | 424. 277.
672 | 498. 283.
673 | 514. 245.
674 | IMAGE=63052.jpg
675 | ID=44
676 | LM=12
677 | 279. 131.
678 | 93. 201.
679 | 71. 247.
680 | 155. 342.
681 | 465. 332.
682 | 562. 327.
683 | 345. 196.
684 | 338. 236.
685 | 431. 263.
686 | 428. 280.
687 | 498. 293.
688 | 523. 254.
689 | IMAGE=63054.jpg
690 | ID=45
691 | LM=12
692 | 247. 107.
693 | 36. 199.
694 | 12. 248.
695 | 106. 344.
696 | 471. 329.
697 | 572. 324.
698 | 315. 174.
699 | 311. 224.
700 | 424. 256.
701 | 420. 274.
702 | 504. 282.
703 | 531. 239.
704 | IMAGE=63056.jpg
705 | ID=46
706 | LM=12
707 | 263. 111.
708 | 46. 202.
709 | 19. 251.
710 | 113. 355.
711 | 474. 343.
712 | 579. 337.
713 | 321. 181.
714 | 310. 233.
715 | 427. 268.
716 | 424. 285.
717 | 520. 297.
718 | 536. 251.
719 | IMAGE=63057.jpg
720 | ID=47
721 | LM=12
722 | 248. 86.
723 | 44. 174.
724 | 17. 222.
725 | 106. 316.
726 | 479. 308.
727 | 596. 304.
728 | 323. 154.
729 | 319. 198.
730 | 432. 234.
731 | 427. 250.
732 | 519. 264.
733 | 548. 219.
734 | IMAGE=63058.jpg
735 | ID=48
736 | LM=12
737 | 298. 129.
738 | 121. 213.
739 | 101. 264.
740 | 188. 342.
741 | 480. 319.
742 | 574. 308.
743 | 355. 189.
744 | 350. 226.
745 | 451. 254.
746 | 447. 267.
747 | 512. 275.
748 | 534. 238.
749 | IMAGE=63061.jpg
750 | ID=49
751 | LM=12
752 | 270. 121.
753 | 93. 202.
754 | 71. 248.
755 | 151. 341.
756 | 465. 325.
757 | 563. 316.
758 | 336. 188.
759 | 332. 228.
760 | 432. 259.
761 | 430. 273.
762 | 499. 284.
763 | 522. 245.
764 | IMAGE=63062.jpg
765 | ID=50
766 | LM=12
767 | 276. 118.
768 | 88. 202.
769 | 68. 247.
770 | 162. 339.
771 | 474. 323.
772 | 571. 311.
773 | 340. 183.
774 | 334. 222.
775 | 440. 251.
776 | 435. 267.
777 | 505. 276.
778 | 530. 240.
779 | IMAGE=63063.jpg
780 | ID=51
781 | LM=12
782 | 310. 114.
783 | 115. 198.
784 | 92. 251.
785 | 190. 340.
786 | 493. 322.
787 | 592. 316.
788 | 364. 181.
789 | 355. 222.
790 | 462. 254.
791 | 459. 270.
792 | 526. 279.
793 | 552. 242.
794 | IMAGE=63064.jpg
795 | ID=52
796 | LM=12
797 | 282. 108.
798 | 89. 189.
799 | 70. 246.
800 | 171. 334.
801 | 465. 302.
802 | 568. 293.
803 | 337. 168.
804 | 333. 208.
805 | 433. 236.
806 | 431. 251.
807 | 502. 260.
808 | 527. 220.
809 | IMAGE=63065.jpg
810 | ID=53
811 | LM=12
812 | 274. 116.
813 | 47. 219.
814 | 27. 275.
815 | 137. 368.
816 | 491. 339.
817 | 607. 334.
818 | 332. 180.
819 | 325. 234.
820 | 455. 266.
821 | 450. 285.
822 | 535. 293.
823 | 563. 250.
824 | IMAGE=63067.jpg
825 | ID=54
826 | LM=12
827 | 258. 89.
828 | 35. 180.
829 | 11. 235.
830 | 119. 334.
831 | 469. 320.
832 | 582. 314.
833 | 321. 154.
834 | 312. 207.
835 | 435. 242.
836 | 432. 259.
837 | 518. 273.
838 | 542. 231.
839 | IMAGE=63069.jpg
840 | ID=55
841 | LM=12
842 | 252. 98.
843 | 42. 188.
844 | 21. 237.
845 | 113. 337.
846 | 466. 327.
847 | 580. 326.
848 | 311. 165.
849 | 310. 218.
850 | 424. 251.
851 | 420. 270.
852 | 512. 282.
853 | 535. 241.
854 | IMAGE=63070.jpg
855 | ID=56
856 | LM=12
857 | 257. 90.
858 | 39. 187.
859 | 15. 243.
860 | 115. 339.
861 | 477. 324.
862 | 583. 319.
863 | 322. 159.
864 | 322. 210.
865 | 439. 245.
866 | 436. 262.
867 | 514. 276.
868 | 542. 231.
869 | IMAGE=63071.jpg
870 | ID=57
871 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ml-morph [](https://zenodo.org/badge/latestdoi/195575274)
2 |
3 | Machine-learning tools for landmark-based morphometrics
4 |
5 |
6 | Porto, A. and Voje, K.L., 2020. ML‐morph: A fast, accurate and general approach for automated detection and landmarking of biological structures in images. Methods in Ecology and Evolution, 11(4), pp.500-512.
7 |
8 | 
9 |
10 | ## Python Dependencies
11 |
12 | - numpy>=1.13.3
13 | - pandas>=0.22.0
14 | - dlib>=19.7.0
15 | - opencv-python>=3.4.0.12
16 |
17 | If their dependencies are satisfied, these modules can be installed using:
18 |
19 | pip install -r requirements.txt
20 |
21 | ## Optional Dependencies
22 | - imglab
23 |
24 | For those who want to visualize the xml files produced by the pipeline, we recommend installing the [`imglab`](https://github.com/davisking/dlib/tree/master/tools/imglab) tool that is included in the `dlib 19.7.0` source code.
25 | Please refer to the [`original repository`](https://github.com/davisking/dlib/tree/master/tools/imglab) for installation details and basic usage.
26 | An alternative [`version of imglab`](https://imglab.in/) is also available and can be used directly from the web browser.
27 |
28 | ## Installation notes and general issues
29 | For Mac users, a series of dependencies for `dlib>=19.7.0` will need to be installed before it can be used. A detailed protocol can be found [here](https://medium.com/@210/install-dlib-on-mac-ff9f4d03ad8).
30 |
31 | For windows users, the `dlib>=19.7.0` installation will sometimes fail. An alternative way to install it is to use a `.whl`:
32 |
33 | pip install https://pypi.python.org/packages/da/06/bd3e241c4eb0a662914b3b4875fc52dd176a9db0d4a2c915ac2ad8800e9e/dlib-19.7.0-cp36-cp36m-win_amd64.whl#md5=b7330a5b2d46420343fbed5df69e6a3f
34 |
35 | Also note that while **ml-morph** can handle multiple image file formats, some care is needed with regards to the presence of special characters in image filenames. So far, we have only had problems with `&`, but it is possible that other special characters might lead the software to throw out an error.
36 |
37 | ## Usage
38 | **ml-morph** uses a combination of [`object detection`](https://lear.inrialpes.fr/people/triggs/pubs/Dalal-cvpr05.pdf) with [`shape prediction`](http://www.nada.kth.se/~sullivan/Papers/Kazemi_cvpr14.pdf) to perform automated landmarking in images of semi-rigid biological structures. To generate such detectors and predictors, we need to train machine learning models using manually annotated datasets. These manually annotated datasets can be generated using the `imglab` tool, or (alternatively) can also be generated by converting traditional morphometric landmark files (`tps` and `standard XY coordinate`).
39 |
40 | The pipeline itself has four mains components, which should be applied in sequence:
41 | 1. Preprocessing `preprocessing.py`
42 | 2. Training and Testing object detectors `detector_trainer.py` `detector_tester.py`
43 | 3. Training and testing shape predictors `shape_trainer.py` `shape_tester.py`
44 | 4. Predicting the landmark positions in a new set of images `prediction.py`
45 |
46 |
47 | ### Vignette
48 |
49 | Here, we will go through the **ml-morph** pipeline using a tiny fly wing dataset (see `image-examples`) to illustrate the full-training process. This dataset contains `58` images of drosophilid wings, which have been annotated for `12` landmarks (see `landmark-examples`). We have provided landmark annotations in two file formats traditionally used by the morphometric community. While this dataset is too small to generate high performing detectors and predictors, it still allows us to demonstrate how this software can be used.
50 |
51 |
52 | #### 1) Preprocessing (_preprocessing.py_)
53 |
54 | In the preprocessing step, **ml-morph** will split a user-defined image folder into `train` and `test` sets. When splitting the image files, **ml-morph** will convert all image files to `.jpg` and _(optionally)_ also generate the `train.xml` and `test.xml` downstream files from previously acquired annotations. The xml files generated during this step contain the landmark and bounding box annotations for each image in the `train` and `test` folders. Please see the following scenarios for usage details:
55 |
56 | ##### option 1 - No previous annotation
57 | When creating a training and testing dataset from scratch, the preprocessing step will only serve to split the images into `train`and `test` sets (80%/20% split). This can be accomplished using:
58 |
59 | python3 preprocessing.py -i image-examples
60 |
61 | Once created, the images in the `train`and `test` folders will need to be manually annotated. The user should choose the appropriate software to do so ([`tpsDig`](https://life.bio.sunysb.edu/morph/), [`geomorph`](https://cran.r-project.org/web/packages/geomorph/index.html), [`imglab`](https://github.com/davisking/dlib/tree/master/tools/imglab), to cite a few possibilities). In order to annotate the training images from scratch using [`imglab`](https://github.com/davisking/dlib/tree/master/tools/imglab), simply create the initial `train.xml` file using:
62 |
63 | ./imglab -c train.xml /train
64 |
65 | This file can then be annotated, in our example, using the following command:
66 |
67 | ./imglab --parts '1 2 3 4 5 6 7 8 9 10 11 12' train.xml
68 |
69 | See `./imglab -h` for other usage possibilities and the `imglab` manual for details of the annotation procedure.
70 |
71 |
72 |
73 | ##### option 2 - Previously annotated dataset
74 | When creating training and testing sets from previously annotated datasets, the preprocessing step will not only split the images into `train` and `test` sets but also generate the downstream input files (`train.xml` and `test.xml`). Using the `.tps` annotation file as an example, simply type:
75 |
76 | python3 preprocessing.py -i image-examples -t landmark-examples/tps-example.tps
77 |
78 | This command will generate the `train` and `test` folders, as well as the `train.xml` and `test.xml` downstream files.
79 |
80 |
81 |
82 |
83 | #### 2) Training and testing object detectors (detector_trainer.py)
84 |
85 | In order to train a model to detect fly wings in images, we can use the **ml-morph** detector trainer. Several parameters can be given to the trainer:
86 |
87 | python3 detector_trainer.py --help
88 |
89 | ```
90 | usage: detector_trainer.py [-h] [-d] [-t] [-o] [-n] [-s] [-e] [-c] [-u] [-w]
91 |
92 | optional arguments:
93 | -h, --help show this help message and exit
94 | -d , --dataset training data (default = train.xml)
95 | -t , --test (optional) test data. if not provided, the model is not
96 | tested
97 | -o , --out output filename (default = detector)
98 | -n , --n-threads number of threads to be used (default = 1)
99 | -s , --symmetrical (True/False) indicating whether objects are bilaterally
100 | symmetrical (default = False)
101 | -e , --epsilon insensitivity parameter (default = 0.01)
102 | -c , --c-param soft margin parameter C (default =5)
103 | -u , --upsample upsample limit (default = 0)
104 | -w , --window-size (optional) detection window size
105 |
106 | ```
107 |
108 |
109 | In the wing example, we have a very small dataset, so we are running the **ml-morph** model trainer with parameters that work reasonably in this context:
110 |
111 | python3 detector_trainer.py -d train.xml -t test.xml -n 7 -w 79000 -e 0.001 -c 15
112 |
113 |
114 | ```
115 | Training with C: 15
116 | Training with epsilon: 0.001
117 | Training using 7 threads.
118 | Training with sliding window 428 pixels wide by 184 pixels tall.
119 | ```
120 |
121 | These parameters should not be used, in any way, as default training parameters. Exploration of the **ml-morph** parameter space can have a large impact on improving the final performance of the detection algorithm. Also note that testing of the model is performed immediately after training if the `--test` flag is provided, but not otherwise.
122 | At the end, you should observe something like this:
123 |
124 | ```
125 | Training complete.
126 | Trained with C: 15
127 | Training with epsilon: 0.001
128 | Trained using 7 threads.
129 | Trained with sliding window 428 pixels wide by 184 pixels tall.
130 | Saved detector to file detector.svm
131 | Training - precision: 1, recall: 1, average precision: 1
132 | Testing - precision: 1, recall: 1, average precision: 1
133 | ```
134 |
135 | One file is generated during the training process (`detector.svm` or the user-defined name). This file represents the support vector machine (SVM) classifier. With this classifier, one can use **ml-morph** to perform object detection in images of fly wings.
136 | If the user wants to test a model that was trained in the past, this can be done using:
137 |
138 | python3 detector_tester.py -t test.xml -d detector.svm
139 |
140 | ```
141 | Testing - precision: 1, recall: 1, average precision: 1
142 | ```
143 |
144 |
145 |
146 | #### 3) Training and testing shape predictors (shape_trainer.py)
147 |
148 | In order to train a model to predict wing shape, we can use the **ml-morph** shape trainer. Several parameters can be given to the trainer:
149 |
150 | python3 shape_trainer.py --help
151 |
152 | ```
153 | usage: shape_trainer.py [-h] [-d] [-t] [-o] [-th] [-dp] [-c] [-nu] [-os] [-s]
154 | [-f] [-n]
155 |
156 | optional arguments:
157 | -h, --help show this help message and exit
158 | -d , --dataset training data (default = train.xml)
159 | -t , --test test data (default = test.xml).if not provided, no
160 | testing is done
161 | -o , --out output filename (default = predictor)
162 | -th , --threads number of threads to be used (default = 1)
163 | -dp , --tree-depth choice of tree depth (default = 4)
164 | -c , --cascade-depth
165 | choice of cascade depth (default = 15)
166 | -nu , --nu regularization parameter (default = 0.1)
167 | -os , --oversampling
168 | oversampling amount (default = 10)
169 | -s , --test-splits number of test splits (default = 20)
170 | -f , --feature-pool-size
171 | choice of feature pool size (default = 500)
172 | -n , --num-trees number of regression trees (default = 500)
173 | ```
174 |
175 |
176 | Again, since we have a very small dataset, we are running the **ml-morph** model trainer with parameters that work reasonably in this context:
177 |
178 | python3 shape_trainer.py -d train.xml -t test.xml -th 7 -dp 3 -c 20 -nu 0.08 -os 200 -f 700
179 |
180 | ```
181 | Training with cascade depth: 20
182 | Training with tree depth: 3
183 | Training with 500 trees per cascade level.
184 | Training with nu: 0.08
185 | Training with random seed:
186 | Training with oversampling amount: 200
187 | Training with feature pool size: 700
188 | Training with feature pool region padding: 0
189 | Training with lambda_param: 0.1
190 | Training with 20 split tests.
191 | Fitting trees...
192 | ```
193 |
194 | As above, these parameters should not be used, in any way, as default training parameters. Also note that testing of the model is performed immediately after training if the `-t` flag is provided, but not otherwise.
195 | At the end, you should observe something like this:
196 |
197 | ```
198 | Training complete
199 | Training complete, saved predictor to file predictor.dat
200 | Training error (average pixel deviation): 0.0018115942028985507
201 | Testing error (average pixel deviation): 2.2899980012040912
202 | ```
203 |
204 | One file is generated during the training process (`predictor.dat` or the user-defined name). This file represents the **ml-morph** cascade shape regression model. With this model, one can perform shape prediction (i.e., landmark coordinates) in objects detected using **ml-morph**.
205 | If the user wants to test a model that was trained in the past, this can be done using:
206 |
207 | python3 shape_tester.py -t test.xml -p predictor.dat
208 |
209 | ```
210 | Testing error (average pixel deviation): 2.2899980012040912
211 | ```
212 |
213 |
214 | #### 4) Predicting the landmark positions in a new set of images (prediction.py)
215 |
216 | Finally, **ml-morph** also allows users to use trained models to perform automated landmarking in a new image set. Several parameters can be given to the prediction algorithm:
217 |
218 | python3 prediction.py --help
219 |
220 | ```
221 | usage: prediction.py [-h] [-i] [-d] [-p] [-o] [-u] [-t] [-l]
222 |
223 | optional arguments:
224 | -h, --help show this help message and exit
225 | -i , --input-dir input directory (default = pred)
226 | -d , --detector trained object detection model (default =
227 | detector.svm)
228 | -p , --predictor trained shape prediction model (default =
229 | predictor.dat)
230 | -o , --out-file output file name (default = output.xml)
231 | -u , --upsample-limit
232 | upsample limit (default= 0 ; max = 2)
233 | -t , --threshold detector's confidence threshold for outputting an
234 | object (default= 0)
235 | -l , --ignore-list (optional) prevents landmarks of choice from being
236 | output
237 | ```
238 |
239 | For simplicity, here we will just predict the landmark positions in the images in the test set. This can be done as follows:
240 |
241 | python3 prediction.py -i test -d detector.svm -p predictor.dat
242 |
243 | A single xml file is produced as an output (`output.xml` or user-defined name). The user can then visualize the predictions using `imglab`, if needed:
244 |
245 | ./imglab output.xml
246 |
247 | Alternatively, the landmark data can also be imported into python using functions within `utils.py`:
248 |
249 | ```python
250 | from utils import *
251 | df = dlib_xml_to_pandas('output.xml')
252 | ```
253 |
254 | As should be noted, the tiny size of the dataset prevents the model from attaining high performance. Still, for such small dataset, the performance is quite reasonable.
255 |
256 | And that is it !
257 |
258 | ## Final remarks
259 |
260 | Any feedback on the software in this repository is greatly appreciated!
261 |
262 |
263 |
264 |
265 |
266 |
267 |
268 |
269 |
270 |
271 |
272 |
273 |
274 |
--------------------------------------------------------------------------------
/utils.py:
--------------------------------------------------------------------------------
1 | #Part of the standard library
2 | import xml.etree.ElementTree as ET
3 | from xml.dom import minidom
4 | import os
5 | import csv
6 | import re
7 | import random
8 | import shutil
9 | import glob
10 |
11 | #Not part of the standard library
12 | import numpy as np
13 | import pandas as pd
14 | import cv2
15 | import dlib
16 |
17 | #Tools for using previously annotated datasets
18 |
19 | def read_csv(input):
20 | '''
21 | This function reads a XY coordinate file (following the tpsDig coordinate system) containing several specimens(rows)
22 | and any number of landmarks. It is generally assumed here that the file contains a header and no other
23 | columns other than an id column (first column) and the X0 Y0 ...Xn Yn coordinates for n landmarks.It is also
24 | assumed that the file contains no missing values.
25 |
26 | Parameters:
27 | input (str): The XY coordinate file (csv format)
28 | Returns:
29 | dict: dictionary containing two keys (im= image id, coords= array with 2D coordinates)
30 |
31 | '''
32 | csv_file = open(input, 'r')
33 | csv =csv_file.read().splitlines()
34 | csv_file.close()
35 | im, coords_array = [], []
36 |
37 | for i, ln in enumerate(csv):
38 | if i > 0:
39 | im.append(ln.split(',')[0])
40 | coord_vec=ln.split(',')[1:]
41 | coords_mat = np.reshape(coord_vec, (int(len(coord_vec)/2),2))
42 | coords = np.array(coords_mat, dtype=float)
43 | coords_array.append(coords)
44 | return {'im': im, 'coords': coords_array}
45 |
46 | def read_tps(input):
47 | '''
48 | This function reads a tps coordinate file containing several specimens and an arbitrary number of landmarks.
49 | A single image file can contain as many specimens as wanted.
50 | It is generally assumed here that all specimens were landmarked in the same order.It is also assumed that
51 | the file contains no missing values.
52 |
53 | Parameters:
54 | input (str): The tps coordinate file
55 | Returns:
56 | dict: dictionary containing four keys
57 | (lm= number of landmarks,im= image id, scl= scale, coords= array with 2D coordinates)
58 |
59 | '''
60 | tps_file = open(input, 'r')
61 | tps = tps_file.read().splitlines()
62 | tps_file.close()
63 | lm, im, sc, coords_array = [], [], [], []
64 |
65 | for i, ln in enumerate(tps):
66 | if ln.startswith("LM"):
67 | lm_num = int(ln.split('=')[1])
68 | lm.append(lm_num)
69 | coords_mat = []
70 | for j in range(i + 1, i + 1 + lm_num):
71 | coords_mat.append(tps[j].split(' '))
72 | coords_mat = np.array(coords_mat, dtype=float)
73 | coords_array.append(coords_mat)
74 |
75 | if ln.startswith("IMAGE"):
76 | im.append(ln.split('=')[1])
77 |
78 | if ln.startswith("SCALE"):
79 | sc.append(ln.split('=')[1])
80 | return {'lm': lm, 'im': im, 'scl': sc, 'coords': coords_array}
81 |
82 |
83 | #dlib xml tools
84 |
85 |
86 | def add_part_element(bbox,num,sz):
87 | '''
88 | Internal function used by generate_dlib_xml. It creates a 'part' xml element containing the XY coordinates
89 | of an arbitrary number of landmarks. Parts are nested within boxes.
90 |
91 | Parameters:
92 | bbox (array): XY coordinates for a specific landmark
93 | num(int)=landmark id
94 | sz (int)=the image file's height in pixels
95 |
96 |
97 | Returns:
98 | part (xml tag): xml element containing the 2D coordinates for a specific landmark id(num)
99 |
100 | '''
101 | part = ET.Element('part')
102 | part.set('name',str(int(num)))
103 | part.set('x',str(int(bbox[0])))
104 | part.set('y',str(int(sz-bbox[1])))
105 | return part
106 |
107 | def add_bbox_element(bbox,sz,padding=0):
108 | '''
109 | Internal function used by generate_dlib_xml. It creates a 'bounding box' xml element containing the
110 | four parameters that define the bounding box (top,left, width, height) based on the minimum and maximum XY
111 | coordinates of its parts(i.e.,landmarks). An optional padding can be added to the bounding box.Boxes are
112 | nested within images.
113 |
114 | Parameters:
115 | bbox (array): XY coordinates for all landmarks within a bounding box
116 | sz (int)= the image file's height in pixels
117 | padding(int)= optional parameter definining the amount of padding around the landmarks that should be
118 | used to define a bounding box, in pixels (int).
119 |
120 |
121 | Returns:
122 | box (xml tag): xml element containing the parameters that define a bounding box and its corresponding parts
123 |
124 | '''
125 | box = ET.Element('box')
126 | height = bbox[:,1].max()-bbox[:,1].min()+2*padding
127 | width = bbox[:,0].max()-bbox[:,0].min()+2*padding
128 | top = sz-bbox[:,1].max()-padding
129 | if top < 1:
130 | top =1
131 | left = bbox[:,0].min()-padding
132 | if left < 1:
133 | left =1
134 |
135 | box.set('top', str(int(top)))
136 | box.set('left', str(int(left)))
137 | box.set('width', str(int(width)))
138 | box.set('height', str(int(height)))
139 | for i in range(0,len(bbox)):
140 | box.append(add_part_element(bbox[i,:],i,sz))
141 | return box
142 |
143 | def add_image_element(image, coords, sz, path):
144 | '''
145 | Internal function used by generate_dlib_xml. It creates a 'image' xml element containing the
146 | image filename and its corresponding bounding boxes and parts.
147 |
148 | Parameters:
149 | image (str): image filename
150 | coords (array)= XY coordinates for all landmarks within a bounding box
151 | sz (int)= the image file's height in pixels
152 |
153 |
154 | Returns:
155 | image (xml tag): xml element containing the parameters that define each image (boxes+parts)
156 |
157 | '''
158 | image_e = ET.Element('image')
159 | image_e.set('file', str(path))
160 | image_e.append(add_bbox_element(coords,sz))
161 | return image_e
162 |
163 | def generate_dlib_xml(images,sizes,folder='train',out_file='output.xml'):
164 | '''
165 | Generates a dlib format xml file for training or testing of machine learning models.
166 |
167 | Parameters:
168 | images (dict): dictionary output by read_tps or read_csv functions
169 | sizes (dict)= dictionary of image file sizes output by the split_train_test function
170 | folder(str)= name of the folder containing the images
171 |
172 |
173 | Returns:
174 | None (file written to disk)
175 | '''
176 | root = ET.Element('dataset')
177 | root.append(ET.Element('name'))
178 | root.append(ET.Element('comment'))
179 |
180 | images_e = ET.Element('images')
181 | root.append(images_e)
182 |
183 | for i in range(0,len(images['im'])):
184 | name=os.path.splitext(images['im'][i])[0]+'.jpg'
185 | path=os.path.join(folder,name)
186 | if os.path.isfile(path) is True:
187 | present_tags=[]
188 | for img in images_e.findall('image'):
189 | present_tags.append(img.get('file'))
190 |
191 | if path in present_tags:
192 | pos=present_tags.index(path)
193 | images_e[pos].append(add_bbox_element(images['coords'][i],sizes[name][0]))
194 |
195 | else:
196 | images_e.append(add_image_element(name,images['coords'][i],sizes[name][0],path))
197 |
198 | et = ET.ElementTree(root)
199 | xmlstr = minidom.parseString(ET.tostring(et.getroot())).toprettyxml(indent=" ")
200 | with open(out_file, "w") as f:
201 | f.write(xmlstr)
202 |
203 | #Directory preparation tools
204 |
205 |
206 | def split_train_test(input_dir):
207 | '''
208 | Splits an image directory into 'train' and 'test' directories. The original image directory is preserved.
209 | When creating the new directories, this function converts all image files to 'jpg'. The function returns
210 | a dictionary containing the image dimensions in the 'train' and 'test' directories.
211 |
212 | Parameters:
213 | input_dir(str)=original image directory
214 |
215 | Returns:
216 | sizes (dict): dictionary containing the image dimensions in the 'train' and 'test' directories.
217 | '''
218 | # Listing the filenames.Folders must contain only image files (extension can vary).Hidden files are ignored
219 | filenames = os.listdir(input_dir)
220 | filenames = [os.path.join(input_dir, f) for f in filenames if not f.startswith('.')]
221 |
222 | # Splitting the images into 'train' and 'test' directories (80/20 split)
223 | random.seed(845)
224 | filenames.sort()
225 | random.shuffle(filenames)
226 | split = int(0.8 * len(filenames))
227 | train_set = filenames[:split]
228 | test_set = filenames[split:]
229 |
230 | filenames = {'train':train_set,
231 | 'test': test_set}
232 | sizes={}
233 | for split in ['train','test']:
234 | sizes[split]={}
235 | if not os.path.exists(split):
236 | os.mkdir(split)
237 | else:
238 | print("Warning: the folder {} already exists. It's being replaced".format(split))
239 | shutil.rmtree(split)
240 | os.mkdir(split)
241 |
242 | for filename in filenames[split]:
243 | basename=os.path.basename(filename)
244 | name=os.path.splitext(basename)[0] + '.jpg'
245 | sizes[split][name]=image_prep(filename,name,split)
246 | return sizes
247 |
248 | def image_prep(file, name, dir_path):
249 | '''
250 | Internal function used by the split_train_test function. Reads the original image files and, while
251 | converting them to jpg, gathers information on the original image dimensions.
252 |
253 | Parameters:
254 | file(str)=original path to the image file
255 | name(str)=basename of the original image file
256 | dir_path(str)= directory where the image file should be saved to
257 |
258 | Returns:
259 | file_sz(array): original image dimensions
260 | '''
261 | img = cv2.imread(file)
262 | if img is None:
263 | print('File {} was ignored'.format(file))
264 | else:
265 | file_sz= [img.shape[0],img.shape[1]]
266 | cv2.imwrite(os.path.join(dir_path,name), img)
267 | return file_sz
268 |
269 |
270 |
271 |
272 | # Tools for predicting objects and shapes in new images
273 |
274 | def predictions_to_xml(detector_name:str, predictor_name:str,dir='pred',upsample=0,threshold=0,ignore=None,out_file='output_prediction.xml'):
275 | '''
276 | Generates a dlib format xml file for model predictions. It uses previously trained models to
277 | identify objects in images and to predict their shape.
278 |
279 | Parameters:
280 | detector_name (str): object detector filename
281 | predictor_name (str): shape predictor filename
282 | dir(str): (optional) name of the directory containing images to be predicted
283 | upsample (int): (optional) number of times that an image should be upsampled (max=2 times)
284 | treshold (float): (optional) confidence threshold. Objects detected with lower confidence than
285 | the threshold are not output
286 | ignore (list): list of landmarks that should be ignored (based on landmark numeric id)
287 | out_file (str): name of the output file (xml format)
288 |
289 | Returns:
290 | None (out_file written to disk)
291 |
292 | '''
293 | predictor = dlib.shape_predictor(predictor_name)
294 | detector = dlib.fhog_object_detector(detector_name)
295 | root = ET.Element('dataset')
296 | root.append(ET.Element('name'))
297 | root.append(ET.Element('comment'))
298 | images_e = ET.Element('images')
299 | root.append(images_e)
300 | for f in glob.glob(dir+"/*.jpg"):
301 | path, file = os.path.split(f)
302 | img = cv2.imread(f)
303 | image_e = ET.Element('image')
304 | image_e.set('file', str(f))
305 | [boxes, confidences, detector_idxs] = dlib.fhog_object_detector.run(
306 | detector, img, upsample_num_times=upsample, adjust_threshold=threshold)
307 | for k, d in enumerate(boxes):
308 | shape = predictor(img, d)
309 | box = ET.Element('box')
310 | box.set('top', str(int(d.top())))
311 | box.set('left', str(int(d.left())))
312 | box.set('width', str(int(d.right()-d.left())))
313 | box.set('height', str(int(d.bottom()-d.top())))
314 | for i in range(0,shape.num_parts):
315 | if ignore is not None:
316 | if i not in ignore:
317 | part = ET.Element('part')
318 | part.set('name',str(int(i)))
319 | part.set('x',str(int(shape.part(i).x)))
320 | part.set('y',str(int(shape.part(i).y)))
321 | box.append(part)
322 | else:
323 | part = ET.Element('part')
324 | part.set('name',str(int(i)))
325 | part.set('x',str(int(shape.part(i).x)))
326 | part.set('y',str(int(shape.part(i).y)))
327 | box.append(part)
328 |
329 | image_e.append(box)
330 | images_e.append(image_e)
331 |
332 | et = ET.ElementTree(root)
333 | xmlstr = minidom.parseString(ET.tostring(et.getroot())).toprettyxml(indent=" ")
334 | with open(out_file, "w") as f:
335 | f.write(xmlstr)
336 |
337 | #Importing to pandas tools
338 |
339 | def natural_sort_XY(l):
340 | '''
341 | Internal function used by the dlib_xml_to_pandas. Performs the natural sorting of an array of XY
342 | coordinate names.
343 |
344 | Parameters:
345 | l(array)=array to be sorted
346 |
347 | Returns:
348 | l(array): naturally sorted array
349 | '''
350 | convert = lambda text: int(text) if text.isdigit() else 0
351 | alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key) ]
352 | return sorted(l, key = alphanum_key)
353 |
354 | def dlib_xml_to_pandas(xml_file: str,parse=False):
355 | '''
356 | Imports dlib xml data into a pandas dataframe. An optional file parsing argument is present
357 | for very specific applications. For most people, the parsing argument should remain as 'False'.
358 |
359 | Parameters:
360 | xml_file(str)=file to be imported (dlib xml format)
361 |
362 | Returns:
363 | df(dataframe): returns a pandas dataframe containing the data in the xml_file.
364 | '''
365 | tree=ET.parse(xml_file)
366 | root=tree.getroot()
367 | landmark_list=[]
368 | for images in root:
369 | for image in images:
370 | for boxes in image:
371 | box=boxes.attrib['top']\
372 | +'_'+boxes.attrib['left']\
373 | +'_'+boxes.attrib['width']\
374 | +'_'+boxes.attrib['height']
375 | for parts in boxes:
376 | if parts.attrib['name'] is not None:
377 | if parse is False:
378 | data={'id':image.attrib['file'],
379 | 'box_id':box,
380 | 'box_top':float(boxes.attrib['top']),
381 | 'box_left':float(boxes.attrib['left']),
382 | 'box_width':float(boxes.attrib['width']),
383 | 'box_height':float(boxes.attrib['height']),
384 | 'X'+parts.attrib['name']:float(parts.attrib['x']),
385 | 'Y'+parts.attrib['name']:float(parts.attrib['y']) }
386 | else:
387 | data={'id':image.attrib['file'].replace('/','_').replace('x','').split('_')[1],
388 | 'side':image.attrib['file'].replace('/','_').replace('x','').split('_')[2],
389 | 'replicate':image.attrib['file'].replace('/','_').replace('x','').split('_')[3],
390 | 'voltage':image.attrib['file'].replace('/','_').replace('x','').split('_')[4],
391 | 'zoom':image.attrib['file'].replace('/','_').replace('x','').split('_')[5],
392 | 'box_id':box,
393 | 'box_top':float(boxes.attrib['top']),
394 | 'box_left':float(boxes.attrib['left']),
395 | 'box_width':float(boxes.attrib['width']),
396 | 'box_height':float(boxes.attrib['height']),
397 | 'X'+parts.attrib['name']:float(parts.attrib['x']),
398 | 'Y'+parts.attrib['name']:float(parts.attrib['y']) }
399 |
400 | landmark_list.append(data)
401 | dataset=pd.DataFrame(landmark_list)
402 | df = dataset.groupby(['id', 'box_id'], sort=False).max()
403 | df=df[natural_sort_XY(df)]
404 | return df
405 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------