From 5597d9cb20c6690646d7c3691941d950bae88b59 Mon Sep 17 00:00:00 2001 From: Davis King Date: Sun, 26 May 2013 14:21:02 -0400 Subject: [PATCH] Starting to flesh out the python interface documentation a little more. --- dlib/add_python_module | 16 ++++ .../compile_dlib_python_module.bat | 4 + python_examples/sequence_segmenter.py | 73 +++++++++++++++++++ tools/python/CMakeLists.txt | 2 + 4 files changed, 95 insertions(+) create mode 100755 python_examples/compile_dlib_python_module.bat create mode 100644 python_examples/sequence_segmenter.py diff --git a/dlib/add_python_module b/dlib/add_python_module index bb2e2eed4..8e98ab538 100644 --- a/dlib/add_python_module +++ b/dlib/add_python_module @@ -4,6 +4,12 @@ # The macro takes the module name as its first argument and then a list of # source files to compile into the module. See ../tools/python/CMakeLists.txt # for an example. +# +# It also sets up a macro called install_${module_name}_to() where +# ${module_name} is whatever you named your module. This install_*_to() macro +# takes a folder name and creates an install target that will copy the compiled +# python module to that folder when you run "make install". Note that the path +# given to install_*_to() is relative to your CMakeLists.txt file. set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:/Program Files (x86)/boost/boost_1_*" @@ -54,5 +60,15 @@ macro(add_python_module module_name module_sources ) OUTPUT_NAME ${module_name} ) endif() + + macro(install_${module_name}_to path) + # Determine the path to our CMakeLists.txt file. + string(REPLACE "CMakeLists.txt" "" base_path ${CMAKE_CURRENT_LIST_FILE}) + INSTALL(TARGETS ${module_name}_ + LIBRARY DESTINATION ${base_path}/${path} + ) + endmacro() endmacro() + + diff --git a/python_examples/compile_dlib_python_module.bat b/python_examples/compile_dlib_python_module.bat new file mode 100755 index 000000000..88daaaf22 --- /dev/null +++ b/python_examples/compile_dlib_python_module.bat @@ -0,0 +1,4 @@ +mkdir build +cd build +cmake ../../tools/python +cmake --build . --config Release --target install diff --git a/python_examples/sequence_segmenter.py b/python_examples/sequence_segmenter.py new file mode 100644 index 000000000..591ff6ff6 --- /dev/null +++ b/python_examples/sequence_segmenter.py @@ -0,0 +1,73 @@ +# You need to compile the dlib python interface before you can use this +# file. To do this, run compile_dlib_python_module.bat. You also need to +# have the boost-python library installed. On Ubuntu, this can be done easily by running +# the command: sudo apt-get install libboost-python-dev + +# asfd +import dlib + +use_sparse_vects = False + +if use_sparse_vects: + samples = dlib.sparse_vectorss() +else: + samples = dlib.vectorss() + +segments = dlib.rangess() + +if use_sparse_vects: + inside = dlib.sparse_vector() + outside = dlib.sparse_vector() + inside.append(dlib.pair(0,1)) + outside.append(dlib.pair(1,1)) +else: + inside = dlib.vector([0, 1]) + outside = dlib.vector([1, 0]) + +samples.resize(2) +segments.resize(2) + +samples[0].append(outside) +samples[0].append(outside) +samples[0].append(inside) +samples[0].append(inside) +samples[0].append(inside) +samples[0].append(outside) +samples[0].append(outside) +samples[0].append(outside) +segments[0] +segments[0].append(dlib.range(2,5)) + + +samples[1].append(outside) +samples[1].append(outside) +samples[1].append(inside) +samples[1].append(inside) +samples[1].append(inside) +samples[1].append(inside) +samples[1].append(outside) +samples[1].append(outside) +segments[1].append(dlib.range(2,6)) + + +params = dlib.segmenter_params() +#params.be_verbose = True +params.window_size = 1 +params.use_high_order_features = False +params.C = 1 +print "params:", params + +df = dlib.train_sequence_segmenter(samples, segments, params) + +print len(df.segment_sequence(samples[0])) +print df.segment_sequence(samples[0])[0] + + + +print df.weights + +#res = dlib.test_sequence_segmenter(df, samples, segments) +res = dlib.cross_validate_sequence_segmenter(samples, segments, 2, params) + +print res + diff --git a/tools/python/CMakeLists.txt b/tools/python/CMakeLists.txt index 32b524de4..abf9be434 100644 --- a/tools/python/CMakeLists.txt +++ b/tools/python/CMakeLists.txt @@ -15,3 +15,5 @@ add_python_module(dlib src/cca.cpp src/sequence_segmenter.cpp ) + +install_dlib_to(../../python_examples)