Add points

This commit is contained in:
Ruben 2016-09-15 10:42:32 +01:00
parent 516bc4cd20
commit 0e3b511721
2 changed files with 204 additions and 34 deletions

View file

@ -143,7 +143,7 @@ class Hit
/** /**
* Gets the value of points. * Gets the value of points.
* *
* @return mixed * @return Points
*/ */
public function getPoints() public function getPoints()
{ {
@ -196,7 +196,7 @@ class Hit
/** /**
* @param string $feature * @param string $feature
* @return string * @return string|false false if not exists
* @throws \Exception * @throws \Exception
*/ */
public function getFeatureFilename(string $feature) { public function getFeatureFilename(string $feature) {
@ -1327,6 +1327,55 @@ class Points{
$this->{"point".$nr} = new Point($x, $y); $this->{"point".$nr} = new Point($x, $y);
} }
return $this; return $this;
$a = $this->getNormalisedPoints();
}
/**
* @return array[Point]
*/
public function getAsArray() {
$points = [];
foreach(range(0, 33) as $i) {
$points[$i] = $this->{"point$i"};
}
return $points;
}
/**
* Fits points within a 100x100 scale
* @return array[Point]
*/
public function getNormalisedPoints() {
$points = $this->getAsArray();
$xs = array_map(function(Point $a){return $a->getX();}, $points);
$ys = array_map(function(Point $a){return $a->getX();}, $points);
$minx = min($xs);
$maxx = max($xs);
$miny = min($ys);
$maxy = max($ys);
$sizex = $maxx-$minx;
$sizey = $maxy-$miny;
$scale = max($sizex, $sizey); // divide by largest diff.
//used to center the face within the square:
$diffx = ($scale - $sizex)/2;
$diffy = ($scale - $sizey)/2;
$normalised_points = [];
foreach($points as $i => $point) {
$x = (($point->getX() - $minx) / $scale) + $diffx;
$y = (($point->getY() - $miny) / $scale) + $diffy;
$normalised_points[$i] = new Point($x, $y);
}
return $normalised_points;
} }
} }
@ -1350,4 +1399,18 @@ class Point{
$this->x = $x; $this->x = $x;
$this->y = $y; $this->y = $y;
} }
/**
* @return float
*/
public function getX() {
return $this->x;
}
/**
* @return float
*/
public function getY() {
return $this->y;
}
} }

View file

