如何使用qualcomn SNPE SDK在gridview中加载图像?

时间:2018-02-28 12:27:47

标签: java android tensorflow qualcomm

我正在关注此链接教程[https://developer.qualcomm.com/software/snapdragon-neural-processing-engine-ai/getting-started,我可以下载所有模型图像,但我的要求是现在我需要如何在网格视图中显示图库图像我的代码是模型概述片段:

public class ModelOverviewFragment extends Fragment {

public static final String EXTRA_MODEL = "model";

enum MenuRuntimeGroup {

    SelectCpuRuntime(NeuralNetwork.Runtime.CPU),
    SelectGpuRuntime(NeuralNetwork.Runtime.GPU),
    SelectDspRuntime(NeuralNetwork.Runtime.DSP);

    public static int ID = 1;

    public NeuralNetwork.Runtime runtime;

    MenuRuntimeGroup(NeuralNetwork.Runtime runtime) {
        this.runtime = runtime;
    }
}

private GridView mImageGrid;

private ModelImagesAdapter mImageGridAdapter;

private ModelOverviewFragmentController mController;

private TextView mDimensionsText;

private TextView mModelNameText;

private Spinner mOutputLayersSpinners;

private TextView mClassificationText;

private TextView mModelVersionText;

public static ModelOverviewFragment create(final Model model) {
    final ModelOverviewFragment fragment = new ModelOverviewFragment();
    final Bundle arguments = new Bundle();
    arguments.putParcelable(EXTRA_MODEL, model);
    fragment.setArguments(arguments);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_model, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mImageGrid = (GridView) view.findViewById(R.id.model_image_grid);
    mImageGridAdapter = new ModelImagesAdapter(getActivity());
    mImageGrid.setAdapter(mImageGridAdapter);
    mImageGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Bitmap bitmap = mImageGridAdapter.getItem(position);
            mController.classify(bitmap);
        }
    });

    mModelNameText = (TextView) view.findViewById(R.id.model_overview_name_text);
    mModelVersionText = (TextView) view.findViewById(R.id.model_overview_version_text);
    mDimensionsText = (TextView) view.findViewById(R.id.model_overview_dimensions_text);
    mOutputLayersSpinners = (Spinner) view.findViewById(R.id.model_overview_layers_spinner);
    mClassificationText = (TextView) view.findViewById(R.id.model_overview_classification_text);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    setHasOptionsMenu(true);
    final Model model = getArguments().getParcelable(EXTRA_MODEL);
    mController = new ModelOverviewFragmentController(
            (Application) getActivity().getApplicationContext(), model);
}

@Override
public void onCreateOptionsMenu(android.view.Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    final SNPE.NeuralNetworkBuilder builder = new SNPE.NeuralNetworkBuilder(
            (Application) (getActivity().getApplicationContext()));
    for (MenuRuntimeGroup item : MenuRuntimeGroup.values()) {
        if (builder.isRuntimeSupported(item.runtime)) {
            menu.add(MenuRuntimeGroup.ID, item.ordinal(), 0, item.runtime.name());
        }
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getGroupId() == MenuRuntimeGroup.ID) {
        final MenuRuntimeGroup option = MenuRuntimeGroup.values()[item.getItemId()];
        mController.setTargetRuntime(option.runtime);
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onStart() {
    super.onStart();
    mController.attach(this);
}

@Override
public void onStop() {
    mController.detach(this);
    super.onStop();
}

public void addSampleBitmap(Bitmap bitmap) {
    if (mImageGridAdapter.getPosition(bitmap) == -1) {
        mImageGridAdapter.add(bitmap);
        mImageGridAdapter.notifyDataSetChanged();
    }
}

public void setNetworkDimensions(Map<String, int[]> inputDimensions) {
    mDimensionsText.setText(Arrays.toString(inputDimensions.get("data")));
}

public void displayModelLoadFailed() {
    mClassificationText.setVisibility(View.VISIBLE);
    mClassificationText.setText(R.string.model_load_failed);
    Toast.makeText(getActivity(), R.string.model_load_failed, Toast.LENGTH_SHORT).show();
}

public void setModelName(String modelName) {
    mModelNameText.setText(modelName);
}

public void setModelVersion(String version) {
    mModelVersionText.setText(version);
}

public void setOutputLayersNames(Set<String> outputLayersNames) {
    mOutputLayersSpinners.setAdapter(new ArrayAdapter<>(
        getActivity(), android.R.layout.simple_list_item_1,
        new LinkedList<>(outputLayersNames)));
}

public void setClassificationResult(String[] classificationResult) {
    if (classificationResult.length > 0) {
        mClassificationText.setText(
                String.format("%s: %s", classificationResult[0], classificationResult[1]));
    }
    mClassificationText.setVisibility(View.VISIBLE);
}

public void setLoadingVisible(boolean visible) {
    mClassificationText.setVisibility(visible ? View.VISIBLE : View.GONE);
    if (visible) {
        mClassificationText.setText(R.string.loading_network);
    }
}

public void displayModelNotLoaded() {
    Toast.makeText(getActivity(), R.string.model_not_loaded, Toast.LENGTH_SHORT).show();
}

public void displayClassificationFailed() {
    Toast.makeText(getActivity(), R.string.classification_failed, Toast.LENGTH_SHORT).show();
}

private static class ModelImagesAdapter extends ArrayAdapter<Bitmap> {

    public ModelImagesAdapter(Context context) {
        super(context, R.layout.model_image_layout);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.model_image_layout, parent, false);
        } else {
            view = convertView;
        }

        final ImageView imageView = ImageView.class.cast(view);
        imageView.setImageBitmap(getItem(position));
        return view;
    }
}

} 用于图像加载代码:

