1 package com.rockchip.gpadc.demo.yolo;/* 2 * Copyright (C) 2022 Rockchip Electronics Co., Ltd. 3 * Authors: 4 * raul.rao <raul.rao@rock-chips.com> 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 import android.content.res.AssetManager; 20 import android.util.Log; 21 22 import java.io.BufferedReader; 23 import java.io.FileInputStream; 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.io.InputStreamReader; 27 import java.util.Vector; 28 29 public class PostProcess { 30 public static final int INPUT_SIZE = 640; 31 public static final int INPUT_CHANNEL = 3; 32 33 public static final String TAG = "rkyolo.PostProcess"; 34 35 // Pre-allocated buffers. 36 private Vector<String> labels = new Vector<String>(); 37 init(AssetManager assetManager)38 public void init(AssetManager assetManager) throws IOException { 39 loadLabelName(assetManager, 40 "file:///android_asset/coco_80_labels_list.txt", labels); 41 } 42 getLabelTitle(int id)43 public String getLabelTitle(int id) { 44 return labels.get(id); 45 } 46 loadLabelName(final AssetManager assetManager, final String locationFilename, Vector<String> labels)47 private void loadLabelName(final AssetManager assetManager, final String locationFilename, 48 Vector<String> labels) throws IOException { 49 // Try to be intelligent about opening from assets or sdcard depending on prefix. 50 final String assetPrefix = "file:///android_asset/"; 51 InputStream is; 52 if (locationFilename.startsWith(assetPrefix)) { 53 is = assetManager.open(locationFilename.split(assetPrefix, -1)[1]); 54 } else { 55 is = new FileInputStream(locationFilename); 56 } 57 58 final BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 59 String line; 60 while ((line = reader.readLine()) != null) { 61 labels.add(line); 62 } 63 reader.close(); 64 65 Log.d(TAG, "Loaded label!"); 66 } 67 } 68