Merge branch 'feature/redirect-watson-output-to-nutritionix-input' of s407312/photoeat into master

This commit is contained in:
Michał Szczepanowski 2019-01-09 09:40:51 +00:00 committed by Gogs
commit f3accfccd6
6 changed files with 69 additions and 27 deletions

View File

@ -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'

View File

@ -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 <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@ -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());

View File

@ -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<File, Integer, CustomFoodClassification> {
protected CustomFoodClassification doInBackground(File... files) {
private class ClassifyImage extends AsyncTask<File, Integer, List<String>> {
protected List<String> doInBackground(File... files) {
WatsonService service = null;
try {
@ -125,19 +127,65 @@ public class PhotoPreviewActivity extends AppCompatActivity {
e.printStackTrace();
}
List<String> 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<String> 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<String> 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<String> getCaloriesFromJson(JSONObject result){
List<String> 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;
}
}
}

View File

@ -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<String, Integer, JSONObject> {
public class NutritionixBackground extends AsyncTask<String, Integer, JSONObject> {
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();
}

View File

@ -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
*/

View File

@ -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 <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/