14 Commits

Author SHA1 Message Date
Matias De lellis
d3b91453bb Dont install useless php extension 2020-04-24 11:57:07 -03:00
Matias De lellis
3e050a4772 Improve travis 2020-04-24 11:40:40 -03:00
Matias De lellis
16d19188c3 Improve travis 2020-04-24 11:10:33 -03:00
Matias De lellis
9d351afd07 Test on tavis. 2020-04-24 10:41:21 -03:00
Matias De lellis
ca007e400a Append more information to phpinfo()
If you think it works slow, make sure it is compiled with any of these instructions

> pdlib
>
> PDlib support => enabled
> Pdlib Version => 1.0.2
> DLIB_USE_CUDA => false
> DLIB_USE_BLAS => true
> DLIB_USE_LAPACK => true
> USE_AVX_INSTRUCTIONS => false
> USE_AVX2_INSTRUCTIONS => false
> USE_NEON_INSTRUCTIONS => false
> USE_SSE2_INSTRUCTIONS => true
> USE_SSE4_INSTRUCTIONS => false
2020-04-19 20:27:51 -03:00
Matias De lellis
2b06ea45c4 Improve messages when configure build. 2020-04-19 19:26:22 -03:00
matiasdelellis
c9e1015197 Merge pull request #2 from matiasdelellis/dlib_length
Dlib length
2020-04-14 20:05:40 -03:00
Matias De lellis
76da4d9b84 Multiply directly instead of using pow ($number, 2);
This small optimization allows us to improve from:

 OLD php: 0.012100038051605 Sec.
 New native: 0.0096198790073395 Sec.

(This is already 20 percent faster)

..to:
 OLD php: 0.012276937961578 Sec.
 New native: 0.0022540950775146 Sec.

What is 80 percent faster than our PHP class.
2020-04-14 10:45:13 -03:00
Matias De lellis
75adc6f801 Use references as an attempt to optimize. 2020-04-14 10:26:26 -03:00
Matias De lellis
4a2e4ff112 Implement dlib_vector_length to calculate the euclidean distance of the vectors
It not use the 'length' native versions of dlib, due I have to convert the
array to the vector, which will take the same time, and then I will use the
native method.
2020-04-14 09:45:16 -03:00
matiasdelellis
78819eb815 More description 2020-04-13 18:54:24 -03:00
matiasdelellis
e69df35435 Explain why the fork 2020-04-13 18:39:48 -03:00
matiasdelellis
a5e7cf3535 Move Instalation doc to wiki 2020-04-13 18:33:54 -03:00
matiasdelellis
80f8101e15 Merge pull request #1 from matiasdelellis/improve-hog
Improve hog
2020-04-13 18:23:59 -03:00
9 changed files with 229 additions and 127 deletions

28
.travis.yml Normal file
View File

@@ -0,0 +1,28 @@
dist: bionic
sudo: false
language: php
php:
- 7.1
- 7.2
- 7.3
- 7.4
before_install:
- sudo sh -c 'echo "deb https://repo.delellis.com.ar bionic bionic" > /etc/apt/sources.list.d/20-pdlib.list'
- wget -qO - https://repo.delellis.com.ar/repo.gpg.key | sudo apt-key add -
- sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 762E3157
- sudo apt update
- sudo apt-get install libdlib-dev
- phpize
- ./configure --enable-debug
- make
- sudo make install
- echo "extension=pdlib.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
script:
- export TEST_PHP_EXECUTABLE=`which php`
- export NO_INTERACTION=1
- php run-tests.php -q --show-diff

View File

