Google OCR在特定领域开展工作

时间:2017-07-05 06:40:24

标签: android android-camera ocr surfaceview

我目前正在使用SurfaceView中的CameraSourcecom.google.android.gms.vision来捕获图像上的检测到的文字,但由于它捕获了SurfaceView区域上的所有内容,我需要丢弃一些被收回的东西。

目标是让SurfaceView像下一张图片一样工作,忽略红色交叉区域内所有检测到的文字,并仅向我提供蓝色方块上的内容。

这甚至可能吗?

SurfaceView working goal

这是布局(没什么特别的):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <SurfaceView
        android:id="@+id/fragment_surface"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

在这里,您可以在课堂上找到与OCR相关的代码:

public class CameraActivity extends AppCompatActivity {

    private SurfaceView surfaceView;
    private CameraSource cameraSource;
    private StringBuilder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        surfaceView = (SurfaceView) findViewById(R.id.fragment_surface);

        TextRecognizer recognizer = new TextRecognizer.Builder(getApplicationContext()).build();
        if (recognizer.isOperational()) {

            cameraSource = new CameraSource.Builder(getApplicationContext(), recognizer)
                    .setFacing(CameraSource.CAMERA_FACING_BACK)
                    .setRequestedPreviewSize(1280, 1024)
                    .setRequestedFps(15.0f)
                    .setAutoFocusEnabled(true)
                    .build();

            surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(CameraActivity.this, new String[]{Manifest.permission.CAMERA}, 100);
                        return;
                    }
                    try {
                        cameraSource.start(surfaceView.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                    //
                }

                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                    cameraSource.stop();
                }
            });
            recognizer.setProcessor(new Detector.Processor<TextBlock>() {
                @Override
                public void release() {
                    //
                }

                @Override
                public void receiveDetections(Detector.Detections<TextBlock> detections) {
                    final SparseArray<TextBlock> items = detections.getDetectedItems();
                    if (items.size() != 0) {
                        builder = new StringBuilder();
                        for (int i = 0; i < items.size(); i++) {
                            TextBlock it = items.valueAt(i);
                            builder.append(it.getValue());
                        }
                        String read = builder.toString().trim().replace(" ", "").replace("\n", "");

                        //It continues doing other things here
                    }
                }
            });
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 100:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    try {
                        if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                            return;
                        }
                        cameraSource.start(surfaceView.getHolder());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

当设备处于纵向模式(如您的图片)时,非红色区域应该是“相机预览”的裁剪部分(充满整个屏幕),因此:

  • 如果要显示整个“相机预览”并仅在裁剪区域上执行OCR:那么您必须获取SurfaceView的“屏幕截图”(完整区域),然后对该区域进行裁剪以即时获取所需的像素
  • intead,如果您只想显示裁剪的区域(因为红色区域被其他界面,按钮,TextView等填充),那么您必须使用SurfaceView来仅渲染所需的一块通过使用其Matrix参数在该特定位置进行Camera Preview

我建议您“升级”到 TextureView ,这有点难以管理/使用,但可以通过在其内部纹理上使用矩阵来按需裁剪,缩放和缩放预览。

答案 1 :(得分:0)

简介

我正在尝试对现有代码进行最小的更改,因此请像现在一样扫描整个图像,并过滤掉超出范围的结果单词(或块)。

代码

使用rect.intersect查找bounding-box中的word,然后查看它是否在rectanglerect)内:

Rect yourRect = new Rect(10, 20, 30, 40);
rect.intersect(yourRect);//also see Drawable d = d.getBounds();

尝试将此代码添加到您的@Override public void receiveDetections()方法中:

        //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);

                   // Check if the word boundingBox is inside the area required

                  // using: rect.intersect(yourRect);
                  //...
                }

所以看起来像这样:

        recognizer.setProcessor(new Detector.Processor<TextBlock>() {
            @Override
            public void release() {
                //
            }

            @Override
            public void receiveDetections(Detector.Detections<TextBlock> detections) {
                final SparseArray<TextBlock> items = detections.getDetectedItems();
                if (items.size() != 0) {
                    builder = new StringBuilder();
                    for (int i = 0; i < items.size(); i++) {
                        TextBlock it = items.valueAt(i);
                        builder.append(it.getValue());
                    }
                    String read = builder.toString().trim().replace(" ", "").replace("\n", "");

        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);

        }

        //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);

                   // Check if the word boundingBox is inside the area required

                  // using: rect.intersect(yourRect);
                  //put the word in a filtered list...
                }

            }

                    //It continues doing other things here
                }
            }
        });

只有六行代码!