百度api智能识别图片

百度api智能识别图片1.说明:最近公司碰到一个需求,需要能拍照,然后识别拍照的内容,根据拍照的东西,做一下智能化的自动处理,因此参考了百度,阿里,腾讯三家的sdk,最后发现还是百度好用,真香。。。(Ps:有了这个智能识图功能,以后鉴黄师可以失业了,我尝试拍了许多的生活物品,又去网上下载了一堆图片,识别正确率还是挺高的)注意,我们这个demo需要先选你识别的类型,然后再去做具体的识别,当然最好用的还是通用识别模…

1.说明:

最近公司碰到一个需求,需要能拍照,然后识别拍照的内容,根据拍照的东西,做一下智能化的自动处理,因此参考了百度,阿里,腾讯三家的sdk,最后发现还是百度好用,真香。。。(Ps:有了这个智能识图功能,以后鉴黄师可以失业了,我尝试拍了许多的生活物品,又去网上下载了一堆图片,识别正确率还是挺高的)

注意,我们这个demo需要先选你识别的类型,然后再去做具体的识别,当然最好用的还是通用识别模块

1.1源码地址·:https://github.com/wrs13634194612/BaiduImage 需要的自行下载

2.效果图:

2.1 百度控制台,需要自己去注册账号和appid

百度api智能识别图片

2.2 运行效果图 这是我的杯子  ,识别的效果图

百度api智能识别图片

3.appid管理类:appid  apikey   secretkey 需要自己去百度开放者平台注册

package com.example.administrator.testz.util;

/**
 * Created by INvo
 * on 2019-07-11.
 */
public class Constants {

    public static final String APP_ID = "176132";
    public static final String API_KEY = "tOjf1g16FzDNofLw6FM7";
    public static final String SECRET_KEY = "myvEcz6jGGjXuSzKvY8FW0gAkknTqH";
    public static final int ACTION_CHOOSE_IMAGE = 1;//访问本地图片的请求码
    public static final int REQUEST_CODE_TAKE_PICTURE = 2;//访问相机的请求码
}

4.主界面:核心代码都在主界面里面,

package com.example.administrator.testz;

import android.os.Bundle;


import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;


import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;


import com.example.administrator.testz.BuildConfig;
import com.example.administrator.testz.R;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import com.example.administrator.testz.util.BitmapToByte;
import com.example.administrator.testz.util.BitmapUtil;
import com.example.administrator.testz.util.Classify;
import com.example.administrator.testz.util.Constants;
import com.example.administrator.testz.util.DataUtil;
import com.example.administrator.testz.util.LineProgress;
import com.example.administrator.testz.util.PhotoProcess;
import com.example.administrator.testz.util.ToastUtil;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProviders;

public class MainActivity extends AppCompatActivity {
    private MainModel mViewModel;
    private Spinner spinnerCountry;
    private ImageView mImage, mChooseLocalImage, mCapture;
    private View line;
    private String imagePath = null;
    private Bitmap mBitmap = null;//待处理的位图,默认为空
    private JSONObject json;
    private static final int ERROR = 5;

    private LineProgress lineHandler = new LineProgress();
    private BitmapUtil mBu = new BitmapUtil();
    private DataUtil mDu = new DataUtil();
    private PhotoProcess photoProcess = new PhotoProcess();
    private BitmapToByte bitmapToByte = new BitmapToByte();
    private Classify classify = new Classify();