@@ -1,69 +1,15 @@
# PDlib - A PHP extension for Dlib
# PDlib - A PHP extension for [Dlib](https://github.com/davisking/dlib)
## Requirements
- Dlib 19.13+
- PHP 7.0+
- C++11
- libx11-dev (on Ubuntu: `sudo apt-get install libx11-dev`)
Dlib is a modern C++ toolkit containing machine learning algorithms and tools for creating complex software in C++ to solve real world problems. See http://dlib.net for the main project documentation and API reference.
## Recommended
- BLAS library
If no BLAS library found - dlib's built in BLAS will be used.
However, if you install an optimized BLAS such as OpenBLAS or the Intel MKL your code
will run faster. On Ubuntu you can install OpenBLAS by executing:
`sudo apt-get install libopenblas-dev liblapack-dev`
PDlib is a PHP extension that implements the PHP binging for the dlib library, that now is used in the Nextcloud [FaceRecognition](https://github.com/matiasdelellis/facerecognition) application.
## Dependencies
### Dlib
Install Dlib as shared library
```bash
git clone https://github.com/davisking/dlib.git
cd dlib/dlib
mkdir build
cd build
cmake -DBUILD_SHARED_LIBS=ON ..
make
sudo make install
```
## Note
This is a fork of the [original pdlib extension](https://github.com/goodspb/pdlib), which we will use temporarily for the Nextcloud [FaceRecognition](https://github.com/matiasdelellis/facerecognition) application while the original author is not responding.
## Installation
```bash
git clone https://github.com/goodspb/pdlib.git
cd pdlib
phpize
./configure --enable-debug
make
sudo make install
```
### Configure PHP installation
```bash
vim youpath/php.ini
```
Append the content below into `php.ini`
```
[pdlib]
extension="pdlib.so"
```
## Tests
For tests, you will need to have bz2 extension installed. On Ubuntu, it boils to:
```bash
sudo apt-get install php-bz2
```
After you successfully compiled everything, just run:
```bash
make test
```
Read [Installation](https://github.com/matiasdelellis/pdlib/wiki/Installation) documentation on the wiki for details.
## Usage

View File

@@ -1,10 +1,3 @@
dnl $Id$
dnl config.m4 for extension pdlib
dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.
if test -z "$PHP_DEBUG"; then
AC_ARG_ENABLE(debug,
[ --enable-debug compile with debugging symbols],[
@@ -14,42 +7,65 @@ if test -z "$PHP_DEBUG"; then
fi
PHP_ARG_WITH(pdlib, for pdlib support,
dnl Make sure that the comment is aligned:
[ --with-pdlib Include pdlib support])
if test "$PHP_PDLIB" != "no"; then
dnl using C++11
CXXFLAGS="-std=c++11"
PHP_REQUIRE_CXX()
PHP_ADD_LIBRARY(stdc++, 1, PDLIB_SHARED_LIBADD)
PHP_SUBST(PDLIB_SHARED_LIBADD)
dnl using C++11
CXXFLAGS="-std=c++11"
PHP_REQUIRE_CXX()
PHP_ADD_LIBRARY(stdc++, 1, PDLIB_SHARED_LIBADD)
PHP_SUBST(PDLIB_SHARED_LIBADD)
pdlib_src_files="pdlib.cc \
src/chinese_whispers.cc \
src/face_detection.cc \
src/face_landmark_detection.cc \
src/face_recognition.cc \
src/cnn_face_detection.cc "
PHP_PDLIB_SOURCES="\
pdlib.cc \
src/chinese_whispers.cc \
src/face_detection.cc \
src/face_landmark_detection.cc \
src/face_recognition.cc \
src/cnn_face_detection.cc \
src/vector.cc \
"
AC_MSG_CHECKING(for pkg-config)
if test ! -f "$PKG_CONFIG"; then
PKG_CONFIG=`which pkg-config`
fi
if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists dlib-1; then
if $PKG_CONFIG dlib-1 --atleast-version 19.00; then
LIBDLIB_CFLAGS=`$PKG_CONFIG dlib-1 --cflags`
LIBDLIB_LIBDIR=`$PKG_CONFIG dlib-1 --libs`
LIBDLIB_VERSON=`$PKG_CONFIG dlib-1 --modversion`
AC_MSG_RESULT(from pkgconfig: dlib version $LIBDLIB_VERSON)
else
AC_MSG_ERROR(system dlib is too old: version 19.00 required)
fi
else
AC_MSG_ERROR(pkg-config not found)
fi
PHP_EVAL_LIBLINE($LIBDLIB_LIBDIR, PDLIB_SHARED_LIBADD)
PHP_EVAL_INCLINE($LIBDLIB_CFLAGS)
PHP_NEW_EXTENSION(pdlib, $pdlib_src_files, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
AC_MSG_CHECKING(for pkg-config)
if test ! -f "$PKG_CONFIG"; then
PKG_CONFIG=`which pkg-config`
fi
if test -f "$PKG_CONFIG"; then
AC_MSG_RESULT(found)
AC_MSG_CHECKING(for dlib-1)
if $PKG_CONFIG --exists dlib-1; then
if $PKG_CONFIG dlib-1 --atleast-version 19.00; then
LIBDLIB_CFLAGS=`$PKG_CONFIG dlib-1 --cflags`
LIBDLIB_LIBDIR=`$PKG_CONFIG dlib-1 --libs`
LIBDLIB_VERSON=`$PKG_CONFIG dlib-1 --modversion`
AC_MSG_RESULT(from pkgconfig: dlib version $LIBDLIB_VERSON)
else
AC_MSG_ERROR(system dlib is too old: version 19.00 required)
fi
else
AC_MSG_ERROR(dlib-1 not found)
fi
else
AC_MSG_ERROR(pkg-config not found)
fi
PHP_EVAL_LIBLINE($LIBDLIB_LIBDIR, PDLIB_SHARED_LIBADD)
PHP_EVAL_INCLINE($LIBDLIB_CFLAGS)
PHP_NEW_EXTENSION(pdlib, $PHP_PDLIB_SOURCES, $ext_shared, , -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
AC_CONFIG_COMMANDS_POST([
echo ""
echo "Build configuration for PDlib v1.0.2 done correctly."
echo ""
echo " CFLAGS : $CFLAGS"
echo " LDFLAGS : $LDFLAGS"
echo " LIBDIR: : $LIBDLIB_LIBDIR"
echo " LIBADD: : $PDLIB_SHARED_LIBADD"
echo " CFLAGS: : $LIBDLIB_CFLAGS"
echo ""
echo "Please submit bug reports at:"
echo " https://github.com/matiasdelellis/pdlib/issues"
echo ""
])

View File

@@ -22,17 +22,20 @@ extern "C" {
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
}
#include <dlib/simd/simd_check.h>
#include "php_pdlib.h"
#include "src/chinese_whispers.h"
#include "src/face_detection.h"
#include "src/face_recognition.h"
#include "src/cnn_face_detection.h"
#include "src/face_landmark_detection.h"
#include "src/vector.h"
/* If you declare any globals in php_pdlib.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(pdlib)
@@ -60,27 +63,6 @@ PHP_INI_END()
*/
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_pdlib_compiled(string arg)
Return a string to confirm that the module is compiled in */
PHP_FUNCTION(confirm_pdlib_compiled)
{
char *arg = NULL;
size_t arg_len, len;
zend_string *strg;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) {
return;
}
strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "pdlib", arg);
RETURN_STR(strg);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
@@ -246,7 +228,49 @@ PHP_RSHUTDOWN_FUNCTION(pdlib)
PHP_MINFO_FUNCTION(pdlib)
{
php_info_print_table_start();
php_info_print_table_header(2, "pdlib support", "enabled");
php_info_print_table_header(2, "PDlib support", "enabled");
php_info_print_table_header(2, "Pdlib Version", PHP_PDLIB_VERSION);
// php_info_print_table_header(2, "Dlib Version", DLIB_VERSION);
#ifdef DLIB_USE_CUDA
php_info_print_table_header(2, "DLIB_USE_CUDA", "true");
#else
php_info_print_table_header(2, "DLIB_USE_CUDA", "false");
#endif
#ifdef DLIB_USE_BLAS
php_info_print_table_header(2, "DLIB_USE_BLAS", "true");
#else
php_info_print_table_header(2, "DLIB_USE_BLAS", "false");
#endif
#ifdef DLIB_USE_LAPACK
php_info_print_table_header(2, "DLIB_USE_LAPACK", "true");
#else
php_info_print_table_header(2, "DLIB_USE_LAPACK", "false");
#endif
#ifdef DLIB_HAVE_AVX
php_info_print_table_header(2, "USE_AVX_INSTRUCTIONS", "true");
#else
php_info_print_table_header(2, "USE_AVX_INSTRUCTIONS", "false");
#endif
#ifdef DLIB_HAVE_AVX2
php_info_print_table_header(2, "USE_AVX2_INSTRUCTIONS", "true");
#else
php_info_print_table_header(2, "USE_AVX2_INSTRUCTIONS", "false");
#endif
#ifdef DLIB_HAVE_NEON
php_info_print_table_header(2, "USE_NEON_INSTRUCTIONS", "true");
#else
php_info_print_table_header(2, "USE_NEON_INSTRUCTIONS", "false");
#endif
#ifdef DLIB_HAVE_SSE2
php_info_print_table_header(2, "USE_SSE2_INSTRUCTIONS", "true");
#else
php_info_print_table_header(2, "USE_SSE2_INSTRUCTIONS", "false");
#endif
#ifdef DLIB_HAVE_SSE41
php_info_print_table_header(2, "USE_SSE4_INSTRUCTIONS", "true");
#else
php_info_print_table_header(2, "USE_SSE4_INSTRUCTIONS", "false");
#endif
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
@@ -260,10 +284,10 @@ PHP_MINFO_FUNCTION(pdlib)
* Every user visible function must have an entry in pdlib_functions[].
*/
const zend_function_entry pdlib_functions[] = {
PHP_FE(confirm_pdlib_compiled, NULL)
PHP_FE(dlib_chinese_whispers, dlib_chinese_whispers_arginfo)
PHP_FE(dlib_face_detection, dlib_face_detection_arginfo)
PHP_FE(dlib_face_landmark_detection, dlib_face_landmark_detection_arginfo)
PHP_FE(dlib_vector_length, dlib_vector_length_arginfo)
PHP_FE_END /* Must be the last line in pdlib_functions[] */
};
/* }}} */
@@ -272,7 +296,7 @@ const zend_function_entry pdlib_functions[] = {
*/
zend_module_entry pdlib_module_entry = {
STANDARD_MODULE_HEADER,
"pdlib",
PHP_PDLIB_NAME,
pdlib_functions,
PHP_MINIT(pdlib),
PHP_MSHUTDOWN(pdlib),

View File

@@ -31,7 +31,8 @@ extern "C" {
extern zend_module_entry pdlib_module_entry;
#define phpext_pdlib_ptr &pdlib_module_entry
#define PHP_PDLIB_VERSION "1.0.1" /* Replace with version number for your extension */
#define PHP_PDLIB_NAME "pdlib"
#define PHP_PDLIB_VERSION "1.0.2"
#ifdef PHP_WIN32
# define PHP_PDLIB_API __declspec(dllexport)

47
src/vector.cc Normal file
View File

@@ -0,0 +1,47 @@
#include "../php_pdlib.h"
#include "vector.h"
#include <cmath>
#include <zend_exceptions.h>
#include <iostream>
using namespace std;
PHP_FUNCTION(dlib_vector_length)
{
zval *x_arg, *y_arg;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "aa", &x_arg, &y_arg) == FAILURE) {
zend_throw_exception_ex(
zend_ce_exception,
0 TSRMLS_CC,
"Unable to parse arrays in dlib_vector_length");
return;
}
zval *elem_x, *elem_y;
double sum = 0.0;
int i, len;
len = zend_hash_num_elements(Z_ARRVAL_P(x_arg));
if (len != zend_hash_num_elements(Z_ARRVAL_P(y_arg))) {
zend_throw_exception_ex(
zend_ce_exception,
0 TSRMLS_CC,
"The arrays have different sizes");
return;
}
for (i = 0 ; i < len ; i++) {
elem_x = zend_hash_index_find(Z_ARRVAL_P(x_arg), i);
elem_y = zend_hash_index_find(Z_ARRVAL_P(y_arg), i);
sum += (Z_DVAL_P(elem_x) - Z_DVAL_P(elem_y))*(Z_DVAL_P(elem_x) - Z_DVAL_P(elem_y));
}
RETURN_DOUBLE(sqrt(sum));
}

10
src/vector.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef PHP_DLIB_VECTOR_H
#define PHP_DLIB_VECTOR_H
ZEND_BEGIN_ARG_INFO_EX(dlib_vector_length_arginfo, 0, 0, 2)
ZEND_ARG_INFO(1, x_arg)
ZEND_ARG_INFO(1, y_arg)
ZEND_END_ARG_INFO()
PHP_FUNCTION(dlib_vector_length);
#endif //PHP_DLIB_VECTOR_H

20
tests/vector_length.phpt Normal file
View File

@@ -0,0 +1,20 @@
--TEST--
Basic tests for dlib_vector_length
--SKIPIF--
<?php if (!extension_loaded("pdlib")) print "skip"; ?>
--FILE--
<?php
$vectorAL = [0.0, 0.0];
$vectorAR = [0.0, 1.0];
var_dump(dlib_vector_length($vectorAL, $vectorAR));
$vectorBL = [0.0, 0.0, -1.0];
$vectorBR = [0.0, 0.0, 1.0];
var_dump(dlib_vector_length($vectorBL, $vectorBR));
$vectorCL = [0.0, 2.5, 1.0];
$vectorCR = [0.0, 1.0, 1.0];
var_dump(dlib_vector_length($vectorCL, $vectorCR));
?>
--EXPECT--
float(1)
float(2)
float(1.5)

10
tests/version.phpt Normal file
View File

@@ -0,0 +1,10 @@
--TEST--
Just test php extension version
--SKIPIF--
<?php if (!extension_loaded("pdlib")) print "skip"; ?>
--FILE--
<?php
var_dump(phpversion('pdlib'));
?>
--EXPECT--
string(5) "1.0.2"