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

http://eyes-free.googlecode.com/ · Java · 235 lines · 131 code · 41 blank · 63 comment · 14 complexity · 10e7fc5d11422ea422172241b45331cf 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.opticflow;
  17. import android.os.AsyncTask;
  18. import android.util.Log;
  19. import com.googlecode.eyesfree.opticflow.TextTrackerProcessor.TrackedRect;
  20. import com.googlecode.tesseract.android.TessBaseAPI;
  21. import java.util.Collection;
  22. import java.util.LinkedList;
  23. /**
  24. * Runs OCR jobs on an availability basis.
  25. *
  26. * @author alanv@google.com (Alan Viverette)
  27. */
  28. public class OcrQueue {
  29. private static final String TAG = "OcrQueue";
  30. private static final String DEFAULT_WHITELIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/:=.@,!-'%()$&?*";
  31. private final TessBaseAPI mOcrAPI;
  32. private final LinkedList<TrackedRect> mRectQueue;
  33. private final String mTessdata;
  34. private final String mLanguage;
  35. private Listener mListener;
  36. /** Whether init() has been called yet. */
  37. private boolean mInitialized;
  38. /**
  39. * Constructs a new recognition queue.
  40. *
  41. * @param tessdata The path containing the {@code tessdata} directory.
  42. * @param language The language pack to use. Defaults to "eng" if not
  43. * available.
  44. */
  45. public OcrQueue(String tessdata, String language) {
  46. mTessdata = tessdata;
  47. mLanguage = language;
  48. mRectQueue = new LinkedList<TrackedRect>();
  49. mOcrAPI = new TessBaseAPI();
  50. }
  51. /**
  52. * Sets a listener to receive recognition results.
  53. *
  54. * @param listener The listener that will receive recognition results.
  55. */
  56. public void setListener(Listener listener) {
  57. mListener = listener;
  58. }
  59. /**
  60. * Initializes the OCR API for the specified language pack.
  61. */
  62. public void init() {
  63. boolean success = mOcrAPI.init(mTessdata, mLanguage);
  64. if (success) {
  65. mOcrAPI.setPageSegMode(TessBaseAPI.PSM_SINGLE_LINE);
  66. mOcrAPI.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, DEFAULT_WHITELIST);
  67. mInitialized = true;
  68. }
  69. }
  70. /**
  71. * Adds a collection of tracked rects to the queue.
  72. *
  73. * @param rects The collection of tracked rects to queue for recognition.
  74. */
  75. public void addAll(Collection<? extends TrackedRect> rects) {
  76. boolean initialized = false;
  77. boolean wasEmpty = false;
  78. synchronized (mRectQueue) {
  79. initialized = mInitialized;
  80. wasEmpty = mRectQueue.isEmpty();
  81. mRectQueue.addAll(rects);
  82. }
  83. if (initialized && wasEmpty) {
  84. next();
  85. }
  86. }
  87. /**
  88. * Adds a tracked rect to the queue.
  89. *
  90. * @param rect The tracked rect to queue for recognition.
  91. */
  92. public void add(TrackedRect rect) {
  93. boolean initialized = false;
  94. boolean wasEmpty = false;
  95. synchronized (mRectQueue) {
  96. initialized = mInitialized;
  97. wasEmpty = mRectQueue.isEmpty();
  98. mRectQueue.addLast(rect);
  99. }
  100. if (initialized && wasEmpty) {
  101. next();
  102. }
  103. }
  104. /**
  105. * Removes a collection of tracked rects from the queue.
  106. *
  107. * @param rects The collection of tracked rects to remove from the recognition queue.
  108. */
  109. public void removeAll(Collection<? extends TrackedRect> rects) {
  110. synchronized (mRectQueue) {
  111. mRectQueue.removeAll(rects);
  112. }
  113. }
  114. /**
  115. * Adds a tracked rect to the queue.
  116. *
  117. * @param rect The tracked rect to remove from the recognition queue.
  118. */
  119. public void remove(TrackedRect rect) {
  120. synchronized (mRectQueue) {
  121. mRectQueue.remove(rect);
  122. }
  123. }
  124. /**
  125. * Runs the next queue item, if one is available.
  126. */
  127. private void next() {
  128. TrackedRect rect = null;
  129. boolean initialized = false;
  130. synchronized (mRectQueue) {
  131. initialized = mInitialized;
  132. if (!mRectQueue.isEmpty()) {
  133. rect = mRectQueue.getFirst();
  134. }
  135. }
  136. if (initialized && rect != null) {
  137. RecognizeTask task = new RecognizeTask();
  138. task.execute(rect);
  139. }
  140. }
  141. /**
  142. * @return the size of the recognition queue
  143. */
  144. public int size() {
  145. int size = 0;
  146. synchronized (mRectQueue) {
  147. size = mRectQueue.size();
  148. }
  149. return size;
  150. }
  151. private class RecognizeTask extends AsyncTask<TrackedRect, Void, RecognitionResult> {
  152. @Override
  153. protected RecognitionResult doInBackground(TrackedRect... rects) {
  154. if (rects.length <= 0 || isCancelled()) {
  155. return null;
  156. }
  157. TrackedRect rect = rects[0];
  158. Log.i(TAG, "Recognizing");
  159. mOcrAPI.setImage(rect.pix);
  160. String utf8 = mOcrAPI.getUTF8Text();
  161. int[] confs = mOcrAPI.wordConfidences();
  162. rect.text = utf8;
  163. return new RecognitionResult(utf8, confs);
  164. }
  165. @Override
  166. protected void onCancelled() {
  167. // TODO(alanv): Cancel current native recognition task.
  168. }
  169. @Override
  170. protected void onPostExecute(RecognitionResult result) {
  171. Log.i(TAG, "Recognized " + result.utf8);
  172. synchronized (mRectQueue) {
  173. if (!mRectQueue.isEmpty()) {
  174. mRectQueue.removeFirst();
  175. next();
  176. }
  177. }
  178. if (mListener != null) {
  179. mListener.onResult(result.utf8, result.confs);
  180. }
  181. }
  182. }
  183. public interface Listener {
  184. public void onResult(String result, int[] confs);
  185. }
  186. private class RecognitionResult {
  187. public final String utf8;
  188. public final int[] confs;
  189. public RecognitionResult(String utf8, int[] confs) {
  190. this.utf8 = utf8;
  191. this.confs = confs;
  192. }
  193. }
  194. }