PageRenderTime 17ms CodeModel.GetById 8ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/ocr/ocrservice/src/com/googlecode/eyesfree/opticflow/TextDetectionProcessor.java

http://eyes-free.googlecode.com/
Java | 75 lines | 40 code | 12 blank | 23 comment | 2 complexity | c18c63526c753545310fbf7c9d85f28b MD5 | raw file
 1/*
 2 * Copyright (C) 2011 Google Inc.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 5 * use this file except in compliance with the License. You may obtain a copy of
 6 * the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.googlecode.eyesfree.opticflow;
18
19import com.googlecode.eyesfree.env.Size;
20import com.googlecode.eyesfree.textdetect.HydrogenTextDetector;
21import com.googlecode.eyesfree.textdetect.HydrogenTextDetector.Parameters;
22import com.googlecode.leptonica.android.Pix;
23import com.googlecode.leptonica.android.Pixa;
24
25/**
26 * Frame processor that runs text detection.
27 *
28 * @author alanv@google.com (Alan Viverette)
29 */
30public class TextDetectionProcessor extends FrameProcessor {
31    private HydrogenTextDetector mHydrogen;
32
33    public TextDetectionProcessor() {
34        mHydrogen = new HydrogenTextDetector();
35
36        // We're relaxing the default parameters a little here...
37        Parameters params = mHydrogen.getParameters();
38        params.edge_thresh = 32;
39        params.edge_avg_thresh = 1;
40        params.cluster_min_blobs = 3;
41        params.skew_enabled = true;
42        mHydrogen.setParameters(params);
43    }
44
45    @Override
46    protected synchronized void onInit(final Size size) {
47        int width = size.width;
48        int height = size.height;
49
50        // TODO(alanv): Implement an image buffer throughout Hydrogen.
51        mHydrogen.setSize(width, height);
52    }
53
54    @Override
55    protected void onProcessFrame(TimestampedFrame frame) {
56        if (frame.isBlurred() || frame.takenWhileFocusing()) {
57            return;
58        }
59
60        Pix pixs = frame.getPixData();
61
62        mHydrogen.setSourceImage(pixs);
63        pixs.recycle();
64
65        mHydrogen.detectText();
66        Pixa pixa = mHydrogen.getTextAreas();
67        float[] conf = mHydrogen.getTextConfs();
68        float angle = mHydrogen.getSkewAngle();
69        frame.setDetectedText(pixa, conf, angle);
70        pixa.recycle();
71
72        // TODO(alanv): This won't be necessary when we start using a buffer.
73        mHydrogen.clear();
74    }
75}