web-dev-qa-db-ja.com

Mobile Vision APIを使用して画像内のテキストの位置を取得するにはどうすればよいですか?

Mobile Vision APIを使用して画像内の画面上のテキストの位置を取得する方法、およびそれらの周りに長方形を描画する方法は?

例:

enter image description here

8
VINNUSAURUS

それを行う方法

ImageViewをレイアウトに配置する

<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
xmlns:tools="http://schemas.Android.com/tools"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical">
<ImageView
    Android:layout_width="match_parent"
    Android:layout_height="250.0dp"
    Android:minWidth="25px"
    Android:minHeight="25px"
    Android:id="@+id/imageView1" />
</LinearLayout>

ImageViewメソッドでonCreateをインスタンス化する

ImageView imgView;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        imgView = FindViewById<ImageView>(Resource.Id.imageView1);
        OCR();
    }

これISテキストを取得し、長方形を描画するための重要なコード

コード上部のコメントを読んでください

public void OCR()
    {
        //Convert The Image To Bitmap
        Bitmap bitmap = BitmapFactory.DecodeResource(ApplicationContext.Resources, Resource.Mipmap.lineindent);

        TextRecognizer textRecognizer = new TextRecognizer.Builder(ApplicationContext).Build();

        if (!textRecognizer.IsOperational)
        {

            Log.Error("Main Activity", "Dependencies not available");

            // Check Android for low storage so dependencies can be loaded, DEPRICATED CHANGE LATER
            IntentFilter intentLowStorage = new IntentFilter(Intent.ActionDeviceStorageLow);

            bool hasLowStorage = RegisterReceiver(null, intentLowStorage) != null;

            if (hasLowStorage)
            {

                Toast.MakeText(this, "Low Memory On Disk", ToastLength.Long);
                Log.Error("Main Activity", "Low Memory On Disk");
            }

        }
        else
        {
            Frame frame = new Frame.Builder().SetBitmap(bitmap).Build();


            SparseArray items = textRecognizer.Detect(frame);
            List<TextBlock> blocks = new List<TextBlock>();

            TextBlock myItem = null;
            for (int i = 0; i < items.Size(); ++i)
            {
                myItem = (TextBlock)items.ValueAt(i);

                //Add All TextBlocks to the `blocks` List
                blocks.Add(myItem);

            }
            //END OF DETECTING TEXT

            //The Color of the Rectangle to Draw on top of Text
            Paint rectPaint = new Paint();
            rectPaint.Color = Color.White;
            rectPaint.SetStyle(Paint.Style.Stroke);
            rectPaint.StrokeWidth = (4.0f);

            //Create the Canvas object,
            //Which ever way you do image that is ScreenShot for example, you 
            //need the views Height and Width to draw recatngles 
            //because the API detects the position of Text on the View
            //So Dimesnions are important for Draw method to draw at that Text 
            //Location
            Bitmap tempBitmap = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, Bitmap.Config.Rgb565);
            Canvas canvas = new Canvas(tempBitmap);
            canvas.DrawBitmap(bitmap, 0, 0, null);

            //Loop through each `Block`
            foreach (TextBlock textBlock in blocks)
            {
                IList<IText> textLines = textBlock.Components; 

                //loop Through each `Line`
                foreach (IText currentLine in textLines)
                {
                    IList<IText>  words = currentLine.Components;

                    //Loop through each `Word`
                    foreach (IText currentword in words)
                    {
                        //Get the Rectangle/boundingBox of the Word
                        RectF rect = new RectF(currentword.BoundingBox);
                        rectPaint.Color = Color.Black;

                        //Finally Draw Rectangle/boundingBox around Word
                        canvas.DrawRect(rect, rectPaint);

                        //Set image to the `View`
                        imgView.SetImageDrawable(new BitmapDrawable(Resources, tempBitmap));


                    }

                }
            }

        }
    }

[〜#〜]結果[〜#〜]

enter image description here

Linesの四角形が必要な場合は、wordsループからコードを削除し、それをLinesループに入れると、同じことがブロックに適用されます

enter image description here

4
VINNUSAURUS