<?php namespace EmotionHero\Models; use Doctrine\ORM\Mapping as ORM; use JMS\Serializer\Annotation as JMS; use Doctrine\Common\Collections\ArrayCollection; /** * Hit of a target and the facial parameters at the moment of the hit * * @ORM\Table(name="hits") * @ORM\Entity * @ORM\Entity(repositoryClass="EmotionHero\Models\HitRepository") */ class Hit { /** * @var integer * * @ORM\Column(name="id",type="integer") * @ORM\Id * @ORM\GeneratedValue */ private $id; /** * @ORM\ManyToOne(targetEntity="Target", inversedBy="hits") * @ORM\JoinColumn(name="target_id", referencedColumnName="id", nullable=false) */ private $target; /** * @ORM\ManyToOne(targetEntity="Game", inversedBy="hits", cascade={"persist"}) * @ORM\JoinColumn(name="game_id", referencedColumnName="id", nullable=false) * @JMS\Exclude */ private $game; /** * @var float The score this hit resulted in * @ORM\Column */ private $score; /** @ORM\Embedded(class="Attributes", columnPrefix=false) */ private $attributes; /** @ORM\Embedded(class="Expressions", columnPrefix=false) */ private $expressions; /** @ORM\Embedded(class = "Emotions", columnPrefix=false) */ private $emotions; /** @ORM\Embedded(class = "Points", columnPrefix=false) */ private $points; public function __construct() { $this->hits = new ArrayCollection(); $this->emotions = new Emotions(); $this->attributes = new Attributes(); $this->expressions = new Expressions(); $this->points = new Points(); } /** * Gets the value of score. * * @return float The score this hit resulted in */ public function getScore() { return $this->score; } /** * Gets the value of id. * * @return integer */ public function getId() { return $this->id; } /** * Gets the value of target. * * @return mixed */ public function getTarget() { return $this->target; } /** * Gets the value of game. * @return mixed */ public function getGame() { return $this->game; } /** * Gets the value of emotions. * * @return mixed */ public function getEmotions() { return $this->emotions; } /** * Gets the value of attributes. * * @return mixed */ public function getAttributes() { return $this->attributes; } /** * Gets the value of expressions. * * @return mixed */ public function getExpressions() { return $this->expressions; } /** * Gets the value of points. * * @return mixed */ public function getPoints() { return $this->points; } public function setGame(Game $game) { $this->game = $game; $game->addHit($this); return $this; } public function setTarget(Target $target) { $this->target = $target; $target->addHit($this); return $this; } /** * Sets the value of score. * * @param float The score this hit resulted in $score the score * * @return self */ public function setScore($score) { $this->score = $score; return $this; } } /** @ORM\Embeddable */ class Attributes { /** * @JMS\Exclude * @var array */ public static $ATTRIBUTES = ['gender','ethnicity','age']; /** * @var string * @ORM\Column(name="gender",columnDefinition="VARCHAR(1)") */ private $gender; /** * @var string * @ORM\Column(name="ethnicity",columnDefinition="VARCHAR(20)") */ private $ethnicity; /** * @var string * @ORM\Column(name="age",columnDefinition="VARCHAR(20)") */ private $age; /** * @var boolean * @ORM\Column(type="boolean") */ private $glasses; /** * Gets the value of gender. * * @return string */ public function getGender() { return $this->gender; } /** * Gets the value of ethnicity. * * @return string */ public function getEthnicity() { return $this->ethnicity; } /** * Gets the value of age. * * @return string */ public function getAge() { return $this->age; } /** * Gets the value of glasses. * * @return boolean */ public function getGlasses() { return $this->glasses; } /** * Sets the value of glasses. * * @param boolean $glasses the glasses * * @return self */ public function setGlasses(bool $glasses) { $this->glasses = $glasses; return $this; } public function setAttribute(string $attribute, string $value) { $attributes = static::$ATTRIBUTES; if(in_array($attribute, $attributes)) { $this->$attribute = $value; } return $this; } } /** @ORM\Embeddable */ class Emotions{ /** * @JMS\Exclude * @var array */ public static $EMOTIONS = ['anger','contempt','disgust','fear','joy','sadness','surprise']; /** * @var float Emotion parameter * @ORM\Column(type="float") */ private $anger; /** * @var float Emotion parameter * @ORM\Column(type="float") */ private $contempt; /** * @var float Emotion parameter * @ORM\Column(type="float") */ private $disgust; /** * @var float Emotion parameter * @ORM\Column(type="float") */ private $fear; /** * @var float Emotion parameter * @ORM\Column(type="float") */ private $joy; /** * @var float Emotion parameter * @ORM\Column(type="float") */ private $sadness; /** * @var float Emotion parameter * @ORM\Column(type="float") */ private $surprise; /** * Gets the value of anger. * * @return float Emotion parameter */ public function getAnger() { return $this->anger; } /** * Gets the value of contempt. * * @return float Emotion parameter */ public function getContempt() { return $this->contempt; } /** * Gets the value of disgust. * * @return float Emotion parameter */ public function getDisgust() { return $this->disgust; } /** * Gets the value of fear. * * @return float Emotion parameter */ public function getFear() { return $this->fear; } /** * Gets the value of joy. * * @return float Emotion parameter */ public function getJoy() { return $this->joy; } /** * Gets the value of sadness. * * @return float Emotion parameter */ public function getSadness() { return $this->sadness; } /** * Gets the value of surprise. * * @return float Emotion parameter */ public function getSurprise() { return $this->surprise; } /** * Get the score of given emotion * @param string|Emotion $name name of the emotion * @return float The score */ public function getEmotionScore($emotion) { if(!is_string($emotion)) { if(is_a($emotion, Emotion::class)) { $emotion = $emotion->getName(); } else { throw new RuntimeException("Invalid emotion", 1); } } $emotions = static::$EMOTIONS; if(in_array($emotion, $emotions)) { return $this->$emotion; } return 0; } public function setEmotion(string $emotion, float $value) { $emotions = static::$EMOTIONS; if(in_array($emotion, $emotions)) { $this->$emotion = $value > 100 ? 100 : $value; } return $this; } } /** @ORM\Embeddable */ class Expressions { /** * @JMS\Exclude * @var array */ public static $EXPRESSIONS = ['roll','pitch','yaw','inter_ocular_distance','mouth_open','lip_press','brow_raise','nose_wrinkler','lip_depressor','brow_furrow','attention','smile','inner_brow_raiser','chin_raiser','smirk','lip_suck','upper_lip_raiser','lip_pucker','eye_closure','engagement','valence']; /** * @JMS\Exclude * @var array */ public static $EXPRESSIONS_2ND_PERSON = [ 'mouth_open' => 'open your mouth', 'lip_press' => 'press your lips', 'brow_raise' => 'raise your brow', 'nose_wrinkler' => 'wrinkle your nose', 'lip_depressor' => 'depress your lips', 'brow_furrow' => 'furrow your brows', 'attention' => 'focus at the camera', 'smile' => 'smile', 'inner_brow_raiser' => 'raise the inner side of your brows', 'chin_raiser' => 'raise your chin', 'smirk' => 'smirk', 'lip_suck' => 'suck your lips', 'upper_lip_raiser' => 'raise your upper lip', 'lip_pucker' => 'pucker your lip', 'eye_closure' => 'close your eyes', ]; /** * @var float Head roll angle * @ORM\Column(type="float") */ private $roll; /** * @var float Head pitch angle * @ORM\Column(type="float") */ private $pitch; /** * @var float Head yaw angle * @ORM\Column(type="float") */ private $yaw; /** * @var float Distance between two outer eye corners (mm?) * @ORM\Column(type="float") */ private $inter_ocular_distance; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $mouth_open; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $lip_press; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $brow_raise; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $nose_wrinkler; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $lip_depressor; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $brow_furrow; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $attention; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $smile; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $inner_brow_raiser; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $chin_raiser; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $smirk; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $lip_suck; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $upper_lip_raiser; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $lip_pucker; /** * @var float Expression parameter * @ORM\Column(type="float") */ private $eye_closure; /** * @var float 'Expression' parameter * @ORM\Column(type="float") */ private $engagement; /** * @var float 'Expression' parameter * @ORM\Column(type="float") */ private $valence; /** * Gets the value of roll. * * @return float Head roll angle */ public function getRoll() { return $this->roll; } /** * Gets the value of pitch. * * @return float Head pitch angle */ public function getPitch() { return $this->pitch; } /** * Gets the value of yaw. * * @return float Head yaw angle */ public function getYaw() { return $this->yaw; } /** * Gets the value of inter_ocular_distance. * * @return float Distance between two outer eye corners (mm?) */ public function getInterOcularDistance() { return $this->inter_ocular_distance; } /** * Gets the value of mouth_open. * * @return float Expression parameter */ public function getMouthOpen() { return $this->mouth_open; } /** * Gets the value of lip_press. * * @return float Expression parameter */ public function getLipPress() { return $this->lip_press; } /** * Gets the value of brow_raise. * * @return float Expression parameter */ public function getBrowRaise() { return $this->brow_raise; } /** * Gets the value of nose_wrinkler. * * @return float Expression parameter */ public function getNoseWrinkler() { return $this->nose_wrinkler; } /** * Gets the value of lip_depressor. * * @return float Expression parameter */ public function getLipDepressor() { return $this->lip_depressor; } /** * Gets the value of brow_furrow. * * @return float Expression parameter */ public function getBrowFurrow() { return $this->brow_furrow; } /** * Gets the value of attention. * * @return float Expression parameter */ public function getAttention() { return $this->attention; } /** * Gets the value of smile. * * @return float Expression parameter */ public function getSmile() { return $this->smile; } /** * Gets the value of inner_brow_raiser. * * @return float Expression parameter */ public function getInnerBrowRaiser() { return $this->inner_brow_raiser; } /** * Gets the value of chin_raiser. * * @return float Expression parameter */ public function getChinRaiser() { return $this->chin_raiser; } /** * Gets the value of smirk. * * @return float Expression parameter */ public function getSmirk() { return $this->smirk; } /** * Gets the value of lip_suck. * * @return float Expression parameter */ public function getLipSuck() { return $this->lip_suck; } /** * Gets the value of upper_lip_raiser. * * @return float Expression parameter */ public function getUpperLipRaiser() { return $this->upper_lip_raiser; } /** * Gets the value of lip_pucker. * * @return float Expression parameter */ public function getLipPucker() { return $this->lip_pucker; } /** * Gets the value of eye_closure. * * @return float Expression parameter */ public function getEyeClosure() { return $this->eye_closure; } /** * Gets the value of engagement. * * @return float 'Expression' parameter */ public function getEngagement() { return $this->engagement; } /** * Gets the value of valence. * * @return float 'Expression' parameter */ public function getValence() { return $this->valence; } public function setExpression(string $expression, float $value) { $expressions = static::$EXPRESSIONS; if(in_array($expression, $expressions)) { $this->$expression = $value > 100 ? 100 : $value; } return $this; } public function getExpressionScore(string $expression){ $expressions = static::$EXPRESSIONS; if(in_array($expression, $expressions)) { return $this->$expression; } return 0; } /** * Get difference between two expressions objects * Get only for those that can be used in 2nd person * * @param Expressions $expressions To compare wiht * @return array Key: expression, value, diff */ public function getDifferences(Expressions $expressions) { $diffs = []; foreach(array_keys(static::$EXPRESSIONS_2ND_PERSON) as $exp) { $diffs[$exp] = $this->getExpressionScore($exp) - $expressions->getExpressionScore($exp); } return $diffs; } } /** @ORM\Embeddable */ class Points{ /** @ORM\Embedded(class="Point", columnPrefix="point_0") @JMS\SerializedName("0") */ private $point0; /** @ORM\Embedded(class="Point", columnPrefix="point_1") @JMS\SerializedName("1")*/ private $point1; /** @ORM\Embedded(class="Point", columnPrefix="point_2") @JMS\SerializedName("2")*/ private $point2; /** @ORM\Embedded(class="Point", columnPrefix="point_3") @JMS\SerializedName("3")*/ private $point3; /** @ORM\Embedded(class="Point", columnPrefix="point_4") @JMS\SerializedName("4")*/ private $point4; /** @ORM\Embedded(class="Point", columnPrefix="point_5") @JMS\SerializedName("5")*/ private $point5; /** @ORM\Embedded(class="Point", columnPrefix="point_6") @JMS\SerializedName("6")*/ private $point6; /** @ORM\Embedded(class="Point", columnPrefix="point_7") @JMS\SerializedName("7")*/ private $point7; /** @ORM\Embedded(class="Point", columnPrefix="point_8") @JMS\SerializedName("8")*/ private $point8; /** @ORM\Embedded(class="Point", columnPrefix="point_9") @JMS\SerializedName("9")*/ private $point9; /** @ORM\Embedded(class="Point", columnPrefix="point_10") @JMS\SerializedName("10") */ private $point10; /** @ORM\Embedded(class="Point", columnPrefix="point_11") @JMS\SerializedName("11") */ private $point11; /** @ORM\Embedded(class="Point", columnPrefix="point_12") @JMS\SerializedName("12") */ private $point12; /** @ORM\Embedded(class="Point", columnPrefix="point_13") @JMS\SerializedName("13") */ private $point13; /** @ORM\Embedded(class="Point", columnPrefix="point_14") @JMS\SerializedName("14") */ private $point14; /** @ORM\Embedded(class="Point", columnPrefix="point_15") @JMS\SerializedName("15") */ private $point15; /** @ORM\Embedded(class="Point", columnPrefix="point_16") @JMS\SerializedName("16") */ private $point16; /** @ORM\Embedded(class="Point", columnPrefix="point_17") @JMS\SerializedName("17") */ private $point17; /** @ORM\Embedded(class="Point", columnPrefix="point_18") @JMS\SerializedName("18") */ private $point18; /** @ORM\Embedded(class="Point", columnPrefix="point_19") @JMS\SerializedName("19") */ private $point19; /** @ORM\Embedded(class="Point", columnPrefix="point_20") @JMS\SerializedName("20") */ private $point20; /** @ORM\Embedded(class="Point", columnPrefix="point_21") @JMS\SerializedName("21") */ private $point21; /** @ORM\Embedded(class="Point", columnPrefix="point_22") @JMS\SerializedName("22") */ private $point22; /** @ORM\Embedded(class="Point", columnPrefix="point_23") @JMS\SerializedName("23") */ private $point23; /** @ORM\Embedded(class="Point", columnPrefix="point_24") @JMS\SerializedName("24") */ private $point24; /** @ORM\Embedded(class="Point", columnPrefix="point_25") @JMS\SerializedName("25") */ private $point25; /** @ORM\Embedded(class="Point", columnPrefix="point_26") @JMS\SerializedName("26") */ private $point26; /** @ORM\Embedded(class="Point", columnPrefix="point_27") @JMS\SerializedName("27") */ private $point27; /** @ORM\Embedded(class="Point", columnPrefix="point_28") @JMS\SerializedName("28") */ private $point28; /** @ORM\Embedded(class="Point", columnPrefix="point_29") @JMS\SerializedName("29") */ private $point29; /** @ORM\Embedded(class="Point", columnPrefix="point_30") @JMS\SerializedName("30") */ private $point30; /** @ORM\Embedded(class="Point", columnPrefix="point_31") @JMS\SerializedName("31") */ private $point31; /** @ORM\Embedded(class="Point", columnPrefix="point_32") @JMS\SerializedName("32") */ private $point32; /** @ORM\Embedded(class="Point", columnPrefix="point_33") @JMS\SerializedName("33") */ private $point33; /* * A full list of facial landmarks from Affectiva docs * http://developer.affectiva.com/fpi/ * * 0 Right Top Jaw * 1 Right Jaw Angle * 2 Tip of Chin * 3 Left Jaw Angle * 4 Left Top Jaw * 5 Outer Right Brow Corner * 6 Right Brow Center * 7 Inner Right Brow Corner * 8 Inner Left Brow Corner * 9 Left Brow Center * 10 Outer Left Brow Corner * 11 Nose Root * 12 Nose Tip * 13 Nose Lower Right Boundary * 14 Nose Bottom Boundary * 15 Nose Lower Left Boundary * 16 Outer Right Eye * 17 Inner Right Eye * 18 Inner Left Eye * 19 Outer Left Eye * 20 Right Lip Corner * 21 Right Apex Upper Lip * 22 Upper Lip Center * 23 Left Apex Upper Lip * 24 Left Lip Corner * 25 Left Edge Lower Lip * 26 Lower Lip Center * 27 Right Edge Lower Lip * 28 Bottom Upper Lip * 29 Top Lower Lip * 30 Upper Corner Right Eye * 31 Lower Corner Right Eye * 32 Upper Corner Left Eye * 33 Lower Corner Left Eye */ /** * Gets the value of point_0y. * * @return Point Facial landmark */ public function getPoint0() { return $this->point0; } /** * Gets the value of point_1y. * * @return Point Facial landmark */ public function getPoint1() { return $this->point1; } /** * Gets the value of point_2y. * * @return Point Facial landmark */ public function getPoint2() { return $this->point2; } /** * Gets the value of point_3y. * * @return Point Facial landmark */ public function getPoint3() { return $this->point3; } /** * Gets the value of point_4y. * * @return Point Facial landmark */ public function getPoint4() { return $this->point4; } /** * Gets the value of point_5y. * * @return Point Facial landmark */ public function getPoint5() { return $this->point5; } /** * Gets the value of point_6y. * * @return Point Facial landmark */ public function getPoint6() { return $this->point6; } /** * Gets the value of point_7y. * * @return Point Facial landmark */ public function getPoint7() { return $this->point7; } /** * Gets the value of point_8y. * * @return Point Facial landmark */ public function getPoint8() { return $this->point8; } /** * Gets the value of point_9y. * * @return Point Facial landmark */ public function getPoint9() { return $this->point9; } /** * Gets the value of point_10y. * * @return Point Facial landmark */ public function getPoint10() { return $this->point10; } /** * Gets the value of point_11y. * * @return Point Facial landmark */ public function getPoint11() { return $this->point11; } /** * Gets the value of point_12y. * * @return Point Facial landmark */ public function getPoint12() { return $this->point12; } /** * Gets the value of point_13y. * * @return Point Facial landmark */ public function getPoint13() { return $this->point13; } /** * Gets the value of point_14y. * * @return Point Facial landmark */ public function getPoint14() { return $this->point14; } /** * Gets the value of point_15y. * * @return Point Facial landmark */ public function getPoint15() { return $this->point15; } /** * Gets the value of point_16y. * * @return Point Facial landmark */ public function getPoint16() { return $this->point16; } /** * Gets the value of point_17y. * * @return Point Facial landmark */ public function getPoint17() { return $this->point17; } /** * Gets the value of point_18y. * * @return Point Facial landmark */ public function getPoint18() { return $this->point18; } /** * Gets the value of point_19y. * * @return Point Facial landmark */ public function getPoint19() { return $this->point19; } /** * Gets the value of point_20y. * * @return Point Facial landmark */ public function getPoint20() { return $this->point20; } /** * Gets the value of point_21y. * * @return Point Facial landmark */ public function getPoint21() { return $this->point21; } /** * Gets the value of point_22y. * * @return Point Facial landmark */ public function getPoint22() { return $this->point22; } /** * Gets the value of point_23y. * * @return Point Facial landmark */ public function getPoint23() { return $this->point23; } /** * Gets the value of point_24y. * * @return Point Facial landmark */ public function getPoint24() { return $this->point24; } /** * Gets the value of point_25y. * * @return Point Facial landmark */ public function getPoint25() { return $this->point25; } /** * Gets the value of point_26y. * * @return Point Facial landmark */ public function getPoint26() { return $this->point26; } /** * Gets the value of point_27y. * * @return Point Facial landmark */ public function getPoint27() { return $this->point27; } /** * Gets the value of point_28y. * * @return Point Facial landmark */ public function getPoint28() { return $this->point28; } /** * Gets the value of point_29y. * * @return Point Facial landmark */ public function getPoint29() { return $this->point29; } /** * Gets the value of point_30y. * * @return Point Facial landmark */ public function getPoint30() { return $this->point30; } /** * Gets the value of point_31y. * * @return Point Facial landmark */ public function getPoint31() { return $this->point31; } /** * Gets the value of point_32y. * * @return Point Facial landmark */ public function getPoint32() { return $this->point32; } /** * Gets the value of point_33y. * * @return Point Facial landmark */ public function getPoint33() { return $this->point33; } public function setPoint(int $nr, float $x, float $y) { if($nr <= 33 && $nr >= 0) { $this->{"point".$nr} = new Point($x, $y); } return $this; } } /** @ORM\Embeddable */ class Point{ /** * @var float Facial landmark * @ORM\Column(type="float") */ private $x; /** * @var float Facial landmark * @ORM\Column(type="float") */ private $y; public function __construct($x, $y) { $this->x = $x; $this->y = $y; } }