189 lines
8.3 KiB
PHP
189 lines
8.3 KiB
PHP
<?php
|
|
|
|
class faceapi
|
|
{
|
|
public static function toMakeFaceBoundingBox($bounding_box, $path, $file_name, $key,$apcntid)
|
|
{
|
|
// ($image_name,$BoundingBox,$destination_path,$extension){
|
|
|
|
log_message('ERROR', "In faceapi helper @ line no : 6 to Make FaceBoundingBox", $path, $file_name);
|
|
$file_path = $path . $file_name;
|
|
// $red = imagecolorallocate($img, 255, 0, 0);
|
|
// $black = imagecolorallocate($img, 0, 0, 0);
|
|
// $white = imagecolorallocate($img, 255, 255, 255);
|
|
|
|
list($imagewidth, $imageheight) = getimagesize($file_path);
|
|
log_message('ERROR', 'In faceapi helper @ line no : 25 to Make FaceBoundingBox image_width' . $imagewidth . 'image_height' . $imageheight);
|
|
|
|
if (count($bounding_box) > 0) {
|
|
$response = array();
|
|
$CmpBoundingBox = $bounding_box['compare_image'];
|
|
$file_path = $path . $file_name;
|
|
$info = getimagesize($file_path);
|
|
$extension = image_type_to_extension($info[2]);
|
|
$SrcBoundingBox = $bounding_box['source_image'];
|
|
if (count($CmpBoundingBox) > 0 && ($key == 'selfie_with_person_met')) {
|
|
$destination_path = $path . 'face_api_image_'.$apcntid . $extension;
|
|
if (!copy($file_path, $destination_path)) {
|
|
log_message('ERROR', 'failed to copy');
|
|
}
|
|
$img = imagecreatefrompng($destination_path);
|
|
|
|
for ($i = 0; $i < count($CmpBoundingBox); $i++) {
|
|
|
|
$Left_coordinate = ($CmpBoundingBox[$i]['Left'] * $imagewidth);
|
|
$Top_coordinate = ($CmpBoundingBox[$i]['Top'] * $imageheight);
|
|
$Face_width = ($CmpBoundingBox[$i]['Width'] * $imagewidth);
|
|
$Face_height = ($CmpBoundingBox[$i]['Height'] * $imageheight);
|
|
$Similarity = $CmpBoundingBox[$i]['Similarity'];
|
|
|
|
$x1 = $Left_coordinate;
|
|
$y1 = $Top_coordinate;
|
|
|
|
$data = self::imgCrop($x1, $y1, $Face_width, $Face_height, $Similarity, $img, $destination_path, $i,$apcntid);
|
|
self::DrawLine($x1, $y1, $Face_width, $Face_height, $Similarity, $img, $destination_path,$apcntid);
|
|
array_push($response,$data);
|
|
|
|
}
|
|
$d['compare_image'] = $response;
|
|
return $d;
|
|
} else {
|
|
$destination_path = $path . 'src_image_'.$apcntid . $extension;
|
|
if (!copy($file_path, $destination_path)) {
|
|
log_message('ERROR', 'failed to copy');
|
|
}
|
|
$img = imagecreatefrompng($destination_path);
|
|
|
|
$Left_coordinate = ($SrcBoundingBox['Left'] * $imagewidth);
|
|
$Top_coordinate = ($SrcBoundingBox['Top'] * $imageheight);
|
|
$Face_width = ($SrcBoundingBox['Width'] * $imagewidth);
|
|
$Face_height = ($SrcBoundingBox['Height'] * $imageheight);
|
|
$Similarity = 0;
|
|
|
|
$x1 = $Left_coordinate;
|
|
$y1 = $Top_coordinate;
|
|
$i = null;
|
|
// self::imgCrop($x1,$y1,$Face_width,$Face_height,$path,$file_name,$i);
|
|
$data = self::imgCrop($x1, $y1, $Face_width, $Face_height, $Similarity, $img, $destination_path, $i,$apcntid);
|
|
unlink($destination_path);
|
|
$response['source_image'] = $data;
|
|
return $response;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static function DrawLine($x1, $y1, $Face_width, $Face_height, $Similarity, $img, $destination_path)
|
|
{
|
|
log_message('ERROR', 'In faceapi helper @ line no : 45 Draw Line x1 : ' . $x1 . ' y1 : ' . $y1 . ' Face_width : ' . $Face_width . ' Face_height : ' . $Face_height . ' Similarity : ' . $Similarity . ' img : ' . $img . ' destination_path : ' . $destination_path);
|
|
$Similarity_split = explode(".", $Similarity);
|
|
$Similarity_value = $Similarity_split[0];
|
|
list($imagewidth, $imageheight) = getimagesize($destination_path);
|
|
if ($Similarity_value > 80) { // above 81 green
|
|
$color = imagecolorallocate($img, 0, 153, 0);
|
|
} else if ($Similarity_value <= 80 && $Similarity_value > 60) { // in between 61 to 80 orange
|
|
$color = imagecolorallocate($img, 255, 165, 0);
|
|
} else if ($Similarity_value <= 60) { // below 60 red
|
|
$color = imagecolorallocate($img, 255, 0, 0);
|
|
}
|
|
$white = imagecolorallocate($img, 255, 255, 255);
|
|
log_message('ERROR', 'In faceapi helper @ line no : 45 Draw Line color : ' . $color);
|
|
$Similarity = $Similarity_value . " %";
|
|
//TOP LINe
|
|
$t_x1 = $x1;
|
|
$t_y1 = $y1;
|
|
$t_x2 = ($x1 + $Face_width);
|
|
$t_y2 = $y1;
|
|
|
|
//bottom line
|
|
$b_x1 = $x1;
|
|
$b_y1 = ($y1 + $Face_height);
|
|
$b_x2 = ($x1 + $Face_width);
|
|
$b_y2 = ($y1 + $Face_height);
|
|
|
|
$imgl = self::imagelinethick($img, $t_x1, $t_y1, $t_x2, $t_y2, $color, 7); //top line
|
|
$imgl = self::imagelinethick($img, $b_x1, $b_y1, $b_x2, $b_y2, $color, 7); //bottom line
|
|
$imgl = self::imagelinethick($img, $t_x1, $t_y1, $b_x1, $b_y1, $color, 7); //left line
|
|
$imgl = self::imagelinethick($img, $t_x2, $t_y2, $b_x2, $b_y2, $color, 7); //right line
|
|
// apppath / doc la roboto.tff
|
|
$font = APPPATH . "docs/roboto.ttf";
|
|
// imagettftext($img, 20, 0, ($t_x1), ($t_y1-5), $color,$font, $Similarity);
|
|
// imagepng($img, $destination_path);
|
|
// imagepng($img, "face4.png");
|
|
// imagefilledrectangle($img, $t_x1-5, $t_y1-18, $t_x1+28, $t_y1, $white);
|
|
// imagettftext($img, 10, 10, ($t_x1), ($t_y1-5), $color,$font, $Similarity);
|
|
imagefilledrectangle($img, $t_x1 - 5, $t_y1 - 30, $t_x1 + 60, $t_y1, $white);
|
|
imagettftext($img, 17, 8, ($t_x1), ($t_y1 - 5), $color, $font, $Similarity);
|
|
|
|
$get_image_format = explode(".", $destination_path);
|
|
$image_format = $get_image_format[1];
|
|
|
|
if ($image_format == 'png') {
|
|
imagepng($img, $destination_path);
|
|
} else if ($image_format == 'jpeg') {
|
|
imagejpeg($img, $destination_path);
|
|
} else if ($image_format == 'jpg') {
|
|
// $destination_path = $get_image_format[0].".jpeg";
|
|
imagejpg($img, $destination_path);
|
|
}
|
|
// $image_data = file_get_contents($destination_path);
|
|
// $base64 = base64_encode($image_data);
|
|
// return array('name' => $temp_image_name, 'base64' => $base64, 'Similarity' => $Similarity);
|
|
}
|
|
|
|
public static function imagelinethick($image, $x1, $y1, $x2, $y2, $color, $thick = 1)
|
|
{
|
|
/* this way it works well only for orthogonal lines
|
|
imagesetthickness($image, $thick);
|
|
return imageline($image, $x1, $y1, $x2, $y2, $color);
|
|
*/
|
|
if ($thick == 1) {
|
|
return imageline($image, $x1, $y1, $x2, $y2, $color);
|
|
}
|
|
$t = $thick / 2 - 0.5;
|
|
if ($x1 == $x2 || $y1 == $y2) {
|
|
return imagefilledrectangle($image, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
|
|
}
|
|
$k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
|
|
$a = $t / sqrt(1 + pow($k, 2));
|
|
$points = array(
|
|
round($x1 - (1 + $k) * $a), round($y1 + (1 - $k) * $a),
|
|
round($x1 - (1 - $k) * $a), round($y1 - (1 + $k) * $a),
|
|
round($x2 + (1 + $k) * $a), round($y2 - (1 - $k) * $a),
|
|
round($x2 + (1 - $k) * $a), round($y2 + (1 + $k) * $a),
|
|
);
|
|
imagefilledpolygon($image, $points, 4, $color);
|
|
return imagepolygon($image, $points, 4, $color);
|
|
}
|
|
|
|
public static function imgCrop($x1, $y1, $Face_width, $Face_height, $Similarity, $img, $destination_path, $i,$apcntid)
|
|
{
|
|
$temp_image_name = $i === null ? "id_proof_crop_".$apcntid.".png" : "face_crop_".$apcntid."_" . $i . ".png";
|
|
log_message('ERROR', 'In faceapi helper - imgCrop @ line no : 126 x1 : ' . $x1 . ' y1 : ' . $y1 . ' Face_width : ' . $Face_width . ' Face_height : ' . $Face_height . ' Similarity : ' . $Similarity . ' img : ' . $img . ' destination_path : ' . $destination_path);
|
|
$destination_path_2 = preg_replace('#/\w+(\.\w+)$#', '/' . $temp_image_name, $destination_path);
|
|
log_message('ERROR', 'In faceapi helper - imgCrop @ line no : 126 destination_path b4 : ' . $destination_path . ' destination_path after : ' . $destination_path_2);
|
|
$to_crop_array = array('x' => $x1, 'y' => $y1, 'width' => $Face_width, 'height' => $Face_height);
|
|
$dest = imagecrop($img, $to_crop_array);
|
|
$get_image_format = explode(".", $destination_path);
|
|
$image_format = $get_image_format[1];
|
|
if ($image_format == 'png') {
|
|
imagepng($dest, $destination_path_2);
|
|
} else if ($image_format == 'jpeg') {
|
|
imagejpeg($dest, $destination_path_2);
|
|
} else if ($image_format == 'jpg') {
|
|
imagejpg($dest, $destination_path_2);
|
|
}
|
|
$image_data = file_get_contents($destination_path_2);
|
|
$base64 = base64_encode($image_data);
|
|
unlink($destination_path_2);
|
|
$Similarity_split = explode(".", $Similarity);
|
|
$Similarity_value = $Similarity_split[0];
|
|
// if($i === null){
|
|
return array('name' => $temp_image_name, 'base64' => $base64, 'Similarity' => $Similarity_value);
|
|
// }else{
|
|
// return array($i => array('name' => $temp_image_name, 'base64' => $base64, 'Similarity' => $Similarity));
|
|
// }
|
|
// return array('name' => $temp_image_name, 'base64' => $base64, 'Similarity' => $Similarity);
|
|
// unlink($destination_path);
|
|
}
|
|
}
|