api.emotionhero.com/src/Api/InterfaceControllerProvider.php

70 lines
2.2 KiB
PHP
Raw Normal View History

2016-11-02 23:14:19 +00:00
<?php
namespace EmotionHero\Api;
use EmotionHero\Application as EH;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Silex\Api\ControllerProviderInterface;
use EmotionHero\Models;
use EmotionHero\Server\Websockets;
class InterfaceControllerProvider implements ControllerProviderInterface
{
/** @var EH */
protected $_eh;
public function __construct()
{
$this->_eh = EH::getInstance();
}
public function connect(Application $app)
{
// creates a new controller based on the default route
$controllers = $app['controllers_factory'];
$controllers->get('/images', function (Application $app) {
2016-11-02 23:17:46 +00:00
$getEmoBlocks = function($emotion, $feature, $steps = 6) {
2016-11-02 23:18:07 +00:00
$hitRepo = $this->_eh->getEm()->getRepository(Models\Hit::class);
2016-11-02 23:14:19 +00:00
$blocks = [];
2016-11-03 00:12:34 +00:00
$position = 1;
2016-11-02 23:14:19 +00:00
foreach(range(0, 100, 100/($steps-1)) as $i) {
/* @var $hitRepo EmotionHero\Models\HitRepository */
$hit = $hitRepo->getClosestHitWithImage($emotion, $i);
$img = "data:image/x-icon;base64,".$hit->getFeatureImgAsString($feature);
$score = $hit->getEmotions()->getEmotionScore($emotion);
$percentage = sprintf("%.0f %%",$score);
2016-11-03 00:12:34 +00:00
$blocks[$position] = [
2016-11-02 23:14:19 +00:00
'hit_id' => $hit->getId(),
'img_data' => $img,
'percentage' => $percentage,
];
2016-11-03 00:12:34 +00:00
$position++;
2016-11-02 23:14:19 +00:00
}
return $blocks;
};
$features = ['brows' => 6, 'nose' => 5, 'mouth' => 8];
$output = [];
foreach($features as $feature => $steps) {
foreach(Models\Emotions::$EMOTIONS as $emotion) {
$output[$feature][$emotion] = $getEmoBlocks(
$emotion,
$feature == 'mouth' ? 'mouth_left':$feature,
$steps
);
}
}
return $app['serializer']->serialize($output, 'json');
});
2016-11-02 23:16:32 +00:00
2016-11-02 23:17:08 +00:00
2016-11-02 23:16:32 +00:00
return $controllers;
2016-11-02 23:14:19 +00:00
}
}