From 162186cea12148f536cde52f7111c3b5a65aef5b Mon Sep 17 00:00:00 2001 From: Ruben Date: Fri, 19 Aug 2016 19:06:18 +0100 Subject: [PATCH] Change score color and store face in scenario rather than extract emotions on each tick --- .../rubenvandeven/emotionhero/Emotion.java | 3 +++ .../emotionhero/GamingActivity.java | 21 ++++----------- .../rubenvandeven/emotionhero/Scenario.java | 20 +++++++++----- .../emotionhero/ScenarioView.java | 26 +++++++++++++------ 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/Emotion.java b/app/src/main/java/com/rubenvandeven/emotionhero/Emotion.java index 94fe54d..2c9b8fb 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/Emotion.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/Emotion.java @@ -19,6 +19,9 @@ public enum Emotion { public float getValueFromFace(Face face) { + if(face == null) + return 0f; + switch (this) { case ANGER: return face.emotions.getAnger(); diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java b/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java index 3f4b630..44a14bb 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java @@ -1,7 +1,6 @@ package com.rubenvandeven.emotionhero; import android.Manifest; -import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.pm.PackageManager; import android.media.AudioAttributes; @@ -9,12 +8,9 @@ import android.media.AudioManager; import android.media.SoundPool; import android.os.Build; import android.support.v4.app.ActivityCompat; -import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; -import android.os.Handler; import android.util.Log; -import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; @@ -176,7 +172,6 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL detector = new CameraDetector(this, CameraDetector.CameraType.CAMERA_FRONT, cameraPreview, 1, Detector.FaceDetectorMode.LARGE_FACES); detector.setLicensePath("emotionhero_dev.license"); - detector.setMaxProcessRate(10); detector.setDetectAllEmotions(true); detector.setDetectAllAppearances(false); detector.setDetectAllEmojis(false); @@ -223,19 +218,13 @@ public class GamingActivity extends AppCompatActivity implements Detector.ImageL } else { hideText(); // hide textView as we want as few elements as possible. Face face = list.get(0); - currentScenario.setCurrentAttributeScoresForFace(face); + currentScenario.setCurrentFace(face); scenarioView.setCurrentAttributeScoresForFace(face); -// String paramString = ""; -// paramString += "Anger " + String.format("%02.2f", face.emotions.getAnger()) + "%\n"; -// paramString += "Contempt " + String.format("%02.2f", face.emotions.getContempt()) + "%\n"; -// paramString += "Disgust " + String.format("%02.2f", face.emotions.getDisgust()) + "%\n"; -// paramString += "Fear " + String.format("%02.2f", face.emotions.getFear()) + "%\n"; -// paramString += "Joy " + String.format("%02.2f", face.emotions.getJoy()) + "%\n"; -// paramString += "Sadness " + String.format("%02.2f", face.emotions.getSadness()) + "%\n"; -// paramString += "Surprise " + String.format("%02.2f", face.emotions.getSurprise()) + "%\n"; -// -// paramText.setText(paramString); +// if(frame instanceof Frame.ByteArrayFrame) +// { +// Frame.ByteArrayFrame byteArrayFrame = (Frame.ByteArrayFrame) frame; +// } } } diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java b/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java index 2b40f57..e94519d 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java @@ -1,5 +1,10 @@ package com.rubenvandeven.emotionhero; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.util.Log; + +import com.affectiva.android.affdex.sdk.Frame; import com.affectiva.android.affdex.sdk.detector.Face; import java.util.ArrayList; @@ -14,6 +19,8 @@ import java.util.TimerTask; abstract public class Scenario { + public Bitmap currentFrameBitmap; + static int DESIRED_FPS = 25; float duration = 0; @@ -42,7 +49,7 @@ abstract public class Scenario { * The scorres in this moment, as to draw them on the screen. * Indexes are Emotion ordinals */ - private Map currentAttributeScores = new HashMap<>(); + private Face currentFace; ArrayList targets = new ArrayList<>(); @@ -63,6 +70,8 @@ abstract public class Scenario { * Extra bonus given */ public boolean isSpotOn = false; +// public Bitmap image; //image at time of score + public Face face; // face at time of score } /** @@ -108,10 +117,11 @@ abstract public class Scenario { continue; } if(target.timestamp <= runningTime) { - float scored_value = currentAttributeScores.get(target.emotion); + float scored_value = target.emotion.getValueFromFace(currentFace); float required_value = target.value; Score score = new Score(); score.value = Math.round(100 - Math.abs(scored_value-required_value)); + score.face = currentFace; scores.add(score); // @@ -241,11 +251,9 @@ abstract public class Scenario { // TODO: create AttributeScoreCollection class, with this method. - public void setCurrentAttributeScoresForFace(Face face) + public void setCurrentFace(Face face) { - for(Emotion emotion: Emotion.values()) { - currentAttributeScores.put(emotion, emotion.getValueFromFace(face)); - } + currentFace = face; } public boolean isFinished() diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioView.java b/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioView.java index 815b06e..e0d9ff6 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioView.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/ScenarioView.java @@ -6,6 +6,7 @@ import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PixelFormat; +import android.graphics.Rect; import android.graphics.Typeface; import android.view.SurfaceHolder; import android.view.SurfaceView; @@ -38,7 +39,7 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback private Paint mainPaint = new Paint(); private Paint attrScorePaint = new Paint(); private Paint linePaint = new Paint(); - + private Paint scorePaint = new Paint(); // see: http://blog.danielnadeau.io/2012/01/android-canvas-beginners-tutorial.html class PanelThread extends Thread { @@ -60,12 +61,12 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback @Override public void run() { + Canvas c; while (_run) { //When setRunning(false) occurs, _run is c = null; //set to false and loop ends, stopping thread try { -// c = _surfaceHolder.lockCanvas(null); c = _surfaceHolder.lockCanvas(null); synchronized (_surfaceHolder) { //Insert methods to modify positions of items in onDraw() @@ -85,10 +86,12 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback getHolder().addCallback(this); _scenario = s; + scorePaint.setColor(Color.YELLOW); + scorePaint.setTextSize(50); + scorePaint.setTypeface(Typeface.DEFAULT_BOLD); + //setup paints for drawing mainPaint.setColor(Color.GRAY); - mainPaint.setTextSize(40); - mainPaint.setTypeface(Typeface.SANS_SERIF); linePaint.setColor(Color.GRAY); linePaint.setStrokeWidth(5); @@ -180,15 +183,22 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback float diff_y = sec_height * _scenario.getTime(); for(Scenario.Target target: _scenario.getTargets()) { - Paint targetPaint = (target.score == null) ? emoPaints.get(target.emotion) : emoScoredPaints.get(target.emotion); +// Paint targetPaint = (target.score == null) ? emoPaints.get(target.emotion) : emoScoredPaints.get(target.emotion); float cy = bottomline_height - (target.timestamp * sec_height) + diff_y; float cx = padding_left + (step_y * target.emotion.ordinal() + step_y / 2); - canvas.drawCircle(cx, cy, max_ball_radius * target.value/100, targetPaint); + canvas.drawCircle(cx, cy, max_ball_radius * target.value/100, emoPaints.get(target.emotion)); if(target.score != null) { - canvas.drawText(Float.toString(target.score.value), cx, cy, targetPaint); + canvas.drawText(Float.toString(target.score.value), cx, cy, emoPaints.get(target.emotion)); + canvas.drawCircle(cx, cy, max_ball_radius * target.score.value/100, emoScoredPaints.get(target.emotion)); + +// TODO: Use target.score.face to set Rect src. +// if(target.score.image != null) { +// Rect rect = new Rect((int) cx-10, (int) cy-10, (int) cx+10, (int) cy+10); +// canvas.drawBitmap(target.score.image, null, rect, null); +// } } @@ -196,7 +206,7 @@ public class ScenarioView extends SurfaceView implements SurfaceHolder.Callback // canvas.drawText(target_text, cx, y_pos + diff_y , emoPaint); } - canvas.drawText("Total: " + Float.toString(_scenario.getTotalScore()), 50, 50, mainPaint); + canvas.drawText("Total: " + Float.toString(_scenario.getTotalScore()), 50, 50, scorePaint); }