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

@ -173,6 +173,39 @@ class Hit
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;
}
}
/** @ORM\Embeddable */

View File

@ -25,4 +25,25 @@ class HitRepository extends EntityRepository
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
require __DIR__ . '/../bootstrap.php';
error_reporting(E_ALL);ini_set('display_errors', true);
$em = \EmotionHero\Application::getInstance()->getEm();
$hitRepo = $em->getRepository(\EmotionHero\Models\Hit::class);
$hits = $hitRepo->findBy([], ['sadness'=>'ASC']);
$currentEmotion = 'anger';
$hits = $hitRepo->findBy([], ["emotions.$currentEmotion"=>'ASC']);
?>
<html>
@ -14,10 +17,29 @@ $hits = $hitRepo->findBy([], ['sadness'=>'ASC']);
<style type="text/css">
body{
background:yellow;
font-family:"Unifont";
font-weight:bold;
}
</style>
<body>
<?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 */
foreach($hits as $hit) {
$filename = __DIR__ . '/../files/hits/brows/'. $hit->getId() . '.jpg';
@ -26,7 +48,13 @@ foreach($hits as $hit) {
$img = file_get_contents($filename);
$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>