PageRenderTime 31ms CodeModel.GetById 1ms app.highlight 22ms RepoModel.GetById 2ms app.codeStats 0ms

/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/intent/OpticalFlowActivity.java

http://eyes-free.googlecode.com/
Java | 130 lines | 81 code | 26 blank | 23 comment | 8 complexity | 1bd1040545f1d69a311bae038739ffbd 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.ocr.intent;
 18
 19import android.os.Bundle;
 20
 21import com.googlecode.eyesfree.ocr.R;
 22import com.googlecode.eyesfree.opticflow.DebugView;
 23import com.googlecode.eyesfree.opticflow.FrameLooper;
 24import com.googlecode.eyesfree.opticflow.ImageBlurProcessor;
 25import com.googlecode.eyesfree.opticflow.OcrProcessor;
 26import com.googlecode.eyesfree.opticflow.OpticalFlowProcessor;
 27import com.googlecode.eyesfree.opticflow.TextDetectionProcessor;
 28import com.googlecode.eyesfree.opticflow.TextTrackerProcessor;
 29
 30import java.io.File;
 31
 32/**
 33 * Computes and displays likely text areas and live OCR results.
 34 *
 35 * @author alanv@google.com (Alan Viverette)
 36 */
 37public class OpticalFlowActivity extends CameraActivity {
 38    private static final String DEFAULT_LANGUAGE = "eng";
 39
 40    private String mLanguage;
 41    private File mDatapath;
 42    private VoiceGestureView mVoiceView;
 43    private DebugView mDebugView;
 44    private FrameLooper mPreviewLooper;
 45
 46    @Override
 47    protected void onCreate(Bundle savedInstanceState) {
 48        super.onCreate(savedInstanceState);
 49
 50        setContentView(R.layout.optical_flow);
 51
 52        mDebugView = (DebugView) findViewById(R.id.debug_view);
 53        mVoiceView = (VoiceGestureView) findViewById(R.id.gesture_view);
 54
 55        final String lang = getString(R.string.lang_pref);
 56        mLanguage = getPreferences(MODE_PRIVATE).getString(lang, DEFAULT_LANGUAGE);
 57        mDatapath = getExternalFilesDir(null);
 58    }
 59
 60    @Override
 61    protected void onStop() {
 62        mPreviewLooper.stopLoop();
 63        mVoiceView.shutdown();
 64
 65        super.onStop();
 66    }
 67
 68    @Override
 69    protected void onCameraStarted() {
 70        initializeContinuous();
 71    }
 72
 73    private void initializeContinuous() {
 74        CameraManager cameraManager = getCameraManager();
 75
 76        final int[] delayMillis = {
 77                0, 10, 100, 1000
 78        };
 79
 80        mPreviewLooper = new FrameLooper(cameraManager, mDebugView, delayMillis);
 81        mDebugView.setCallback(mPreviewLooper);
 82
 83        final OpticalFlowProcessor opticalFlow = new OpticalFlowProcessor();
 84        mPreviewLooper.addPreviewProcessor(opticalFlow, 1);
 85
 86        final TextDetectionProcessor textDetect = new TextDetectionProcessor();
 87        mPreviewLooper.addPreviewProcessor(textDetect, 2);
 88
 89        final TextTrackerProcessor textTracker = new TextTrackerProcessor(
 90                opticalFlow.getOpticalFlow());
 91        mPreviewLooper.addPreviewProcessor(textTracker, 2);
 92
 93        final OcrProcessor ocr = new OcrProcessor(mDatapath.toString(), mLanguage, textTracker);
 94        mPreviewLooper.addPreviewProcessor(ocr, 2);
 95
 96        final ImageBlurProcessor imageBlur = new ImageBlurProcessor(cameraManager);
 97        mPreviewLooper.addPreviewProcessor(imageBlur, 3);
 98
 99        // This is a workaround for an issue where the previewLooper isn't
100        // running properly when the activity is restarted.
101        // TODO(mrcasey): Figure out why this seems to fix the issue.
102        if (mPreviewLooper.isRunning()) {
103            mPreviewLooper.stopLoop();
104        }
105
106        ocr.setListener(ocrListener);
107
108        mPreviewLooper.initAllProcessors();
109        mPreviewLooper.startLoop();
110    }
111
112    private final OcrProcessor.Listener ocrListener = new OcrProcessor.Listener() {
113        @Override
114        public void onResult(String result, int[] confs) {
115            if (result != null && result.length() > 0 && confs.length > 0) {
116                int avgConf = 0;
117                int minConf = confs[0];
118                for (int conf : confs) {
119                    avgConf += conf;
120                    minConf = Math.min(minConf, conf);
121                }
122                avgConf /= confs.length;
123
124                if (avgConf > 70 && minConf > 60) {
125                    mVoiceView.addUtterance(result);
126                }
127            }
128        }
129    };
130}