New faces test

This commit is contained in:
Ruben 2016-09-14 16:34:01 +01:00
parent 1bc8b11ee1
commit 5a765edef3
3 changed files with 86 additions and 5 deletions

View File

@ -172,6 +172,39 @@ class Hit
$this->score = $score; $this->score = $score;
return $this; return $this;
} }
public static $FEATURES = ['brows','nose','mouth_left','mouth_right'];
/**
* @param string $feature
* @return string
* @throws \Exception
*/
public function getFeatureFilename(string $feature) {
if(!in_array($feature, static::$FEATURES)) {
throw new \Exception("Invalid feature!");
}
return realpath(__DIR__ . "/../../files/hits/".$feature."/".$this->getId() );
}
/**
* @param string $feature
* @return string|null
*/
public function getFeatureImgAsString($feature) {
$filename = $this->getFeatureFilename($feature);
if(!file_exists($filename)) {
return null;
}
$contents = file_get_contents($filename);
$string = base64_encode($contents);
if ($string === false) {
return null;
}
return $string;
}
} }
@ -271,7 +304,6 @@ class Attributes {
} }
return $this; return $this;
} }
} }
/** @ORM\Embeddable */ /** @ORM\Embeddable */

View File

@ -25,4 +25,25 @@ class HitRepository extends EntityRepository
return $betterHit; return $betterHit;
} }
/**
*
* @param string $emotionField
* @param float $score
* @return Hit
* @throws \Exception
*/
public function getClosestHit(string $emotionField, float $score) {
if(!in_array($emotionField, Emotions::$EMOTIONS))
throw new \Exception("Invalid emotion!");
$query = $this->_em->createQuery(
"SELECT h, ABS(:score - h.emotions.$emotionField) as HIDDEN distance FROM ".Hit::class." h ORDER BY distance ASC "
)
->setParameters([
'score' => $score,
])
->setMaxResults(1);
return $query->getSingleResult();
}
} }

View File

@ -1,10 +1,13 @@
<?php <?php
require __DIR__ . '/../bootstrap.php'; require __DIR__ . '/../bootstrap.php';
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);
$hits = $hitRepo->findBy([], ['sadness'=>'ASC']);
$currentEmotion = 'anger';
$hits = $hitRepo->findBy([], ["emotions.$currentEmotion"=>'ASC']);
?> ?>
<html> <html>
@ -14,10 +17,29 @@ $hits = $hitRepo->findBy([], ['sadness'=>'ASC']);
<style type="text/css"> <style type="text/css">
body{ body{
background:yellow; background:yellow;
font-family:"Unifont";
font-weight:bold;
} }
</style> </style>
<body> <body>
<?php <?php
foreach(range(0, 100, 10) as $i) {
/* @var $hitRepo EmotionHero\Models\HitRepository */
$hitRepo = $em->getRepository(EmotionHero\Models\Hit::class);
$hit = $hitRepo->getClosestHit("anger", $i);
$img = $hit->getFeatureImgAsString("anger");
$score = $hit->getEmotions()->getEmotionScore("anger");
$percentage = sprintf("%.4f %%",$score);
echo <<< EOSNIPPET
<div class="block">
<span class="perc">$percentage</span>
<img src="data:image/x-icon;base64,$img" title='{$hit->getId()}'>
</div>
EOSNIPPET;
}
/* @var $hit EmotionHero\Models\Hit */ /* @var $hit EmotionHero\Models\Hit */
foreach($hits as $hit) { foreach($hits as $hit) {
$filename = __DIR__ . '/../files/hits/brows/'. $hit->getId() . '.jpg'; $filename = __DIR__ . '/../files/hits/brows/'. $hit->getId() . '.jpg';
@ -26,8 +48,14 @@ foreach($hits as $hit) {
$img = file_get_contents($filename); $img = file_get_contents($filename);
$data = base64_encode($img); $data = base64_encode($img);
echo "<img src=\"data:image/x-icon;base64,$data\">"; $percentage = sprintf("%.4f %%",$hit->getEmotions()->getEmotionScore($currentEmotion));
echo <<< EOSNIPPET
<div class="block">
<span class="perc">$percentage</span>
<img src="data:image/x-icon;base64,$data" title='{$hit->getId()}'>
</div>
EOSNIPPET;
} }
?> ?>
</body> </body>
</html> </html>