public class LoadImageTask extends AsyncTask<File, Void, Bitmap> {

private final ModelOverviewFragmentController mController;

private final File mImageFile;

public LoadImageTask(ModelOverviewFragmentController controller, final File imageFile) {
    mController = controller;
    mImageFile = imageFile;
}

@Override
protected Bitmap doInBackground(File... params) {
    return BitmapFactory.decodeFile(mImageFile.getAbsolutePath());
}

@Override
protected void onPostExecute(Bitmap bitmap) {
    super.onPostExecute(bitmap);
    mController.onBitmapLoaded(mImageFile, bitmap);
}

}

/ * public class LoadImageTask扩展了AsyncTask {

private final ModelOverviewFragmentController mController;
private File path;



public LoadImageTask(ModelOverviewFragmentController controller, File jpeg)
{
    mController=controller;
    path=jpeg;
}

@Override
protected Bitmap doInBackground(Object... params) {
    Bitmap bitmap = null;
    File file = new File(
            Environment.getExternalStorageDirectory().getAbsolutePath() + path);


    if(file.exists()){
        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
        Log.d("ImagePath",bitmap.toString());
    }

    return bitmap;

}
@Override
protected void onPostExecute(Bitmap result) {
    super.onPostExecute(result);
    mController.onBitmapLoaded(path,result);
}

} 对于模型:

public class Model implements Parcelable {

public static final Uri MODELS_URI = Uri.parse("content://snpe/models");

// public static final Uri MODELS_URI = Uri.parse(&#34; content:// mtp:// [usb:002,029] / Internal%20shared%20storage / DCIM&#34;);

public static final String INVALID_ID = "null";

public File file;
public String[] labels;
public File[] rawImages;
public File[] jpgImages;
public String name;
public File meanImage;

protected Model(Parcel in) {
    name = in.readString();
    file = new File(in.readString());

    final String[] rawPaths = new String[in.readInt()];
    in.readStringArray(rawPaths);
    rawImages = fromPaths(rawPaths);

    final String[] jpgPaths = new String[in.readInt()];
    in.readStringArray(jpgPaths);
    jpgImages = fromPaths(jpgPaths);

    meanImage = new File(in.readString());

    labels = new String[in.readInt()];
    in.readStringArray(labels);
}

public Model() {}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(name);
    dest.writeString(file.getAbsolutePath());
    dest.writeInt(rawImages.length);
    dest.writeStringArray(toPaths(rawImages));
    dest.writeInt(jpgImages.length);
    dest.writeStringArray(toPaths(jpgImages));
    dest.writeString(meanImage.getAbsolutePath());
    dest.writeInt(labels.length);
    dest.writeStringArray(labels);
}

private File[] fromPaths(String[] paths) {
    final File[] files = new File[paths.length];
    for (int i = 0; i < paths.length; i++) {
        files[i] = new File(paths[i]);
    }
    return files;
}

private String[] toPaths(File[] files) {
    final String[] paths = new String[files.length];
    for (int i = 0; i < files.length; i++) {
        paths[i] = files[i].getAbsolutePath();
    }
    return paths;
}

@Override
public int describeContents() {
    return 0;
}

public static final Creator<Model> CREATOR = new Creator<Model>() {
    @Override
    public Model createFromParcel(Parcel in) {
        return new Model(in);
    }

    @Override
    public Model[] newArray(int size) {
        return new Model[size];
    }
};

@Override
public String toString() {
    return name.toUpperCase();
}

}

1 个答案:

答案 0 :(得分:0)

要在网格视图中加载SD卡图像,请在ModelOverviewFragmentController.java的loadImageSamples方法中进行一些更改。当前,图像是从设备内部存储中的snpe文件夹中拾取的,并填充在“网格视图”中。要从库中获取所有图像,请创建AsyncTask类,以在Content Provider的帮助下将图像从库加载到Grid View。 请使用此链接https://stackoverflow.com/a/10877404获取代码参考,以在网格视图上加载图像。

希望这会有所帮助!

谢谢