@ -5,9 +5,28 @@ error_reporting(E_ALL);ini_set('display_errors', true);
$em = \EmotionHero\Application::getInstance()->getEm(); $em = \EmotionHero\Application::getInstance()->getEm();
$hitRepo = $em->getRepository(\EmotionHero\Models\Hit::class); $hitRepo = $em->getRepository(\EmotionHero\Models\Hit::class);
$currentEmotion = 'anger'; /* @var $currentHit \EmotionHero\Models\Hit */
$currentHit = $hitRepo->findOneBy(['hasImage'=>true], ['id'=>'DESC']);
$printEmoBlocks = function($emotion, $feature, $steps = 6) use($em) {
foreach(range(0, 100, 100/($steps-1)) as $i) {
/* @var $hitRepo EmotionHero\Models\HitRepository */
$hitRepo = $em->getRepository(EmotionHero\Models\Hit::class);
$hit = $hitRepo->getClosestHitWithImage($emotion, $i);
$img = $hit->getFeatureImgAsString($feature);
$score = $hit->getEmotions()->getEmotionScore($emotion);
$percentage = sprintf("%.0f %%",$score);
echo <<< EOSNIPPET
<div class="block">
<img src="data:image/x-icon;base64,$img" title="{$hit->getId()}">
<span class="perc">$percentage</span>
</div>
EOSNIPPET;
}
};
$hits = $hitRepo->findBy([], ["emotions.$currentEmotion"=>'ASC']);
?> ?>
<html> <html>
@ -20,42 +39,130 @@ $hits = $hitRepo->findBy([], ["emotions.$currentEmotion"=>'ASC']);
font-family:"Unifont"; font-family:"Unifont";
font-weight:bold; font-weight:bold;
} }
.block{
width:150px;
}
img{
width:100%;
filter:saturate(0%);
mix-blend-mode: multiply;
/*mix-blend-mode: exclusion;*/
}
.nose .block{
width:70px;
}
.perc{
display:block;
width:100%;
text-align:center;
}
.emo-block{
float:left;
margin-right:10px;
text-align:center;
}
.emo-block-emos{
position:relative;
}
.emo{
text-align:center;
}
.brows{
width:650px;
}
.nose{
width:240px;
}
.current{
width:200px;
margin:0 auto;
}
img.curImg{
position:absolute;
left:0;
mix-blend-mode:difference;
}
</style> </style>
<body> <body>
<?php <?php
foreach(range(0, 100, 10) as $i) { echo "<div class='current'>";
/* @var $hitRepo EmotionHero\Models\HitRepository */ echo "<img src=\"data:image/x-icon;base64,{$currentHit->getFeatureImgAsString("brows")}\" title=\"{$currentHit->getId()}\">";
$hitRepo = $em->getRepository(EmotionHero\Models\Hit::class); echo "<dl>";
$hit = $hitRepo->getClosestHitWithImage("anger", $i); foreach(EmotionHero\Models\Emotions::$EMOTIONS as $emotion) {
$img = $hit->getFeatureImgAsString("brows"); echo "<dt>$emotion</dt><dd>" . sprintf("$.2f", $currentHit->getEmotions()->getEmotionScore($emotion)) . "</dd>";
$score = $hit->getEmotions()->getEmotionScore("anger"); }
echo "</dl>";
$percentage = sprintf("%.4f %%",$score); echo "</div>";
echo <<< EOSNIPPET
<div class="block"> echo "<div class='brows emo'><h3>eye brows</h3>";
<span class="perc">$percentage</span> echo "<div class='emo-block'>anger<div class='emo-block-emos'>";
<img src="data:image/x-icon;base64,$img" title='{$hit->getId()}'> $printEmoBlocks('anger','brows');
</div> echo "<img class='curImg' src=\"data:image/x-icon;base64,{$currentHit->getFeatureImgAsString("brows")}\" title=\"{$currentHit->getId()}\" style=\"top:{$currentHit->getEmotions()->getAnger()}%\">";
EOSNIPPET; echo "</div></div>";
echo "<div class='emo-block'>joy<div class='emo-block-emos'>";
$printEmoBlocks('joy','brows');
echo "<img class='curImg' src=\"data:image/x-icon;base64,{$currentHit->getFeatureImgAsString("brows")}\" title=\"{$currentHit->getId()}\" style=\"top:{$currentHit->getEmotions()->getJoy()}%\">";
echo "</div></div>";
echo "<div class='emo-block'>sadness<div class='emo-block-emos'>";
$printEmoBlocks('sadness','brows');
echo "<img class='curImg' src=\"data:image/x-icon;base64,{$currentHit->getFeatureImgAsString("brows")}\" title=\"{$currentHit->getId()}\" style=\"top:{$currentHit->getEmotions()->getSadness()}%\">";
echo "</div></div>";
echo "<div class='emo-block'>surprise<div class='emo-block-emos'>";
$printEmoBlocks('surprise','brows');
echo "<img class='curImg' src=\"data:image/x-icon;base64,{$currentHit->getFeatureImgAsString("brows")}\" title=\"{$currentHit->getId()}\" style=\"top:{$currentHit->getEmotions()->getSurprise()}%\">";
echo "</div></div>";
echo "</div>"; // .brows
echo "<div class='nose emo'><h3>nose</h3>";
echo "<div class='emo-block'>anger";
$printEmoBlocks('joy','nose');
echo "</div>";
echo "<div class='emo-block'>joy";
$printEmoBlocks('disgust','nose');
echo "</div>";
echo "<div class='emo-block'>sadness";
$printEmoBlocks('sadness','nose');
echo "</div>";
echo "</div>"; // .nose
$circles = "";
foreach($currentHit->getPoints()->getNormalisedPoints() as $i => $point) {
$circles .= '<circle
style="fill:#ff0000;"
id="point'.$i.'"
cx="'.$point->getX().'"
cy="'.$point->getY().'"
r="1" />';
} }
/* @var $hit EmotionHero\Models\Hit */ echo <<< EOSVG
foreach($hits as $hit) { <svg
$filename = __DIR__ . '/../files/hits/brows/'. $hit->getId() . '.jpg'; xmlns:svg="http://www.w3.org/2000/svg"
if(!file_exists($filename)) xmlns="http://www.w3.org/2000/svg"
continue; viewBox="0 0 100 100"
id="svg2"
$img = file_get_contents($filename); version="1.1">
$data = base64_encode($img); <g id="layer1">
$percentage = sprintf("%.4f %%",$hit->getEmotions()->getEmotionScore($currentEmotion)); $circles
echo <<< EOSNIPPET </g>
<div class="block"> </svg>
<span class="perc">$percentage</span> EOSVG;
<img src="data:image/x-icon;base64,$data" title='{$hit->getId()}'>
</div>
EOSNIPPET;
}
?> ?>
</body> </body>
</html> </html>