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