diff --git a/src/Models/Hit.php b/src/Models/Hit.php index 9b50572..c8499e2 100644 --- a/src/Models/Hit.php +++ b/src/Models/Hit.php @@ -172,6 +172,39 @@ class Hit $this->score = $score; 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 */ diff --git a/src/Models/HitRepository.php b/src/Models/HitRepository.php index dd79d52..62c28c9 100644 --- a/src/Models/HitRepository.php +++ b/src/Models/HitRepository.php @@ -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(); + } } \ No newline at end of file diff --git a/www/faces.php b/www/faces.php index d9690b4..aab3f3d 100644 --- a/www/faces.php +++ b/www/faces.php @@ -1,10 +1,13 @@ getEm(); $hitRepo = $em->getRepository(\EmotionHero\Models\Hit::class); -$hits = $hitRepo->findBy([], ['sadness'=>'ASC']); + +$currentEmotion = 'anger'; + +$hits = $hitRepo->findBy([], ["emotions.$currentEmotion"=>'ASC']); ?> @@ -14,10 +17,29 @@ $hits = $hitRepo->findBy([], ['sadness'=>'ASC']); 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 +
+$percentage + +
+EOSNIPPET; +} + /* @var $hit EmotionHero\Models\Hit */ foreach($hits as $hit) { $filename = __DIR__ . '/../files/hits/brows/'. $hit->getId() . '.jpg'; @@ -26,8 +48,14 @@ foreach($hits as $hit) { $img = file_get_contents($filename); $data = base64_encode($img); - echo ""; + $percentage = sprintf("%.4f %%",$hit->getEmotions()->getEmotionScore($currentEmotion)); + echo <<< EOSNIPPET +
+$percentage + +
+EOSNIPPET; } ?> - + \ No newline at end of file