hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package com.rockchip.gpadc.demo;
 
import android.content.res.AssetManager;
import android.graphics.RectF;
 
import com.rockchip.gpadc.demo.yolo.InferenceWrapper;
import com.rockchip.gpadc.demo.yolo.PostProcess;
import com.rockchip.gpadc.demo.tracker.ObjectTracker;
 
import java.io.IOException;
import java.util.ArrayList;
 
import static com.rockchip.gpadc.demo.yolo.PostProcess.INPUT_SIZE;
import static com.rockchip.gpadc.demo.rga.HALDefine.CAMERA_PREVIEW_WIDTH;
import static com.rockchip.gpadc.demo.rga.HALDefine.CAMERA_PREVIEW_HEIGHT;
import static java.lang.System.arraycopy;
 
public class InferenceResult {
 
    OutputBuffer mOutputBuffer;
    ArrayList<Recognition> recognitions = null;
    private boolean mIsVaild = false;   //是否需要重新计算
    PostProcess mPostProcess = new PostProcess();
    private ObjectTracker mSSDObjectTracker;
 
    public void init(AssetManager assetManager) throws IOException {
        mOutputBuffer = new OutputBuffer();
 
        mPostProcess.init(assetManager);
 
        mSSDObjectTracker = new ObjectTracker(CAMERA_PREVIEW_WIDTH, CAMERA_PREVIEW_HEIGHT, 3);
    }
 
    public void reset() {
        if (recognitions != null) {
            recognitions.clear();
            mIsVaild = true;
        }
    }
    public synchronized void setResult(OutputBuffer outputs) {
 
        if (mOutputBuffer.mGrid0Out == null) {
            mOutputBuffer.mGrid0Out = outputs.mGrid0Out.clone();
            mOutputBuffer.mGrid1Out = outputs.mGrid1Out.clone();
            mOutputBuffer.mGrid2Out = outputs.mGrid2Out.clone();
        } else {
            arraycopy(outputs.mGrid0Out, 0, mOutputBuffer.mGrid0Out, 0,
                    outputs.mGrid0Out.length);
            arraycopy(outputs.mGrid1Out, 0, mOutputBuffer.mGrid1Out, 0,
                    outputs.mGrid1Out.length);
            arraycopy(outputs.mGrid2Out, 0, mOutputBuffer.mGrid2Out, 0,
                    outputs.mGrid2Out.length);
        }
        mIsVaild = false;
    }
 
    public synchronized ArrayList<Recognition> getResult(InferenceWrapper mInferenceWrapper) {
        if (!mIsVaild) {
            mIsVaild = true;
 
            recognitions = mInferenceWrapper.postProcess(mOutputBuffer);
 
            recognitions = mSSDObjectTracker.tracker(recognitions);
        }
 
        return recognitions;
    }
 
    public static class OutputBuffer {
        public byte[] mGrid0Out;
        public byte[] mGrid1Out;
        public byte[] mGrid2Out;
    }
 
    /**
     * An immutable result returned by a Classifier describing what was recognized.
     */
    public static class Recognition {
 
        private int trackId = 0;
 
        /**
         * A unique identifier for what has been recognized. Specific to the class, not the instance of
         * the object.
         */
        private final int id;
 
        /**
         * A sortable score for how good the recognition is relative to others. Higher should be better.
         */
        private final Float confidence;
 
        /** Optional location within the source image for the location of the recognized object. */
        private RectF location;
 
        public Recognition(
                final int id, final Float confidence, final RectF location) {
            this.id = id;
            this.confidence = confidence;
            this.location = location;
            // TODO -- add name field, and show it.
        }
 
        public int getId() {
            return id;
        }
 
        public Float getConfidence() {
            return confidence;
        }
 
        public RectF getLocation() {
            return new RectF(location);
        }
 
        public void setLocation(RectF location) {
            this.location = location;
        }
 
        public void setTrackId(int trackId) {
            this.trackId = trackId;
        }
 
        public int getTrackId() {
            return this.trackId;
        }
 
        @Override
        public String toString() {
            String resultString = "";
 
            resultString += "[" + id + "] ";
 
            if (confidence != null) {
                resultString += String.format("(%.1f%%) ", confidence * 100.0f);
            }
 
            if (location != null) {
                resultString += location + " ";
            }
 
            return resultString.trim();
        }
    }
 
    /**
     * Detected objects, returned from native yolo_post_process
     */
    public static class DetectResultGroup {
        /**
         * detected objects count.
         */
        public int count = 0;
 
        /**
         * id for each detected object.
         */
        public int[] ids;
 
        /**
         * score for each detected object.
         */
        public float[] scores;
 
        /**
         * box for each detected object.
         */
        public float[] boxes;
 
//        public DetectResultGroup(
//                int count, int[] ids, float[] scores, float[] boxes
//        ) {
//            this.count = count;
//            this.ids = ids;
//            this.scores = scores;
//            this.boxes = boxes;
//        }
//
//        public int getCount() {
//            return count;
//        }
//
//        public void setCount(int count) {
//            this.count = count;
//        }
//
//        public int[] getIds() {
//            return ids;
//        }
//
//        public void setIds(int[] ids) {
//            this.ids = ids;
//        }
//
//        public float[] getScores() {
//            return scores;
//        }
//
//        public void setScores(float[] scores) {
//            this.scores = scores;
//        }
//
//        public float[] getBoxes() {
//            return boxes;
//        }
//
//        public void setBoxes(float[] boxes) {
//            this.boxes = boxes;
//        }
    }
}