5
5
import android .content .Context ;
6
6
import android .content .res .AssetFileDescriptor ;
7
7
import android .content .res .AssetManager ;
8
+ import android .graphics .Bitmap ;
9
+ import android .graphics .Color ;
8
10
import android .util .Log ;
9
11
12
+ import com .vladih .computer_vision .flutter_vision .utils .utils ;
13
+
10
14
import org .opencv .core .Core ;
11
15
import org .opencv .core .CvType ;
12
16
import org .opencv .core .Mat ;
17
+ import org .opencv .core .MatOfPoint ;
18
+ import org .opencv .core .Point ;
13
19
import org .opencv .imgproc .Imgproc ;
14
20
import org .tensorflow .lite .Interpreter ;
15
21
import org .tensorflow .lite .gpu .CompatibilityList ;
20
26
21
27
import java .io .File ;
22
28
import java .io .FileInputStream ;
29
+ import java .lang .reflect .Array ;
23
30
import java .nio .ByteBuffer ;
24
31
import java .nio .MappedByteBuffer ;
25
32
import java .nio .channels .FileChannel ;
29
36
import java .util .HashMap ;
30
37
import java .util .List ;
31
38
import java .util .Map ;
39
+ import java .util .UUID ;
32
40
import java .util .Vector ;
33
41
34
- public class Yolov8 extends Yolo {
42
+ public class Yolov8 extends Yolo {
35
43
public Yolov8 (Context context ,
36
44
String model_path ,
37
45
boolean is_assets ,
@@ -44,63 +52,89 @@ public Yolov8(Context context,
44
52
}
45
53
46
54
@ Override
47
- protected List <float []>filter_box (float [][][] model_outputs , float iou_threshold ,
48
- float conf_threshold , float class_threshold , float input_width , float input_height ){
55
+ public List <Map <String , Object >> detect_task (ByteBuffer byteBuffer ,
56
+ int source_height ,
57
+ int source_width ,
58
+ float iou_threshold ,
59
+ float conf_threshold ,
60
+ float class_threshold ) throws Exception {
61
+ try {
62
+ int [] input_shape = this .interpreter .getInputTensor (0 ).shape ();
63
+ this .interpreter .run (byteBuffer , this .output );
64
+ //INFO: output from detection model is not normalized
65
+ List <float []> boxes = filter_box (this .output , iou_threshold , conf_threshold ,
66
+ class_threshold , input_shape [1 ], input_shape [2 ]);
67
+ boxes = restore_size (boxes , input_shape [1 ], input_shape [2 ], source_width , source_height );
68
+ return out (boxes , this .labels );
69
+ } catch (Exception e ) {
70
+ throw e ;
71
+ } finally {
72
+ byteBuffer .clear ();
73
+ }
74
+ }
75
+
76
+ @ Override
77
+ protected List <float []> filter_box (float [][][] model_outputs , float iou_threshold ,
78
+ float conf_threshold , float class_threshold , float input_width , float input_height ) {
49
79
try {
50
80
//model_outputs = [1,box+class,detected_box]
51
81
List <float []> pre_box = new ArrayList <>();
52
82
int class_index = 4 ;
53
83
int dimension = model_outputs [0 ][0 ].length ;
54
84
int rows = model_outputs [0 ].length ;
55
- float x1 ,y1 ,x2 ,y2 ;
56
- for (int i =0 ; i <dimension ;i ++){
57
- //convert xywh to xyxy
58
- x1 = (model_outputs [0 ][0 ][i ]-model_outputs [0 ][2 ][i ]/2f );
59
- y1 = (model_outputs [0 ][1 ][i ]-model_outputs [0 ][3 ][i ]/2f );
60
- x2 = (model_outputs [0 ][0 ][i ]+model_outputs [0 ][2 ][i ]/2f );
61
- y2 = (model_outputs [0 ][1 ][i ]+model_outputs [0 ][3 ][i ]/2f );
62
- float max = 0 ;
63
- int max_index = 0 ;
64
- for (int j =class_index ;j <rows ;j ++){
65
- if (model_outputs [0 ][j ][i ]<class_threshold ) continue ;
66
- if (max <model_outputs [0 ][j ][i ]){
67
- max = model_outputs [0 ][j ][i ];
85
+ int max_index = 0 ;
86
+ float max = 0f ;
87
+ for (int i = 0 ; i < dimension ; i ++) {
88
+ float x1 = (model_outputs [0 ][0 ][i ] - model_outputs [0 ][2 ][i ] / 2f );
89
+ float y1 = (model_outputs [0 ][1 ][i ] - model_outputs [0 ][3 ][i ] / 2f );
90
+ float x2 = (model_outputs [0 ][0 ][i ] + model_outputs [0 ][2 ][i ] / 2f );
91
+ float y2 = (model_outputs [0 ][1 ][i ] + model_outputs [0 ][3 ][i ] / 2f );
92
+
93
+ max_index = class_index ;
94
+ max = model_outputs [0 ][max_index ][i ];
95
+
96
+ for (int j = class_index + 1 ; j < rows ; j ++) {
97
+ float current = model_outputs [0 ][j ][i ];
98
+ if (current > max ) {
99
+ max = current ;
68
100
max_index = j ;
69
101
}
70
102
}
71
- if (max >0 ){
103
+
104
+ if (max > class_threshold ) {
72
105
float [] tmp = new float [6 ];
73
- tmp [0 ]= x1 ;
74
- tmp [1 ]= y1 ;
75
- tmp [2 ]= x2 ;
76
- tmp [3 ]= y2 ;
77
- tmp [4 ]= model_outputs [ 0 ][ max_index ][ i ] ;
78
- tmp [5 ]= (max_index - class_index )* 1f ;
106
+ tmp [0 ] = x1 ;
107
+ tmp [1 ] = y1 ;
108
+ tmp [2 ] = x2 ;
109
+ tmp [3 ] = y2 ;
110
+ tmp [4 ] = max ;
111
+ tmp [5 ] = (max_index - class_index ) * 1f ;
79
112
pre_box .add (tmp );
80
113
}
81
114
}
82
115
if (pre_box .isEmpty ()) return new ArrayList <>();
83
116
//for reverse orden, insteand of using .reversed method
84
- Comparator <float []> compareValues = (v1 , v2 )-> Float .compare (v1 [ 1 ], v2 [ 1 ]);
117
+ Comparator <float []> compareValues = (v1 , v2 ) -> Float .compare (v2 [ 4 ], v1 [ 4 ]);
85
118
//Collections.sort(pre_box,compareValues.reversed());
86
- Collections .sort (pre_box ,compareValues );
119
+ Collections .sort (pre_box , compareValues );
87
120
return nms (pre_box , iou_threshold );
88
- }catch (Exception e ){
89
- throw e ;
121
+ } catch (Exception e ) {
122
+ throw e ;
90
123
}
91
124
}
125
+
92
126
@ Override
93
- protected List <Map <String , Object >> out (List <float []> yolo_result , Vector <String > labels ){
127
+ protected List <Map <String , Object >> out (List <float []> yolo_result , Vector <String > labels ) {
94
128
try {
95
129
List <Map <String , Object >> result = new ArrayList <>();
96
- for (float [] box : yolo_result ) {
130
+ for (float [] box : yolo_result ) {
97
131
Map <String , Object > output = new HashMap <>();
98
- output .put ("box" ,new float []{box [0 ], box [1 ], box [2 ], box [3 ], box [4 ]}); //x1,y1,x2,y2,conf_class
99
- output .put ("tag" ,labels .get ((int )box [5 ]));
132
+ output .put ("box" , new float []{box [0 ], box [1 ], box [2 ], box [3 ], box [4 ]}); //x1,y1,x2,y2,conf_class
133
+ output .put ("tag" , labels .get ((int ) box [5 ]));
100
134
result .add (output );
101
135
}
102
136
return result ;
103
- }catch (Exception e ){
137
+ } catch (Exception e ) {
104
138
throw e ;
105
139
}
106
140
}
0 commit comments