├── .gitignore ├── tests ├── Data │ ├── empty.png │ ├── lena512color.jpg │ └── lena1024color.jpg └── Unit │ └── FaceDetectionTest.php ├── .travis.yml ├── src ├── Facades │ └── FaceDetection.php ├── FaceDetectionServiceProvider.php ├── config │ └── config.php └── FaceDetection.php ├── phpunit.xml ├── composer.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | .phpunit* 6 | /demo 7 | .phpunit.* -------------------------------------------------------------------------------- /tests/Data/empty.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/freearhey/laravel-face-detection/HEAD/tests/Data/empty.png -------------------------------------------------------------------------------- /tests/Data/lena512color.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/freearhey/laravel-face-detection/HEAD/tests/Data/lena512color.jpg -------------------------------------------------------------------------------- /tests/Data/lena1024color.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/freearhey/laravel-face-detection/HEAD/tests/Data/lena1024color.jpg -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 7.3 5 | 6 | before_script: 7 | - yes | pecl install imagick 8 | - composer install 9 | 10 | script: phpunit 11 | -------------------------------------------------------------------------------- /src/Facades/FaceDetection.php: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | ./tests/ 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "freearhey/laravel-face-detection", 3 | "description": "A Laravel Package for Face Detection and Cropping in Images.", 4 | "license": "MIT", 5 | "keywords": [ 6 | "Laravel", 7 | "Face Detection" 8 | ], 9 | "authors": [ 10 | { 11 | "name": "Arhey" 12 | } 13 | ], 14 | "require": { 15 | "php": "^8.1", 16 | "illuminate/support": "^9.0", 17 | "intervention/image": "^2.5" 18 | }, 19 | "require-dev": { 20 | "phpunit/phpunit": "^9.0" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Arhey\\FaceDetection\\": "src" 25 | } 26 | }, 27 | "autoload-dev": { 28 | "psr-4": { 29 | "Arhey\\FaceDetection\\Tests\\": "tests" 30 | } 31 | }, 32 | "minimum-stability": "stable" 33 | } 34 | -------------------------------------------------------------------------------- /src/FaceDetectionServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->bind('FaceDetection', '\Arhey\FaceDetection\FaceDetection'); 25 | 26 | } 27 | 28 | public function boot() 29 | { 30 | $this->publishes([ 31 | __DIR__ . '/config/config.php' => base_path('config/facedetection.php'), 32 | ]); 33 | } 34 | 35 | /** 36 | * Get the services provided by the provider. 37 | * 38 | * @return array 39 | */ 40 | public function provides() 41 | { 42 | return []; 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | 'gd', 18 | 19 | /* 20 | |-------------------------------------------------------------------------- 21 | | Face Padding 22 | |-------------------------------------------------------------------------- 23 | | 24 | | You can add padding around the face. By default, there 25 | | is no indentation. 26 | | 27 | */ 28 | 'padding_width' => 0, 29 | 'padding_height' => 0, 30 | 31 | ]; 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Face Detection [![Build Status](https://app.travis-ci.com/freearhey/laravel-face-detection.svg?branch=master)](https://app.travis-ci.com/freearhey/laravel-face-detection) 2 | 3 | A Laravel Package for Face Detection. 4 | 5 | ## Installation 6 | 7 | 1. Install the package via composer: 8 | 9 | ```sh 10 | composer require freearhey/laravel-face-detection 11 | ``` 12 | 13 | 2. Add the service provider to the `config/app.php` file in Laravel: 14 | 15 | ```php 16 | 'providers' => [ 17 | ... 18 | Arhey\FaceDetection\FaceDetectionServiceProvider::class, 19 | ... 20 | ], 21 | ``` 22 | 23 | 3. Publish the config file by running: 24 | 25 | ```sh 26 | php artisan vendor:publish 27 | ``` 28 | 29 | ## Usage 30 | 31 | ```php 32 | use Arhey\FaceDetection\Facades\FaceDetection; 33 | 34 | $face = FaceDetection::extract('path/to/image.jpg'); 35 | ``` 36 | 37 | To detect if face is found in a image: 38 | 39 | ```php 40 | if($face->found) { 41 | // face found 42 | } else { 43 | // face not found 44 | } 45 | ``` 46 | 47 | To get the facial boundaries: 48 | 49 | ```php 50 | var_dump($face->bounds); 51 | 52 | /* 53 | array( 54 | 'x' => 292.0, 55 | 'y' => 167.0, 56 | 'w' => 204.8, 57 | 'h' => 204.8, 58 | ) 59 | */ 60 | ``` 61 | 62 | To save the found face image: 63 | 64 | ```php 65 | $face->save('path/to/output.jpg'); 66 | ``` 67 | 68 | ## Testing 69 | 70 | ```sh 71 | vendor/bin/phpunit 72 | ``` 73 | 74 | ## License 75 | 76 | [MIT](LICENSE) 77 | -------------------------------------------------------------------------------- /tests/Unit/FaceDetectionTest.php: -------------------------------------------------------------------------------- 1 | normalFilePath = 'tests/Data/lena512color.jpg'; 19 | $this->largeFilePath = 'tests/Data/lena1024color.jpg'; 20 | $this->emptyFilePath = 'tests/Data/empty.png'; 21 | $this->tmpFilePath = 'tests/Data/face.jpg'; 22 | } 23 | 24 | public function tearDown(): void 25 | { 26 | if(file_exists($this->tmpFilePath)) { 27 | unlink($this->tmpFilePath); 28 | } 29 | } 30 | 31 | public function testDetectFace() 32 | { 33 | $detector = new FaceDetection(); 34 | 35 | $face = $detector->extract($this->normalFilePath); 36 | 37 | $this->assertEquals(true, $face->found); 38 | 39 | $this->assertEquals([ 40 | 'x' => 192.0, 41 | 'y' => 192.0, 42 | 'w' => 204.8, 43 | 'h' => 204.8, 44 | ], $face->bounds); 45 | } 46 | 47 | public function testEmptyImage() 48 | { 49 | $detector = new FaceDetection(); 50 | 51 | $face = $detector->extract($this->emptyFilePath); 52 | 53 | $this->assertEquals(false, $face->found); 54 | 55 | $this->assertEquals(null, $face->bounds); 56 | } 57 | 58 | public function testDetectFaceInLargeImage() 59 | { 60 | $detector = new FaceDetection(); 61 | 62 | $face = $detector->extract($this->largeFilePath); 63 | 64 | $this->assertEquals(true, $face->found); 65 | 66 | $this->assertEquals([ 67 | 'x' => 454.4, 68 | 'y' => 268.8, 69 | 'w' => 137.6, 70 | 'h' => 137.6, 71 | ], $face->bounds); 72 | } 73 | 74 | public function testSaveFaceImage() 75 | { 76 | $detector = new FaceDetection(); 77 | 78 | $face = $detector->extract($this->normalFilePath); 79 | 80 | $face->save($this->tmpFilePath); 81 | 82 | $this->assertFileExists($this->tmpFilePath); 83 | } 84 | 85 | public function testSetDifferentDriver() 86 | { 87 | $detector = new FaceDetection(); 88 | 89 | $detector->driver = new ImageManager(['driver' => 'imagick']); 90 | 91 | $face = $detector->extract($this->normalFilePath); 92 | 93 | $this->assertEquals(true, $face->found); 94 | 95 | $this->assertEquals([ 96 | 'x' => 192.0, 97 | 'y' => 192.0, 98 | 'w' => 204.8, 99 | 'h' => 204.8, 100 | ], $face->bounds); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /src/FaceDetection.php: -------------------------------------------------------------------------------- 1 | driver = $this->defaultDriver(); 24 | 25 | if (function_exists('config')) { 26 | $this->padding_width = config('facedetection.padding_width'); 27 | $this->padding_height = config('facedetection.padding_height'); 28 | } 29 | 30 | $detection_file = 'src/Data/face.dat'; 31 | if (function_exists('base_path')) { 32 | $detection_file = base_path() . '/vendor/freearhey/laravel-face-detection/src/Data/face.dat'; 33 | } 34 | 35 | if (is_file($detection_file)) { 36 | $this->detection_data = unserialize(file_get_contents($detection_file)); 37 | } else { 38 | throw new \Exception("Couldn't load detection data"); 39 | } 40 | } 41 | 42 | public function extract($file) 43 | { 44 | 45 | $this->image = $this->driver->make($file); 46 | 47 | $im_width = $this->image->width(); 48 | $im_height = $this->image->height(); 49 | 50 | //Resample before detection? 51 | $ratio = 0; 52 | $diff_width = 320 - $im_width; 53 | $diff_height = 240 - $im_height; 54 | if ($diff_width > $diff_height) { 55 | $ratio = $im_width / 320; 56 | } else { 57 | $ratio = $im_height / 240; 58 | } 59 | 60 | if ($ratio != 0) { 61 | $this->image->backup(); 62 | $this->image->fit(intval($im_width / $ratio), intval($im_height / $ratio)); 63 | 64 | $stats = $this->get_img_stats($this->image); 65 | $this->bounds = $this->do_detect_greedy_big_to_small($stats['ii'], $stats['ii2'], $stats['width'], $stats['height']); 66 | if ($this->bounds) { 67 | $this->bounds['h'] = $this->bounds['w']; 68 | if ($this->bounds['w'] > 0) { 69 | $this->bounds['x'] *= $ratio; 70 | $this->bounds['y'] *= $ratio; 71 | $this->bounds['w'] *= $ratio; 72 | $this->bounds['h'] *= $ratio; 73 | } 74 | } 75 | $this->image->reset(); 76 | } else { 77 | $stats = $this->get_img_stats($this->image); 78 | $this->bounds = $this->do_detect_greedy_big_to_small($stats['ii'], $stats['ii2'], $stats['width'], $stats['height']); 79 | } 80 | 81 | if ($this->bounds) { 82 | if ($this->bounds['w'] > 0) { 83 | $this->found = true; 84 | } 85 | 86 | $this->bounds['x'] = round($this->bounds['x'], 1); 87 | $this->bounds['y'] = round($this->bounds['y'], 1); 88 | $this->bounds['w'] = round($this->bounds['w'], 1); 89 | $this->bounds['h'] = round($this->bounds['h'], 1); 90 | } 91 | 92 | return $this; 93 | } 94 | 95 | public function save($file_name) 96 | { 97 | if (file_exists($file_name)) { 98 | throw new \Exception("Save File Already Exists ($file_name)"); 99 | } 100 | 101 | $to_crop = [ 102 | 'x' => $this->bounds['x'] - ($this->padding_width / 2), 103 | 'y' => $this->bounds['y'] - ($this->padding_height / 2), 104 | 'width' => $this->bounds['w'] + $this->padding_width, 105 | 'height' => $this->bounds['w'] + $this->padding_height, 106 | ]; 107 | 108 | $cropped_image = $this->driver->make($this->image); 109 | $cropped_image->crop(intval($to_crop['width']), intval($to_crop['height']), intval($to_crop['x']), intval($to_crop['y'])); 110 | $cropped_image->save($file_name, 100, 'jpg'); 111 | } 112 | 113 | protected function defaultDriver() 114 | { 115 | $driver = 'gd'; 116 | 117 | if (function_exists('config')) { 118 | $driver = config('facedetection.driver'); 119 | } 120 | 121 | return new ImageManager(['driver' => $driver]); 122 | } 123 | 124 | protected function get_img_stats($image) 125 | { 126 | $image_width = $image->width(); 127 | $image_height = $image->height(); 128 | $iis = $this->compute_ii($image, $image_width, $image_height); 129 | return array( 130 | 'width' => $image_width, 131 | 'height' => $image_height, 132 | 'ii' => $iis['ii'], 133 | 'ii2' => $iis['ii2'], 134 | ); 135 | } 136 | 137 | protected function compute_ii($image, $image_width, $image_height) 138 | { 139 | $ii_w = $image_width + 1; 140 | $ii_h = $image_height + 1; 141 | $ii = array(); 142 | $ii2 = array(); 143 | 144 | for ($i = 0; $i < $ii_w; $i++) { 145 | $ii[$i] = 0; 146 | $ii2[$i] = 0; 147 | } 148 | 149 | for ($i = 1; $i < $ii_h - 1; $i++) { 150 | $ii[$i * $ii_w] = 0; 151 | $ii2[$i * $ii_w] = 0; 152 | $rowsum = 0; 153 | $rowsum2 = 0; 154 | for ($j = 1; $j < $ii_w - 1; $j++) { 155 | $rgb = $image->pickColor($j, $i, 'int'); 156 | $red = ($rgb >> 16) & 0xFF; 157 | $green = ($rgb >> 8) & 0xFF; 158 | $blue = $rgb & 0xFF; 159 | $grey = floor(0.2989 * $red + 0.587 * $green + 0.114 * $blue); // this is what matlab uses 160 | $rowsum += $grey; 161 | $rowsum2 += $grey * $grey; 162 | 163 | $ii_above = ($i - 1) * $ii_w + $j; 164 | $ii_this = $i * $ii_w + $j; 165 | 166 | $ii[$ii_this] = $ii[$ii_above] + $rowsum; 167 | $ii2[$ii_this] = $ii2[$ii_above] + $rowsum2; 168 | } 169 | } 170 | return array('ii' => $ii, 'ii2' => $ii2); 171 | } 172 | 173 | protected function do_detect_greedy_big_to_small($ii, $ii2, $width, $height) 174 | { 175 | $s_w = $width / 20.0; 176 | $s_h = $height / 20.0; 177 | $start_scale = $s_h < $s_w ? $s_h : $s_w; 178 | $scale_update = 1 / 1.2; 179 | for ($scale = $start_scale; $scale > 1; $scale *= $scale_update) { 180 | $w = floor(20 * $scale); 181 | $endx = $width - $w - 1; 182 | $endy = $height - $w - 1; 183 | $step = floor(max($scale, 2)); 184 | $inv_area = 1 / ($w * $w); 185 | for ($y = 0; $y < $endy; $y += $step) { 186 | for ($x = 0; $x < $endx; $x += $step) { 187 | $passed = $this->detect_on_sub_image($x, $y, $scale, $ii, $ii2, $w, $width + 1, $inv_area); 188 | if ($passed) { 189 | return array('x' => $x, 'y' => $y, 'w' => $w); 190 | } 191 | } // end x 192 | } // end y 193 | } // end scale 194 | return null; 195 | } 196 | 197 | protected function detect_on_sub_image($x, $y, $scale, $ii, $ii2, $w, $iiw, $inv_area) 198 | { 199 | $mean = ($ii[($y + $w) * $iiw + $x + $w] + $ii[$y * $iiw + $x] - $ii[($y + $w) * $iiw + $x] - $ii[$y * $iiw + $x + $w]) * $inv_area; 200 | $vnorm = ($ii2[($y + $w) * $iiw + $x + $w] + $ii2[$y * $iiw + $x] - $ii2[($y + $w) * $iiw + $x] - $ii2[$y * $iiw + $x + $w]) * $inv_area - ($mean * $mean); 201 | $vnorm = $vnorm > 1 ? sqrt($vnorm) : 1; 202 | 203 | $passed = true; 204 | for ($i_stage = 0; $i_stage < count($this->detection_data); $i_stage++) { 205 | $stage = $this->detection_data[$i_stage]; 206 | $trees = $stage[0]; 207 | 208 | $stage_thresh = $stage[1]; 209 | $stage_sum = 0; 210 | 211 | for ($i_tree = 0; $i_tree < count($trees); $i_tree++) { 212 | $tree = $trees[$i_tree]; 213 | $current_node = $tree[0]; 214 | $tree_sum = 0; 215 | while ($current_node != null) { 216 | $vals = $current_node[0]; 217 | $node_thresh = $vals[0]; 218 | $leftval = $vals[1]; 219 | $rightval = $vals[2]; 220 | $leftidx = $vals[3]; 221 | $rightidx = $vals[4]; 222 | $rects = $current_node[1]; 223 | 224 | $rect_sum = 0; 225 | for ($i_rect = 0; $i_rect < count($rects); $i_rect++) { 226 | $s = $scale; 227 | $rect = $rects[$i_rect]; 228 | $rx = floor($rect[0] * $s + $x); 229 | $ry = floor($rect[1] * $s + $y); 230 | $rw = floor($rect[2] * $s); 231 | $rh = floor($rect[3] * $s); 232 | $wt = $rect[4]; 233 | 234 | $r_sum = ($ii[($ry + $rh) * $iiw + $rx + $rw] + $ii[$ry * $iiw + $rx] - $ii[($ry + $rh) * $iiw + $rx] - $ii[$ry * $iiw + $rx + $rw]) * $wt; 235 | $rect_sum += $r_sum; 236 | } 237 | 238 | $rect_sum *= $inv_area; 239 | 240 | $current_node = null; 241 | if ($rect_sum >= $node_thresh * $vnorm) { 242 | if ($rightidx == -1) { 243 | $tree_sum = $rightval; 244 | } else { 245 | $current_node = $tree[$rightidx]; 246 | } 247 | 248 | } else { 249 | if ($leftidx == -1) { 250 | $tree_sum = $leftval; 251 | } else { 252 | $current_node = $tree[$leftidx]; 253 | } 254 | 255 | } 256 | } 257 | $stage_sum += $tree_sum; 258 | } 259 | if ($stage_sum < $stage_thresh) { 260 | return false; 261 | } 262 | } 263 | return true; 264 | } 265 | } 266 | --------------------------------------------------------------------------------