From fbd056b0d2a4b92e4b8760b99fe50b8ad024f03e Mon Sep 17 00:00:00 2001 From: Krokogator Date: Wed, 9 Jan 2019 10:39:03 +0100 Subject: [PATCH] =?UTF-8?q?Po=C5=82=C4=85czenie=20rozpoznawania=20obrazu?= =?UTF-8?q?=20z=20kaloryczno=C5=9Bci=C4=85=20jedzenia=20(po=C5=82=C4=85cze?= =?UTF-8?q?nie=20API=20IBM=20Watson=20z=20API=20Nutritionix)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/app/build.gradle | 6 +- .../photoeat/ExampleInstrumentedTest.java | 4 +- .../photoeat/PhotoPreviewActivity.java | 72 +++++++++++++++---- .../photoeat/TestNutritionixActivity.java | 10 +-- .../photoeat/service/NutritionixService.java | 2 +- .../krokogator/photoeat/ExampleUnitTest.java | 2 +- 6 files changed, 69 insertions(+), 27 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index d4ea7ce..28cea6a 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -8,7 +8,7 @@ android { targetSdkVersion 26 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner "android.support.getCalories.runner.AndroidJUnitRunner" } buildTypes { release { @@ -37,8 +37,8 @@ dependencies { implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + androidTestImplementation 'com.android.support.getCalories:runner:1.0.2' + androidTestImplementation 'com.android.support.getCalories.espresso:espresso-core:3.0.2' implementation 'com.android.support:cardview-v7:26.1.0' implementation 'com.android.support:recyclerview-v7:26.1.0' implementation 'com.ibm.watson.developer_cloud:java-sdk:6.9.0' diff --git a/android/app/src/androidTest/java/com/krokogator/photoeat/ExampleInstrumentedTest.java b/android/app/src/androidTest/java/com/krokogator/photoeat/ExampleInstrumentedTest.java index 94d35de..d377206 100644 --- a/android/app/src/androidTest/java/com/krokogator/photoeat/ExampleInstrumentedTest.java +++ b/android/app/src/androidTest/java/com/krokogator/photoeat/ExampleInstrumentedTest.java @@ -10,7 +10,7 @@ import org.junit.runner.RunWith; import static org.junit.Assert.*; /** - * Instrumented test, which will execute on an Android device. + * Instrumented getCalories, which will execute on an Android device. * * @see Testing documentation */ @@ -18,7 +18,7 @@ import static org.junit.Assert.*; public class ExampleInstrumentedTest { @Test public void useAppContext() throws Exception { - // Context of the app under test. + // Context of the app under getCalories. Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals("com.krokogator.photoeat", appContext.getPackageName()); diff --git a/android/app/src/main/java/com/krokogator/photoeat/PhotoPreviewActivity.java b/android/app/src/main/java/com/krokogator/photoeat/PhotoPreviewActivity.java index ec4c7d1..83d71aa 100644 --- a/android/app/src/main/java/com/krokogator/photoeat/PhotoPreviewActivity.java +++ b/android/app/src/main/java/com/krokogator/photoeat/PhotoPreviewActivity.java @@ -8,18 +8,20 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.RecyclerView; -import android.util.Log; -import android.widget.Adapter; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import com.krokogator.photoeat.service.CustomFoodClassification; +import com.krokogator.photoeat.service.NutritionixService; import com.krokogator.photoeat.service.WatsonService; +import org.json.JSONException; +import org.json.JSONObject; + import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -108,8 +110,8 @@ public class PhotoPreviewActivity extends AppCompatActivity { } - private class ClassifyImage extends AsyncTask { - protected CustomFoodClassification doInBackground(File... files) { + private class ClassifyImage extends AsyncTask> { + protected List doInBackground(File... files) { WatsonService service = null; try { @@ -125,19 +127,65 @@ public class PhotoPreviewActivity extends AppCompatActivity { e.printStackTrace(); } + List result = getClassificationsArray(customFoodClassification); + // Escape early if cancel() is called if (isCancelled()) return null; - return customFoodClassification; + return result; } - protected void onPostExecute(CustomFoodClassification result) { - Log.i("watson.result", result.toString()); - for(String imageClass : result.getClassifications().keySet()){ - Float classValue = result.getClassifications().get(imageClass); - classifications.add(imageClass + " : " + classValue); - } + protected void onPostExecute(List res) { +// for(String imageClass : result.getClassifications().keySet()){ +// Float classValue = result.getClassifications().get(imageClass); +// classifications.add(imageClass + " : " + classValue); +// +// } + + for (String result : res){ + classifications.add(result); + } adapter.notifyDataSetChanged(); } + + private List getClassificationsArray(CustomFoodClassification res){ + StringBuilder food = new StringBuilder(); + for(String imageClass : res.getClassifications().keySet()){ + Float classValue = res.getClassifications().get(imageClass); + //classifications.add(imageClass + " : " + classValue); + food.append(imageClass + ", "); + } + + NutritionixService nutritionix = new NutritionixService(); + JSONObject json = null; + try { + json = nutritionix.getCalories(food.toString()); + } catch (IOException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + + return getCaloriesFromJson(json); + } + + private List getCaloriesFromJson(JSONObject result){ + List out = new ArrayList<>(); + + try { + int length = 0; + length = result.getJSONArray("foods").length(); + for (int i = 0; i < length; i++) { + StringBuilder builder = new StringBuilder(); + builder.append(result.getJSONArray("foods").getJSONObject(i).getString("food_name")); + builder.append(" "); + builder.append(result.getJSONArray("foods").getJSONObject(i).getString("nf_calories")); + builder.append("kcal"); + out.add(builder.toString()); + } + } catch (Exception e){} + + return out; + } } } diff --git a/android/app/src/main/java/com/krokogator/photoeat/TestNutritionixActivity.java b/android/app/src/main/java/com/krokogator/photoeat/TestNutritionixActivity.java index a9c871b..1d90b0d 100644 --- a/android/app/src/main/java/com/krokogator/photoeat/TestNutritionixActivity.java +++ b/android/app/src/main/java/com/krokogator/photoeat/TestNutritionixActivity.java @@ -7,14 +7,8 @@ import android.widget.Button; import android.widget.EditText; import android.widget.TextView; -import com.google.api.client.http.GenericUrl; -import com.google.api.client.http.HttpRequest; -import com.google.api.client.http.HttpRequestFactory; -import com.google.api.client.http.javanet.NetHttpTransport; import com.krokogator.photoeat.service.NutritionixService; -import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; @@ -50,14 +44,14 @@ public class TestNutritionixActivity extends AppCompatActivity { textView.setText(results); } - private class NutritionixBackground extends AsyncTask { + public class NutritionixBackground extends AsyncTask { protected JSONObject doInBackground(String... files) { JSONObject out = null; NutritionixService service = new NutritionixService(); try { - out = service.test(files[0]); + out = service.getCalories(files[0]); } catch (Exception e) { e.printStackTrace(); } diff --git a/android/app/src/main/java/com/krokogator/photoeat/service/NutritionixService.java b/android/app/src/main/java/com/krokogator/photoeat/service/NutritionixService.java index a048bb3..7b1ec92 100644 --- a/android/app/src/main/java/com/krokogator/photoeat/service/NutritionixService.java +++ b/android/app/src/main/java/com/krokogator/photoeat/service/NutritionixService.java @@ -19,7 +19,7 @@ import java.lang.reflect.Type; public class NutritionixService { - public JSONObject test(String input) throws IOException, JSONException { + public JSONObject getCalories(String input) throws IOException, JSONException { /** * NUTRITIONIX API CALL */ diff --git a/android/app/src/test/java/com/krokogator/photoeat/ExampleUnitTest.java b/android/app/src/test/java/com/krokogator/photoeat/ExampleUnitTest.java index f0b77d0..e0f5b06 100644 --- a/android/app/src/test/java/com/krokogator/photoeat/ExampleUnitTest.java +++ b/android/app/src/test/java/com/krokogator/photoeat/ExampleUnitTest.java @@ -5,7 +5,7 @@ import org.junit.Test; import static org.junit.Assert.*; /** - * Example local unit test, which will execute on the development machine (host). + * Example local unit getCalories, which will execute on the development machine (host). * * @see Testing documentation */