sparqapi/api/application/helpers/faceapi_helper.php
2022-06-04 07:38:38 +00:00

124 lines
4.9 KiB
PHP

<?php
class faceapi
{
public static function toMakeFaceBoundingBox($image_name,$BoundingBox,$destination_path){
log_message('ERROR',"In faceapi helper @ line no : 6 to Make FaceBoundingBox");
$get_image_format = explode(".",$image_name);
$image_format = $get_image_format[1];
log_message('ERROR','In faceapi helper @ line no : 9 to Make FaceBoundingBox image_format :'.$image_format);
// print_r($get_image_format);die();
$img = imagecreatefrompng($destination_path);
// if($image_format == 'png'){
// $img = imagecreatefrompng($destination_path);
// }else if($image_format == 'jpeg'){
// $img = imagecreatefromjpeg($destination_path);
// }else if($image_format == 'jpg'){
// $img = imagecreatefromjpg($destination_path);
// }
$red = imagecolorallocate($img, 255, 0, 0);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
list($imagewidth, $imageheight) = getimagesize($destination_path);
log_message('ERROR','In faceapi helper @ line no : 25 to Make FaceBoundingBox image_width'.$imagewidth.'image_height'.$imageheight);
for($i=0;$i<count($BoundingBox);$i++){
$Left_coordinate = ($BoundingBox[$i]['Left'] * $imagewidth) ;
$Top_coordinate = ($BoundingBox[$i]['Top'] * $imageheight) ;
$Face_width = ($BoundingBox[$i]['Width'] * $imagewidth) ;
$Face_height = ($BoundingBox[$i]['Height'] * $imageheight) ;
$Similarity = $BoundingBox[$i]['Similarity'];
$x1 = $Left_coordinate;
$y1 = $Top_coordinate;
self::DrawLine($x1,$y1,$Face_width,$Face_height,$Similarity,$img,$destination_path);
}
}
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 >= 70 ){// above 70 green
$color = imagecolorallocate($img, 0, 153, 0);
}else if($Similarity_value < 70 ){// below 70 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);
}
}
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);
}
}
?>