dish 识别
This commit is contained in:
@@ -44,7 +44,7 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".camera.FoodMaterialCamera" />
|
||||
<activity android:name=".camera.ClassifierCamera" />
|
||||
|
||||
<receiver
|
||||
android:name=".step.StepStarter"
|
||||
|
||||
@@ -150,7 +150,7 @@ public class NutritionMaster extends Application {
|
||||
*/
|
||||
private void initBD() {
|
||||
ConstantUtils.BD_ACCESS_TOKEN = getAccessToken();
|
||||
Logger.d(ConstantUtils.BD_ACCESS_TOKEN);
|
||||
// Logger.d(ConstantUtils.BD_ACCESS_TOKEN);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.example.ninefourone.nutritionmaster.bean;
|
||||
|
||||
/**
|
||||
* Created by ScorpioMiku on 2018/10/4.
|
||||
*/
|
||||
|
||||
public class ClassifyResult {
|
||||
|
||||
private String imgPath ;
|
||||
private double probability;
|
||||
private String name;
|
||||
private int calorie;
|
||||
private Boolean has_calorie;
|
||||
|
||||
public String getImgPath() {
|
||||
return imgPath;
|
||||
}
|
||||
|
||||
public void setImgPath(String imgPath) {
|
||||
this.imgPath = imgPath;
|
||||
}
|
||||
|
||||
public double getProbability() {
|
||||
return probability;
|
||||
}
|
||||
|
||||
public void setProbability(double probability) {
|
||||
this.probability = probability;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getCalorie() {
|
||||
return calorie;
|
||||
}
|
||||
|
||||
public void setCalorie(int calorie) {
|
||||
this.calorie = calorie;
|
||||
}
|
||||
|
||||
public Boolean getHas_calorie() {
|
||||
return has_calorie;
|
||||
}
|
||||
|
||||
public void setHas_calorie(Boolean has_calorie) {
|
||||
this.has_calorie = has_calorie;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name + ";置信度" + probability + ";卡路里" + calorie;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,13 @@ package com.example.ninefourone.nutritionmaster.camera;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.hardware.Camera;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.view.Display;
|
||||
import android.util.Base64;
|
||||
import android.view.Surface;
|
||||
import android.view.View;
|
||||
import android.view.Window;
|
||||
@@ -17,9 +18,19 @@ import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.example.ninefourone.nutritionmaster.R;
|
||||
import com.example.ninefourone.nutritionmaster.bean.ClassifyResult;
|
||||
import com.example.ninefourone.nutritionmaster.utils.ConstantUtils;
|
||||
import com.example.ninefourone.nutritionmaster.utils.MessageUtils;
|
||||
import com.example.ninefourone.nutritionmaster.utils.WebUtils;
|
||||
import com.orhanobut.logger.Logger;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
@@ -28,7 +39,11 @@ import butterknife.OnClick;
|
||||
* Created by ScorpioMiku on 2018/9/3.
|
||||
*/
|
||||
|
||||
public class FoodMaterialCamera extends AppCompatActivity {
|
||||
public class ClassifierCamera extends AppCompatActivity {
|
||||
|
||||
public static int MATERAIL_CODE = 0;
|
||||
public static int DISH_CODE = 1;
|
||||
|
||||
@BindView(R.id.camera_preview)
|
||||
FrameLayout mCameraLayout;
|
||||
@BindView(R.id.results_text_view)
|
||||
@@ -37,20 +52,22 @@ public class FoodMaterialCamera extends AppCompatActivity {
|
||||
ImageView moreTakePhotoButtonCapture;
|
||||
@BindView(R.id.more_takephoto_ok)
|
||||
ImageView moreTakephotoOk;
|
||||
@BindView(R.id.more_camera_cover_linearlayout)
|
||||
FrameLayout moreCameraCoverLinearlayout;
|
||||
@BindView(R.id.camera_cover_linearlayout)
|
||||
FrameLayout cameraCoverLinearlayout;
|
||||
|
||||
private Camera mCamera;
|
||||
private CameraPreview mPreview;
|
||||
private int mCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
|
||||
|
||||
private int widthPixel;
|
||||
private float heightPixel;
|
||||
private ArrayList<ClassifyResult> resultList = new ArrayList<>();
|
||||
|
||||
private int code = -1;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
Intent intent = getIntent();
|
||||
code = intent.getIntExtra("CODE", -1);
|
||||
//取消toolbar
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
//设置全屏
|
||||
@@ -59,9 +76,6 @@ public class FoodMaterialCamera extends AppCompatActivity {
|
||||
//注意:上面两个设置必须写在setContentView前面
|
||||
setContentView(R.layout.cameras_layout);
|
||||
ButterKnife.bind(this);
|
||||
Display display = getWindowManager().getDefaultDisplay();
|
||||
widthPixel = display.getWidth();
|
||||
heightPixel = display.getHeight() * (14.0f / 16);
|
||||
|
||||
if (!checkCameraHardware(this)) {
|
||||
MessageUtils.MakeToast("不支持相机");
|
||||
@@ -101,13 +115,6 @@ public class FoodMaterialCamera extends AppCompatActivity {
|
||||
if (null == mCamera) {
|
||||
mCamera = getCameraInstance();
|
||||
mPreview = new CameraPreview(this, mCamera);
|
||||
// mPreview.setOnTouchListener(new View.OnTouchListener() {
|
||||
// @Override
|
||||
// public boolean onTouch(View v, MotionEvent event) {
|
||||
// mCamera.autoFocus(null);
|
||||
// return false;
|
||||
// }
|
||||
// });
|
||||
mCameraLayout.addView(mPreview);
|
||||
mCamera.startPreview();
|
||||
}
|
||||
@@ -124,8 +131,10 @@ public class FoodMaterialCamera extends AppCompatActivity {
|
||||
c = Camera.open();
|
||||
|
||||
Camera.Parameters mParameters = c.getParameters();
|
||||
Logger.d(widthPixel+", "+heightPixel);
|
||||
mParameters.setPictureSize(widthPixel, (int) heightPixel);
|
||||
List<Camera.Size> sizes = mParameters.getSupportedPreviewSizes();
|
||||
|
||||
mParameters.setPictureSize(2048, 1536);
|
||||
mParameters.setPreviewSize(2048, 1536);
|
||||
c.setParameters(mParameters);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -152,7 +161,45 @@ public class FoodMaterialCamera extends AppCompatActivity {
|
||||
private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
|
||||
@Override
|
||||
public void onPictureTaken(final byte[] data, Camera camera) {
|
||||
MessageUtils.MakeToast("拍照!");
|
||||
try {
|
||||
String imgStr = Base64.encodeToString(data, Base64.DEFAULT);
|
||||
String imgParam = URLEncoder.encode(imgStr, "UTF-8");
|
||||
final String param = "image=" + imgParam + "&top_num=" + 1;
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String result = null;
|
||||
try {
|
||||
if (code == 0) {
|
||||
result = WebUtils.HttpPost(ConstantUtils.BD_DISH_URL,
|
||||
ConstantUtils.BD_ACCESS_TOKEN, param);
|
||||
} else if (code == 1) {
|
||||
result = WebUtils.HttpPost(ConstantUtils.BD_DISH_URL,
|
||||
ConstantUtils.BD_ACCESS_TOKEN, param);
|
||||
} else {
|
||||
Logger.e("拍照code为-1");
|
||||
}
|
||||
JSONObject jsonObject = new JSONObject(result);
|
||||
ClassifyResult classifyResult = new ClassifyResult();
|
||||
JSONArray resultObject = jsonObject.getJSONArray("result");
|
||||
jsonObject = resultObject.getJSONObject(0);
|
||||
classifyResult.setCalorie(jsonObject.getInt("calorie"));
|
||||
classifyResult.setHas_calorie(jsonObject.getBoolean("has_calorie"));
|
||||
classifyResult.setProbability(jsonObject.getDouble("probability"));
|
||||
classifyResult.setName(jsonObject.getString("name"));
|
||||
Logger.d(classifyResult);
|
||||
resultList.add(classifyResult);
|
||||
refreshUI();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
thread.start();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// MessageUtils.MakeToast("拍照!");
|
||||
mCamera.startPreview();
|
||||
}
|
||||
};
|
||||
@@ -168,8 +215,10 @@ public class FoodMaterialCamera extends AppCompatActivity {
|
||||
switch (view.getId()) {
|
||||
case R.id.more_take_photo_button_capture:
|
||||
mCamera.autoFocus(mAutoFocusCallback);
|
||||
cameraCoverLinearlayout.setVisibility(View.VISIBLE);
|
||||
break;
|
||||
case R.id.more_takephoto_ok:
|
||||
resultList.clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -209,4 +258,19 @@ public class FoodMaterialCamera extends AppCompatActivity {
|
||||
}
|
||||
camera.setDisplayOrientation(result);
|
||||
}
|
||||
|
||||
|
||||
private void refreshUI() {
|
||||
resultsTextView.post(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String text = "";
|
||||
for (int i = 0; i < resultList.size(); i++) {
|
||||
text += resultList.get(i).getName() + " ";
|
||||
}
|
||||
resultsTextView.setText(text);
|
||||
cameraCoverLinearlayout.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -19,13 +19,12 @@ import android.widget.TextView;
|
||||
|
||||
import com.ToxicBakery.viewpager.transforms.CubeOutTransformer;
|
||||
import com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar;
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.cb.ratingbar.CBRatingBar;
|
||||
import com.example.ninefourone.nutritionmaster.NutritionMaster;
|
||||
import com.example.ninefourone.nutritionmaster.R;
|
||||
import com.example.ninefourone.nutritionmaster.adapter.HomePagerAdapter;
|
||||
import com.example.ninefourone.nutritionmaster.base.BaseActivity;
|
||||
import com.example.ninefourone.nutritionmaster.camera.FoodMaterialCamera;
|
||||
import com.example.ninefourone.nutritionmaster.camera.ClassifierCamera;
|
||||
import com.example.ninefourone.nutritionmaster.modules.addinformation.AddInformationActivity;
|
||||
import com.example.ninefourone.nutritionmaster.modules.addinformation.AddPhysiqueActivity;
|
||||
import com.example.ninefourone.nutritionmaster.modules.information.InformationActivity;
|
||||
@@ -316,14 +315,24 @@ public class MainActivity extends BaseActivity {
|
||||
@Override
|
||||
public void onBoomButtonClick(int index) {
|
||||
Intent cameraIntent = new Intent(MainActivity.this,
|
||||
FoodMaterialCamera.class);
|
||||
ClassifierCamera.class);
|
||||
cameraIntent.putExtra("CODE", ClassifierCamera.MATERAIL_CODE);
|
||||
startActivity(cameraIntent);
|
||||
}
|
||||
});
|
||||
boomMenuButton.addBuilder(builder);
|
||||
HamButton.Builder builder2 = new HamButton.Builder()
|
||||
.normalImageRes(R.drawable.foods)
|
||||
.normalTextRes(R.string.food_title);
|
||||
.normalTextRes(R.string.food_title)
|
||||
.listener(new OnBMClickListener() {
|
||||
@Override
|
||||
public void onBoomButtonClick(int index) {
|
||||
Intent cameraIntent = new Intent(MainActivity.this,
|
||||
ClassifierCamera.class);
|
||||
cameraIntent.putExtra("CODE", ClassifierCamera.DISH_CODE);
|
||||
startActivity(cameraIntent);
|
||||
}
|
||||
});
|
||||
boomMenuButton.addBuilder(builder2);
|
||||
}
|
||||
|
||||
|
||||
@@ -273,4 +273,6 @@ public class ConstantUtils {
|
||||
public static String BD_SECRET_KEY = "EEXe6pG7QGVdrqDM8eR0S3RY2WPf4Gwn";
|
||||
public static String BD_ACCESS_TOKEN = "";
|
||||
|
||||
public static String BD_DISH_URL = "https://aip.baidubce.com/rest/2.0/image-classify/v2/dish";
|
||||
|
||||
}
|
||||
|
||||
@@ -7,8 +7,21 @@ import com.example.ninefourone.nutritionmaster.bean.Occupation;
|
||||
import com.google.gson.Gson;
|
||||
import com.orhanobut.logger.Logger;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
@@ -235,6 +248,54 @@ public class WebUtils {
|
||||
}
|
||||
|
||||
|
||||
public static String HttpPost(String requestUrl, String accessToken, String params) throws Exception {
|
||||
System.out.println(params);
|
||||
String generalUrl = "";
|
||||
generalUrl = requestUrl + "?access_token=" + accessToken;
|
||||
System.out.println("发送的连接为:" + generalUrl);
|
||||
URL url = new URL(generalUrl);
|
||||
// 打开和URL之间的连接
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
System.out.println("打开链接,开始发送请求" + new Date().getTime() / 1000);
|
||||
connection.setRequestMethod("POST");
|
||||
// 设置通用的请求属性
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setRequestProperty("Connection", "Keep-Alive");
|
||||
connection.setUseCaches(false);
|
||||
connection.setDoOutput(true);
|
||||
connection.setDoInput(true);
|
||||
|
||||
// 得到请求的输出流对象
|
||||
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
||||
out.writeBytes(params);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
// 建立实际的连接
|
||||
connection.connect();
|
||||
// 获取所有响应头字段
|
||||
Map<String, List<String>> headers = connection.getHeaderFields();
|
||||
// 遍历所有的响应头字段
|
||||
for (String key : headers.keySet()) {
|
||||
System.out.println(key + "--->" + headers.get(key));
|
||||
}
|
||||
// 定义 BufferedReader输入流来读取URL的响应
|
||||
BufferedReader in = null;
|
||||
if (requestUrl.contains("nlp"))
|
||||
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "GBK"));
|
||||
else
|
||||
in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
|
||||
String result = "";
|
||||
String getLine;
|
||||
while ((getLine = in.readLine()) != null) {
|
||||
result += getLine;
|
||||
}
|
||||
in.close();
|
||||
System.out.println("请求结束" + new Date().getTime() / 1000);
|
||||
System.out.println("result:" + result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/*WebUtils.getMenu("雪丽对虾", new Callback() {
|
||||
@Override
|
||||
|
||||
BIN
app/src/main/res/drawable/camera_bg.jpg
Normal file
BIN
app/src/main/res/drawable/camera_bg.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 40 KiB |
@@ -1,4 +1,5 @@
|
||||
<vector android:height="24dp" android:viewportHeight="1024.0"
|
||||
android:viewportWidth="1024.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="#ffffff" android:pathData="M512,62.4c-248.3,0 -449.6,201.3 -449.6,449.6s201.3,449.6 449.6,449.6 449.6,-201.3 449.6,-449.6S760.3,62.4 512,62.4zM728.1,403.9 L478.8,653.3c-4.6,4.6 -10.6,6.9 -16.6,6.9s-12,-2.3 -16.6,-6.9c-37.4,-37.4 -149.6,-149.6 -149.6,-149.6 -9.2,-9.2 -9.2,-24.1 0,-33.2 9.2,-9.2 24.1,-9.2 33.2,0l133,109.5 232.7,-209.2c9.2,-9.2 24.1,-9.2 33.2,0C737.3,379.9 737.3,394.8 728.1,403.9z"/>
|
||||
<path android:fillColor="#4fa67c" android:pathData="M512,512m-512,0a512,512 0,1 0,1024 0,512 512,0 1,0 -1024,0Z"/>
|
||||
<path android:fillColor="#FFFFFF" android:pathData="M227.5,554.3l112.6,-92.2 92.2,92.2s174.1,-163.8 322.6,-250.9v61.4c-204.8,189.4 -332.8,394.2 -332.8,394.2l-194.6,-204.8z"/>
|
||||
</vector>
|
||||
|
||||
5
app/src/main/res/drawable/ic_take_photo.xml
Normal file
5
app/src/main/res/drawable/ic_take_photo.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:viewportHeight="1024.0"
|
||||
android:viewportWidth="1024.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="#1db127" android:pathData="M591.9,434.4a114.3,114.3 0,0 1,21.1 32l1.4,-0.4L705.4,806C800.9,743 864,634.9 864,512a353.6,353.6 0,0 0,-8.8 -77.6zM845.1,398.9a352.8,352.8 0,0 0,-146.2 -184.8L514,398.9zM434.8,595.8l-0.3,0.3 -2.8,-2.8c-1.2,-1.1 -2.3,-2.3 -3.4,-3.4L196.1,357.6A349.9,349.9 0,0 0,160 512c0,91.7 35.4,175 92.9,237.6l225.1,-130a114.5,114.5 0,0 1,-43.2 -23.8zM568,608.2l0.2,0.3 -289,166.9C341.2,830.3 422.5,864 512,864a349.7,349.7 0,0 0,161.6 -39.5l-68,-253.8a115,115 0,0 1,-37.6 37.5zM423.8,439.1l-0.1,-0.1 0.8,-0.8a112.4,112.4 0,0 1,8.3 -8.4l233.7,-233.7A349.3,349.3 0,0 0,512 160c-125.8,0 -235.8,66.2 -298,165.3l183,183a111.9,111.9 0,0 1,26.8 -69.2z"/>
|
||||
<path android:fillColor="#34a13c" android:pathData="M512,0C229.2,0 0,229.2 0,512s229.2,512 512,512 512,-229.2 512,-512S794.8,0 512,0zM817.5,817.5a430.4,430.4 0,1 1,92.6 -137.3,430.7 430.7,0 0,1 -92.6,137.3z"/>
|
||||
</vector>
|
||||
@@ -30,6 +30,8 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:gravity="center"
|
||||
android:textColor="#fff"
|
||||
android:textSize="18sp"
|
||||
tools:text="@string/app_name" />
|
||||
|
||||
</FrameLayout>
|
||||
@@ -39,15 +41,15 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="2"
|
||||
android:alpha="0.2"
|
||||
android:background="#8a515050">
|
||||
android:alpha="0.5"
|
||||
android:background="@drawable/camera_bg">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/more_take_photo_button_capture"
|
||||
android:layout_width="60dp"
|
||||
android:layout_height="60dp"
|
||||
android:layout_centerInParent="true"
|
||||
android:src="@drawable/selector" />
|
||||
android:src="@drawable/ic_take_photo" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/more_takephoto_ok"
|
||||
@@ -63,7 +65,7 @@
|
||||
</LinearLayout>
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/more_camera_cover_linearlayout"
|
||||
android:id="@+id/camera_cover_linearlayout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:alpha="0.3"
|
||||
|
||||
Reference in New Issue
Block a user