Face detection using Laravel

|
| By Webner

Method of Face detection using Laravel

Laravel is a PHP framework for Web Development. It provides many features to make things easy for Web Development. It also takes care of the security of the application. If you have an intermediate level knowledge of PHP, then you can take advantage of its features and can boost your development speed. There are many libraries to make it better. There is a library to detect a person’s face in the Laravel. It may not work on the large images or on the images having a person’s rotated image. However, it works well for a single person with a normal image. Below are the steps to detect face using laravel.

Firstly, install the library for face detection in your laravel project:
Do entries in your composer.json file with “softon/laravel-face-detect”: “dev-master”
Now run command “composer update” in your project location. It will download and install the library in your project.

Create a controller using the command:
php artisan make:controller FaceDetectionController

Now paste the below code in the controller:

 <?php
namespace App\Http\Controllers;
use \Softon\LaravelFaceDetect\Facades\FaceDetect;

class FaceDetection extends Controller
{
   function detect(){
//here we are detecting the coordinates of face
$co = FaceDetect::extract(‘your_image_path’)->face;   
   
//now we are creating new image form the existing image
$output = imagecreatefromjpeg(‘your_image_path’);

//the below function will draw green rectangle around the face 
imagerectangle($output, $co['x'], $co['y'], $co['x']+$co['w'], $co['y']+$co['w'], 0x00ff00);
header('Content-Type: image/jpeg');
return imagejpeg($output);
   }
}
?>

After writing code in the controller. Now do an entry in the web.php as given below
Route::get(‘/detect’, ‘FaceDetection@detect’);

Copy an image having a medium sized person in the path storage/app/ folder and run your server by “php artisan serve” command.

Now open any browser. I am using Google Chrome and hit the following url:
http://127.0.0.1:8000/detect

It will open the image having a rectangle on the face of the person. As given below:
Before

After

Leave a Reply

Your email address will not be published. Required fields are marked *