From 907e9005afddd40755b68d659e4ceb1f20c97802 Mon Sep 17 00:00:00 2001 From: Ruben Date: Tue, 20 Sep 2016 17:23:49 +0100 Subject: [PATCH] Frame snapshot sync fix (hopefully) and other randomisation of snapshot and only send 3 snaps (no mouth_right anymore) --- .idea/misc.xml | 2 +- .../emotionhero/ApiRestClient.java | 42 +++++++++---------- .../emotionhero/GameOpenHelper.java | 2 + .../emotionhero/GamingActivity.java | 3 +- .../rubenvandeven/emotionhero/Scenario.java | 27 ++++++++++-- 5 files changed, 49 insertions(+), 27 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index bd04605..1b77328 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -37,7 +37,7 @@ - + diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/ApiRestClient.java b/app/src/main/java/com/rubenvandeven/emotionhero/ApiRestClient.java index 02b5228..460b694 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/ApiRestClient.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/ApiRestClient.java @@ -406,27 +406,27 @@ public class ApiRestClient { // RIGHT MOUTH CORNER // Crop: http://stackoverflow.com/a/31698091 // reuse margin of brows to make a square image - left = (int) hit.points[24].x - margin; - right = (int) hit.points[24].x + 3 * margin; - top = (int) hit.points[24].y - 2 * margin; - bottom = (int) hit.points[24].y + 2 * margin; - Rect rect3 = new Rect(left, top, right, bottom); -// Be sure that there is at least 1px to slice. - if(!(rect3.left < rect3.right && rect3.top < rect3.bottom)) { - Log.e("API", "Error in point positions."+" left: " + rect3.left + " right: " + rect3.right + " top: " + rect3.top + " bottom: " + rect3.bottom ); - continue; // strange bug... skip it and drop the file anyway - } - - croppedBmp = Bitmap.createBitmap(rect3.right-rect3.left, rect3.bottom-rect3.top, Bitmap.Config.ARGB_8888); -// draw source bitmap into resulting image at given position: - new Canvas(croppedBmp).drawBitmap(img, -rect3.left, -rect3.top, null); - - ByteArrayOutputStream stream3 = new ByteArrayOutputStream(); - croppedBmp.compress(Bitmap.CompressFormat.JPEG, 90, stream3); - byte[] imageBytes3 = stream3.toByteArray(); - - params.put(hit.remoteId + ":mouth_right", new ByteArrayInputStream(imageBytes3), hit.remoteId + "-mouth_right.jpg", "image/jpeg"); - Log.v("API", "add param: " + hit.remoteId + ":mouth_right - length:" + imageBytes.length ); +// left = (int) hit.points[24].x - margin; +// right = (int) hit.points[24].x + 3 * margin; +// top = (int) hit.points[24].y - 2 * margin; +// bottom = (int) hit.points[24].y + 2 * margin; +// Rect rect3 = new Rect(left, top, right, bottom); +//// Be sure that there is at least 1px to slice. +// if(!(rect3.left < rect3.right && rect3.top < rect3.bottom)) { +// Log.e("API", "Error in point positions."+" left: " + rect3.left + " right: " + rect3.right + " top: " + rect3.top + " bottom: " + rect3.bottom ); +// continue; // strange bug... skip it and drop the file anyway +// } +// +// croppedBmp = Bitmap.createBitmap(rect3.right-rect3.left, rect3.bottom-rect3.top, Bitmap.Config.ARGB_8888); +//// draw source bitmap into resulting image at given position: +// new Canvas(croppedBmp).drawBitmap(img, -rect3.left, -rect3.top, null); +// +// ByteArrayOutputStream stream3 = new ByteArrayOutputStream(); +// croppedBmp.compress(Bitmap.CompressFormat.JPEG, 90, stream3); +// byte[] imageBytes3 = stream3.toByteArray(); +// +// params.put(hit.remoteId + ":mouth_right", new ByteArrayInputStream(imageBytes3), hit.remoteId + "-mouth_right.jpg", "image/jpeg"); +// Log.v("API", "add param: " + hit.remoteId + ":mouth_right - length:" + imageBytes.length ); // LEFT MOUTH CORNER (doing whole mouth :-) diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/GameOpenHelper.java b/app/src/main/java/com/rubenvandeven/emotionhero/GameOpenHelper.java index cd0c0ba..afbfcfa 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/GameOpenHelper.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/GameOpenHelper.java @@ -273,6 +273,8 @@ public class GameOpenHelper extends SQLiteOpenHelper { } saveAchievementsForGame(game); + + db.close(); } public Game[] getGamesForLevel(int lvl_id) { diff --git a/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java b/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java index 49f1390..f780468 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/GamingActivity.java @@ -258,9 +258,8 @@ 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.setCurrentFace(face); + currentScenario.setCurrentFaceAndFrame(face, (Frame.ByteArrayFrame) frame); scenarioView.setCurrentAttributeScoresForFace(face); - currentScenario.currentFrame = (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 4a9e382..200b82f 100644 --- a/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java +++ b/app/src/main/java/com/rubenvandeven/emotionhero/Scenario.java @@ -13,9 +13,14 @@ import com.affectiva.android.affdex.sdk.Frame; import com.affectiva.android.affdex.sdk.detector.Face; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; import java.util.Map; +import java.util.Random; import java.util.Timer; import java.util.TimerTask; @@ -115,6 +120,12 @@ public class Scenario { */ protected boolean lostFace = false; + public static int MAX_SNAPSHOTS = 5; + /** + * Which targets to capture an image of + */ + List snapTargets; + /** * The scorres in this moment, as to draw them on the screen. * Indexes are Emotion ordinals @@ -165,6 +176,16 @@ public class Scenario { rs = RenderScript.create(_activity); squarePaint = new Paint(); squarePaint.setColor(Color.YELLOW); + + // determine before start which targets will be stored and send + // only do it randomly so we don't need to do ALL and yet have spread across the level. + snapTargets = pickNRandom(targets, MAX_SNAPSHOTS); + } + + public static List pickNRandom(List lst, int n) { + List copy = new LinkedList(lst); + Collections.shuffle(copy); + return copy.subList(0, n); } /** @@ -208,7 +229,7 @@ public class Scenario { Bitmap outputBitmap = null; - if(currentFrame != null && bitmapCount < 5) { + if(currentFrame != null && snapTargets.contains(target) ) { // convert NV21 byteArrayFrame from camera to RGB bitmap. Frame.ByteArrayFrame byteArrayFrame = (Frame.ByteArrayFrame) currentFrame; outputBitmap = Nv21Image.nv21ToBitmap(rs, byteArrayFrame.getByteArray(), byteArrayFrame.getWidth(), byteArrayFrame.getHeight()); @@ -222,7 +243,6 @@ public class Scenario { matrix.postRotate((int) rotation.toDouble()); // int width = rotation == Frame.ROTATE.BY_180 ? outputBitmap.getWidth() : outputBitmap.getHeight(); // int height = rotation == Frame.ROTATE.BY_180 ? outputBitmap.getHeight() : outputBitmap.getWidth(); - // TODO: max 5 bitmaps outputBitmap = Bitmap.createBitmap(outputBitmap , 0, 0, outputBitmap .getWidth(), outputBitmap .getHeight(), matrix, true); } @@ -382,9 +402,10 @@ public class Scenario { // TODO: create AttributeScoreCollection class, with this method. - public void setCurrentFace(Face face) + public synchronized void setCurrentFaceAndFrame(Face face, Frame.ByteArrayFrame frame) { currentFace = face; + currentFrame = frame; } public boolean isFinished()