    private  String countries[] = {"通用物体识别","菜品识别","车型识别","logo商标识别"
            ,"动物识别","植物识别","图像主体检测","地标识别"
            ,"花卉识别","食材识别","红酒识别","货币识别"};
    private int style = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();
        mImage = findViewById(R.id.initImage_5);
        spinnerCountry = findViewById(R.id.spinnerCountry);
        mImage.setImageResource(R.drawable.wallpaper);
        line = findViewById(R.id.progress_line_5);
        mChooseLocalImage = findViewById(R.id.local_image_5);
        mChooseLocalImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getPhoto();
            }
        });

        mCapture = findViewById(R.id.capture_image_5);
        mCapture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                capturePhoto();
            }
        });

        final ImageView mUpload = findViewById(R.id.upload_5);
        mUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                processPhoto();
            }
        });


        final ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,countries);
        spinnerCountry.setAdapter(adapter);
        spinnerCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                //String data = (String)spinnerCountry.getItemAtPosition(i);//从spinner中获取被选择的数据
                style = i;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        mViewModel = ViewModelProviders.of(this).get(MainModel.class);
    }

    private void getPhoto() {
        Intent chooseImage = new Intent(Intent.ACTION_PICK);
        chooseImage.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        if (chooseImage.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(chooseImage, Constants.ACTION_CHOOSE_IMAGE);
        } else {
            ToastUtil.showToast(getApplicationContext(), "访问图库失败!");
        }
    }

    private void capturePhoto() {
        Intent capture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "photo_invo.jpg");
        try {
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (capture.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(capture, Constants.REQUEST_CODE_TAKE_PICTURE);
        } else {
            ToastUtil.showToast(getApplicationContext(), "没有读取到拍摄图片!");
        }
    }

    private void processPhoto() {
        if (mBitmap == null) {
            ToastUtil.showToast(getApplicationContext(), "没有位图!");
            return;
        } else {
            lineHandler.lineProgress(mImage, line);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    classify.detected();
                    try {
                        int what = services();
                        Message message = Message.obtain();
                        message.what = what;
                        message.obj = json;
                        Log.e("TAG","res_five:"+json);
                        mHandler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                        Message message = Message.obtain();
                        message.what = ERROR;
                        message.obj = e;
                        mHandler.sendMessage(message);
                    }
                }
            }).start();
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            lineHandler.getLineAnimation().cancel();
            line.setVisibility(View.INVISIBLE);
            try {
                JSONObject jsonObject5 = (JSONObject) msg.obj;
                JSONArray jsonArray5 = new JSONArray(jsonObject5.optString("result"));
                String name5 = jsonArray5.optJSONObject(0).optString("baike_info");
                String score5 = jsonArray5.optJSONObject(0).optString("score");
                String[] mitems5 = {"名称:" + name5, "可能性:" + score5};
                AlertDialog.Builder alertDialog5 = new AlertDialog.Builder(MainActivity.this);
                alertDialog5.setTitle("通用识别报告").setItems(mitems5, null).create().show();
            } catch (JSONException e) {
                e.printStackTrace();
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                alertDialog.setTitle("解析错误,请看log日志").setMessage("图片无法解析").create().show();
            }
        }
    };

    // advancedGeneral
    protected int services() {
        classify.detected();
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("baike_num", "5");


    switch (style){
        case 0:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().advancedGeneral(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().advancedGeneral(bitmapToByte.getArrays(), options);
            }
            break;
        case 1:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().dishDetect(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().dishDetect(bitmapToByte.getArrays(), options);
            }
            break;
        case 2:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().carDetect(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().carDetect(bitmapToByte.getArrays(), options);
            }
            break;
        case 3:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().logoSearch(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().logoSearch(bitmapToByte.getArrays(), options);
            }
            break;

        case 4:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().animalDetect(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().animalDetect(bitmapToByte.getArrays(), options);
            }
            break;
        case 5:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().plantDetect(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().plantDetect(bitmapToByte.getArrays(), options);
            }
            break;
        case 6:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().objectDetect(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().objectDetect(bitmapToByte.getArrays(), options);
            }
            break;
        case 7:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().landmark(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().landmark(bitmapToByte.getArrays(), options);
            }
            break;
        case 8:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().flower(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().flower(bitmapToByte.getArrays(), options);
            }
            break;
        case 9:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().ingredient(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().ingredient(bitmapToByte.getArrays(), options);
            }
            break;
        case 10:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().redwine(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().redwine(bitmapToByte.getArrays(), options);
            }
            break;
        case 11:
            if (imagePath == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(), R.id.initImage_1);
                json = classify.getClient().currency(bitmapToByte.getArrays(), options);
            } else {
                json = classify.getClient().currency(bitmapToByte.getArrays(), options);

            }
            break;

    }

        return 5;

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (requestCode == Constants.ACTION_CHOOSE_IMAGE) {
                    if (data == null) {
                        ToastUtil.showToast(getApplicationContext(), "没有选中内容!");
                        return;
                    } else {
                        Uri uri = data.getData();
                        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
                        cursor.moveToNext();
                        imagePath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
                        cursor.close();
                    }
                }
                break;
            case 2:
                if (requestCode == Constants.REQUEST_CODE_TAKE_PICTURE) {
                    if (data == null) {
                        ToastUtil.showToast(getApplicationContext(), "没有拍摄内容!");
                        return;
                    } else {
                        if (data.hasExtra("data")) {
                            mDu.setData(data);
                            mBitmap = (Bitmap) mDu.getData().getExtras().get("data");
                        } else {
                            ToastUtil.showToast(getApplicationContext(), "没有捕捉到二次图像");
                        }
                    }
                }
                break;
        }
        if (requestCode == Constants.ACTION_CHOOSE_IMAGE || requestCode == Constants.REQUEST_CODE_TAKE_PICTURE) {
            if (requestCode == Constants.ACTION_CHOOSE_IMAGE) {
                photoProcess.handle_photo(mBitmap, imagePath);
                mBitmap = photoProcess.getBitmap();
                bitmapToByte.bitmapToByte(mBitmap);
                mBitmap = bitmapToByte.getBitmap();
            } else if (requestCode == Constants.REQUEST_CODE_TAKE_PICTURE) {
                bitmapToByte.bitmapToByte(mBitmap);
                mBitmap = bitmapToByte.getBitmap();
            }
            mImage.setImageBitmap(mBitmap);
            mBu.setBitmap(mBitmap);
        }
    }


}

5.主界面布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/plant"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <ImageView
        android:id="@+id/initImage_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxHeight="400dp"
        android:maxWidth="400dp"
        android:minHeight="300dp"
        android:minWidth="300dp"
        android:scaleType="fitStart"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/spinnerCountry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="60dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.994"/>

    <ImageView
        android:id="@+id/local_image_5"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/album"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.994" />

    <ImageView
        android:id="@+id/capture_image_5"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/camera"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.994" />

    <ImageView
        android:id="@+id/upload_5"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/upload"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.994" />

    <View
        android:id="@+id/progress_line_5"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:background="@color/colorAccent"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/initImage_1"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/initImage_1"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>



6.清单文件:别忘记各种拍照,和存储权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.administrator.testz">

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />


    <application
        android:allowBackup="true"
        android:icon="@drawable/upload"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

7.builder需要引用jar包,jar包可以再官网下载sdk,解压就有了

implementation files('libs/aip-java-sdk-4.11.3.jar')
implementation files('libs/json-20160810.jar')
implementation files('libs/slf4j-api-1.7.25.jar')
implementation files('libs/slf4j-simple-1.7.25.jar')

8.最后看一下我的目录结构:这四个jar包,必须全部复制,少了的话  运行会软件闪退

百度api智能识别图片

end

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/38155.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注