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,69 +91,11 @@ 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
100
String model = "" ;
175
101
final Object is_asset_obj = args .get ("is_asset" );
@@ -323,68 +249,6 @@ private void close_yolo_model(Result result) {
323
249
}
324
250
}
325
251
326
- private void load_tesseract_model (Map <String , Object > args ) throws Exception {
327
- final String tess_data = args .get ("tess_data" ).toString ();
328
- final Map <String , String > arg = (Map <String , String >) args .get ("arg" );
329
- final String language = args .get ("language" ).toString ();
330
- tesseract_model = new Tesseract (tess_data , arg , language );
331
- tesseract_model .initialize_model ();
332
- }
333
-
334
- class PredictionTask implements Runnable {
335
- private Tesseract tesseract ;
336
- private Bitmap bitmap ;
337
- private Result result ;
338
-
339
- public PredictionTask (Tesseract tesseract , Map <String , Object > args , Result result ) {
340
- byte [] image = (byte []) args .get ("bytesList" );
341
- this .tesseract = tesseract ;
342
- this .bitmap = BitmapFactory .decodeByteArray (image , 0 , image .length );
343
- this .result = result ;
344
- }
345
-
346
- @ Override
347
- public void run () {
348
- try {
349
- Mat mat = utils .rgbBitmapToMatGray (bitmap );
350
- double angle = utils .computeSkewAngle (mat .clone ());
351
- mat = utils .deskew (mat , angle );
352
- mat = utils .filterTextFromImage (mat );
353
- bitmap = Bitmap .createBitmap (mat .width (), mat .height (), Bitmap .Config .ARGB_8888 );
354
- Utils .matToBitmap (mat , bitmap );
355
- // utils.getScreenshotBmp(bitmap,"TESSEREACT");
356
- result .success (tesseract .predict_text (bitmap ));
357
- } catch (Exception e ) {
358
- result .error ("100" , "Prediction text Error" , e );
359
- }
360
- }
361
- }
362
-
363
- private void tesseract_on_image (Map <String , Object > args , Result result ) {
364
- try {
365
- PredictionTask predictionTask = new PredictionTask (tesseract_model , args , result );
366
- executor .submit (predictionTask );
367
- } catch (Exception e ) {
368
- result .error ("100" , "Prediction Error" , e );
369
- }
370
- }
371
-
372
- private void close_tesseract_model (Result result ) {
373
- try {
374
- close_tesseract ();
375
- result .success ("Tesseract model closed succesfully" );
376
- } catch (Exception e ) {
377
- result .error ("100" , "close_tesseract_model error" , e );
378
- }
379
- }
380
-
381
- private void close_tesseract (){
382
- if (tesseract_model != null ) {
383
- tesseract_model .close ();
384
- tesseract_model = null ;
385
- }
386
- }
387
-
388
252
private void close_yolo (){
389
253
if (yolo_model != null ) {
390
254
yolo_model .close ();
0 commit comments