1

我遵循此链接教程 [ 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);
}

}

/*公共类 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("content://mtp://[usb:002,029]/Internal%20shared%20storage/DCIM");

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();
}

}

4

1 回答 1

0

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

希望这可以帮助!

谢谢你

于 2019-08-27T12:59:12.587 回答