【问题标题】:Google mobile vision text API- read specific text with in a recatangleGoogle 移动视觉文本 API - 读取矩形内的特定文本
【发布时间】:2017-05-31 16:55:37
【问题描述】:

我正在使用 Google 移动视觉文本 API 来使用提供的 API 阅读文本。

我的用例:

最初我在视图表面绘制一个可调整大小的矩形。 将 google api 识别的文本限制在矩形内。

public class GraphicOverlay<T extends GraphicOverlay.Graphic> extends View 
{

    protected void onDraw(Canvas canvas) 
        {
             super.onDraw(canvas);
            synchronized (mLock) 
        {

            if ((mPreviewWidth != 0) && (mPreviewHeight != 0)) 
        {        
                mWidthScaleFactor = (float) canvas.getWidth() / (float)     mPreviewWidth;
                    mHeightScaleFactor = (float) canvas.getHeight() / (float) mPreviewHeight;
            }

            for (Graphic graphic : mGraphics) 
        {
                graphic.draw(canvas);
            }

            paint.setColor(Color.parseColor("#55000000"));
            paint.setStyle(Paint.Style.FILL);
            paint.setStrokeJoin(Paint.Join.ROUND);
            // mPaint.setStrokeCap(Paint.Cap.ROUND);
            paint.setStrokeWidth(5);

            canvas.drawPaint(paint);
            paint.setColor(Color.parseColor("#55FFFFFF"));

            if (groupId == 1) {
                mCurrentRect= new Rect(point2.x+colorballs.get(1).getWidthOfBall() / 2,
                                        point4.y +colorballs.get(1).getWidthOfBall() / 2,
                                        point4.x+colorballs.get(1).getWidthOfBall() / 2,
                                        point2.y+colorballs.get(1).getWidthOfBall() / 2);
                canvas.drawRect(mCurrentRect, paint);
            } else {
                mCurrentRect= new Rect(point2.x + colorballs.get(1).getWidthOfBall() / 2,
                        point4.y + colorballs.get(3).getWidthOfBall() / 2, point4.x
                        + colorballs.get(3).getWidthOfBall() / 2, point2.y
                        + colorballs.get(1).getWidthOfBall() / 2);
                canvas.drawRect(mCurrentRect, paint);
            }
            BitmapDrawable mBitmap;
            mBitmap = new BitmapDrawable();

            // draw the balls on the canvas`enter code here`
            for (ColorBall ball : colorballs) {
                canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(),
                        new Paint());
            }
        }
    }
}



class CustomTextRecognizer extends Detector<TextBlock> {
    private Detector<TextBlock> mDelegate;
    private GraphicOverlay<OcrGraphic> mOcrGraphicOverlay;

    CustomTextRecognizer(Detector<TextBlock> delegate, GraphicOverlay<OcrGraphic> ocrGraphicOverlay) {
        mDelegate = delegate;
        mOcrGraphicOverlay= ocrGraphicOverlay;
    }

    @Override
    public void receiveFrame(Frame frame) {
        Bitmap bt= frame.getBitmap();
        super.receiveFrame(frame);
    }


    public SparseArray<TextBlock> detect(Frame frame) {

     //How to compare the items that is inside my rectangle.
    }
 }

【问题讨论】:

  • 请建议如何使用矩形覆盖限制检测

标签: android


【解决方案1】:
  /*
     * This method will call before each block detection and resolves only with  valid detection that
     * scanned with in the rectangle provided.
     */
    public SparseArray<TextBlock> detect(Frame frame) {
        SparseArray<TextBlock> validDetections = new SparseArray<TextBlock>();
        Rect overlayRectangle = mOcrGraphicOverlay.getCurrentRectangle();
        SparseArray<TextBlock> detectedItems = mDelegate.detect(frame);
        for (int index = 0; index < detectedItems.size(); ++index) {
            TextBlock detectedBlock = detectedItems.valueAt(index);
            Rect boundingBox = detectedBlock.getBoundingBox();
            //  mOcrGraphicOverlay.getColorball(4).setX(overlayRectangle.left);
            //  mOcrGraphicOverlay.getColorball(4).setY(overlayRectangle.top);
           /* if(((overlayRectangle.top <boundingBox.top) &&
               (overlayRectangle.top+overlayRectangle.height())> (boundingBox.top +boundingBox.height())) &&
                ((overlayRectangle.left <boundingBox.left) &&
                 (overlayRectangle.left+overlayRectangle.width())> (boundingBox.left +boundingBox.width())))*/
            mOcrGraphicOverlay.getColorball(4).setX(boundingBox.left);
            mOcrGraphicOverlay.getColorball(4).setY(boundingBox.top);
            if ((overlayRectangle.top < boundingBox.top) &&
                    (overlayRectangle.top + overlayRectangle.height()) > (boundingBox.top + boundingBox.height())) {
                validDetections.put(index, detectedBlock);
            }
        }
        return validDetections;
    }

找到一个解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-10
    • 2018-02-25
    • 2022-08-18
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2021-09-16
    相关资源
    最近更新 更多