3
3
import android .content .Context ;
4
4
import android .graphics .Bitmap ;
5
5
import android .graphics .BitmapFactory ;
6
- import android .graphics .Matrix ;
7
6
8
7
import androidx .annotation .NonNull ;
9
8
10
- import com .vladih .computer_vision .flutter_vision .models .Tesseract ;
11
9
import com .vladih .computer_vision .flutter_vision .models .Yolo ;
12
10
import com .vladih .computer_vision .flutter_vision .models .Yolov8 ;
13
11
import com .vladih .computer_vision .flutter_vision .models .Yolov5 ;
14
12
import com .vladih .computer_vision .flutter_vision .models .Yolov8Seg ;
15
13
import com .vladih .computer_vision .flutter_vision .utils .utils ;
16
14
17
15
import org .opencv .android .OpenCVLoader ;
18
- import org .opencv .android .Utils ;
19
- import org .opencv .core .Mat ;
20
16
21
17
import java .nio .ByteBuffer ;
22
18
import java .util .ArrayList ;
@@ -41,7 +37,6 @@ public class FlutterVisionPlugin implements FlutterPlugin, MethodCallHandler {
41
37
private Context context ;
42
38
private FlutterAssets assets ;
43
39
private Yolo yolo_model ;
44
- private Tesseract tesseract_model ;
45
40
46
41
private ExecutorService executor ;
47
42
@@ -61,7 +56,6 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
61
56
this .methodChannel .setMethodCallHandler (null );
62
57
this .methodChannel = null ;
63
58
this .assets = null ;
64
- close_tesseract ();
65
59
close_yolo ();
66
60
this .executor .shutdownNow ();
67
61
} catch (Exception e ) {
@@ -84,17 +78,7 @@ private void setupChannel(Context context, FlutterAssets assets, BinaryMessenger
84
78
@ Override
85
79
public void onMethodCall (@ NonNull MethodCall call , @ NonNull Result result ) {
86
80
// Handle method calls from Flutter
87
- if (call .method .equals ("loadOcrModel" )) {
88
- try {
89
- load_ocr_model ((Map ) call .arguments );
90
- } catch (Exception e ) {
91
- result .error ("100" , "Error on load ocr components" , e );
92
- }
93
- } else if (call .method .equals ("ocrOnFrame" )) {
94
- ocr_on_frame ((Map ) call .arguments , result );
95
- } else if (call .method .equals ("closeOcrModel" )) {
96
- close_ocr_model (result );
97
- } else if (call .method .equals ("loadYoloModel" )) {
81
+ if (call .method .equals ("loadYoloModel" )) {
98
82
try {
99
83
load_yolo_model ((Map ) call .arguments );
100
84
result .success ("ok" );
@@ -107,77 +91,26 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
107
91
yolo_on_image ((Map ) call .arguments , result );
108
92
} else if (call .method .equals ("closeYoloModel" )) {
109
93
close_yolo_model (result );
110
- } else if (call .method .equals ("loadTesseractModel" )) {
111
- try {
112
- load_tesseract_model ((Map ) call .arguments );
113
- result .success ("ok" );
114
- } catch (Exception e ) {
115
- result .error ("100" , "Error on load Tesseract model" , e );
116
- }
117
- } else if (call .method .equals ("tesseractOnImage" )) {
118
- tesseract_on_image ((Map ) call .arguments , result );
119
- } else if (call .method .equals ("closeTesseractModel" )) {
120
- close_tesseract_model (result );
121
- } else {
94
+ }else {
122
95
result .notImplemented ();
123
96
}
124
97
}
125
98
126
- private void load_ocr_model (Map <String , Object > args ) throws Exception {
127
- load_yolo_model (args );
128
- load_tesseract_model (args );
129
- }
130
-
131
- private void ocr_on_frame (Map <String , Object > args , Result result ) {
132
- try {
133
- List <byte []> image = (ArrayList ) args .get ("bytesList" );
134
- int image_height = (int ) args .get ("image_height" );
135
- int image_width = (int ) args .get ("image_width" );
136
- float iou_threshold = (float ) (double ) (args .get ("iou_threshold" ));
137
- float conf_threshold = (float ) (double ) (args .get ("conf_threshold" ));
138
- float class_threshold = (float ) (double ) (args .get ("class_threshold" ));
139
- List <Integer > class_is_text = (List <Integer >) args .get ("class_is_text" );
140
- Bitmap bitmap = utils .feedInputToBitmap (context .getApplicationContext (), image , image_height , image_width , 90 );
141
- int [] shape = yolo_model .getInputTensor ().shape ();
142
- ByteBuffer byteBuffer = utils .feedInputTensor (bitmap , shape [1 ], shape [2 ], image_width , image_height , 0 , 255 );
143
-
144
- List <Map <String , Object >> yolo_results = yolo_model .detect_task (byteBuffer , image_height , image_width , iou_threshold , conf_threshold , class_threshold );
145
- for (Map <String , Object > yolo_result : yolo_results ) {
146
- float [] box = (float []) yolo_result .get ("box" );
147
- if (class_is_text .contains ((int ) box [5 ])) {
148
- Bitmap crop = utils .crop_bitmap (bitmap ,
149
- box [0 ], box [1 ], box [2 ], box [3 ]);
150
- //utils.getScreenshotBmp(crop, "crop");
151
- Bitmap tmp = crop .copy (crop .getConfig (), crop .isMutable ());
152
- yolo_result .put ("text" , tesseract_model .predict_text (tmp ));
153
- } else {
154
- yolo_result .put ("text" , "" );
155
- }
156
- }
157
- result .success (yolo_results );
158
- } catch (Exception e ) {
159
- result .error ("100" , "Ocr error" , e );
160
- }
161
- }
162
-
163
- private void close_ocr_model (Result result ) {
164
- try {
165
- close_tesseract ();
166
- close_yolo ();
167
- result .success ("OCR model closed succesfully" );
168
- } catch (Exception e ) {
169
- result .error ("100" , "Fail closed ocr model" , e );
170
- }
171
- }
172
-
173
99
private void load_yolo_model (Map <String , Object > args ) throws Exception {
174
- final String model = this . assets . getAssetFilePathByName ( args . get ( "model_path" ). toString ()) ;
175
- final Object is_asset_obj = args .get ("is_asset" );
100
+ String model = "" ;
101
+ final Object is_asset_obj = args .get ("is_asset" );
176
102
final boolean is_asset = is_asset_obj == null ? false : (boolean ) is_asset_obj ;
103
+ String label_path = "" ;
104
+ if (is_asset ){
105
+ model = this .assets .getAssetFilePathByName (args .get ("model_path" ).toString ());
106
+ label_path = this .assets .getAssetFilePathByName (args .get ("label_path" ).toString ());
107
+ }else {
108
+ model = args .get ("model_path" ).toString ();
109
+ label_path = args .get ("label_path" ).toString ();
110
+ }
177
111
final int num_threads = (int ) args .get ("num_threads" );
178
112
final boolean quantization = (boolean ) args .get ("quantization" );
179
113
final boolean use_gpu = (boolean ) args .get ("use_gpu" );
180
- final String label_path = this .assets .getAssetFilePathByName (args .get ("label_path" ).toString ());
181
114
final int rotation = (int ) args .get ("rotation" );
182
115
final String version = args .get ("model_version" ).toString ();
183
116
switch (version ) {
@@ -316,68 +249,6 @@ private void close_yolo_model(Result result) {
316
249
}
317
250
}
318
251
319
- private void load_tesseract_model (Map <String , Object > args ) throws Exception {
320
- final String tess_data = args .get ("tess_data" ).toString ();
321
- final Map <String , String > arg = (Map <String , String >) args .get ("arg" );
322
- final String language = args .get ("language" ).toString ();
323
- tesseract_model = new Tesseract (tess_data , arg , language );
324
- tesseract_model .initialize_model ();
325
- }
326
-
327
- class PredictionTask implements Runnable {
328
- private Tesseract tesseract ;
329
- private Bitmap bitmap ;
330
- private Result result ;
331
-
332
- public PredictionTask (Tesseract tesseract , Map <String , Object > args , Result result ) {
333
- byte [] image = (byte []) args .get ("bytesList" );
334
- this .tesseract = tesseract ;
335
- this .bitmap = BitmapFactory .decodeByteArray (image , 0 , image .length );
336
- this .result = result ;
337
- }
338
-
339
- @ Override
340
- public void run () {
341
- try {
342
- Mat mat = utils .rgbBitmapToMatGray (bitmap );
343
- double angle = utils .computeSkewAngle (mat .clone ());
344
- mat = utils .deskew (mat , angle );
345
- mat = utils .filterTextFromImage (mat );
346
- bitmap = Bitmap .createBitmap (mat .width (), mat .height (), Bitmap .Config .ARGB_8888 );
347
- Utils .matToBitmap (mat , bitmap );
348
- // utils.getScreenshotBmp(bitmap,"TESSEREACT");
349
- result .success (tesseract .predict_text (bitmap ));
350
- } catch (Exception e ) {
351
- result .error ("100" , "Prediction text Error" , e );
352
- }
353
- }
354
- }
355
-
356
- private void tesseract_on_image (Map <String , Object > args , Result result ) {
357
- try {
358
- PredictionTask predictionTask = new PredictionTask (tesseract_model , args , result );
359
- executor .submit (predictionTask );
360
- } catch (Exception e ) {
361
- result .error ("100" , "Prediction Error" , e );
362
- }
363
- }
364
-
365
- private void close_tesseract_model (Result result ) {
366
- try {
367
- close_tesseract ();
368
- result .success ("Tesseract model closed succesfully" );
369
- } catch (Exception e ) {
370
- result .error ("100" , "close_tesseract_model error" , e );
371
- }
372
- }
373
-
374
- private void close_tesseract (){
375
- if (tesseract_model != null ) {
376
- tesseract_model .close ();
377
- tesseract_model = null ;
378
- }
379
- }
380
-
381
252
private void close_yolo (){
382
253
if (yolo_model != null ) {
383
254
yolo_model .close ();
0 commit comments