Face Detector PHP
Identify the location of the face in the image using GD.
- It was created based on the mauricesvay/php-facedetection project.
- By porting to js and applying the improvements, I rewrote the PHP code with error_reporting = E_ALL safe.
- The reason the license is GPL-2.0 is because it must retain the license of the original.
- There is a big difference in recognition rate depending on the image size. Because I did not understand the principles of the algorithm, I was unable to improve the recognition rate other than size. Most images used in testing have the best results when they are 281px, so the default value is 281.
- Because it consumes a lot of resources, if you want to use it for thumbnail sorting purposes, it is better to detect it at the time of upload and save the direction value together, rather than applying it in real time.
$ php composer.phar require " crucifyer/facedetector-php " " dev-main "
$ detector = new Xeno Image FaceDetector (imagefile or gdresource or imagebinary);
$ face = $ detector -> FaceDetect ();
[ ' x ' => int, ' y ' => int, ' w ' => int]
$ faces = $ detector -> FaceDetect ( true );
[
[ ' x ' => int, ' y ' => int, ' w ' => int],
[ ' x ' => int, ' y ' => int, ' w ' => int],
[ ' x ' => int, ' y ' => int, ' w ' => int],
[ ' x ' => int, ' y ' => int, ' w ' => int],
[ ' x ' => int, ' y ' => int, ' w ' => int],
]
FaceDetector:: FaceDetect ([multiplue bool], [resSize int])
- GIF, jpeg, png, webp image files, gd resources, and image binaries are available.
multiple
- If you set the multiple value to true, up to 10 more face positions will be found and returned as an array.
- You can enter a number instead of true, limited to 2 to 50.
- The recognition rate for multiple faces is particularly poor, so there is a high probability that not all faces will be recognized even if the image size is changed.
resSize
- Instead of using the original image as is, use a reduced image size. It is reduced based on the smaller of width and height, and the default value is 281.
$ face = $ detector -> FaceDetect ();
$ size = $ detector -> getImageSize ();
$ direction = Xeno Image FaceDetector:: AlignDirection ( $ size [ ' width ' ], $ size [ ' height ' ], $ face [ ' x ' ], $ face [ ' y ' ], $ face [ ' w ' ]);
- Returns the direction to align towards the face.
- If the width is long, it returns the position where the face was detected among left, center, and right, and if the height is long, it returns the position where the face was detected, among top, middle, and bottom.
- We judge that it is biased by about 30% or more.
$ faces = Xeno Image FaceDetector:: FilterSmallFaces ( $ faces );
- Filter out anything that is at least 60% smaller than the largest one.
$ detector = new Xeno Image FaceDetector (imagefile or gdresource or imagebinary);
$ direction = $ detector -> getDirection ();
- Automates the process up to direction recognition. When multiple faces are recognized, they are centered.
$ detector = new Xeno Image FaceDetector (imagefile or gdresource or imagebinary);
[gd resource or boolean] = $ detector -> cropThumbnail (size, [direction], [file], [type]);
- Returns the cropped thumbnail. Even if the original is smaller than size, it is not enlarged to fit, but is simply cropped according to the ratio.
direction
- If omitted, getDirection is used to fill it.
file
- If omitted, gd resource is returned.
type
- gif, jpg, png, png8, and webp are supported. If omitted, it is determined by the file extension.
- png8 uses indexed colors in the same way as gif, so its size is small and the image quality is poor.
A comprehensive example is contained in tests/example.php. Edit your autoload.php location and test it in the console.
js project: https://github.com/crucifyer/facedetector-js