moodmeter/images.php

117 lines
3.4 KiB
PHP
Raw Normal View History

2017-12-04 21:47:24 +01:00
<?php
$basedir = "/home/crowd/pictures/";
$framename = basename($_GET['i']);
$outputnr = (int) $_GET['nr'];
$params = explode(',',$_GET['params']);
$availableParams = array("smile",
"innerBrowRaise",
"browRaise",
"browFurrow",
"noseWrinkle",
"upperLipRaise",
"lipCornerDepressor",
"chinRaise",
"lipPucker",
"lipPress",
"lipSuck",
"mouthOpen",
"smirk",
"eyeClosure",
"eyeWiden",
"cheekRaise",
"lidTighten",
"dimpler",
"lipStretch",
"jawDrop"
);
foreach($params as $param) {
if(!in_array($param, $availableParams)) {
throw new Exception("Invalid param" . $param);
}
}
$framefile = $basedir ."/".$outputnr ."/".$framename.".jpg";
$framejson = $basedir ."/".$outputnr ."/".$framename.".json";
$im = imagecreatefromjpeg($framefile);
$json = json_decode(file_get_contents($framejson), true);
$colorFg = imagecolorallocate($im, 255,255,255);
$avg = [];
foreach($params as $param) {
$values = array_map(function($f) use($param){return $f[$param];}, $json['faces']);
$avg[$param] = array_sum($values) / count($values);
}
foreach($json['faces'] as &$face) {
$face['diff'] = 0;
foreach($params as $param) {
$face['diff'] += abs($avg[$param] - $face[$param]);
}
$face['diff'] = sqrt($face['diff']);
}
usort($json['faces'], function ($f1, $f2) {
if ($f1['diff'] == $f2['diff']) return 0;
return $f1['diff'] > $f2['diff'] ? -1 : 1;
});
$targetIm = imagecreatetruecolor(1920, 1080);
$white = imagecolorallocate($targetIm, 255, 255, 255);
imagefill($targetIm, 0, 0, $white);
$i = 0;
$faceDim = 120;
$faceMargin = 20;
$itemsPerRow = (int) 1080 / ($faceDim + $faceMargin);
$maxDiff = sqrt(count($params) * 100);
foreach($json['faces'] as $face) {
$posX = $i % $itemsPerRow;
$posY = (int) ($i / $itemsPerRow);
$destX = $posX * ($faceDim + $faceMargin);
$destY = $posY * ($faceDim + $faceMargin);
imagecopyresampled ( $targetIm , $im , $destX , $destY , (int) $face['rect']['x'] , (int) $face['rect']['y'] , $faceDim , $faceDim , (int) $face['rect']['w'], (int) $face['rect']['h']);
$score = ($face['diff'] / $maxDiff) * 255; // score on 0-100 -> 255 scale for color:
$colorBg = imagecolorallocate($im, (int) $score, (int) 255 - $score, 0);
imagettftext($targetIm, 12, 0, $destX, $destY + $faceDim + $faceMargin/1.2, $colorBg, "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", round($face['diff'],2));
$i++;
}
$im = $targetIm;
/*
foreach($json['faces'] as $face) {
// Draw a rectangle in color representing score on params
imagecolorallocate($im, 200,200,200);
$total = 0;
foreach($params as $param) {
$total += $face[$param];
}
$score = ($total / count($params)) * 2.55; // score on 0-100 -> 255 scale for color:
$colorBg = imagecolorallocate($im, (int) 255 - $score, (int) $score, 0);
//~ var_dump($face['rect']);
//~ imagefilledrectangle($im, $face['rect']['x'], $face['rect']['y'], $face['rect']['x']+$face['rect']['width'], $face['rect']['y']+$face['rect']['height'], $colorBg);
$face['rect']['x2'] = round($face['rect']['x'] + $face['rect']['w']);
$face['rect']['y2'] = round($face['rect']['y'] + $face['rect']['h']);
$face['rect']['x'] = round($face['rect']['x']);
$face['rect']['y'] = round($face['rect']['y']);
imagefilledrectangle($im, $face['rect']['x'], $face['rect']['y'], $face['rect']['x2'], $face['rect']['y2'], $colorBg);
}
$im = imagescale($im, 1920, 1080);
*/
header("Content-Type: image/jpeg");
// Save the image
imagejpeg($im, NULL, 80);
//~ imagedestroy($im);